KalmanFilter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
dl 0
loc 63
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A value() 0 20 2
1
<?php
2
3
namespace Phperf\Pipeline\Vector;
4
5
class KalmanFilter implements VectorProcessor
6
{
7
    /** @var float|int Process noise (how variable data is expected to come) */
8
    public $processNoise;
9
    /** @var float|int Measurement noise (how strong is ) */
10
    public $measurementNoise;
11
12
    public $stateVector;
13
    public $controlVector;
14
    public $measurementVector;
15
    public $cov;
16
    public $x;
17
18
19
    /**
20
     * Create 1-dimensional kalman filter
21
     * @param float|int $processNoise Process noise
22
     * @param float|int $measurementNoise Measurement noise
23
     * @param float|int $stateVector State vector
24
     * @param float|int $controlVector Control vector
25
     * @param float|int $measurementVector Measurement vector
26
     * @param $cov
27
     * @param $x
28
     */
29
    function __construct($processNoise = 1, $measurementNoise = 1, $stateVector = 1, $controlVector = 0, $measurementVector = 1, $cov = null, $x = null)
30
    {
31
        $this->processNoise = $processNoise; // noise power desirable
32
        $this->measurementNoise = $measurementNoise; // noise power estimated
33
34
        $this->stateVector = $stateVector;
35
        $this->controlVector = $controlVector;
36
        $this->measurementVector = $measurementVector;
37
38
        $this->cov = $cov;
39
        $this->x = $x; // estimated signal without noise
40
    }
41
42
    /**
43
     * Filter a new value
44
     * @param  float $value Measurement
45
     * @param  float|int $u Control
46
     * @return float
47
     */
48
    function value($value, $u = 0)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
49
    {
50
        if (null === $this->x) {
51
            $this->x = (1 / $this->measurementVector) * $value;
52
            $this->cov = (1 / $this->measurementVector) * $this->measurementNoise * (1 / $this->measurementVector);
53
        } else {
54
            // Compute prediction
55
            $predX = ($this->stateVector * $this->x) + ($this->controlVector * $u);
56
            $predCov = (($this->stateVector * $this->cov) * $this->stateVector) + $this->processNoise;
57
58
            // Kalman gain
59
            $K = $predCov * $this->measurementVector *
60
                (1 / (($this->measurementVector * $predCov * $this->measurementVector) + $this->measurementNoise));
61
62
            // Correction
63
            $this->x = $predX + $K * ($value - ($this->measurementVector * $predX));
64
            $this->cov = $predCov - ($K * $this->measurementVector * $predCov);
65
        }
66
67
        return $this->x;
68
    }
69
}
70