 ## Simple Scalar Kalman Filter. C Implementation Using the Octave GNU Tool

### 1. Abbreviation  ### 2. Introduction

##### See the Figure 2-1. ### 3. Autoregressive (AR) Model of ‘x’ state (signal) Generation

##### To describe the Kalman filter, it is convenient to represent the ‘x’ state (signal) using the autoregressive model (AR). The AR model of the N-th order: AR (N) is defined by: ##### We will use the first-order model AR(1) and const = 0 for the scalar Kalman filter. Then (7) in this case will be rewritten as: ##### This model is stable at values of φ: ##### I will use the values of φ: ##### Formulas (9) and (11) define the first order IIR Low Pass Filter through which white noise is passed. See the Figure 3-1: ### 4. Model of the ‘y’ signal Measurement

##### To build a Kalman filter, we need to define a measurement model. We will use the measurement model shown in Figure 4-1. In this case it is necessary to determine the ‘x’ signal, but in reality we can measure only the value of the ‘y’ signal:  ### 5. Kalman Filter

##### Knowing the signal model (Figure 3-1) and the measurement channel (Figure 4-1), we can build a Kalman filter that minimizes the error of ‘x’ signal (state) estimation. The structure of the discrete Kalman filter is shown in Figure 5-1: #####   ### 6. Example

##### Let’s consider a sinusoidal signal as an example: ##### To construct a Kalman filter, it is necessary to represent this signal (30) in the form of an AR(1) model. See the Figure 3-1. We shall to determine the parameters: ##### It is known that the power of the sinusoidal signal (30): ##### To determine φ, we use the formula (16a) for n=1: ##### Note: Next we will use the well-known formula: ##### Substituting (30) and (31) into (32) and using (33), as well as moving from the ensemble average to the time average: ##### Then for a sinusoidal signal we get: ##### Let’s determine the noise power using (15) and (31): ##### Now we need to set the parameters of our measurement model. See the Figure 4-1. Let’s take as an example: ##### It is obvious that the Signal-To-Noise Ratio (SNR) for the given example: ##### Figure 6-1 Original ‘x’ signal   ##### Figure 6-3: Signal and Noise from Measurement Model   ##### Figure 6-5: Kalman Filter: Kalman Gain ### 7. Octave GNU Script SimpleKalmanFilterSupport.m

##### Script Functions:

% Float Test Signal Table to File
% Support C-Code
% Input parameters:
% Variance_w – noise power for the AR(1) signal model
% Variance_v – disturb noise power
% phiValue – AR(1) model parameter
% hValue – measurement model
% signalLength – signal length
% FileNameString – output file name
% Output:
% txt file with the signal data
% testSignal = Amplitude*cos(2*pi*n/signalLength) + whiteNoise
function testSignal = FloatTestSignal2file(Variance_w, Variance_v, phiValue, hValue, periodLength, FileNameString)

% Scalar Kalman Filter
% Input parameters:
% signalIn – Input Signal
% Variance_w – noise power for the AR(1) signal model
% Variance_v – disturb noise power
% phiValue – AR(1) model parameter
% hValue – measurement model
% P0 – start value of the estimate error
% x0 – start value of the x
% Output:
% signalOut – output of the Kalman Filter
% KalmanGain – Kalman Gain
% P_Variance – P estimate error
function [signalOut, KalmanGain, P_Variance] = ScalarKalmanFilter(signalIn, Variance_w, Variance_v, phiValue, hValue, P0, x0)

### 8. C Code file SimpleKalmanFilter.c/h

##### Functions:

/*
Scalar Kalman Filter
Input
float input – input measured signal
scalar_kalman_filter_data_s* kalman_data – Kalman filter data
Return
float x estimate value
*/
float ScalarKalmanFilter(float input, scalar_kalman_filter_data_s* kalman_data)

/* Only Test */
int main() Test support