2
votes

I've been using some code which implements a low pass filter on audio signal data sampled every 100 microseconds (EDIT: i originally mistakenly typed nano) (aka 10Khz) and then subtracts that lowpassed data from the original signal to yield a highpassed signal. If I'm not mistaken, this code is an implementation of the single pole lowpass filter described here.

My question is how do i calculate the cutoff frequency this code is giving me?

EDIT: added declaration of valueLowpassed

void hw_timer_sample_fn(void) {

  // init this to typical bias level from op amp
  static float valueLowpassed = 555.0f;

  // read a sample from the Analog to Digital Converter (ADC)
  uint16_t adcValue = system_adc_read();

  // single pole, low-pass filter -- what is FREQUENCY?
  valueLowpassed = valueLowpassed + (adcValue - valueLowpassed) / 64.0f;

  // remove lowpassed from original signal
  const float valueHighpassed = adcValue - valueLowpassed;

  // etc. etc. 
}

I have a vague sense that if I increase 64.0f, this is making the filter less drastic but its effect is more sluggish and steady -- is this lowering or raising the cutoff frequency? I decrease that number it has a much more active 'correction' of the original signal.

1
I would use some existing implementation, for recursive filtering design is complex. Besides, why using subtraction techniques? Is this FFT based? - Michael Chourdakis
In your equation, 64.0f is alpha [per article]. But, the article calculates alpha from: delta_t = t[1]-t[0]; tau = 0.002; alpha = delta_t / tau. So, that might be a better basis to start with rather than hardwiring it. - Craig Estey
What type is valueLowpassed? Integer or Float? - Michael Dorgan
@MichaelDorgan it's a static float declared locally to a function. i've edited the post to reflect this. - S. Imp

1 Answers

1
votes
  • In the article, the code for the filter is y += alpha * (x - y), so you are using an alpha of 1.0/64.0 = 0.015625.
  • In the article, the equation for alpha is alpha = 1 - exp(-deltaT/tau) or, approximately, alpha ~= deltaT/tau when deltaT is small compared to tau. Solving for tau gives tau = -deltaT/log(1 - alpha) or, approximately, tau ~= deltaT/alpha when deltaT is small compared to alpha.
  • For your signal sampled at 10 kHz, deltaT = 0.0001.
  • In the article, tau is the reciprocal of the "cutoff frequency". That's bad terminology in the article, since the 3dB-down frequency is actually 1/(2*pi*tau). But using the article's definition, the cutoff frequency is 1/tau ~= alpha/deltaT = 0.015625/0.0001 = 156.25 Hz. The 3dB-down frequency is a factor of 1/(2*pi) lower, at 24.868 Hz.
  • Notice that deltaT is, indeed, small compared to alpha, so the approximation should be pretty good. Using the more complicated formula, you would get 1/tau = -log(1-alpha)/deltaT = -log(1-0.015625)/0.0001 = 157.48357 Hz, and the 3dB-down frequency would be 25.064 Hz.
  • Increasing your constant 64 has the effect of decreasing alpha, which increases tau, which decreases the cutoff frequency. So the low-pass filter would filter away more of the signal, while the high-pass filter would let more of the signal through.