%% Display Frequency Input from Spectral Estimation
% Use the Spectrum Analyzer to display frequency input from spectral estimates 
% of sinusoids embedded in white Gaussian noise.
%% Initialization
% Initialize two |dsp.SpectrumEstimator| objects to display. Set one object to use 
% the Welch-based spectral estimation technique with a Hann window, set the other 
% object use a filter bank estimation. Specify a noisy sine wave input signal 
% with four sinusoids at 0.16, 0.2, 0.205, and 0.25 cycles/sample. View the spectral 
% estimate using a third object, a spectrum analyzer, set to process frequency input. 

FrameSize = 420;
Fs = 1;
Frequency = [0.16 0.2 0.205 0.25];
sinegen = dsp.SineWave('SampleRate',Fs,'SamplesPerFrame',FrameSize,...
    'Frequency',Frequency,'Amplitude',[2e-5 1  0.05  0.5]);
NoiseVar = 1e-10;
numAvgs = 8;

hannEstimator = dsp.SpectrumEstimator('PowerUnits','dBm',...
    'Window','Hann','FrequencyRange','onesided',...
    'SpectralAverages',numAvgs,'SampleRate',Fs);

filterBankEstimator = dsp.SpectrumEstimator('PowerUnits','dBm',...
    'Method','Filter bank','FrequencyRange','onesided',...
    'SpectralAverages',numAvgs,'SampleRate',Fs);

spectrumPlotter = dsp.SpectrumAnalyzer('InputDomain','Frequency',...
    'SampleRate',Fs/FrameSize,...
    'SpectrumUnits','dBm','YLimits',[-120,40],...
    'PlotAsTwoSidedSpectrum',false,...
    'ChannelNames',{'Hann window','Filter bank'},'ShowLegend',true);
%% Streaming
% Stream the input. Compare the spectral estimates in the spectrum analyzer.
%%
for i = 1:1000
    x = sum(sinegen(),2) + sqrt(NoiseVar)*randn(FrameSize,1);
    Pse_hann = hannEstimator(x);
    Pfb = filterBankEstimator(x);
    spectrumPlotter([Pse_hann,Pfb])
end

%% 
% Copyright 2012 The MathWorks, Inc.