7

There is sinusoidally controlled signal, which other than being noisy, can change values for amplitude, frequency, phase and offset. At every new sample a new sine is fitted for the last N samples. These fitted signals might be different due to noise or due to the signal changing value. To filter it, I would like to use a Kalman filter to estimate the actual sine and to smoothen the transition of the above mentioned parameters.

I tried to get familiar with Kalman filters, but most of the examples deal with only estimating one parameter, and in my case the parameters are not independent.

Could somebody provide some hints on how to get started, or knows how to do it?

Royi
  • 19,608
  • 4
  • 197
  • 238
  • "a sine is fitted": How so? The usual method of doing this would be employing a PLL, not making estimates only based on the last few samples. – Marcus Müller Jul 24 '21 at 13:09
  • Okay, this one is not done with a PLL. Can we focus on the Kalman filtering part? – user3761419 Jul 24 '21 at 14:17
  • no, we can't: the Kalman needs a model of the way you estimate and how consecutive estimates depend on each other. So, the information you're omitting here is critical to any answer. – Marcus Müller Jul 24 '21 at 14:18
  • Can’t it be handled as 2 different, but inaccurate observations? – user3761419 Jul 24 '21 at 14:33
  • no, it can't. The whole idea of the Kalman filter is that you have a state transition matrix and an observation matrix. – Marcus Müller Jul 24 '21 at 14:46
  • How many cycles of the source sine wave are you getting? Many? Some small but exact number of cycles? Just a few cycles, and not an integer number of them? – TimWescott Jul 24 '21 at 16:58
  • "At every new sample a new sine is fitted for the last N samples." That's not a good fit for a Kalman filter -- is this a system requirement, or just how you want to do it? Do you have an idea of how the sine wave parameters change with time, that is good enough that you can make a model of the parameters' dynamics? – TimWescott Jul 24 '21 at 17:00
  • Less than 1 cycle. If I could model it, I would. – user3761419 Jul 24 '21 at 17:10
  • Is it my understanding that a kalman filter operates on statistical distributions of how likely the observation is, given the previous observations and the control of the system. Offset + Amplitude * sin(2pif*t + phi) – user3761419 Jul 24 '21 at 17:55
  • 1
    That is correct -- and it's sounding like it's not suitable because, first, you need an accurate model, second, the Kalman filter isn't the best choice if you're always working with a fixed number of samples after the fact, and, third, this is a nonlinear optimization problem for which a Kalman might be suitable if you had a decent model and were doing this on-line instead of after the fact with a fixed-size vector, but certainly isn't going to be optimal for the situation you describe. – TimWescott Jul 24 '21 at 23:58
  • 1
    You should edit your question with that information about the signal being less than a cycle, and about you not being able to model it. If you can't model it though, then there's very little basis for designing any kind of estimator -- while you're writing the rest into your question, add in what you do know about your signal (and the noise) that might be pertinent. – TimWescott Jul 25 '21 at 00:00
  • @user.. Sounds like you need help mathematically describing your signal and your estimation method more than you need help with anything related to a Kalman filter. You should really edit your question and describe all you know about your signal, and equally importantly, what your "secret" method of fitting the sine is. – mmmm Jul 25 '21 at 02:21
  • I just think discussing the fitting method is diverting from the topic. – user3761419 Jul 25 '21 at 06:44

1 Answers1

7

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:

enter image description here

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).

Royi
  • 19,608
  • 4
  • 197
  • 238
  • Thank you! Your interpretation is correct. – user3761419 Jul 25 '21 at 06:46
  • Thank you very much Royi! – user3761419 Jul 25 '21 at 19:05
  • @user3761419, You're welcome. You're invited to read and +1 the other question I linked to. It will assist you as well. – Royi Jul 25 '21 at 20:21
  • This is very interesting, normally I wouldn't see EKF as an appropriate use of this. I still wonder how you would select the right parameters though to balance measurement noise vs state change considering the model is dynamic – FourierFlux Jul 26 '21 at 07:28
  • @FourierFlux, Well what you ask is basically the art of Kalman Filter. – Royi Jul 26 '21 at 14:27
  • @Royi, thank you again, it is excellent and very useful help. I had time to try get familiar with your solution and have 2 more questions:
    1. If there is an offset, that may vary with time as well, how would the matrices, equations and variances change?
    2. If I would like the filter to pick up faster on the changes in the sine wave, even on the expense of accuracy, could you give me some advice on which parameters should I play with, and what order of magnitude of change is reasonable?
    – user3761419 Sep 01 '21 at 21:39
  • 1
    For DC you may have a look at https://dsp.stackexchange.com/questions/36135. For higher bandwidth filter you may increase the process noise ($ \sigma_{a} $ and $ \sigma_{\omega} $). – Royi Sep 02 '21 at 03:44
  • Thank you @Royi! One more thing. I have tried the extended and also the unscented kalman filter you linked, and in some cases they lose stability. Either by starting to oscillate; or by not following the signal anymore, and taking up a seemingly fixed output. Could you advice me on what could be the cause and what should I pay attention to? – user3761419 Sep 22 '21 at 16:52
  • @user3761419, Without seeing the exact data and code it would be hard to tell. But usually optimal balance between process and measurement noise is the secret sauce of the Kalman Filter. – Royi Sep 22 '21 at 19:52
  • @Royi: For example dT, what would be the optimal value? How would it be calculated? – user3761419 Sep 29 '21 at 13:23
  • Usually dT is a property of the system (The measurement rate). In this model, which assumes white noises, the smaller dT while noises are still white, the better. – Royi Sep 29 '21 at 17:02
  • Amazing answer. Would it be possible to create a multiresolution version of this? So if there are $N$ sinusoidal components, the state would be of size $3N$ - is it possible to do that or would the various components blend into each other? – SuperCodeBrah Aug 28 '22 at 04:50
  • @SuperCodeBrah, Thanks for the compliments. Open a question and let's give it a try :-). – Royi Aug 29 '22 at 18:38
  • Just added a question here: https://dsp.stackexchange.com/questions/84361/unscented-kalman-filter-for-tracking-amplitude-frequency-and-phase-of-a-multi. For some reason, I couldn't get it to work even for a single component, but maybe you'll be able to spot something. Feel free to edit the question or title as needed. – SuperCodeBrah Aug 30 '22 at 05:42
  • I also found a paper here that indicates that it is possible: https://arxiv.org/pdf/1711.04644.pdf. It has a different implementation but I went with the one you provided since it was more intuitive to me. – SuperCodeBrah Aug 30 '22 at 05:44
  • @Royi thanks a lot. If anyone interested, i've made a wed interactive demo of this in JS at observablehq.com/d/a033acc0859cc0de based on github.com/piercus/kalman-filter – piercus Mar 24 '23 at 17:49
  • Could you tell me, like, step by step deriving and modeling the F and Q matrices from scratch ? or do you have literature about that? Thanks.! – unwantednoise Dec 11 '23 at 05:31