Created
August 2, 2017 11:21
-
-
Save dsamarin/d794489b8b70b71bb78efba68557cd87 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cmath> | |
#include <complex> | |
class Oscillator { | |
private: | |
double mRate; | |
double mReal; | |
double mImag; | |
public: | |
Oscillator(unsigned int rate) | |
: mReal(0.0), mImag(1.0), mRate(static_cast<double>(rate)) {} | |
double next(double freq); | |
}; | |
double Oscillator::next(double freq) { | |
const double tau = 6.28318530717958623199592693709; | |
double a = tau * freq / mRate; | |
double u = cos(a); | |
double v = sin(a); | |
double x = mReal * u - mImag * v; | |
double y = mReal * v + mImag * u; | |
mReal = x; | |
mImag = y; | |
return mReal; | |
} | |
class Oscillator2 { | |
private: | |
double mRate; | |
double mPhase; | |
public: | |
Oscillator2(unsigned int rate) | |
: mPhase(0.0), mRate(static_cast<double>(rate)) {} | |
double next(double freq); | |
}; | |
double Oscillator2::next(double freq) { | |
const double tau = 6.28318530717958623199592693709; | |
mPhase += tau * freq / mRate; | |
if (mPhase > tau) { | |
mPhase -= tau; | |
} | |
return sin(mPhase); | |
} | |
class Oscillator3 { | |
private: | |
double mRate; | |
std::complex<double> mValue; | |
public: | |
Oscillator3(unsigned int rate) | |
: mValue(0.0), mRate(static_cast<double>(rate)) {} | |
double next(double freq); | |
}; | |
double Oscillator3::next(double freq) { | |
const double tau = 6.28318530717958623199592693709; | |
mValue *= std::exp(std::complex<double>(0.0f, tau * freq / mRate)); | |
return mValue.real(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment