Digital Signal Processing Lab 1: FFT, Spectral Leakage, Zero Padding Moslem Amiri, Václav Přenosil Embedded Systems Laboratory Faculty of Informatics, Masaryk University Brno, Czech Republic amiri@mail.muni.cz prenosil@fi.muni.cz Spring, 2015
Open MATLAB Create an m-file An m-file, or script file, is a simple text file where you can place MATLAB commands Choose New from the File menu and select Script Generate two separate 64 length buffers of the two sinusoids: Use a sampling frequency of 1 khz x 1 (t) = 100 sin(2π101.56t) x 2 (t) = 2 sin(2π156.25t) Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 2 / 29
y = 100*sin(2*pi*101.56*t); figure(1);plot(fs*t(1:50),y(1:50)); title( Signal ); xlabel( time (milliseconds) ); Run the m-file Press F5 Or type filename at the MATLAB command window prompt Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 3 / 29
y = 2*sin(2*pi*156.25*t); figure(2);plot(fs*t(1:50),y(1:50)); title( Signal ); xlabel( time (milliseconds) ); Execute the commands Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 4 / 29
Evaluate the FFT of each of these signals using a rectangular window, and determine which frequency bins have the highest peaks Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 5 / 29
y = 100*sin(2*pi*101.56*t); Y = fft(y,l)/l; f = Fs/2*linspace(0,1,L/2+1); % Plot single-sided amplitude spectrum. figure(1);plot(f,2*abs(y(1:l/2+1))) Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 6 / 29
We should divide FFT magnitudes by N/2 for real inputs The division by L is done in Y = fft(y,l)/l; Multiplication by 2 is done in plot(f,2*abs(y(1:l/2+1))) Command y = linspace(a,b,n) Generates a row vector y of n points linearly spaced between and including a and b For n < 2, linspace returns b f = Fs/2*linspace(0,1,L/2+1); Frequency of index bins is mfs/n For L = 64, it must be Fs, i.e. one full interval in frequency domain, so for half of that (L/2+1), it is Fs/2 Execute the commands Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 7 / 29
y = 2*sin(2*pi*156.25*t); Y = fft(y,l)/l; f = Fs/2*linspace(0,1,L/2+1); figure(4);plot(f,2*abs(y(1:l/2+1))) Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 8 / 29
Repeat the FFT evaluations done previously, but using a 1024 point FFT Applying a 1024 point FFT to a 64 length data buffer has the effect of appending (1024-64) zeros to the end of the data, which increases the number of points at which the spectrum is evaluated Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 9 / 29
y = 100*sin(2*pi*101.56*t); Y = fft(y,1024)/l; f = Fs/2*linspace(0,1,1024/2+1); figure(1);plot(f,2*abs(y(1:1024/2+1))) Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 10 / 29
Y = fft(x,n) returns the n-point DFT fft(x) is equivalent to fft(x,n) where n is the size of X in the first nonsingleton dimension If the length of X is less than n, X is padded with trailing zeros to length n If the length of X is greater than n, the sequence X is truncated When X is a matrix, the length of the columns are adjusted in the same manner Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 11 / 29
y = 2*sin(2*pi*156.25*t); Y = fft(y,1024)/l; f = Fs/2*linspace(0,1,1024/2+1); figure(4);plot(f,2*abs(y(1:1024/2+1))) Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 12 / 29
The Hamming window is defined as: w(n) = 0.54 0.46 cos(2πn/n) for n = 0, 1, 2,..., N 1 Generate and plot the Hamming window Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 13 / 29
n = (0:63); w = 0.54-0.46 * cos(2 * pi * n / 64); plot(n,w); Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 14 / 29
Now repeat FFT evaluations (without and with zero padding), but applying first a Hamming window to the 64 length data buffers before evaluating the FFTs Observe the outputs of the FFTs Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 15 / 29
y = 100*sin(2*pi*101.56*t); w = 0.54-0.46 * cos(2 * pi * (0 : 63) / 64); win = y.* w; Y = fft(win,l)/l; f = Fs/2*linspace(0,1,L/2+1); figure(2);plot(f,2*abs(y(1:l/2+1))) Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 16 / 29
y = 100*sin(2*pi*101.56*t); w = 0.54-0.46 * cos(2 * pi * (0 : 63) / 64); win = y.* w; Y = fft(win,1024)/l; f = Fs/2*linspace(0,1,1024/2+1); figure(3);plot(f,2*abs(y(1:1024/2+1))) Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 17 / 29
y = 2*sin(2*pi*156.25*t); w = 0.54-0.46 * cos(2 * pi * (0 : 63) / 64); win = y.* w; Y = fft(win,l)/l; f = Fs/2*linspace(0,1,L/2+1); figure(1);plot(f,2*abs(y(1:l/2+1))) Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 18 / 29
y = 2*sin(2*pi*156.25*t); w = 0.54-0.46 * cos(2 * pi * (0 : 63) / 64); win = y.* w; Y = fft(win,1024)/l; f = Fs/2*linspace(0,1,1024/2+1); figure(1);plot(f,2*abs(y(1:1024/2+1))) Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 19 / 29
Now add these two sine waves together, and apply both 64-length and 1024-length FFTs, with both rectangular and Hamming windows Observe how spectral leakage can mask a small signal in a large one Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 20 / 29
y = 100*sin(2*pi*101.56*t) + 2*sin(2*pi*156.25*t); Y = fft(y,l)/l; f = Fs/2*linspace(0,1,L/2+1); figure(2);plot(f,2*abs(y(1:l/2+1))) Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 21 / 29
y = 100*sin(2*pi*101.56*t) + 2*sin(2*pi*156.25*t); w = 0.54-0.46 * cos(2 * pi * (0 : 63) / 64); win = y.* w; Y = fft(win,l)/l; f = Fs/2*linspace(0,1,L/2+1); figure(2);plot(f,2*abs(y(1:l/2+1))) Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 22 / 29
y = 100*sin(2*pi*101.56*t) + 2*sin(2*pi*156.25*t); Y = fft(y,1024)/l; f = Fs/2*linspace(0,1,1024/2+1); figure(1);plot(f,2*abs(y(1:1024/2+1))) Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 23 / 29
y = 100*sin(2*pi*101.56*t) + 2*sin(2*pi*156.25*t); w = 0.54-0.46 * cos(2 * pi * (0 : 63) / 64); win = y.* w; Y = fft(win,1024)/l; f = Fs/2*linspace(0,1,1024/2+1); figure(3);plot(f,2*abs(y(1:1024/2+1))) Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 24 / 29
Now add Gaussian noise to the combined signals, with a variance of 10 Compare the results of evaluating the 1024 point FFT on a 64-length data buffer (i.e. with zero padding), with a 1024 point FFT applied to a 1024-length data buffer (i.e. generate more data) In MATLAB, to generate the noise, try: noise = sqrt (variance) * randn(); Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 25 / 29
x = 100*sin(2*pi*101.56*t) + 2*sin(2*pi*156.25*t); y = x + sqrt(10)*randn(size(t)); % Sinusoids plus noise Y = fft(y,1024)/l; f = Fs/2*linspace(0,1,1024/2+1); figure(1);plot(f,2*abs(y(1:1024/2+1))) Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 26 / 29
L = 1024; x = 100*sin(2*pi*101.56*t) + 2*sin(2*pi*156.25*t); y = x + sqrt(10)*randn(size(t)); % Sinusoids plus noise Y = fft(y,1024)/l; f = Fs/2*linspace(0,1,1024/2+1); figure(2);plot(f,2*abs(y(1:1024/2+1))) Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 27 / 29
Logarithmic axis scaling Log-log and semi-log plots are created with commands that act just like the plot command Command Name: loglog, Plot type: log(y) versus log(x) Command Name: semilogx, Plot type: y versus log(x) Command Name: semilogy, Plot type: log(y) versus x Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 28 / 29
L = 1024; x = 100*sin(2*pi*101.56*t) + 2*sin(2*pi*156.25*t); y = x + sqrt(10)*randn(size(t)); % Sinusoids plus noise Y = fft(y,1024)/l; f = Fs/2*linspace(0,1,1024/2+1); figure(2);semilogy(f,2*abs(y(1:1024/2+1))) Moslem Amiri, Václav Přenosil Digital Signal Processing Spring, 2015 29 / 29