Leakage Example Octave Script

From Class Wiki
Jump to navigation Jump to search

Leakage

Leakage is what happens due to the end effects of the sampled signal. Discrete Fourier Transforms assume that the signal is periodic with period NT. If it isn't, then the DFT results can be surprising. One way to remove the high frequency components that "leak" out of the bins they should be in is to minimize the effects of the beginning and end of the data, but multiplying by a window function like the Hamming window. This example script shows the effects of leakage. Play with the parameters N, and the windowing function and even the frequency of the signals to see what happens.

% This program demonstrates leakage.
clear all;
clf;
T=1/50
N=75  %  Use 75 for a lot of leakage, or 100 for none.
t=0:.001:N*T;
xc=cos(2*pi*t);
k=0:1:N-1;
W=hamming(N)';  % Use other window functions if you like.  ones(size(k));
x=cos(2*pi*k*T);
plot(W)
title('Window Function')
figure(1);
pause(3);
xw=x.*W;
X=fft(x);
Xw=fft(xw);
plot(t,xc,'b',k*T,x,'b*',k*T,xw,'g*');
title('Time Domain')
legend('Original Function','Sample Points','Windowed Data')
pause(3);
figure(2);
plot(k,abs(X),'bo',k,abs(Xw),'go');
title('FFT Unshifted Results')
legend('Unwindowed','Windowed')
pause(3);
figure(3);
f=-1/(2*T):1/(N*T):1/(2*T)-1/(N*T);
plot(f,abs(fftshift(X)),'bo',f,abs(fftshift(X)),f,abs(fftshift(Xw)),'go',f,abs(fftshift(Xw)));
xlabel('Frequency (Hz)');
title('Magnitude Plot of the DFT of cos(2*pi*t)');
legend('Unwindowed Points','Unwindowed (connect the points)','Windowed Points','Windowed (connect the points)')