Fit Data To Fourier Series
LausX
I want to fit some experimental data to a fourier series and have big problems to do the fit. Also a simple sin function did not work.
Attached is an example igor file.
I tried to fit with the following function:
Function fitFourierModel(w, x): FitFunc
Wave w
Variable x
return w[0] + w[1]*cos(x + w[11]) + w[2]*sin(x + w[11]) + w[3]*cos(2*(x + w[11])) + w[4]*sin(2*(x + w[11])) + w[5]*cos(3*(x + w[11])) + w[6]*sin(3*(x + w[11])) + w[7]*cos(4*(x + w[11])) + w[8]*sin(4*(x + w[11])) + w[9]*cos(5*(x + w[11])) + w[10]*sin(5*(x + w[11]))
End
Wave w
Variable x
return w[0] + w[1]*cos(x + w[11]) + w[2]*sin(x + w[11]) + w[3]*cos(2*(x + w[11])) + w[4]*sin(2*(x + w[11])) + w[5]*cos(3*(x + w[11])) + w[6]*sin(3*(x + w[11])) + w[7]*cos(4*(x + w[11])) + w[8]*sin(4*(x + w[11])) + w[9]*cos(5*(x + w[11])) + w[10]*sin(5*(x + w[11]))
End
Do you have any suggestions?
Thanks
I had a quick look at your experiment and it seem you confuse degrees (in your plots) and rad (in your fit function).
Readjustment or substitution of
x
byx/w[12]
might help (w[12]
should be very close to 180/pi in this case).HJ
June 19, 2015 at 06:20 am - Permalink
June 19, 2015 at 11:06 am - Permalink
Wave w
Variable x
Variable rads = x*pi/180
return w[0] + w[1]*cos(rads + w[11]) + w[2]*sin(rads + w[11]) + w[3]*cos(2*(rads + w[11])) + w[4]*sin(2*(rads + w[11])) + w[5]*cos(3*(rads + w[11])) + w[6]*sin(3*(rads + w[11])) + w[7]*cos(4*(rads + w[11])) + w[8]*sin(4*(rads + w[11])) + w[9]*cos(5*(rads + w[11])) + w[10]*sin(5*(rads + w[11]))
End
Steve- correct me if I'm wrong, but it seems like fitting both sin and cos phase angle (w[11]) and fitting separate sin and cos amplitudes creates a linear dependency. Isn't the phase angle set by the amplitude ratio?
John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
June 19, 2015 at 01:56 pm - Permalink
-0.0146 1 -1 -1 1 1 -1 -1 -1 1 -1 1 -0.311
0.0146 -1 1 1 -1 -1 1 1 1 -1 1 -1 -0.344
0.0146 -1 1 1 -1 -1 1 1 1 -1 1 -1 0.195
-0.0146 1 -1 -1 1 1 -1 -1 -1 1 -1 1 2.76
-0.0146 1 -1 -1 1 1 -1 -1 -1 1 -1 1 -0.699
0.0146 -1 1 1 -1 -1 1 1 1 -1 1 -1 -1.95
0.0146 -1 1 1 -1 -1 1 1 1 -1 1 -1 -1.57
0.0146 -1 1 1 -1 -1 1 1 1 -1 1 -1 2.7
-0.0146 1 -1 -1 1 1 -1 -1 -1 1 -1 1 -0.304
0.0146 -1 1 1 -1 -1 1 1 1 -1 1 -1 -0.642
-0.0146 1 -1 -1 1 1 -1 -1 -1 1 -1 1 1.98
But eliminating all the sin terms makes a bad fit. What am I missing?
John Weeks
WaveMetrics, Inc.
support@wavemetrics.com
June 19, 2015 at 02:15 pm - Permalink
You are correct. I was paying attention only to the degree-to-radian conversion. If you think of each sine and cosine as the decomposition of a single complex term, they must have a common phase, with relative amplitude related to cos(phi) and sin(phi). Of course, that phase angle may change with the Fourier harmonic, so using a common phase coefficient for all harmonics is incorrect too.
June 19, 2015 at 02:19 pm - Permalink
After thinking more about John's comments (and my own previous response), I realized that if you want to express the fitting function as a sum of sine AND cosine terms, then no phase argument, i.e. w[11], should be included. The sum of a sine and cosine with different amplitudes is equivalent to a single sinusoid with a phase coefficient (you can look up the trig identities). The choice of form should depend on which is most useful to you. I repeated the pxp without phase angle coefficients (and with John's factorization speed-up), and have attached the result. Although the fit for data set 3 looks good, I don't understand its large V_chisq.
June 20, 2015 at 01:27 am - Permalink
However, the phase shift is constant for all summands. This is identical to a situation in which you set it to zero and adjust the x-scaling of the corresponding wave.
Nevertheless you run into trouble if you consider the periodicity of the trigonometric functions. Further trouble arises from the addition rules etc.
Ultimately, w[11] should be constrained to [0,pi/2[ (open interval) to avoid these traps.
Happy fitting,
HJ
Edit:
I'm not entirely sure if a low frequency ,e.g., sine is really covered by high frequency cosines in case you use a small amount of components.
June 20, 2015 at 06:30 am - Permalink
My idea was to fit the data according to the attached paper to equation 7 on page 6.
Also thanks for the tip with the linear dependency, I even didn't taught about that.
Let's see how the extracted values will agree with other literature values.
June 22, 2015 at 05:27 am - Permalink
It is also worth thinking about whether your data is affected by systemic, repeatable artifacts or by temporally variable random noise. The latter would contribute to a broad-power spectrum, and could be reduced by signal averaging. The former might be calibrated and subtracted from the data,
June 22, 2015 at 06:08 am - Permalink
DisplayHelpTopic "Fourier Transforms"
FU?
June 22, 2015 at 06:28 am - Permalink
June 22, 2015 at 07:04 am - Permalink
I will try it.
And yes, I'm at the FU-Berlin :)
June 22, 2015 at 07:42 am - Permalink
Wave w
Variable x
Variable rad = x*Pi/180
Variable i, nmax, temp
nmax = dimSize(w,0)
temp = w[0]
for (i = 1; i < 10; i+= 2)
temp += w[i]*cos(rad) + w[i+1]*sin(rad)
endfor
return temp
// return w[0] + w[1]*cos(rad) + w[2]*sin(rad) + w[3]*cos(2*rad) + w[4]*sin(2*rad) + w[5]*cos(3*rad) + w[6]*sin(3*rad) + w[7]*cos(4*rad) + w[8]*sin(4*rad) + w[9]*cos(5*rad) + w[10]*sin(5*rad)
End
But with this for-loop it is not possible to fit. Is this normal? Or do you have any ideas?
Thanks
June 24, 2015 at 12:00 am - Permalink
i*
in the arguments to get the harmonics... -->cos(i*rad)
HJ
PS: Maybe you want to have a look at the "other" kind of fitfunctions using coefficient-, y-, and x-waves .
displayhelptopic "All-At-Once Fitting Functions"
June 24, 2015 at 03:44 am - Permalink
June 24, 2015 at 03:54 am - Permalink