www.gusucode.com > dsp 案例源码程序 matlab代码 > dsp/UsingSpectrumEstimatorSystemObjectExample.m
%% Estimate the PSD Using dsp.SpectrumEstimator % Using the |dsp.SpectrumAnalyzer| System object(TM), you can view and analyze, % but cannot process the PSD data. To process the data, you must % compute the spectral density using one of the Estimator objects in the % |Estimation| library. To view the objects % in the |Estimation| library, type |help dsp| in the MATLAB(R) command % prompt, and click |Estimation|. %% Initialization % Use the same source as in the previous section on using the % |dsp.SpectrumAnalyzer| to estimate the PSD. % The input sine wave has two frequencies: one at 1000 Hz and the other % at 5000 Hz. % Initialize |dsp.SpectrumEstimator| to compute the PSD of the % signal, and |dsp.ArrayPlot| object to view the PSD of the signal. Fs = 44100; Sineobject1 = dsp.SineWave('SamplesPerFrame',1024,'PhaseOffset',10,... 'SampleRate',Fs,'Frequency',1000); Sineobject2 = dsp.SineWave('SamplesPerFrame',1024,... 'SampleRate',Fs,'Frequency',5000); SpecEst = dsp.SpectrumEstimator('SpectrumType','Power density',... 'PowerUnits','dBm','SampleRate',Fs,... 'FrequencyRange','onesided'); ArrPlot = dsp.ArrayPlot('PlotType','Line','ChannelNames',{'PSD of the input'},... 'YLimits',[-90 20],'XLabel','Number of samples per frame','YLabel',... 'Power (dBm)','Title','One-sided power spectrum with respect to samples'); %% Estimation % Stream in and estimate the PSD of the signal. % Construct a |for|-loop to run for 5000 iterations. In each iteration, % stream in 1024 samples (one frame) of each sine wave and compute the PSD of % each frame. % Add Gaussian noise with mean at 0 and a standard deviation of % 0.001 to the input signal. for Iter = 1:5000 Sinewave1 = Sineobject1(); Sinewave2 = Sineobject2(); Input = Sinewave1 + Sinewave2; NoisyInput = Input + 0.001*randn(1024,1); PSDoutput = SpecEst(NoisyInput); ArrPlot(PSDoutput); end %% Convert _x_-axis to Represent Frequency % By default, |dsp.ArrayPlot| plots the PSD data with respect to the % number of samples per frame. The number of points on the _x_-axis equals % the length of the input frame. For a one-sided spectrum, the span of _x_-axis % equals half the input frame. % The spectrum analyzer plots the PSD data with respect to frequency. % For a one-sided spectrum, the frequency varies in the range [0 Fs/2]. % For a two-sided spectrum, frequency varies in the range [-Fs/2 Fs/2]. % To convert the _x_-axis of the array plot from sample based to frequency % based, you must set the $SampleIncrement$ property of the |dsp.ArrayPlot| % object to $Fs/framelength$. For a one-sided spectrum, $XOffset$ property of % |dsp.ArrayPlot| must be 0. For a two-sided spectrum, $XOffset$ must % be -Fs/2. % In this example, for a one-sided spectrum, $SampleIncrement$ must be % 44100/1024. ArrPlot.SampleIncrement = Fs/1024; ArrPlot.XLabel = 'Frequency (kHz)'; ArrPlot.Title = 'One-sided power spectrum with respect to frequency'; for Iter = 1:5000 Sinewave1 = Sineobject1(); Sinewave2 = Sineobject2(); Input = Sinewave1 + Sinewave2; NoisyInput = Input + 0.001*randn(1024,1); PSDoutput = SpecEst(NoisyInput); ArrPlot(PSDoutput); end %% Live Processing % To process the spectral data while streaming, pass the output of the estimator % block into a processing logic.