% program to demonstrate gibbs phenomena of a boxcar fir filter

clear all
close all

%total length of f1, f2 time series in seconds
N=1000;
df=1/N;
M=N/2;

%time step size in seconds
f=0:df:1;

%first approximation of ideal FIR impulse response
% equation 14 in Aster Filter notes.
n=1;
f1=M/N+cos(pi*N*f)/N+2*cos(2*pi*n*f)*(sin(pi*n*M/N)/sin(pi*n/N))/N;

disp(['plotting ideal lowpass with n=',num2str(n)])
plot(f,f1)
hold on
pause;

% add a couple more degrees of approximation and plot
for n=2:5,
  f1=f1+2*cos(2*pi*n*f)*sin(pi*n*M/N)/sin(pi*n/N)/N;
  plot(f,f1)
  disp(['plotting n=',num2str(n)])
  pause;
end

% add still more without plotting
for n=6:50,
  f1=f1+2*cos(2*pi*n*f)*sin(pi*n*M/N)/sin(pi*n/N)/N;
end

%now plot the last
disp(['plotting n=',num2str(n)])
plot(f,f1)
