%% Power and Max-Hold Spectra of Noisy Sine Wave
% *Note*: If you are using R2016a or an earlier release, replace each call 
% to the object with the equivalent |step| syntax. For example, |obj(x)| 
% becomes |step(obj,x)|.
% 
% Generate a sine wave.

sineWave = dsp.SineWave('Frequency',100,'SampleRate',1000, ...
    'SamplesPerFrame',1000);
%% 
% Use the spectrum estimator to compute the power spectrum and the max-hold 
% spectrum of the sine wave. Use the Array Plot to display the spectra.
%%
SE = dsp.SpectrumEstimator('SampleRate',sineWave.SampleRate, ...
    'SpectrumType','Power','PowerUnits','dBm', ...
    'FrequencyRange','centered','OutputMaxHoldSpectrum',true);
plotter = dsp.ArrayPlot('PlotType','Line','XOffset',-500, ...
    'YLimits',[-60 30], 'Title','Power Spectrum of 100 Hz Sine Wave', ...
    'YLabel','Power Spectrum (dBm)','XLabel','Frequency (Hz)');
%% 
% Add random noise to the sine wave. Stream in the data, and plot the power 
% spectrum of the signal.
%%
for ii = 1:10
    x = sineWave() + 0.05*randn(1000,1);
    [Pxx,Pmax] = SE(x);
    plotter([Pxx Pmax])
end
%%
% 
% <<../arrayplot_powerspectrumnoisy.png>>
% 

%% 
% Copyright 2012 The MathWorks, Inc.