468,753 Members | 1,136 Online

# Sine Wave Curve Fit Question

Python Folks

I'm a newbie to Python and am looking for a library / function that can help
me fit a 1D data vector to a sine wave. I know the frequency of the wave,
so its really only phase and amplitude information I need.

I can't find anything in the most widely known libraries (they seem to be
strong on polynomial fitting, but not, apparently, on trig functions) and I
wondered if any one here had recommendations?

Something that implemented IEEE 1057 , or similar, would be perfect.
TIA
Iain
Jan 30 '08 #1
4 6580
Iain Mackay napisal(a):
Python Folks

I'm a newbie to Python and am looking for a library / function that can help
me fit a 1D data vector to a sine wave. I know the frequency of the wave,
so its really only phase and amplitude information I need.

I can't find anything in the most widely known libraries (they seem to be
strong on polynomial fitting, but not, apparently, on trig functions) and I
wondered if any one here had recommendations?

Something that implemented IEEE 1057 , or similar, would be perfect.
TIA
Iain
I'm not aware of any specialized library, but how about using numpy
and running FFT on your data? If you already know the frequency, you
can easily extract phase and scaled amplitude from the result.

Regards,
Marek
Jan 30 '08 #2
Iain Mackay wrote:
Python Folks

I'm a newbie to Python and am looking for a library / function that can help
me fit a 1D data vector to a sine wave. I know the frequency of the wave,
so its really only phase and amplitude information I need.

I can't find anything in the most widely known libraries (they seem to be
strong on polynomial fitting, but not, apparently, on trig functions) and I
wondered if any one here had recommendations?

Something that implemented IEEE 1057 , or similar, would be perfect.
Let's do a bit math first.

Your model is A*sin(omega*t+alpha) where A and alpha are sought.
Let T=(t_1,...,t_N)' and Y=(y_1,..,y_N)' your measurements (t_i,y_i)
( ' denotes transposition )

First, A*sin(omega*t+alpha) =
A*cos(alpha)*sin(omega*t) + A*sin(alpha)*cos(omega*t) =
B*sin(omega*t) + D*cos(omega*t)

by setting B=A*cos(alpha) and D=A*sin(alpha)

Once, you have B and D, tan(alpha)= D/B A=sqrt(B^2+D^2)
Then in vector notation S=sin(omega*T) C=cos(omega*T)
you get the 2x2 system for B and D :

(S'*S) * B + (S'*C) * D = S'*Y
(S'*C) * B + (C'*C) * D = C'*Y

where S'*C is the scalar product of the vectors S and C and similarly.

Now, for Python, to handle vectors and scalar products efficiently, have a look
at numpy.

--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
Jan 30 '08 #3
Thanks folks - I'll have a think about both of these options.
Jan 31 '08 #4
Helmut Jarausch <ja******@skynet.bewrites:
You're right: standard Python's math library missing the function arctan2.
It's math.atan2 .
Jan 31 '08 #5

### This discussion thread is closed

Replies have been disabled for this discussion.