%% Filter Signal Through Complex Bandpass Decimator
% Filter an input signal through a complex bandpass decimator and visualize 
% the filtered spectrum in a spectrum analyzer.
%% Initialization
% Create a |dsp.ComplexBandpassDecimator| System object(TM) with center frequency 
% set to 2000 Hz, bandwidth of interest set to 1000 Hz, and sample rate set to 
% 48 kHz. The decimation factor is computed as the ratio of the sample rate to 
% the bandwidth of interest. The input to the decimator is a sine wave with a 
% frame length of 1200 samples with tones at 1625 Hz, 2000 Hz, and 2125 Hz. Create 
% a |dsp.SpectrumAnalyzer| scope to visualize the signal spectrum.
%%
Fs = 48e3;
CF = 2000;
BW = 1000;
D  =  Fs/BW;
FrameLength = 1200;
bpdecim = dsp.ComplexBandpassDecimator(D,CF,Fs);

sa = dsp.SpectrumAnalyzer('SampleRate',Fs/D,...
    'YLimits',[-120 40],...
    'FrequencyOffset',CF);

tones = [1625 2000 2125];
sin = dsp.SineWave('SampleRate',Fs,'Frequency',tones,...
    'SamplesPerFrame',FrameLength);

%% Visualize Filter Stages
% Using the |visualizeFilterStages| function, you can visualize the response 
% of each individual filter stage using FVTool.
%%
visualizeFilterStages(bpdecim)

%% Display Filter |info|
% The |info| function displays information about the bandpass decimator.
%%
fprintf('%s',info(bpdecim))

%% Stream In and Filter Signal
% Construct a for-loop to run for 1000 iterations. In each iteration, stream 
% in 1200 samples (one frame) of the noisy sine wave and apply the complex bandpass 
% decimator on each frame of the input signal. Visualize the input and output 
% spectrum in the spectrum analyzer, |sa|.
%%
for index = 1:1000
    x = sum(sin(),2) +  1e-4*randn(FrameLength,1);
    z = bpdecim(x);
    sa(z);
end
%% 
% The bandpass decimator with center frequency at 2000 Hz and a bandwidth 
% of 1000 Hz passes the three sine wave tones at 1625 Hz, 2000 Hz, and 2125 Hz. 
% 
% Change the center frequency of the decimator to 2400 Hz and filter the 
% signal.
%%
release(bpdecim);
bpdecim.CenterFrequency = 2400
%% 
% Configure the spectrum analyzer to show the bandwidth of interest, [-1900, 2900] Hz.
release(sa)
sa.FrequencyOffset = 2400;
%% 
% Stream in the data and filter the signal.

for index = 1:1000
    x = sum(sin(),2) +  1e-4 * randn(FrameLength,1);
    z = bpdecim(x);
    sa(z);
end
%% 
% The tones at 2000 Hz and 2125 Hz are passed through the decimator, while 
% the tone at 1625 Hz is filtered out.

%% 
% Copyright 2012 The MathWorks, Inc.