www.gusucode.com > dsp 案例源码程序 matlab代码 > dsp/MovingVarianceOfNoisySquareWaveSignalExample.m
%% Moving Variance of Noisy Square Wave Signal % Compute the moving variance of a noisy square wave signal with varying % amplitude using the |dsp.MovingVariance| object. %% Initialization % Set up |movvarWindow| and |movvarExp| objects. % |movvarWindow| uses the sliding window method with a window length % of 800. |movvarExp| uses the exponentially weighting method with a % forgetting factor of 0.999. Create a time scope for viewing the output. FrameLength = 100; Fs = 100; movvarWindow = dsp.MovingVariance(800); movvarExp = dsp.MovingVariance('Method','Exponential weighting',... 'ForgettingFactor',0.999); scope = dsp.TimeScope('SampleRate',Fs,... 'TimeSpanOverrunAction','Scroll',... 'TimeSpan',1000,... 'ShowGrid',true,... 'BufferLength',1e7,... 'YLimits',[0 7e-4]); title = 'Actual (yellow) Sliding Window (blue) Exponentially Weighted (red) variance'; scope.Title = title; %% Compute the Variance % Generate a noisy square wave signal. Vary the amplitude of the square wave % after a given number of frames. Apply the sliding window method and the % exponentially weighting method on this signal. The actual variance is % _np_. This value is used while adding noise to the data. Compare the actual % variance with the computed variances on the time scope. count = 1; noisepower = 1e-4 * [1 2 3 4]; index = 1; for index = 1:length(noisepower) np = noisepower(index); yexp = np*ones(FrameLength,1); for i = 1:250 x = 1 + sqrt(np) * randn(FrameLength,1); y1 = movvarWindow(x); y2 = movvarExp(x); scope([yexp,y1,y2]); end end