06-26-2014 08:05 AM
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.
06-26-2014 03:33 PM
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.
06-27-2014 03:12 AM
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?
06-27-2014 03:18 AM
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.
06-27-2014 03:58 AM
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.
06-27-2014 04:24 AM
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 )
06-27-2014 04:57 AM
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.
06-27-2014 05:18 AM
...I can only argue based on the information you provide and if you do different things than you post discussion is a bit inefficient
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.
06-27-2014 07:32 AM
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.
06-27-2014 08:46 AM
Right now I find it difficult to judge where the problem is - try to provide a sample others can play with, see here,