LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

NonLinearFitWithMaxIters does not give correct results for phase

Dear all,
I am trying to fit a sin cuve with NonLinearFitWithMaxIters function of labwindows, but the results given by this function are dispersed a lot (the function does not give same results).

In fact, I have two waveforms signals v and i (of 104 points), and I  find the phase between the two signals with NonLinearFitWithMaxIters.

The two singals v and i are  measured with an osciloscope.

I use the following fit functions :
v=p1*sin(wt+p2) ;
i=p3*sin(wt+p4) ;

I use the NonLinearFitWithMaxIters function to fit the data and to have the parameters  p1, p2, p3, p4. Then, the phase is claculated as follow phase=p4-p3.

The problem is that the phase calculted between the two signals (v and i) is different for each run for the same conditions.
In fact, for a same condition, I measure different times the signals v and i, and I calculate the phase p4-p3, using NonLinearFitWithMaxIters. The goal is to calculate a mean of phases for the same condition. For example, there are cases where the phase=-5 degree, and other cases when phase=12 degree.
 
For ten measures of v and i, the phase calculated is different. I get a big dispersion between the phases.
I would like to know please why I have a big difference in phases calculated with the same condition?whern using NonLinearFitWithMaxIters.
I read that this function does not give all time the correct results, is there a way to know when the results are not corrects and when they are correct ?
And is there any solution to find accuratly the phase between the two waveforms.

Thank you for your precious answer.

0 Kudos
Message 1 of 14
(4,957 Views)

I would have assumed that p1 and p3 are amplitudes and p2, p4 are phases, so if you are interested in phase differences I would have calculated p4 - p2, not p4 - p3.

0 Kudos
Message 2 of 14
(4,940 Views)

Sorry for th typo, it is p4 - p2.

Could you please tell me why NonLinearFitWithMaxIters  does not give correct results, and is there any other solution to find phase between the two waveforms accuaretly?

0 Kudos
Message 3 of 14
(4,929 Views)

It would be useful if you could provide the two sample data sets and the correspomding fit parameters you have obtained from NonLinearFitWithMaxIters

 

Also, did you check the return/status value of NonLinearFitWithMaxIters, i.e., when callaing status = NonLinearFitWithMaxIters (...)?

 

An alternative to fitting the data could be the use of FFT.

 

0 Kudos
Message 4 of 14
(4,927 Views)

From NonLinearFitWithMaxIters, I get the two amplitude paramleters (p1,p3), and the phases(p4, p2).

For the amplitude, It seems to be fine, as I find close results for the ten runs. However, for the phase(p4-p2), I get dispersed values, like.

run 1: phase=0.5

run2: phase=-5.21

run 3: phase =0.495

run 4: phase=-5.01

run 5: phase=0.48

run 6: phase =-5.3

run7: phase=-5.1

run8: phase=0.491

run9: phase=-5.4

run10: phase=0.49

 

It is like I have two groups of phases for the ten runs, a group of almost -5 degree, and a group of almost 0.4 degree.

The status of NonLinearFitWithMaxIters is equal to 0 for the ten runs.

 

 

0 Kudos
Message 5 of 14
(4,922 Views)

Two thoughts/comments:

 

- The argument of sin (), cos () etc. is given in radians, not degrees.

- The sin () function is periodic with a period of 2 pi, so the fit result may not be unique but differ by multiples of 2 pi. Could this explain your observation? For example, you could end up with either phase = p4 - p2, phase = p4 + 2pi - p2, or phase = p4 - ( p2 + 2 pi )

0 Kudos
Message 6 of 14
(4,917 Views)

I made conversions of the phase (p4-p2).

Yes, the results given by sin, cos, etc are in radian, but I converted them to degree.

Yes for the periodicity of the phase, but I moved out the factor of periodicty, to have phases all times between -90 degree and +90 degree, without the periodic factor of 360 degree.

0 Kudos
Message 7 of 14
(4,915 Views)

...I can only argue based on the information you provide and if you do different things than you post discussion is a bit inefficient Smiley Sad

 

Why don't you provide a graph with the plots of the data and the two different fits and also the raw data and the relevant lines of code?

 

It might also be of interest to know the number of iterations and your starting parameters; nonlinear fits do not necessarily arrive at the global optimum, they may find a local optimum which then depends on the starting parameters: do you always provide the identical starting parameters? And what is the mean square error?

 

From the help:

 

You must pass a pointer to the nonlinear function f(x,a) along with a set of initial guess coefficients a. NonLinearFitWithMaxIters does not always give the correct answer. The correct output sometimes depends on the initial choice of a. It is very important to verify the final result.

0 Kudos
Message 8 of 14
(4,910 Views)

The pseudocode which I am using is :

v_err=NonLinearFitWithMaxIters(array_x,array_v,v_y_fit,1252,100,sinus,v_coef,2,&v_mean_squareError);
The fit function is
double sinus(double x, double a[],int noef){
    return (a[0]*sin((w*x)+a[1]));
}
 I use the same inital coeficient of v_coef for each run :     
v_coef[0]=0.03;
v_coef[1]=0.2;
These coeficient are choosen arbitrary.

In this case, the number of points of the data is 1252 (the data of array_v).
The number of iterations is 100.

For the array_x, the distance between adjacents values of the array is dt=0.4*1e-9(difference between array_x[i] anv array_x[i+1]=dt=0.4*1e-9 ).

for (i=0;i<1252;i++){

array_x[i]=i*(0.4*1e-9);

}



The means square error returnned by the function when it is complished is  small, of the order of 0,001.

I read the help
« From the help:

You must pass a pointer to the nonlinear function f(x,a) along with a set of initial guess coefficients a. NonLinearFitWithMaxIters does not always give the correct answer. The correct output sometimes depends on the initial choice of a. It is very important to verify the final result.
»
That means that the function cannot be used, as it does not give correct results. How
can we check if the results are good or not ? in my case.

I think that in my case, the function does not give correct results, but how can I check if the results are good, or not ? The mean square error is small.

0 Kudos
Message 9 of 14
(4,902 Views)

Right now I find it difficult to judge where the problem is - try to provide a sample others can play with, see here,

0 Kudos
Message 10 of 14
(4,895 Views)