www.gusucode.com > dsp 案例源码程序 matlab代码 > dsp/EstimateAChangingScalarExample.m

    %% Estimate a Changing Scalar
%%
% *Note*: This example runs only in R2016b or later. If you are using an
% earlier release, replace each call to the function with the equivalent
% |step| syntax. For example, myObject(x) becomes step(myObject,x).

%%
% Create the System objects for the changing scalar input, the Kalman
% filter, and the scope (for plotting).
numSamples = 4000;
R = 0.02;
hSig = dsp.SignalSource;
hSig.Signal = [ones(numSamples/4,1);   -3*ones(numSamples/4,1);...
    4*ones(numSamples/4,1); -0.5*ones(numSamples/4,1)];
hTScope = dsp.TimeScope('NumInputPorts', 3, 'TimeSpan', numSamples, ...
    'TimeUnits', 'Seconds', 'YLimits',[-5 5], ...
    'ShowLegend', true); % Create the Time Scope
hKalman = dsp.KalmanFilter('ProcessNoiseCovariance', 0.0001,...
    'MeasurementNoiseCovariance', R,...
    'InitialStateEstimate', 5,...
    'InitialErrorCovarianceEstimate', 1,...
    'ControlInputPort',false); %Create Kalman filter

%%
% Add noise to the scalar, and pass the result to the Kalman filter.
% Stream the data, and plot the filtered signal.
while(~isDone(hSig))
   trueVal = hSig();
   noisyVal = trueVal + sqrt(R)*randn;
   estVal = hKalman(noisyVal);
   hTScope(noisyVal,trueVal,estVal);
end