
% File: FIG3_26.M

clear;

% Calculate the h(t) for a Raised-cosine Filter
% See SA2-9 and (2-207) for development of 
% IFFT relationships
r=input('Enter value for r between 0.05 and 0.95:');
M = 8;
N = 2^M;
n = 0:1:N-1;
fo = 1;
fs = 20;
dt = 1/fs;
T = N*dt;
df=1/T;
f = n/T;
t = n*dt;
fdel=r*fo;
f1=fo-fdel;
B=fo+fdel;
N1=round(f1/df)+1;
N2=round(f1/df)-1;
NB=round(B/df)+1;
NB2=round(B/df)-1;

% Creating Spectrum
H=zeros(length(f),1);
for (i = 1:1:N1)
   H(i)=1;
end;
for  (i=N1:1:NB)
   H(i) = 0.5*(1+cos(0.5*pi*(f(i)-f1)/fdel));
end;
for (i=((length(f))-NB2):1:(length(f)-N2))
   H(i) = 0.5*(1+cos(0.5*pi*(f(i)-fs+f1)/fdel));
end;
for  (i=(length(f)-N2):1:length(f))
   H(i)=1;
end;   

% Calculating IFFT
h = fs*ifft(H);

% Plot Spectrum
plot(f,abs(H));
xlabel('f (Hz) -->');
ylabel('|H(f)|');
grid;
title('MAGNITUDE SPECTRUM ');
pause;

% Plot Impulse Response
plot(t,h);
axis([0 2 -0.5 2]);
xlabel('t (sec) -->');
ylabel('h(t)');
title('Impulse Response');
grid;
