%% Compute FFT of a Subband Using Zoom FFT
% Compute FFT of the [1500 Hz 2500 Hz] subband using zoom FFT for a signal sampled 
% at 48 kHz.
%% Initialization
% Set the center frequency to 2 kHz and the bandwidth of interest to 1 kHz. 
% The bandwidth is centered at the center frequency. The decimation factor is 
% the ratio of the input sample rate, 48 kHz, and the bandwidth of interest, 1 
% kHz. Choose an FFT length of 64. Set the input frame size to be the decimation 
% factor times the FFT length. Create a |dsp.ZoomFFT| object with the specified 
% decimation factor, center frequency, sample rate, and FFT length.
%%
Fs = 48e3;    
CF = 2e3;    
BW = 1e3;
D  =  Fs/BW;
fftlen = 64;
L  = D * fftlen; 
zfft = dsp.ZoomFFT(D,CF,Fs,'FFTLength',fftlen);
%% Frequencies
% The FFT is computed over frequencies starting at 1500 Hz and spaced by $Fs/(D*fftLen)$Hz 
% apart, which is the resolution or the minimum frequency that can be discriminated. 
% The number of frequencies at which the zoom FFT is computed equals the FFT length. 
%%
Fsd = Fs/D;
F = CF + (Fsd/fftlen)*(0:fftlen-1)-Fsd/2;
%% Initialize the Scope
% Create an array plot to show the frequencies in |F|.
%%
ap = dsp.ArrayPlot('XDataMode','Custom','CustomXData',F,...
    'YLabel','z .* conj(z)','XLabel','Frequency (Hz)','YLimits',[0 1.1e3],...
    'Title',sprintf('Decimation Factor = %d. Center Frequency = %d Hz. Resolution = %f Hz',D, CF,(Fs/D)/fftlen));
%% Sine Wave Generator
% Create a sine wave with frequencies at 1625 Hz, 2000 Hz, and 2125 Hz.
%%
tones = [1625 2000 2125];
sine = dsp.SineWave('SampleRate',Fs,'Frequency',tones,'SamplesPerFrame',L);
%% Streaming
% Pass a noisy sine wave with a sample rate of 48 kHz. Compute the zoom FFT 
% of this sine wave in the subband [1500 Hz 2500 Hz]. Rearrange the Fourier transform 
% by shifting the zero-frequency component to the center of the array. View the 
% tones at 1625 Hz, 2000 Hz, and 2125 Hz in the array plot.
%%
for i = 1:1000
    x = sum(sine(),2)+1e-1*randn(L,1);
    z = zfft(x);
    z = fftshift(z);
    ap(z.*conj(z));
end
%%
% 
% <<../arrayplot_zoomfft.png>>
% 

%% 
% Copyright 2012 The MathWorks, Inc.