I'm messing around with some audio stuff and the algorithm I'm trying to implement calls for a band-pass second-order FIR filter given by the equation
H(z) = z - z^(-1)
How do I implement such a bandpass filter in C?
I have raw audio data as well as an FFT on that audio data available to me, but I'm still not sure how to implement this filter, neither am I sure exactly what the equation means.
In the image below, I am trying to implement HF3:
z^-1
is a unit (one sample) delay, z
is one sample into the future. So your filter output at sample i
depends on input samples at i-1
and i+1
. (In general you can think of z^-n
is an n sample delay.)
If you have time domain samples in an input buffer x[]
, and you want to filter these samples to an an output buffer y[]
, then you would implement the given transfer function like this:
y[i] = x[i+1] - x[i-1]
E.g. in C you might process a buffer of N samples like this:
for (i = 1; i < N - 1; ++i)
{
y[i] = x[i + 1] - x[i - 1];
}
This is a very simple first-order non-recursive high pass filter - it has zeroes at +1 and -1, so the magnitude response is zero at DC (0) and at Nyquist (Fs / 2), and it peaks at Fs / 4. So it's a very broad bandpass filter.
A FIR filter multiplies by coefficients and accumulates a bunch of adjacent input data samples for every output data sample. The number of coefficients will be the same as the number of z terms on the right size of your Z transform.
Note that a bandpass FIR filter usually requires a lot more terms or coefficients, roughly proportional to the steepness of the bandpass transitions desired, so 2 taps is probably too short for any useful bandpass filtering.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With