% Spectral Leakage Example
%
close all;clear
fs = 200
T = .5
N = T*fs
n = [0:N-1].';
t =  n/fs;
fN = n/N;

xa = cos(2*pi*40*t);
figure(1)
subplot(3,1,1);bar(n,xa);axis tight
subplot(3,1,3);stem(fN,abs(fft(xa)));axis([0 1  0 60])

xb = cos(2*pi*41*t);
figure(2)
subplot(3,1,1);bar(n,xb);axis tight
xc = (cos(2*pi*40*t)+cos(2*pi*42*t))/2;
subplot(3,1,2);stem(fN,abs(fft(xc)));axis([0 1  0 60])
text(0.3,40,'FAKE')
subplot(3,1,3);stem(fN,abs(fft(xb)));axis([0 1  0 60])
text(0.3,40,'TRUE')

nGraph = 1024

[Xa,f] = freqz(xa,1,nGraph,'whole',1);
figure(1)
subplot(3,1,2);plot(f,abs(Xa));axis([0 1  0 60])

[Xb,f] = freqz(xb,1,nGraph,'whole',1);
figure(2)
subplot(3,1,2);plot(f,abs(Xb));axis([0 1  0 60])

figure(3)
plot(f,abs(Xb),'k');axis([0 1  0 60])
hold on
stem(fN,abs(fft(xb)))

figure(4)
plot(f,abs(Xa),'k');axis([0 1  0 60])
hold on
stem(fN,abs(fft(xa)))

figure(2)

w = hamming(N);
xbw = xb.*w;
[XbW,f] = freqz(xbw,1,nGraph,'whole',1);
figure(5)
subplot(3,1,1);bar(n,xbw);axis tight
subplot(3,1,2);plot(f,abs(XbW));axis([0 1  0 60])
subplot(3,1,3);stem(fN,abs(fft(xbw)));axis([0 1  0 60])

w = kaiser(N,4);
xbw = xb.*w;
[XbW,f] = freqz(xbw,1,nGraph,'whole',1);
figure(6)
subplot(3,1,1);bar(n,xbw);axis tight
subplot(3,1,2);plot(f,abs(XbW));axis([0 1  0 60])
subplot(3,1,3);stem(fN,abs(fft(xbw)));axis([0 1  0 60])

[Xc,f] = freqz(xc,1,nGraph,'whole',1);
figure(7)
subplot(3,1,1);bar(n,xc);axis tight
subplot(3,1,2);plot(f,abs(Xc));axis([0 1  0 60])
subplot(3,1,3);stem(fN,abs(fft(xc)));axis([0 1  0 60])

w = kaiser(N,4);
xcw = xc.*w;
[XcW,f] = freqz(xcw,1,nGraph,'whole',1);
figure(8)
subplot(3,1,1);bar(n,xcw);axis tight
subplot(3,1,2);plot(f,abs(XcW));axis([0 1  0 60])
subplot(3,1,3);stem(fN,abs(fft(xcw)));axis([0 1  0 60])
