# Smoothing data by using Kalman filter

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.

• Where Kalman filters usually start is with a signal model. Do you have a description of how the "smooth" version of the signal should look? Quantization can be modeled as simply additive noise, though it will depend on how severe the quantization is.
– Peter K.
Apr 29, 2013 at 14:51

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.

Here,

• 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,:));

• Thanks Peter for your reply. Yes the model looks like what you described. I have a question about your code. Based on the link above that you gave me, there is an acceleration. Can I know what acceleration value that you gave here? Is it random or a known value? May 3, 2013 at 8:05
• The acceleration in this model is just a Gaussian random variable with zero mean and standard deviation sigma_a. If you need to estimate acceleration, then the model can be updated to include that as part of the state vector, and either include jerk as the driver (as, again, a white noise process).
– Peter K.
May 3, 2013 at 11:45
• I tried to implement your example in Matlab, however the result is not as we expected. This is my code: inline code in backticks, May 3, 2013 at 11:54
• You might have to change things around to work in Matlab. The code above is scilab; close, but not really the same.
– Peter K.
May 3, 2013 at 11:55
• Yes. I have change it to Matlab. Only the part kalm() function in your example I have to do it manually. By the way, how can I add code and pictures like you did? May 3, 2013 at 11:57

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.

• Hi Naresh. Thanks for your reply. In my case here, the smoothness of data is more important than the response due to fluctuations. That's mean the gain parameter should be low right? May 3, 2013 at 8:10
• Yes. Precisely. May 3, 2013 at 8:32
• But how can we lower the gain parameter? Is it by increasing the measurement noise value, R? May 3, 2013 at 8:49