%% Compute Cross-Power Spectrum of Two Noisy Sine Waves
% 
% *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 two sine waves.

sin1 = dsp.SineWave('Frequency',200, 'SampleRate', 1000);
sin1.SamplesPerFrame = 1000;
sin2 = dsp.SineWave('Frequency',100, 'SampleRate', 1000);
sin2.SamplesPerFrame =  1000;
%% 
% Use the |dsp.CrossSpectrumEstimator| System object(TM) to compute the cross-spectrum of the 
% signals. Also, use the |dsp.ArrayPlot| object to display the spectra.
%%
cse = dsp.CrossSpectrumEstimator('SampleRate', sin1.SampleRate,...
    'FrequencyRange','centered');
aplot = dsp.ArrayPlot('PlotType','Line','XOffset',-500,'YLimits',...
    [-150 -60],'YLabel','Power Spectrum Density (Watts/Hz)',...
    'XLabel','Frequency (Hz)',...
    'Title','Cross Power Spectrum of Two Signals');
%% 
% Add random noise to the sine waves. Stream in the data, and plot the cross-power 
% spectrum of the two signals.
%%
for ii = 1:10
x = sin1() + 0.05*randn(1000,1);
y = sin2() + 0.05*randn(1000,1);
Pxy = cse(x, y);
aplot(20*log10(abs(Pxy)));
end

%% 
% Copyright 2012 The MathWorks, Inc.