Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!rutgers!uwm.edu!gem.mps.ohio-state.edu!tut.cis.ohio-state.edu!ucbvax!hplabs!hpl-opus!jewett
From: jewett@hpl-opus.HP.COM (Bob Jewett)
Newsgroups: comp.dsp
Subject: Re: Sine curve fit algorithms
Message-ID: <71330001@hpl-opus.HP.COM>
Date: 29 Sep 89 20:47:18 GMT
References: <2421@radio.oakhill.UUCP>
Organization: HP Labs, High Speed Electronics Dept., Palo Alto, CA
Lines: 34

> I am looking for C source code for sine wave curve fitting algorithms.
> Such an algorithm can be used to compute THD/Noise in a digitized sine
> wave.  The fitting algorithm with adapt to phase frequency, and
> amplitude and create a minimum RMS error replica of the digitzed
> sinewave.  The RMS error thus indicates error in the original digitzer.

The requirement can be restated as: Find A, B, C, and F to minimize:

    sum [ (signal(i)-fit(i))**2 ]

where fit(i) = A + B*sin(2*pi*F*time(i)) + C*cos(2*pi*F*time(i))

If F is fixed, a standard linear algebra package should let you find A, B and
C easily.  Often in A/D testing, you know exactly the ratio of sample rate to
signal frequency, and fiddling with F gives an optimistic result.  If you
really want to fit F as well, try getting the approximate frequency with an
FFT, then search for a minimum of distortion versus F.

This method has several advantages over an FFT:

1.  All of the data points are weighted equally.  Most FFTs need to be
    windowed.

2.  The data need not be spaced evenly, as long as you know the time of
    each sample.

3.  You can get reasonable answers with only a fraction of a cycle of
    the input frequency.

I have an alpha version of a stand-alone program that takes ASCII values (or
time-value pairs) and gives distortion numbers, which is available on
request.

Bob Jewett   jewett@hplabs