% method of class @signal
% function sig=generateirn(sig,delay,g,niter)
%   INPUT VALUES:
%       sig: original @signal with length and samplerate 
%       delay: delay, after which the noise is added again
%       g: gain
%       niter: number of iterations that are added
% 
%   RETURN VALUE:
%       sig:  @signal 
%
% (c) 2003-2008, University of Cambridge, Medical Research Council 
% Maintained by Tom Walters (tcw24@cam.ac.uk), written by Stefan Bleeck (stefan@bleeck.de)
% http://www.pdn.cam.ac.uk/cnbh/aim2006
% $Date: 2008-06-10 18:00:16 +0100 (Tue, 10 Jun 2008) $
% $Revision: 585 $

function sig=generateirn(sig,delay,g,niter)

srate=getsr(sig);
dur=getlength(sig);

dels=round(delay*srate);
npts=round(dur*srate);

nois=randn(size(1:npts));

for i=1:niter;
    dnois=nois;
    dnoist=dnois(1:dels);
    dnois=[dnois dnoist];
    dnois=dnois(dels+1:npts+dels);
    dnois=dnois.*g;
    nois=nois+dnois;
end;

rms=sqrt(mean(nois.*nois));
b=nois./rms;

sig=setvalues(sig,b);