I would like to ask about smoothing data by using Kalman filter. Due to quantization, I have data that is not smooth. How can I smooth this data by using Kalman Filter. For your information, the data is not constant and have some gradient.
y(t) = round(H*x_truth(:,t) + rand(1,1,"normal")*sqrt(R));
as the signal model's output equation, we can apply the same Kalman filter.
This is not really accurate, because the
round function is a nonlinearity sort of like quantization. However, quantization can also be modeled as an additive noise, so we'll proceed.
The results are shown in the plot below.
- the black line is the true position,
- the red
+signs are the quantized, noisy position measurements, and
- the green line is the Kalman filter's estimate of the position.
Is this the sort of "smoothing" you're interested in?
And the error between the true position and the Kalman filter's estimate is:
Including full scilab script for reference.
// Signal Model DeltaT = 0.1; F = [1 DeltaT; 0 1]; G = [DeltaT^2/2; DeltaT]; H = [1 0]; x0 = [0;0]; sigma_a = 0.1; Q = sigma_a^2; R = 0.1; N = 1000; a = rand(1,N,"normal")*sigma_a; x_truth(:,1) = x0; for t=1:N, x_truth(:,t+1) = F*x_truth(:,t) + G*a(t); y(t) = round(H*x_truth(:,t) + rand(1,1,"normal")*sqrt(R)); // <== Changed line!! end // Kalman Filter p0 = 100*eye(2,2); xx(:,1) = x0; pp = p0; pp_norm(1) = norm(pp); for t=1:N, [x1,p1,x,p] = kalm(y(t),xx(:,t),pp,F,G,H,Q,R); xx(:,t+1) = x1; pp = p1; pp_norm(t+1) = norm(pp); end // Plots figure(1); clf; plot(x_truth(1,:),'ko'); plot(y,'r+'); plot(xx(1,:),'g.'); figure(2); clf; plot(x_truth(1,:) - xx(1,:));
The Kalman gain is a function of the relative certainty of the measurements and current state estimate, and can be "tuned" to achieve particular performance.
A. If we can tune the gain parameter to be high, the filter will emphasize(follow) on the sensor measurements and will follow the sensor measurements.
-This kind of a setting will not allow you to smooth your data
B. If we can tune the gain parameter to be low, the filter will emphasize more(follow) on the values as predicted by the system/signal model and smooth out the noise in the data.
-This type of tuning would lead to smoothed data at the cost of responsiveness of the data to fluctuations
At the extremes, a gain of one causes the filter to ignore the state estimate entirely, while a gain of zero causes the measurements to be ignored.