Plus & AmpKnight
Hey AmpKnight, how about we dive into designing a super‑precise digital filter for your synth? I’ve been tinkering with a little code that could help us hit that crystal‑clear tone you’re after—let’s see if we can merge some high‑resolution audio math with a touch of creative coding!
Sure, but only if the coefficients are computed to machine epsilon, no rounding, and the filter is 128‑tap, sinc‑based. No tricks, no shortcuts. Let’s write it in C, not Python, and test the impulse response until the ripple is under 0.0001 dB. Anything else is just noise.
Here’s a quick C sketch to get you started on a 128‑tap sinc filter with double precision, no rounding on the coefficient calc. Just tweak the cutoff and test the impulse response with a simple loop.
```
#include <stdio.h>
#include <math.h>
#define N 128
#define PI 3.14159265358979323846
int main() {
double h[N];
double fc = 0.2; // normalized cutoff 0.2 * Nyquist
double sum = 0.0;
for (int i = 0; i < N; i++) {
int n = i - (N/2);
if (n == 0)
h[i] = 2 * PI * fc;
else
h[i] = sin(2 * PI * fc * n) / n;
sum += h[i];
}
// Normalize so DC gain is 1
for (int i = 0; i < N; i++)
h[i] /= sum;
// Impulse response test
for (int i = 0; i < N; i++) {
double y = h[i];
printf("%d: %0.12e\\n", i, y);
}
return 0;
}
```
Compile with `-O2 -std=c11` and run it. Grab the impulse response data, plot it, and measure the ripple. If you hit 0.0001 dB or better, you’re golden. Happy filtering!
Nice skeleton, but a couple of points. The sinc should be sin(2πfc n)/(π n), not sin(2πfc n)/n. So replace the denominator with M_PI*n. Also h[0] should be 2 fc, not 2πfc. That will give you a true brick‑wall shape. And keep the sum for DC normalization—good. Once you tweak that, plot the frequency response with a tool like Audacity or Python. That will show you if you’re under 0.0001 dB ripple. Happy hunting.
Got it! Here’s the tweaked C code with the correct sinc denominator and h[0] = 2*fc:
```
#include <stdio.h>
#include <math.h>
#define N 128
#define PI 3.14159265358979323846
int main() {
double h[N];
double fc = 0.2; // normalized cutoff
double sum = 0.0;
for (int i = 0; i < N; i++) {
int n = i - N/2;
if (n == 0)
h[i] = 2 * fc; // h[0] = 2*fc
else
h[i] = sin(2 * PI * fc * n) / (PI * n); // sinc
sum += h[i];
}
// Normalize DC to 1
for (int i = 0; i < N; i++)
h[i] /= sum;
// Output impulse response
for (int i = 0; i < N; i++)
printf("%d: %.12e\n", i, h[i]);
return 0;
}
```
Compile with `gcc -O2 -std=c11` and run it. Copy the output into Audacity or a Python script to plot the frequency response. Once you see the ripple < 0.0001 dB, you’re all set. Happy hunting!
Looks solid, but a 128‑tap sinc without a window will bleed. Add a Hamming or Blackman window after the sinc, then renormalise. Also check the phase – the filter is symmetric, so it's all‑pass in phase, but if you add a window you’ll lose linearity. Once you’ve done that, plot the magnitude and see if the ripple stays below 0.0001 dB. Happy filtering.