# Original link:http://tecdat.cn/?p=22467

This paper shows how to use extended Kalman filter for fault detection. In this paper, the extended Kalman filter is used to estimate the friction of a simple DC motor on-line. Significant changes in the estimated friction are detected and indicate the presence of a fault.

### Motor model

The motor is simulated to have a damping coefficient C_ Moment of inertia_ J. Driven by a torque U. Angular velocity W and acceleration of motor ˙ W is the measurement output.

In order to estimate the damping coefficient C using the extended Kalman filter, an auxiliary state is introduced for the damping coefficient and its derivative is set to zero.

Therefore, the model state, x = [w; C], and the measurement, y, equation is:

Use approximation ˙ X = xn + 1-xnts converts the continuous time equation into discrete time, where TS is the discrete sampling period. This leads to the discrete-time model equation.

Specify motor parameters

```
J = 10; % inertia
Ts = 0.01; % Sampling time
```

Specify the initial state.

```
x = \[...
0 ... % angular velocity
1\]; % friction
```

```
% State renewal equation of motor with friction as state
% Status update equation
x1 = \[...
x0(1)+Ts/J*(u-x0(1)*x0(2)); ...
x0(2)\];
```

```
% Measurement equation of motor in friction state
% Output.
% y - Motor measurement elements \ [angular velocity; angular acceleration \].
y = \[...
x(1); ...
(u-x(1)*x(2))/J\];
```

The motor experiences state (process) noise interference, Q, and measurement noise interference, R. The noise terms are additive.

The average value of process and measurement noise is zero, E [q] = E [R] = 0, covariance q = E [QQ ‘] and R = E [RR’]. The friction state has high process noise interference. This reflects our hope that the friction will change during normal operation of the motor, and the filter can track this change. The noise of acceleration and velocity state is very low, but the noise of velocity and acceleration measurement is relatively large.

Specifies the process noise covariance.

```
\[...
1e-6 0 ... % angular velocity
0 1e-2\]; % friction
```

Specifies the measurement noise covariance.

```
\[...
1e-4 0 ... % velocity measurement
0 1e-4\]; % Acceleration measurement
```

### Create an extended Kalman filter

An extended Kalman filter is created to estimate the state of the model. We pay particular attention to the damping state because a sharp change in the value of this state indicates the presence of a fault event.

Create an extended Kalman filter object and specify the Jacobian coefficients of the state transition and measurement functions.

The input parameters of the extended Kalman filter are the previously defined state transition and measurement functions. The initial state value x0, the initial state covariance, the process and measurement noise covariance are also the inputs of the extended Kalman filter. In this example, the exact Jacobian function can be obtained from the state transition function f and the measurement function H.

```
% output
Jac - State Jacobian coefficient calculated at x
% Jacobs coefficient
Jac = \[
1-Ts/J\*x(2) -Ts/J\*x(1); ...
0 1\];
% Jacobs coefficient of measurement equation of motor model
% output
Jac - stay x Measured Jacobian coefficient calculated at
% Jacobs coefficient
J = \[ ...
1 0;
-x(2)/J -x(1)/J\];
```

### Simulation

In order to simulate the factory, a loop is created and a fault is introduced into the motor (fictitious motor drastic change). In the simulation loop, the extended Kalman filter is used to estimate the motor state, especially to track the friction state and detect when the friction changes statistically.

The motor is simulated as a pulse sequence, accelerating and decelerating repeatedly. This type of motor operation is typical for the picking robot on the production line.

When simulating the motor, the process and measurement noise similar to the covariance difference of Q and R noise used in the construction of extended Kalman filter are added. For friction, a much smaller noise value is used because friction is mostly constant except when faults occur. Artificially induced faults in the simulation process.

```
Qv = chol(Q); % Standard deviation of process noise
Qv(end) = 1e-2; % Less friction noise
Rv = chol(R); % Standard deviation of measurement noise
```

The state update equation is used to simulate the model, and the process noise is added to the model state. After ten seconds of simulation, the friction force of the motor is forcibly changed. The model measurement function is used to simulate the motor sensor, and the measurement noise is added to the model output.

```
for ct = 1:numel(t)
% Model output update
y = y+Rv*randn(2,1); % Add measurement noise
% Model status update
xSig(:,ct) = x0;
% Induced friction change
if t(ct) == 10
x1(2) = 10; % Step change
x1n = x1+Qv*randn(nx % Adding process noise
```

`Significant friction change at 10.450000`

In order to estimate the motor state from the motor measurements, the prediction and correction commands of the extended Kalman filter are used.

```
% State estimation using extended Kalman filter
x_ corr = correct(ekf,y,u(ct),J,Ts); % The state estimation is corrected according to the current measurement results.
predict(ekf,u(ct),J,Ts); % Predict the next state based on the current state and input.
```

In order to detect the change of friction, a 4-second moving window is used to calculate the estimated mean and standard deviation of friction. After the first 7 seconds, lock the calculated mean and standard deviation. This initially calculated average is the expected failure free average of friction. After 7 seconds, if the estimated friction is more than 3 standard deviations from the expected failure free mean, it means that the friction has changed significantly. In order to reduce the influence of noise and estimated friction, the average value of estimated friction is used when compared with the limit of 3 standard deviations.

```
% Calculate the estimated mean and standard deviation.
else
% Store the calculated mean and standard deviation, no
%Recalculate.
fMean(ct) = fMean(ct-1)
% Use the expected friction mean and standard deviation to detect
%Friction changes.
estFriction = mean(xSigEst(2,
if fChanged(ct) && ~fChanged(ct-1)
% Detect the rising edge of the friction change signal | fchanged|
```

The estimated state is used to calculate the estimated output. Calculate the error between the measured output and the estimated output, and calculate the error statistics. Error statistics can be used to detect the change of friction. This will be discussed in detail later.

```
kurtosis(ySigEst(1,idx)-ySig(1,idx));
kurtosis(ySigEst(2,idx)-ySig(2,idx))\];
```

### Extended Kalman filter performance

Note that a friction change was detected at 10.45 seconds. Let’s now describe how this fault detection rule is derived. First, check the simulation results and the performance of the filter.

```
figure,
plot(t, Sig(1,:) Sig(2,:));
```

The input-output response of the model shows that it is difficult to detect the change of friction directly from the measured signal. The extended Kalman filter can estimate the state, especially the friction state. Compare the real model state and estimated state. The estimated state shows the confidence interval corresponding to 3 standard deviations.

`plot(t, True(1,:), t, Est(1,:), ...`

Note that the estimated value of the filter tracks the real value, and the confidence interval is still bounded. Checking the estimation error can get a deeper understanding of the filter.

`plot(t,True(1,:)-Est(1,:)`

The error diagram shows that the filter is adjusted after 10 seconds of friction change, and the estimation error is reduced to zero. However, error graphs cannot be used for fault detection because they depend on the understanding of the real state. Comparing the measured state values with the estimated state values of acceleration and velocity can provide a detection mechanism.

`plot(t,Sig(1,:-Est(1,:)`

The acceleration error diagram shows that there is a small difference in the average error about 10 seconds after the fault is introduced. Check the error statistics to see if the fault can be detected from the calculated error. Acceleration and velocity errors are expected to be normally distributed (noise models are Gaussian). Therefore, the kurtosis of acceleration error may help to identify the change from symmetry to asymmetry due to the change of friction and the resulting error distribution.

`plot(t,Kur(1,:)`

Ignoring that the estimator is still converging and collecting data for the first 4 seconds, the kurtosis of the error is relatively stable, and there is a small change near 3 (the expected kurtosis value of Gaussian distribution). Therefore, in this application, error statistics cannot be used to automatically detect the change of friction. In this application, it is also very difficult to use the kurtosis of the error, because the filter is adapting and constantly pushing the error to zero, and only gives a short time window with an error distribution different from zero.

Therefore, in this application, the use of estimated changes in friction provides the best way to automatically detect motor faults. Friction estimates (mean and standard deviation) from known fault free data provide expected limits of friction, which can be easily detected when these limits are exceeded. The following figure emphasizes this fault detection method.

`plot(t,x,\[nan t\],\[Mean+3\*STD,Mean-3\*STD\]`

### abstract

This example shows how to use the extended Kalman filter to estimate the friction of a simple DC motor and use the friction estimation for fault detection.

Most popular insights

1.**Matlab uses empirical mode decomposition EMD to denoise the signal**

2.**Matlab uses Hampel filter to remove outliers**

3.**Matlab partial least squares regression (PLSR) and principal component regression (PCR)**

4.**Matlab predicts arma-garch conditional mean and variance model**

5.**Using VMD (variational modal decomposition) in MATLAB**

6.**Deep learning using Bayesian Optimization in MATLAB**

7.**Matlab Bayesian hidden Markov HMM model**

8.**Implementation of hidden Markov model (HMM) in MATLAB**

9.**Realizing Markov switching ARMA – GARCH model of MCMC with MATLAB**