14#include "Utility/dsp.h"
32template <
int num_harmonics = 16>
42 void Init(
float sample_rate)
44 sample_rate_ = sample_rate;
47 for(
int i = 0; i < num_harmonics; ++i)
50 newamplitude_[i] = 0.f;
53 newamplitude_[0] = 1.f;
67 for(
int i = 0; i < num_harmonics; ++i)
70 *
static_cast<float>(first_harmonic_index_ + i);
75 amplitude_[i] = newamplitude_[i] * (1.0f - f * 2.0f);
84 const float two_x = 2.0f * sinf(phase_ * TWOPI_F);
85 float previous, current;
86 if(first_harmonic_index_ == 1)
89 current = two_x * 0.5f;
93 const float k = first_harmonic_index_;
94 previous = sinf((phase_ * (k - 1.0f) + 0.25f) * TWOPI_F);
95 current = sinf((phase_ * k) * TWOPI_F);
99 for(
int i = 0; i < num_harmonics; ++i)
101 sum += amplitude_[i] * current;
102 float temp = current;
103 current = two_x * current - previous;
116 freq = freq / sample_rate_;
117 freq = freq >= .5f ? .5f : freq;
118 freq = freq <= -.5f ? -.5f : freq;
119 recalc_ = cmp(freq, frequency_) || recalc_;
128 idx = idx < 1 ? 1 : idx;
129 recalc_ = cmp(idx, first_harmonic_index_) || recalc_;
130 first_harmonic_index_ = idx;
138 for(
int i = 0; i < num_harmonics; i++)
140 recalc_ = cmp(newamplitude_[i], amplitudes[i]) || recalc_;
141 newamplitude_[i] = amplitudes[i];
151 if(idx < 0 || idx >= num_harmonics)
155 recalc_ = cmp(amplitude_[idx], amp) || recalc_;
156 newamplitude_[idx] = amp;
161 bool cmp(
float a,
float b) {
return fabsf(a - b) > .000001f; }
166 float amplitude_[num_harmonics];
167 float newamplitude_[num_harmonics];
170 int first_harmonic_index_;
Harmonic Oscillator Module based on Chebyshev polynomials.
Definition harmonic_osc.h:34
float Process()
Definition harmonic_osc.h:62
void Init(float sample_rate)
Definition harmonic_osc.h:42
void SetFreq(float freq)
Definition harmonic_osc.h:113
void SetAmplitudes(const float *amplitudes)
Definition harmonic_osc.h:136
void SetFirstHarmIdx(int idx)
Definition harmonic_osc.h:126
void SetSingleAmp(const float amp, int idx)
Definition harmonic_osc.h:149
FIR Filter implementation, generic and ARM CMSIS DSP based.
Definition adenv.h:16