# Kalman Filter on Sinusoidal Signal

Suppose a system follows this equation: $$x(t)=A \cos(\omega t + \phi)+\eta$$

where: $$\omega = 2\pi f$$ and $$\eta$$ is a random error

using Extended Kalman Filter, how does estimated value $$\hat{x}$$ be?

I'm copying my answer to Estimate and Track the Amplitude, Frequency and Phase of a Sine Signal Using a Kalman Filter which solves a more general problem with example code:

We can build a non linear dynamic model in order to estimate the parameters of a sine signal.

Let's model the signal as $$a \sin \left( \phi \right)$$ where $$\phi$$ is the instantaneous phase. So the model could be also written as $$a \sin \left( \omega t + \psi \right)$$.

Then the model can be:

$${a}_{k} \sin \left( {\omega}_{k} {t}_{k} + \psi \right) = {a}_{k} \sin \left( {\phi}_{k} \right)$$

With some math and pre processing of Kalman Filter you may derive the model with the matrices:

$$\boldsymbol{x}_{k} = \begin{bmatrix} {a}_{k} \\ {\omega}_{k} \\ {\phi}_{k} \end{bmatrix}, F = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & \Delta t & 1 \end{bmatrix}, Q = \begin{bmatrix} \Delta t {\sigma}_{a}^{2} & 0 & 0 \\ 0 & \Delta t {\sigma}_{\omega}^{2} & \frac{ {\Delta t}^{2} {\sigma}_{\omega}^{2}}{2} \\ 0 & \frac{ {\Delta t}^{2} {\sigma}_{\omega}^{2}}{2} & \frac{ {\Delta t}^{3} {\sigma}_{\omega}^{2}}{3} \end{bmatrix}$$

Where $${\sigma}_{a}^{2}$$ is the process variance of the amplitude and $${\sigma}_{\omega}^{2}$$ is the variance of the process noise of instant angular frequency.

The measurement model is a bit more tricky. The measurement model is:

$${z}_{k} = h \left( \boldsymbol{x}_{k} \right) = {a}_{k} \sin \left( {\phi}_{k} \right)$$

Hence the Jacobian is given by $$\frac{\partial h \left( \boldsymbol{x}_{k} \right )}{\partial \boldsymbol{x}_{k}} = \left[ \sin \left( {\phi}_{k} \right), 0, {a}_{k} \cos \left( {\phi}_{k} \right) \right]$$.

Wrapping all this into a Kalman Model will yield:

You may see that the model can effectively track changes in the parameters.
There are other alternatives to this dynamic model but I think this is a simple and effective one.

You may also use the Unscented Kalman Filter. I implemented it at Extended Kalman Filter (EKF) for Non Linear (Coordinate Conversion - Polar to Cartesian) Measurements and Linear Predictions.

The code is available at my StackExchange Signal Processing Q76443 GitHub Repository (Look at the SignalProcessing\Q76443 folder).

– Peter K.
Dec 6, 2021 at 18:46
• @PeterK., Thanks. There are many ways to derive the model matrix for harmonic signals.
– Royi
Dec 6, 2021 at 18:50
• The parameters dT is the rate new measurement is measured. Of course it has to be at least by Nyquist. You may use dT = 1 and then everything is in normalized frequency.
– Royi
Dec 8, 2021 at 11:23
• I am not sure what you mean. Assume you have which measures a sine signal. By Sampling Theorem you must sample it at sampling rate which is larger than 2 times the bandwidth. But in many cases we sample at much higher rate. In any case, dT must be the sampling rate in practice and not the Nyquist rate.
– Royi
Dec 8, 2021 at 16:47
• @piercus, This is really nice! I wish I knew JS.
– Royi
Mar 24, 2023 at 18:11

This isn't quite what you're asking, because it neglects the amplitude, $$A$$, but it's a relatively straightforward example of application of an extended Kalman filter to the frequency tracking problem. See section 1.2 of this PDF, that I wrote some time ago.

I'd also recommend starting with B. D. O. Anderson and J. B. Moore, Optimal Filtering, Prentice-Hall, Inc., Engle- wood Cliffs, New Jersey 07632, 1979.

• I like the reference Peter, your PDF is a nice summary Dec 6, 2021 at 13:31
• @DanBoschen For an unpublished article (in the journal or conference sense), that PDF has received more citations than the IEEE TSP paper for my PhD. Oh well. ;-)
– Peter K.
Dec 6, 2021 at 15:53
• I'm giving a callout to Dan Simon's "Optimal State Estimation: Kalman, H Infinity, and Nonlinear Approaches", Wiley, 2006. I think it'll be clear to someone who's taken a senior-level statistics class and state-space control. The only downside is that after it was published someone came up with a formal way to determine the constellation for an Unscented Kalman filter, and I can't remember who wrote the paper or when (aside from "after 2006"). Dec 6, 2021 at 20:24
• @TimWescott thanks,Tim! I’ll see if I can get that.
– Peter K.
Dec 6, 2021 at 20:47
• @TimWescott, Could it be you're talking about Cubature Kalman Filter? If so, then it is a generalization of the UKF and actually it shows that in most cases the UKF is the optimal constellation.
– Royi
Dec 8, 2021 at 11:24