% File: SA7_1.M

clear;

% Calculate output waveform from an ideal (brickwall) 
%   low-pass filter
% Filter input is a rectangular pulse of width tdur

M = 7;
N = 2^M;
n = 0:1:N-1;
tstart = 2;
tend = 3;
tdur = tend - tstart;
T = 10;
dt = T/N;
t = n*dt;

% Creating time waveform
w = zeros(length(t),1);
for (i = 1:1:length(w))
  if ((t(i) <= tend) & (t(i) >= tstart))
    w(i) = 1;
  end;
end;

% Calculating FFT
W = dt*fft(w);
f = n/T;
% Calculating position of 4th NULL
pos = index(f,4/tdur);

% Calculating filtered waveform
% foff sets filter cutoff frequency
fprintf('\nEquivalent Bandwidth of filter is now set to B=2/T.\n')
fprintf('\nTo change bandwidth, change 2 in foff=2/tdur to a different value; tdur=T.\n')
%
foff = 2/tdur;
%
Wfil = W;
for (i = 1:1:length(Wfil))
  if ((f(i) >= foff) & (f(i) <= ((1/dt) - foff)) )
     Wfil(i) = 0;
  end;
end;
wfil = (1/dt)*ifft(Wfil);

plot_pr(3);
plot(t,w);
axis([0 T 0 1.5]);
xlabel('t (sec) -->');
ylabel('w(t)');
title('Time Waveform');
pause;

plot(t,real(wfil));
axis([0 T -0.5 1.5]);
xlabel('t (sec) -->');
ylabel('wfil(t)');
title('Filtered Time Waveform');
pause;

subplot(311);
plot(f(1:pos),abs(Wfil(1:pos)));
xlabel('f (Hz) -->');
ylabel('|Wfil(f)|');
title('MAGNITUDE SPECTRUM out to 4th Null');

subplot(312);
plot(f(1:pos),180/pi*angle(Wfil(1:pos)));
xlabel('f (Hz) -->');
ylabel('theta(f) (degrees)');
title('PHASE SPECTRUM out to 4th Null');
grid;

subplot(313);
plot(f,abs(Wfil));
xlabel('f (Hz) -->');
ylabel('|Wfil(f)|');
title('MAGNITUDE SPECTRUM over whole FFT frequency range');




