LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Sine-fit algorithm

Hi,

 

I  would like to fit my data (about 200 kpoints) with a sine-wave (with 4 parameters : A.sin(2pi.B.t + C) + D) in LabVIEW. I'm looking for a VI with a convergent algorithm (such as least-square solver) which return me the 4 parameters + the RMS error betwen measurement data and model data. Or why not a more efficient algorithm...

 

I have a look in "Optimisation function set" and also in "Ajustement function set" but I didn't find the good VI, Someone can advice me ?

 

Regards,

 

Benjamin

Message 1 of 17
(14,730 Views)

This is a fit, not an optimization. Look for nonlinear curve fit.

 

How many cycles do you have in your dataset? Make absolutely you have reasonable paramter guesses or you'll end up finding an alias frequency.

Message 2 of 17
(14,719 Views)

As altenbach said, fitting to a sine is not a straightforward process.  The fitting algorithms tend to use derivatives to "move" the values of the parameters toward the desired function.  However, the derivative of sine(Bt) is a function with the same frequency, so it does not tend to find the correct frequency well.If you have multiple cycles in your data an FFT can give you a good estimate of the frequency to use as a starting point for fitting.

 

Lynn

Message 3 of 17
(14,702 Views)

You can use the levenberg-marquard fit , but usually the extract single tone information work good enough 🙂

There is a paper from 99 ni'week about the performance of this vi, I added it, because I missed the link

 

Depending on your signal, it might be wise to try blocks of complete periods and mean the results....

 

I attached a vi that uses this single tone vi to monitor the line frequency , just abuse a headphone as a coil receiver and plug it into the mic input of your soundcard and place the headphone near a transformer...

 

Greetings from Germany
Henrik

LV since v3.1

“ground” is a convenient fantasy

'˙˙˙˙uıɐƃɐ lɐıp puɐ °06 ǝuoɥd ɹnoʎ uɹnʇ ǝsɐǝld 'ʎɹɐuıƃɐɯı sı pǝlɐıp ǝʌɐɥ noʎ ɹǝqɯnu ǝɥʇ'


Message 4 of 17
(14,686 Views)

That's why I asked about the number of cycles. If the number of cycles is very small and fractional, FFT based algorithms won't work well because of spectral leakage and theoretical limitations. It would get even harder of the offset D is much larger than A, for example.

 

Also, you quote A.sin(2pi.B.t + C) + D), but your paranthesis are no balanced. Guessing Y(t)=A * sin(2pi * B * t + C) + D 

 

Can you attach some typical data so we get a beter idea what you are talking about.

0 Kudos
Message 5 of 17
(14,672 Views)

Hi,

 

Thanks for your answers,

 

First, I know well my signal and I can give to the algorithm very good first approximation of each parameters. For instance, with a FFT transform, I have a very near value of the real frequency value with no ambiguity with others harmonics (my sine-wave is very pure).

 

Secondly, I have very huge data which as sampled at 20 GSa/s by a numerizer and frequency signal is from 1 MHz to 10 GHz. So, I have a lot of periods but for bigest frequencies there arn't a lot of sample/period.

 

I saw the VI "Nonlinear Curve fit" but I didn't understand how to use it.

What is "data", "x" and "y"inputs  in my case ?

What must I wire to "f(x,a)" input ? 

 

@altenbach : what is the diference between fit and optimization ? Fitting methods can work without an optimization stage ?

 

Regards,

 

Benjamin

 

 

0 Kudos
Message 6 of 17
(14,662 Views)

benj wrote:

I saw the VI "Nonlinear Curve fit" but I didn't understand how to use it.

What is "data", "x" and "y"inputs  in my case ?

What must I wire to "f(x,a)" input ? 


Have a look at the example finder. THere are quite a few fitting examples.

 

  • x is an array of times (optional, you can incorporate the x0, dx in the model since the points are linearly spaced)
  • y is a 1D array containing your signal.
  • f(x,a) is a strictly typed VI reference that contains the model, in your case Y = Y(t)=A * sin(2pi * B * t + C) + D (Check the help for details and where to dinf a template).

 


@benj wrote:

@altenbach : what is the diference between fit and optimization ? Fitting methods can work without an optimization stage ?


While they are closely related, they are different mathematical disciplines. In LabVIEW they are in different sub-palettes.

 

Have a look at the VIs in each. Fitting typically deals with parameterizing real data according to a mathematical model, while optimization typically deals with finding key points (e.g. a minimum) of a mathematical model functions.

 

 

Message 7 of 17
(14,654 Views)

Hi,

I fist worked about acquisistion problem and I have just come back my sine-fitting subject.

1) I did a "sin model VI" :

 

model diagram.png

 

 

2) I did another VI which call my model and use "Fit non linear....vi"

 

 

sine-fitting diagram.png

 

3) but I obtain this message error :

 

error msg.png

 

What is the problem ?
What about my solution for calling the "sin model.vi". It looks like complicated to obtain the VI refnum but it works ? Is there a easier way to do it ?

For more details, you can download my project (.zip).

 

Regards,

 

Benjamin

0 Kudos
Message 8 of 17
(14,611 Views)

@benj wrote:

It looks like complicated to obtain the VI refnum but it works ? Is there a easier way to do it ?



Yes, use a static VI reference, point ot to the model, and make it strict.

 

(I'll have a look at your code later)

0 Kudos
Message 9 of 17
(14,600 Views)

benj wrote:

model diagram.png

 

 

What is the problem ?


Look at the blue diagram constants! You are indexing element 2 twice and omitting element 3.

 

If you want elements in order, you should never wire the indices of index array. Delete all these blue diagam constants and it is much less likely to make mistakes like that.

Message 10 of 17
(14,597 Views)