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