next About this document ...
up Music 270a: Matlab Tutorial 3
previous Zero-padding


Using a window

The previous examples can all be interpreted as using a rectangular window to select a finite segment (of length $ N$ ) from a sampled sinusoid. To see the spectral characteristics of the rectangle window, try the following in Matlab:

N = 64;
zpf = 8;

x = [ones(1, N) zeros(1, (zpf-1)*N)]; %rectangle window

subplot(311); plot(x);
title('Rectangle Window');
xlabel('Time(s)');
ylabel('Amplitude');

Nfft = N*zpf;
X = fft(x);
magX = abs(X);
spec = 20*log10(magX);
spec = spec-max(spec);
spec = max(spec, -40*ones(1, length(spec)));
fn = 0:1/Nfft:1-1/Nfft;
subplot(312); plot(fn, spec);
xlabel('Normalized Frequency (cycles per sample)');
ylabel('Magnitude (dB)');

%replot showing negative frequencies below zero
X = fftshift(X);
magX = abs(X);
spec = 20*log10(magX);
spec = spec-max(spec);
spec = max(spec, -40*ones(1, length(spec)));
fn = -.5:1/Nfft:.5-1/Nfft;
subplot(313); plot(fn, spec);
xlabel('Normalized Frequency (cycles per sample)');
ylabel('Magnitude (dB)');

In practical spectrum analysis, such excerpts are normally analyzed using a window which is tapered more gracefully to zero on the left and right. In this section, we will look at using a Blackman window on our example sinusoid. The Blackman window has good (though suboptimal) characteristics for audio work.

In Octave or the Matlab Signal Processing Toolbox, a Blackman window of length $ M=64$ can be designed very easily:

M = 64;
w = blackman(M);

Many other standard windows are defined as well, including hamming, hanning, and bartlett windows.

In Matlab without the Signal Processing Toolbox, the Blackman window is readily computed from its mathematical definition:

w = .42 - .5*cos(2*pi*(0:M-1)/(M-1)) ...
       + .08*cos(4*pi*(0:M-1)/(M-1));

To see the spectral characteristics of the Blackman window try the following:

M = 64;  
w = blackman(M);
figure(1); 
subplot(3,1,1); plot(w,'*'); title('Blackman Window');
xlabel('Time (samples)'); ylabel('Amplitude'); text(-8,1,'a)'); 

% Also show the window transform:
zpf = 8;                      % zero-padding factor
xw = [w,zeros(1,(zpf-1)*M)];  % zero-padded window
Xw = fft(xw);                 % Blackman window transform
spec = 20*log10(abs(Xw));     % Spectral magnitude in dB
spec = spec - max(spec);      % Normalize to 0 db max
nfft = zpf*M;
spec = max(spec,-100*ones(1,nfft)); % clip to -100 dB
fni = [0:1.0/nfft:1-1.0/nfft];   % Normalized frequency axis
subplot(3,1,2); plot(fni,spec,'-'); axis([0,1,-100,10]); 
xlabel('Normalized Frequency (cycles per sample))'); 
ylabel('Magnitude (dB)'); grid; text(-.12,20,'b)'); 

% Replot interpreting upper bin numbers as frequencies<0:
nh = nfft/2;
specnf = [spec(nh+1:nfft),spec(1:nh)];  % see fftshift()
fninf = fni - 0.5;
subplot(3,1,3);
plot(fninf,specnf,'-'); axis([-0.5,0.5,-100,10]); grid;
xlabel('Normalized Frequency (cycles per sample))'); 
ylabel('Magnitude (dB)');
text(-.62,20,'c)'); 
cmd = ['print -deps ', '../eps/blackman.eps'];
disp(cmd); eval(cmd);
disp 'pausing for RETURN (check the plot). . .'; pause


next About this document ...
up Music 270a: Matlab Tutorial 3
previous Zero-padding

``CMPT 318: Fundamentals of Computerized Sound'' by Tamara Smyth, Computing Science, Simon Fraser University.
Download PDF version (matlabtut3.pdf)
Download compressed PostScript version (matlabtut3.ps.gz)

Copyright © 2013-01-28 by Tamara Smyth.
Please email errata, comments, and suggestions to Tamara Smyth<trsmyth@ucsd.edu>