function pulse=srrc(fs,Rs,Nfilter,r)

% PULSE=SRRC(FS,RS,NFILTER,R)
% PULSE is the impulse response of a square root raised cosine filter. FS is
% the sampling frequency, RS is the symbol rate and NFILTER is the duration
% of the impulse response in symbol times. R is the rolloff factor.
%
% Daniel Aronsson, 2005-02-10

if abs(fs/Rs - round(fs/Rs)) > 1e-6
    error('fs/R must be an integer.');
end
if Nfilter ~= round(Nfilter)
    error('Nfilter must be an integer');
end
if r<0 | r>1
    error('R must obey 0<R<1');
end

N=fs/Rs*Nfilter;
T=1/Rs;
Ttot=T*Nfilter;
t=linspace(0,Ttot-1/fs,N);
f=fftshift(linspace(-fs/2,fs/2-1/Ttot,N));


X=  0                                      .*(f <= -(1+r)/2/T) + ...
    T/2*(1+cos(pi*T/r*(-f-(1-r)/2/T)))     .*(f > -(1+r)/2/T & f <= -(1-r)/2/T) + ...
    T                                      .*(f > -(1-r)/2/T & f <= (1-r)/2/T) + ...
    T/2*(1+cos(pi*T/r*(f-(1-r)/2/T)))      .*(f > (1-r)/2/T & f <= (1+r)/2/T) + ...
    0                                      .*(f > (1+r)/2/T);

X=fs*X;  % Want the peak to be at 1
X=sqrt(X);

pulse=real(fftshift(ifft(X)));

% This gives (almost) the same result as
% rcosfir(r, Nfilter/2, fs/Rs, Rs, 'sqrt') (remove the last element)

