%% Window Leakage and Tone Resolution
% Generate a two-channel signal sampled at 100 Hz for 2 seconds.
% The first channel consists of a 20 Hz tone and a 21 Hz tone. Both tones have unit amplitude.
% The second channel also has two tones. One tone has unit amplitude and a frequency of 20 Hz. The other tone has an amplitude of 1/100 and a frequency of 30 Hz.

fs = 100;
t = (0:1/fs:2-1/fs)';

x = sin(2*pi*[20 20].*t) + [1 1/100].*sin(2*pi*[21 30].*t);

%
% Embed the signal in white noise. Specify a signal-to-noise ratio of 40 dB. Plot the signals.

x = x + randn(size(x)).*std(x)/db2mag(40);

plot(t,x)

%
% Compute the spectra of the two channels and display them.

pspectrum(x,t)

%
% The default value for the spectral leakage, 0.5, corresponds to a resolution bandwidth of about 1.29 Hz. The two tones in the first channel are not resolved. The 30 Hz tone in the second channel is visible, despite being much weaker than the other one.
% Increase the leakage to 0.85, equivalent to a resolution of about 0.74 Hz. The weak tone in the second channel is clearly visible.

pspectrum(x,t,'Leakage',0.85)

%
% Increase the leakage to the maximum value. The resolution bandwidth is approximately 0.5 Hz. The two tones in the first channel are resolved. The weak tone in the second channel is masked by the large window sidelobes.

pspectrum(x,t,'Leakage',1)

% Copyright 2012 The MathWorks, Inc.