[rec.audio] Sine curve fit algorithms

charlie@oakhill.UUCP (Charlie Thompson) (09/28/89)

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.
 
Thanks,
Charlie Thompson

wrf@mab.ecse.rpi.edu (Wm Randolph Franklin) (09/28/89)

In <2421@radio.oakhill.UUCP> charlie@oakhill.UUCP (Charlie Thompson) writes:
>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.

For simplicity a Taylor series would be ok.   However, here  is a fourth
order Chebyshev series  that is much faster for  the accuracy.  It finds
sin(y) for y from  0 to  Pi/2 with a  max  error of 0.000124.   This was
obtained with Maple.

                                                  2                3
c2 := .00011389456 + .9962889583 y + .0195161068 y  - .2031187635 y 

                   4
   + .02856566868 y 

If I plot both sin(y)  and c2 you can't tell  any difference, so here is
the error plotted:

> plot(sin(y)-c2,y=0..Pi/2);

+ 0.000124                                                                     
|     AAA                                                                      
|     A  AA                                     AAAAAAA                       A
|    A     A                                   A       A                      A
|   A      A                                 AA        A                      A
+ 0.0000624 A                               A           AA                    A
|  A         A                             A              A                  A 
|  A         A                            A               A                  A 
| A           A                          A                 A                 A 
| A            A                        A                   A                A 
| A            A                       A                     A              A y
+-0-------------*---+-----------------*+-------------------+--*-------------*-+
0 A             A .393               .785                1.18 A            1.57
| A              A                   A                         A            A  
|A                A                 A                          A            A  
|A                 A               A                            A          A   
+A-0.0000624        A             A                              A         A   
|A                   A           A                                A       A    
*                    A          A                                  A     A     
*                     AA      AA                                    A   A      
*                       A   AA                                       AAA       
*                        AAA                                                   
+ -0.000125                                                                    
>

You can   work out  how many  Taylor  terms  it would  take  to get that
accuracy.  Editorial: Taylor series are for wimps.

If the angle is outside [0,1.57] use the obvious reduction formulae.

If you want the formula in degrees, or a different accuracy, tell me and
I'll work it out.

Historical  note: IBM in Fortran finds  sin(x)  by reducing the angle to
[0,1.57],  then using a half  angle formula to reduce   x further to [0,
22.5 degrees in radians], then uses a 2 term Chebyshev.  This gives full
single (maybe double) precision accuracy.

Note on Chebyshev   series: They give  the  best maximum  error over  an
interval, for  polynonials of  a   given   degree.  However,  the  error
increases very fast outside  the interval, so  don't try to  enlarge the
interval   w/o recalculating  the series.    Also,   if you want reduced
accuracy and increased speed, it  is not sufficient to  just drop a high
order term or 2.  You have to go back to the Chebyshev basis.

-- 
						   Wm. Randolph Franklin
Internet: wrf@ecse.rpi.edu (or @cs.rpi.edu)    Bitnet: Wrfrankl@Rpitsmts
Telephone: (518) 276-6077;  Telex: 6716050 RPI TROU; Fax: (518) 276-6261
Paper: ECSE Dept., 6026 JEC, Rensselaer Polytechnic Inst, Troy NY, 12180

ted@nmsu.edu (Ted Dunning) (09/29/89)

In article <1989Sep28.161516.10353@rpi.edu> wrf@mab.ecse.rpi.edu (Wm Randolph Franklin) writes:

   In <2421@radio.oakhill.UUCP> charlie@oakhill.UUCP (Charlie Thompson) writes:
   >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.

   For simplicity a Taylor series would be ok.   However, here  is a fourth
   order Chebyshev series  that is much faster for  the accuracy.  It finds
   sin(y) for y from  0 to  Pi/2 with a  max  error of 0.000124.   This was
   obtained with Maple.


wrf missed the point entirel, i think.  what charlie was wanting is a
way to compute the amount of distortion present in a digitized sine
wave. 

happily, what he wants is relatively easy to provide.  the fourier
transform does exactly the right thing.  in the discrete time case,
you will have to either sample for a long time, or know the freqency
of the original sine wave very accurately.

so... the answer to the question is to computer the fourier transform,
eliminate the coefficient corresponding to the sine wave you are
interested in and compute the total power in the rest of the spectrum.

--
ted@nmsu.edu
			remember, when extensions and subsets are outlawed,
			only outlaws will have extensions or subsets