clear, figure(1), clf reset; clf

% plot ideal  low/high pass filter
% and their DFT's
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
N = 1023; N1 = N+1;
k = (-N:N);
m = 5;
a = 1/m;
b = m;

% the filter coefficients
% h = b*sin(a*k)./(pi*k);      
% but to avoid division by zero ...

k2 = (-N:-1);
h(1 : N) = b*sin(a*pi*k2)./(pi*k2);
h(N1) = b * a;
h(N+2 : 2*N+1) = fliplr(h(1 : N));

figure(1); subplot(3,2,1); plot(k,h)
title('The signal x(k)');xlabel('k');
axis([-N, N, -0.5, 1.02]);

disp('A plot of the signal')
disp('... push an arbitrary key to continue'),pause

ns = 20; snip=(-ns:ns);
figure(1); subplot(3,2,3); plot(k(N1+snip), h(N1+snip))
title(['Taking ',int2str(2*ns+1),' samples']); xlabel('k');
axis([-ns, ns, -0.5, 1.02]);

disp(['Taking only ',int2str(2*ns+1),' samples'])
disp('... push an arbitrary key to continue'), pause

H = DTFTfast(h, N1);
om = (-N1:N1)*pi/N1;

figure(1); subplot(3,2,4); plot(om,abs(H))                   
axis([-pi, pi, -0.1*b, 1.1*b])
title('|X(omega)|=|FFT(x(k))|');xlabel('omega');

disp('Plot of the Fourier transform')
disp('... push an arbitrary key to continue'),pause


% de ideale hoogdoorlaatfilter heeft coefficienten precies het tegengestelde
% van de ideale laagdoorlaatfilter, BEHALVE voor k=0;
% dit veroorzaakt een "discontinuiteit": de ideale hoogdoorlaat-filter in het
% punt nul is NIET de limiet van het functievoorschrift:
% -b.sin(ak)/(pi k)
% als we even verder denken naar het continue geval (Fourier transformatie)
% is dit logisch: een hoogdoorlaat-spectrum heeft "bijna" (in maattheoretische
% zin) alle frequenties 1, behalve in het midden. Deze
% frequentie-karakteristiek is niet kwadratisch integreerbaar, dus moet er
% een dirac-impuls verschijnen op het punt nul.

g = -h;
g(N1) = g(N1) + b;

figure(1); subplot(3,2,5); plot(k(N1+snip), g(N1+snip))
title(['Taking only ',int2str(2*ns+1),' samples']);xlabel('k');
axis([-ns, ns, -1.1, 1.1*(b-1)]);

disp(['Taking only ',int2str(2*ns+1),' samples'])
disp('... push an arbitrary key to continue'),pause

G = DTFTfast(g, N1);

figure(1); subplot(3,2,6); plot(om,abs(G))
axis([-pi, pi, -0.1*b, 1.1*b])
title('|X(omega)|=|FFT(x(k))|');xlabel('omega');

disp('Plot of the Fourier transform')
disp('End of demo ... ')
