topic Sine-fit algorithm in LabVIEW
https://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1616536#M585209
<P>Hi,</P>
<P> </P>
<P>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...</P>
<P> </P>
<P>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 ?</P>
<P> </P>
<P>Regards,</P>
<P> </P>
<P>Benjamin</P>Tue, 28 Jun 2011 19:28:26 GMTbenj2011-06-28T19:28:26ZSine-fit algorithm
https://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1616536#M585209
<P>Hi,</P>
<P> </P>
<P>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...</P>
<P> </P>
<P>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 ?</P>
<P> </P>
<P>Regards,</P>
<P> </P>
<P>Benjamin</P>Tue, 28 Jun 2011 19:28:26 GMThttps://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1616536#M585209benj2011-06-28T19:28:26ZRe: Sine-fit algorithm
https://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1616578#M585223
<P>This is a fit, not an optimization. Look for <A href="http://zone.ni.com/reference/en-XX/help/371361G-01/gmath/nonlinear_curve_fit/" target="_self">nonlinear curve fit</A>.</P>
<P> </P>
<P>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.</P>Tue, 28 Jun 2011 19:42:16 GMThttps://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1616578#M585223altenbach2011-06-28T19:42:16ZRe: Sine-fit algorithm
https://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1617022#M585306
<P>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.</P>
<P> </P>
<P>Lynn</P>Wed, 29 Jun 2011 01:23:37 GMThttps://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1617022#M585306johnsold2011-06-29T01:23:37ZRe: Sine-fit algorithm
https://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1617458#M585388
<P>You can use the levenberg-marquard fit , but usually the extract single tone information work good enough <span class="lia-unicode-emoji" title=":slightly_smiling_face:">ðŸ™‚</span></P>
<P>There is a paper from 99 ni'week about the performance of this vi, I added it, because I missed the link</P>
<P> </P>
<P>Depending on your signal, it might be wise to try blocks of complete periods and mean the results....</P>
<P> </P>
<P>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...</P>
<P> </P>Wed, 29 Jun 2011 11:39:18 GMThttps://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1617458#M585388Henrik_Volkers2011-06-29T11:39:18ZRe: Sine-fit algorithm
https://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1617912#M585480
<P>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.</P>
<P> </P>
<P>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 </P>
<P> </P>
<P>Can you attach some typical data so we get a beter idea what you are talking about.</P>Wed, 29 Jun 2011 15:42:10 GMThttps://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1617912#M585480altenbach2011-06-29T15:42:10ZRe: Sine-fit algorithm
https://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1618242#M585546
<P>Hi,</P>
<P> </P>
<P>Thanks for your answers,</P>
<P> </P>
<P>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).</P>
<P> </P>
<P>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.</P>
<P> </P>
<P>I saw the VI "Nonlinear Curve fit" but I didn't understand how to use it.</P>
<P>What is "data", "x" and "y"inputs in my case ?</P>
<P>What must I wire to "f(x,a)" input ? </P>
<P> </P>
<P>@altenbach : what is the diference between fit and optimization ? Fitting methods can work without an optimization stage ?ï»¿</P>
<P> </P>
<P>Regards,</P>
<P> </P>
<P>Benjamin</P>
<P> </P>
<P> </P>Wed, 29 Jun 2011 18:20:17 GMThttps://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1618242#M585546benj2011-06-29T18:20:17ZRe: Sine-fit algorithm
https://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1618276#M585557
<BLOCKQUOTE><HR />benj wrote:
<P>I saw the VI "Nonlinear Curve fit" but I didn't understand how to use it.</P>
<P>What is "data", "x" and "y"inputs in my case ?</P>
<P>What must I wire to "f(x,a)" input ? </P>
<HR /></BLOCKQUOTE>
<P>Have a look at the example finder. THere are quite a few fitting examples.</P>
<P> </P>
<UL>
<LI>x is an array of times (optional, you can incorporate the x0, dx in the model since the points are linearly spaced)</LI>
<LI>y is a 1D array containing your signal.</LI>
<LI>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).</LI>
</UL>
<P> </P>
<BLOCKQUOTE><HR /><LI-USER uid="37939"></LI-USER> wrote:<BR />
<P><LI-USER uid="7614"></LI-USER> : what is the diference between fit and optimization ? Fitting methods can work without an optimization stage ?ï»¿</P>
<HR /></BLOCKQUOTE>
<P>While they are closely related, they are different mathematical disciplines. In LabVIEW they are in different sub-palettes.</P>
<P> </P>
<P>Have a look at the VIs in each. Fitting typically deals with parameterizing <U>real data</U> according to a mathematical model, while optimization typically deals with finding key points (e.g. a minimum) of a mathematical model functions.<BR /><BR /></P>
<P> </P>
<P> </P>Wed, 29 Jun 2011 18:40:41 GMThttps://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1618276#M585557altenbach2011-06-29T18:40:41ZRe: Sine-fit algorithm
https://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1629804#M587698
<P>Hi,</P>
<P>I fist worked about acquisistion problem and I have just come back my sine-fitting subject.</P>
<P>1) I did a "sin model VI" :</P>
<P> </P>
<P><IMG src="http://forums.ni.com/t5/image/serverpage/image-id/47526i00810FF876602E25/image-size/original?v=mpbl-1&px=-1" border="0" alt="model diagram.png" title="model diagram.png" align="center" /></P>
<P> </P>
<P> </P>
<P>2) I did another VI which call my model and use "Fit non linear....vi"</P>
<P> </P>
<P> </P>
<P><IMG src="http://forums.ni.com/t5/image/serverpage/image-id/47528i03633BAF317BCF71/image-size/original?v=mpbl-1&px=-1" border="0" alt="sine-fitting diagram.png" title="sine-fitting diagram.png" align="center" /></P>
<P> </P>
<P>3) but I obtain this message error :</P>
<P> </P>
<P><IMG src="http://forums.ni.com/t5/image/serverpage/image-id/47530iFFD6BFFFA56B148A/image-size/original?v=mpbl-1&px=-1" border="0" alt="error msg.png" title="error msg.png" align="center" /></P>
<P> </P>
<P>What is the problem ?<BR />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 ?</P>
<P>For more details, you can download my project (.zip).</P>
<P> </P>
<P>Regards,</P>
<P> </P>
<P>Benjamin</P>Mon, 11 Jul 2011 09:36:42 GMThttps://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1629804#M587698benj2011-07-11T09:36:42ZRe: Sine-fit algorithm
https://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1630382#M587800
<BLOCKQUOTE><HR /><LI-USER uid="37939"></LI-USER> wrote:<BR />
<P>It looks like complicated to obtain the VI refnum but it works ? Is there a easier way to do it ?</P>
<HR /></BLOCKQUOTE>
<P><BR />Yes, use a <A href="http://zone.ni.com/reference/en-XX/help/371361G-01/glang/static_vi_ref/" target="_self">static VI reference</A>, point ot to the model, and make it strict.</P>
<P> </P>
<P>(I'll have a look at your code later)</P>Mon, 11 Jul 2011 15:56:43 GMThttps://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1630382#M587800altenbach2011-07-11T15:56:43ZRe: Sine-fit algorithm
https://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1630404#M587804
<BLOCKQUOTE><HR />benj wrote:
<P><IMG src="http://forums.ni.com/t5/image/serverpage/image-id/47526i00810FF876602E25/image-size/original?v=mpbl-1&px=-1" border="0" alt="model diagram.png" title="model diagram.png" align="center" /></P>
<P> </P>
<P> </P>
<P>What is the problem ?</P>
<HR /></BLOCKQUOTE>
<P>Look at the blue diagram constants! You are indexing element 2 twice and omitting element 3.</P>
<P> </P>
<P>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.</P>Mon, 11 Jul 2011 16:03:11 GMThttps://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1630404#M587804altenbach2011-07-11T16:03:11ZRe: Sine-fit algorithm
https://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1630606#M587838
<P>Try this quick draft. (sorry, I did not triple check for units, etc.)</P>
<P> </P>
<P> </P>Mon, 11 Jul 2011 18:25:39 GMThttps://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1630606#M587838altenbach2011-07-11T18:25:39ZRe: Sine-fit algorithm
https://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1650696#M591744
<P>Thanks,</P>
<P> </P>
<P>I correct my mistake and use your example and the sine-fitting works. The algorithm works fine (good convergence) but it's long (6 seconds to find the solution). It exist solutions or options to speed up the algorithm (and maybe lost in precision) ?</P>
<P> </P>
<P>Regards,</P>
<P> </P>
<P>Benjamin</P>
<P> </P>
<P><EM>Remarks : just a little mistake in "Test sine-fittingMOD2 Folder.zip". It calculate 1/Fsample instead of Fsample, so the value of frequency found by the FFT analysis doesn't works (but in my sample data, Fsample = 1, so there is no difference ! ).</EM></P>Thu, 28 Jul 2011 06:27:27 GMThttps://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1650696#M591744benj2011-07-28T06:27:27ZRe: Sine-fit algorithm
https://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1650736#M591754
<BLOCKQUOTE><HR /><LI-USER uid="37939"></LI-USER> wrote:<BR />
<P>I correct my mistake and use your example and the sine-fitting works. The algorithm works fine (good convergence) but it's long (6 seconds to find the solution). It exist solutions or options to speed up the algorithm (and maybe lost in precision) ?</P>
<HR /></BLOCKQUOTE>
<P>It executes pretty fast on my computer, but you hopefully noticed that the initial estimates from the "detect single tone" would be equally good for all practical purpose. Use those and forget about the nonlinear fitting. It does not add any real value in this case.</P>Thu, 28 Jul 2011 07:24:58 GMThttps://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1650736#M591754altenbach2011-07-28T07:24:58ZRe: Sine-fit algorithm
https://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1652952#M592196
<P>Hi,</P>
<P> </P>
<P>I don't want only an estimation of the frequency but I need to fit my data in order to have information about residual rms error between observation-data and fit-data to calculate ENOB (effective number of bits). So, it'is possible to speed-up the fitting alogritm ?</P>
<P> </P>
<P>Regards,</P>
<P> </P>
<P>Benjamin</P>Fri, 29 Jul 2011 15:09:28 GMThttps://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1652952#M592196benj2011-07-29T15:09:28ZRe: Sine-fit algorithm
https://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1652972#M592205
<P>In my demo I already show how to calculate the function from the outputs of extract single tone (using call by reference node).</P>
<P> </P>
<P>Now use this as the best fit and you can calculate your RMS error (and anyting else) by comparing to the raw data.</P>Fri, 29 Jul 2011 15:23:11 GMThttps://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/1652972#M592205altenbach2011-07-29T15:23:11ZRe: Sine-fit algorithm
https://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/4059480#M1165015
<BLOCKQUOTE><HR /><LI-USER login="altenbach" uid="7614"></LI-USER> wrote:<BR />
<P>Try this quick draft. (sorry, I did not triple check for units, etc.)</P>
<P> </P>
<P> </P>
<HR /></BLOCKQUOTE>
<P>A bit old post, but anyway, dear <LI-USER login="altenbach" uid="7614"></LI-USER>, thanks for posting this example. On my laptop with i7-6700 and LV2019 the function "Extract Single Tone Information VI" executes for about 2ms for 16k input samples. This is more than 2 orders of magnitude faster than non-linear curve fit (~260ms). For 1.6k data length I get less than 1ms. This is exactly what I need, I will try this on cRIO NI-9035 and hopefully it work for my real-time application.</P>Sun, 14 Jun 2020 18:02:15 GMThttps://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/4059480#M1165015Alex20122020-06-14T18:02:15ZRe: Sine-fit algorithm
https://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/4059483#M1165016
<P>Of course extract single tone would be much faster than any iterative fitting procedure, especially if export mode=none (you can look inside to see what it does!). Depending on the needs, a simple FFT followed by R(max), might be sufficient and even faster. <span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:">ðŸ˜„</span></P>
<P> </P>
<P>Fitting to a sine function is <U>not the same</U> as extracting a single tone. Giving reasonable initial guesses, fitting will continue to work even if the data consists of only a partial period, so the use case is very different.</P>Sun, 14 Jun 2020 19:33:23 GMThttps://forums.ni.com/t5/LabVIEW/Sine-fit-algorithm/m-p/4059483#M1165016altenbach2020-06-14T19:33:23Z