naivesound/glitch

lpf issue

tsulej opened this issue · 4 comments

Hi, something is wrong with lpf filter implementation. It doesn't filter properly.

Why not use simplest possible filter (taken from some java code)?

float timeInterval = 1.0/rate;
float tau = 1.0 / (cutoff * 2*PI);
float alpha = timeInterval / (tau + timeInterval);
float stage1 = sample * alpha;
float stage2 = prev - (prev * alpha);
prev = (stage1 + stage2);
return prev;

where:

  • rate - sample rate
  • cutoff - cutoff freq
  • sample - sample value
  • prev - previous value of the sample

Currently lpf and hpf are implemented as a simple IIR filter (somewhat similar to the code you've provided):

float wa = tan(PI * cutoff / SAMPLE_RATE);
float a = wa / (1.0 + wa);
iir->value = iir->value + (signal - iir->value) * a;

Cutoff is a frequency, in Hz. iir->value is similar to prev in your script, keeps the previous value of the filter.

I've noticed these filters don't sound well, and I was thinking about maybe replacing them with bi-quad filters. I'll try your code, too, thanks!

I've tested my code on results from the app - and it produces the same results as nyquist lpf from Audacity. You have to run it minimum 2-3 times to get filtered properly.
biquad will work also well.

Just added bi-quad filters for lpf, hpf, bpf and bsf (notch), the syntax is lpf(signal, cutoff, Q). Sounds good to me, I hope you'll like it, too!

Perfect, thx!