Hi,
just to add my 5c and just because I spent several hours digging again into
digital filter design 2 nights ago, here are the results. I've used
floats even in the filter, but changing it to int (32bit) shouldn't be a
big issue.
You get an approximated 2nd order (12dB/octave) Butterworth low-pass
IIR filter with a cutoff frequency (-3dB) of 120 Hz by applying the following
piece of 'code':
{
float x, y1 = 0, y2 = 0, y;
float alpha_0 = 7.22031e-05, beta_1 = -1.97582, beta_2 = 0.976111;
while (/* input samples left */)
{
/* read sample into x */
/* filter */
x = x * alpha_0;
y = y1 + x;
y1 = y2 + 2 * x - y * beta_1;
y2 = x - y * beta_2;
/* output is in y */
}
}
To compute the coefficients:
f_a = 44100
f_g = 120
F_g = f_g / f_a
norm = cos (pi * F_g) / sin (pi * F_g)
/* Coefficients for Butterworth 2. order, continuous world */
a_1 = 1.4142
b_1 = 1.0000
/* discrete coefficients */
alpha_0 = 1 / (1 + a_1 * norm + b_1 * norm * norm)
beta_1 = 2 * (1 - b_1 * norm * norm) / (1 + a_1 * norm + b_1 * norm * norm)
float beta_2 = (1 - a_1 * norm + b_1 * norm * norm) /
(1 + a_1 * norm + b_1 * norm * norm)
If 12dB/octave are not enough, you can cascade several filters but you have
to use different coefficients a_1 and b_1 in each of them to get the same
cutoff frequency:
Given that there are N 2nd order filter (see above), for each stage k
(1 <= k <= N) you get the coefficients like this:
a_1[k] = 2 * cos (( 2 * k - 1) * pi / ( 4 * N ))
b_1[k] = 1
E.g. for an 24dB/oct filter (4th order):
a_1[1] = 1.84776, b_1[1] = 1
a_1[2] = 0.765367, b_1[2] = 1
alpha_0[1] = 7.19397e-05, beta_1[1] = -1.96861, beta_2[1] = 0.968901
alpha_0[2] = 7.2601e-05, beta_1[2] = -1.98671, beta_2[2] = 0.987
You might get instabilities with low cutoff frequencies and high N.
Even though you are already happy with your solution, I hope someone
besides myself will find this at least interesting ;-)
Jacob
On Mon, Aug 15, 2005 at 09:54:00PM +0200, Tomas wrote:
> Well... following my calculations and Dave's suggestion I have just used
> a 128 samples moving average, and it works great.
>
> [...]
>
> [IDC]Dragon wrote:
>
> >I suggest using an IIR filter instead of FIR. You need way less past
> >samples
> >for each step.
> >[...]
_______________________________________________
http://cool.haxx.se/mailman/listinfo/rockbox
Received on Wed Aug 17 01:29:57 2005