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

    %% Use a Unit Step To Track a Ramp Signal
%%
% *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).

%%
% Use a unit step as the control input to track a ramp signal.
% Create the ramp signal to be tracked, the control input, the Time Scope,
% and the Kalman Filter.
numSamples = 200;
R = 100;
hSig = dsp.SignalSource;
hSig.Signal = (1:numSamples)';
hControl = dsp.SignalSource;
hControl.Signal = ones(numSamples,1);

hTScope = dsp.TimeScope('NumInputPorts', 3, 'TimeSpan', numSamples, ...
    'TimeUnits', 'Seconds', 'YLimits',[-5 205], ...
    'Title', ['Noisy(channel 1), True(channel 2) and ',...
    'estimated(channel 3) values'], ...
    'ShowLegend', true);
hKalman = dsp.KalmanFilter('ProcessNoiseCovariance', 0.0001,...
    'MeasurementNoiseCovariance', R,...
    'InitialStateEstimate', 1,...
    'InitialErrorCovarianceEstimate', 1);

%%
% Add noise to the signal. Filter the signal using kalman filter. View the
% output using time scope.
while(~isDone(hSig))
    trueVal = hSig();
    ctrl = hControl();
    noisyVal = trueVal + sqrt(R)*randn;
    estVal = hKalman(noisyVal,ctrl);
    hTScope(noisyVal,trueVal,estVal);
end