Coefficient estimation

Discussions and questions about topics not covered above
jase
Posts: 4
Joined: 21 Mar 2016, 15:12
Software used: Spike2
1401 type: Many 1401 types
Location: London
Contact:

Coefficient estimation

Unread postby jase » 03 Sep 2020, 09:29

Hi,

I am wondering if anyone can comment on how spike2 estimates the initial coefficients for fitting a sinusoid model to data in particular the phase shift coefficient estimate i.e. a2 in the below formula

f(x) = a0 * sin(a1 * x + a2) + a3

I have tried calculating the phase difference at the first peak between my data and zero-phased lagged dummy data calculate with the other estimated coefficients but this returns a very different number compared to the same data in spike2.

My trigonometry is poor and any help would be much appreciated.

Cheers,

Jason Somers

User avatar
Tim Bergel
Site Admin
Posts: 2381
Joined: 19 Jun 2008, 14:40
Location: Cambridge, England
Contact:

Re: Coefficient estimation

Unread postby Tim Bergel » 03 Sep 2020, 16:11

The phase estimation is very simple, it appears to look for the first level crossing and base its estimate on that, clearly this could easily go awry and it relies upon the fitting process to get it correct.

I have tried calculating the phase difference at the first peak between my data and zero-phased lagged dummy data calculate with the other estimated coefficients but this returns a very different number compared to the same data in spike2

Do you mean it is different than Spike2's initial guess, or than the fitted data co-efficients which Spike2 generates? I am sure that, in some cases, the initial guess could be widely wrong. If you are seeing that the sine fitting is failing to get the phase correct, then that sounds like it needs more investigation - can you post a picture here of your data with the fitted curve drawn on top of it - if the initial guess is out but the fitting process generates the correct result then that is not a problem really.
Tim Bergel Cambridge Electronic Design

jase
Posts: 4
Joined: 21 Mar 2016, 15:12
Software used: Spike2
1401 type: Many 1401 types
Location: London
Contact:

Re: Coefficient estimation

Unread postby jase » 04 Sep 2020, 09:39

Hi Tim,

Thank you for your response.

Do you mean it is different than Spike2's initial guess, or than the fitted data co-efficients which Spike2 generates?


Yes, my initial estimate of phase using first peak, or first level crossing seems to be far different than Spike2's (see attached images) and I had initial thought if I could replicate how spike2 was performing this initial estimate I could increase the reliability of my fitting.

However, after trying to fit other data sections I realise that my other coefficient guesses fall short of spike2's which leads me to more questions about how spike2 performs initial estimates for the other three coefficients - if you dont mind me expanding the scope of this thread...

For a3 (or DC offset estimate) I just take the mean of the entire fit section - this guess is most consistent with spike2.

For a0 (or amplitude) I take max-min/2 - my guess is typically always higher than spike2.

And finally for a1 I take a fast fft and extract the strongest peak but due to the number of data points per typical fit and sample rate I have a resolution of only 10Hz - Spike2 seems to have a much better resolution here so I am considering using a slower temporal method (ie how many cycles/time unit).

If you are able to share any more information about how spike2 performs these initial guesses on all the coefficients I would be more appreciative.

idx[1]_coef_estimate.PNG
Spike2 initial guess at coefficients

idx[1]_Rcoeff_estimation.png
R phase estimate using first level crossing and other coefficients estimates from spike2

User avatar
Tim Bergel
Site Admin
Posts: 2381
Joined: 19 Jun 2008, 14:40
Location: Cambridge, England
Contact:

Re: Coefficient estimation

Unread postby Tim Bergel » 04 Sep 2020, 14:26

Hi Jase,

The other initial estimates are made by various methods - for example I guess that the offset is done in the same way as you. Others are clearly being done in a more complicated manner.

But the point is, as long as the initial guesses are roughly correct, the fitting process should converge on the correct result. You write of increasing the reliability of fitting - are you suffering from problems here? If the fits are OK, then surely the actual initial guesses made are unimportant?
Tim Bergel Cambridge Electronic Design

jase
Posts: 4
Joined: 21 Mar 2016, 15:12
Software used: Spike2
1401 type: Many 1401 types
Location: London
Contact:

Re: Coefficient estimation

Unread postby jase » 04 Sep 2020, 16:21

But the point is, as long as the initial guesses are roughly correct, the fitting process should converge on the correct result.


"Roughly" seems to be highly relative in some cases, in particular, if either frequency (a1) or the phase (a2) estimates are too different then my fitting fails. For frequency this difference can be less than 10Hz and still fail. Which leads to the answer to the next question...

You write of increasing the reliability of fitting - are you suffering from problems here? If the fits are OK, then surely the actual initial guesses made are unimportant?


... and I can usually rescue the fit using my method by substituting my initial estimates with one of the initial guesses (again usually the frequency or the phase) from the Spike2 estimates. Hence why I am trying to replicate the Spike2 estimations as close as possible as they, almost exclusively, perform better than my estimates.

There is certainly also problems with the actual regression I am doing for the fit but I first thought I would nail down the parameter estimates and once they are as close as possible to the spike2 estimates, I would then move on to improving the regression algorithm ;)

User avatar
Tim Bergel
Site Admin
Posts: 2381
Joined: 19 Jun 2008, 14:40
Location: Cambridge, England
Contact:

Re: Coefficient estimation

Unread postby Tim Bergel » 05 Sep 2020, 11:44

OK, the code starts by finding the mean and RMS values of the data. Then:

amplitude = rms*1.41
offset = mean
The frequency is estimated by using thresholds 1 RMS above and below the mean. Zero crossings are estimated by counting the times when these thresholds are crossed - the code is complex - and that gives us a frequency.
Phase is estimated looking at the first crossing of the mean+-RMS thresholds.

I hope that helps.
Tim Bergel Cambridge Electronic Design

jase
Posts: 4
Joined: 21 Mar 2016, 15:12
Software used: Spike2
1401 type: Many 1401 types
Location: London
Contact:

Re: Coefficient estimation

Unread postby jase » 06 Sep 2020, 11:54

I think I am almost there and should be able to get even closer with your latest advice. I hadnt include an RMS calculation in the amplitude estimate so that should help me get closer to the spike2's initial guesses. I had also changed to a similar way for the frequency estimate also - how many midline crossings in x time which provides a much closer estimate than a fast fft.

It is now working more consistently and agrees with the spike2 results.

Thank you for your help!


Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest