Audio Code Library: Sinewave Generator


This code was written in an effort to create an efficient method for generating a sine wave. The algorithm started by converting the Z transform of sin(wt)u(t) into a simple filter network with two variables with an initial state. By juggling the gain factors a bit, the algorithm can generate a single output sample with one multipy and a subtraction. I'm not sure how drift due to roundoff errors affects the system.

You probably wouldn't want to use this algorithm in very many situations. It's advantages are no memory requirement for a wavetable and no interpolation is needed (but about the same amount of work as linear interpolation). However, allowing the frequency to adjusted once the algorithm is running will require some more computation which may render it impractical. There also seems to be a problem with the algorithm at very low frequencies (< 40 Hz.)

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: 6/28/98

Sine.h

/*********************************************************

Sine.h - A simple sinewave 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 SINE_H
#define SINE_H

#include "Source.h"

class Sine : public Source {
public:
  Sine(float amp, float freq, float phase);
  void Initialize(void);
  void Generate(void);
  void Cleanup(void);
  ~Sine(){;}

private:
  float frequency, startPhase, coeff, value1, value2, amplitude;
  float * outputSignal;
  Sine(void){;}
  Sine(Sine&){;}
};

#endif

Sine.cpp

/*********************************************************

Sine.cpp - A simple sinewave 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 "Sine.h"
#include 


// ****************  Sine(float, float, float)  *************

// amp - Amplitude, should be between 0 and 1
// freq - frequency
// phase - Initial phase of sine, from 0 to 1 (fraction of wavelenth)

Sine :: Sine (float amp, float freq, float phase)
{
  SetNumOutputs(1);

  this->amplitude = amp;
  this->startPhase = phase * TWOPI;
  this->frequency = freq * TWOPI/this->samplingRate;

  return;
}


// ********************  Initialize(void)  ***************

void Sine :: Initialize (void)
{
  // compute initial states and coefficient
  coeff = 2*cos(frequency);
  value1 = sin(startPhase); // first output sample
  value2 = sin(-frequency + startPhase); // What would have been the 
                                         // previus output sample

  outputSignal = outputs[0]; //pointer to only output buffer

  return;
}


// ******************  Generate(void)  ******************

void Sine :: Generate (void)
{
  int i;
  float temp;
  for(i=0; i<frameLength; i++) {
    outputSignal[i] = amplitude * value1;
    temp = value1;
    value1 = coeff * value1 - value2;
    value2 = temp;
   }

  return;
}


// ********************  Cleanup(void)  *******************

void Sine :: Cleanup (void)
{
  return;
}

Back to the Audio Programming Page

Back to Harmony Central® Home Page

Email: webmaster@harmony-central.com
Copyright © 1995-98 Harmony Central, Inc. All rights reserved.