Unless you really need the two waveforms in quadrature phase, you probably wouldn't even consider using this algorithm. If you can devote some memory to make a wavetable, an oscillator with linear interpolation will require fewer multiplies.
The code has to break the complex numbers into real and imaginary pairs, but it otherwise straightforward. There is an issue of stability, though it may not be much of an issue with floating point arithmetic. Errors in the calculation can cause our vector to stray from the unit circle. The error can be corrected by normalizing the magnitude of the vector back to 1, placing it on the unit circle. This portion of the code is commented out below, but shown to illustrate the method should you encounter this problem.
The code allows for the frequency, initial phase offset, and amplitude to be specified in the constructor.
See Also:
Please do not redistribute this code. In the event that it contains a bug, this will ensure that it can be fixed without the buggy copies floating around indefinitely.
Last Modified: 9/21/98
/*********************************************************
SinCos.h - A quadrature sine/cosine generator
Copyright (c) 1998, Scott Lehman, slehman@harmony-central.com
This code may be used and modified freely provided that credit
is given to the author in any public release. Any derivative
programs must be distributed freely and/or the modified source
code made publicly available. All code is provided AS IS and
without warranty of any kind.
*********************************************************/
#ifndef SINCOS_H
#define SINCOS_H
#include "Source.h"
class SinCos : public Source {
public:
SinCos(float amp, float freq, float phase);
void Initialize(void);
void Generate(void);
void Cleanup(void);
~SinCos(void){;}
private:
float * sinOut, * cosOut, amplitude, frequency, phase;
float rInc, iInc, rVal, iVal, temp;
int i;
SinCos(SinCos&){};
};
#endif
|
/********************************************************* SinCos.cpp - A quadrature sine/cosine generator Copyright (c) 1998, Scott Lehman, slehman@harmony-central.com This code may be used and modified freely provided that credit is given to the author in any public release. Any derivative programs must be distributed freely and/or the modified source code made publicly available. All code is provided AS IS and without warranty of any kind. *********************************************************/ #include "SinCos.h" #include |