Skip to content

Instantly share code, notes, and snippets.

@Micrified
Created June 9, 2019 13:46
Show Gist options
  • Save Micrified/c209b127922d87691b9932fcbdc78bef to your computer and use it in GitHub Desktop.
Save Micrified/c209b127922d87691b9932fcbdc78bef to your computer and use it in GitHub Desktop.
Butterworth Sample
fp gain = 0x80;
fp k1 = 0x2E00;
fp k2 = 0x6C00;
fp x_phi[MAX_COEF];
fp y_phi[MAX_COEF];
int32_t c_phi;
void butterworth_phi () {
// Save the sign, then remove it
int32_t sign = (c_phi >> 31);
c_phi *= sign;
// Shuffle older samples back
x_phi[0] = x_phi[1]; x_phi[1] = x_phi[2];
y_phi[0] = y_phi[1]; y_phi[1] = y_phi[2];
// Insert latest sample (gain is actually 1/gain)
x_phi[2] = mul(INT_TO_FP(c_phi), gain);
// Compute next output sample
fp a = mul(k1, y_phi[0]);
fp b = mul(k2, y_phi[1]);
y_phi[2] = x_phi[0] + (x_phi[1] << 1) + x_phi[2] - a + b;
// Update the global variable
c_phi = FP_TO_INT(y_phi[2]);
// Restore the sign
c_phi *= sign;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment