Skip to content

Instantly share code, notes, and snippets.

@ValdemarOrn
Last active November 28, 2015 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ValdemarOrn/859508b930fcbca0a541 to your computer and use it in GitHub Desktop.
Save ValdemarOrn/859508b930fcbca0a541 to your computer and use it in GitHub Desktop.
struct ZeroDelay2Lp
{
double z1_state;
float g;
// directory from page 48
// https://www.native-instruments.com/fileadmin/ni_media/downloads/pdf/VAFilterDesign_1.1.1.pdf
inline float Process(float x)
{
// perform one sample tick of the lowpass filter
float v = (x - z1_state) * g / (1 + g);
double y = v + z1_state;
z1_state = y + v;
return y;
}
inline void SetG(double g)
{
this->g = g;
}
};
float FilterCascadeZero::ProcessSample(float input)
{
float fb = totalResonance * 3.9f * (feedback); // totalResonance = [0 ... 0.999]
float value = input - fb;
// just for testing, prevent nan and infs
if (value > 10)
value = 10;
else if (value < -10)
value = -10;
else if (isnan(value))
value = 0;
// 4 cascaded low pass stages
value = lp1.Process(value);
value = lp2.Process(value);
value = lp3.Process(value);
value = lp4.Process(value);
feedback = value;
output = feedback;
return output;
}
// Setting the resonance and g parameters is straight forward. The problem occurs at low fc with high resonance, and also at higher fc with moderate resonance
// I'm using a sampling frequency of 96 Khz, the input data is bandlimited to 20Khz.
// fsinv is 1.0 / Samplerate
// fc is 10...20000 hz
float g = fc * M_PI * fsinv;
lp1.SetG(g);
lp2.SetG(g);
lp3.SetG(g);
lp4.SetG(g);
float FilterCascadeZero::ProcessSample(float input)
{
input *= gain;
float output = 0.0;
float x = input;
float k = totalResonance * 3.9f;
float g = lp1.g;
float g2 = g * g;
float G = g2 * g2;
float S = g2 * g * lp1.z1_state + g2 * lp2.z1_state + g * lp3.z1_state + lp4.z1_state;
float value = (x - k * S) / (1 + k * G);
// 4 cascaded low pass stages
value = lp1.Process(value);
value = lp2.Process(value);
value = lp3.Process(value);
value = lp4.Process(value);
output = value;
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment