0

I need to fit a sine curve created from two sine waves and extract the parameters for the fitted curve (such as frequency, amplitude, etc).

Data example:

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

x = np.arange(0, 50, 0.01)
x2 = np.arange(0, 100, 0.02)
x3 = np.arange(0, 150, 0.03)
sin1 = np.sin(x)
sin2 = np.sin(x2)
sin3= np.sin(x3/2)

sin4 = sin1 + sin2+sin3
plt.plot(x, sin4)
plt.show()

enter image description here

I used the codes provided in this answer.

yy = sin4
tt = x
res = fit_sin(tt, yy)
print(str(i), "Amplitude=%(amp)s, Angular freq.=%(omega)s, phase=%(phase)s, offset=%(offset)s, Max. Cov.=%(maxcov)s" % res )
fit_values=res["fitfunc"](tt)
Frequenc_fit= res['freq']
print(i, Frequenc_fit)
Frequenc_fit=Frequenc_fit
Amp_fit=res['amp']
Omega_fit=res['omega']
Phase_fit=res['phase']
Offset_fit=res['offset']
maxcov_fit=res['maxcov']
plt.plot(tt, yy, "-k", label="y", linewidth=2)
plt.plot(tt,fit_values, "r-", label="y fit curve", linewidth=2)
plt.legend(loc="best")
plt.show()

I got a fitted sine curve with a single frequency and amplitude as follows:

2 Amplitude=1.0149282025860233, Angular freq.=2.01112187048004, phase=-0.2730905030152767, offset=0.003304158823058212, Max. Cov.=0.0015266032307905222
2 0.3200799868471169

enter image description here

Is there a method to obtain fitted curve matches with the original one?

Ahmad Senousi
  • 613
  • 2
  • 12
  • 24
  • I guess the function you're trying to fit to, is just a single sine wave. You have two sine waves, with different frequencies, hence you need to fit to a different function. – 9769953 Mar 19 '21 at 15:16
  • Just to be clear here. You are wanting to display the frequency and amplitude for y and y fit curve? – Enrique92 Mar 19 '21 at 15:39
  • Thank you all for your comments. @Enrique92 No, I need to obtain a newly fitted curve that matches the original one (i.e., the fitted curve consist oftwo waves like the original one) – Ahmad Senousi Mar 20 '21 at 04:10
  • 2
    This looks like exactly what the Fourier transform does - decomposes periodic functions into sines and cosines – ForceBru Mar 20 '21 at 09:43
  • Exactly, Is there code or open software to decompose the wave to multiple functions? – Ahmad Senousi Mar 20 '21 at 09:47

1 Answers1

1

Supposing that the function to be fitted is

y(x)=a * sin( w * x )+b * sin( W * x )

the principle of the method below is explained in https://fr.scribd.com/doc/14674814/Regressions-et-equations-integrales

enter image description here

The graphical representation of the result is :

Blue curve : From data obtained by scanning the graph given in the question.

Black curve : From the above calculus.

enter image description here

The available data was not accurate because it comes from scanning of the original figure. The deviation is mainly due to the numerical integrations in computing the values of SS and SSSS (Four successive numerical integrations is not accurate especially with biaised data).

Probably the correct result should be : w=2 , W=1 , a=1 , b=1.

NOTE : The above method is not iterative and thus doesn't requires guessed values of the parameters to start an iterative process. The approximate results of the parameters can be good initial values in order to use an iterative non-linear regression process.

NOTE : If the values of w and W where known a-priori the solving thanks to linear regression would be very simple and much accurate (Only the last 2X2 matrix calculus shown above).

JJacquelin
  • 1,529
  • 1
  • 9
  • 11
  • 1
    Meanwhile the OP changed the wording of his question. He delated his original graph and gave a different graph. So my answer no longer corresponds to the new question. I don't like to do the calculus again and to give a new answer. – JJacquelin Mar 20 '21 at 11:48
  • First of all, thank you for your reply. The function used to fit the curve is suitable for two sinusoidal functions, but not for another curve, as in the new figures in the question. – Ahmad Senousi Mar 20 '21 at 12:14
  • 2
    The above proposed method is mainely usefull if the measured points are numerous on one period or only on a part of period. If the mesurements extend along several periods the Fourier transform is more convenient as already suggested by ForceBru. Also the Fourier transform is the most appropriate in case of a function made of a lot of sines with different periodes. – JJacquelin Mar 20 '21 at 15:40