I've implemented a very simple first order recursive low pass filter in c using the algorithm:
double a, b, prevOutput;
double lowPassFilter(double input)
{
double output = a * input + b * prevOutput;
prevOutput = output;
return output;
}
where:
a = 1.0 - b
According to The Scientist and Engineer's Guide to Digital Signal Processing, the cutoff frequency of the filter is defined by the relation:
$$b = e^{-2πf_c}$$
$f_c$ is the -3db cutoff frequency as a fraction of the sampling frequency. Using $f_c = 0.25$, I calculated that $b = 0.20788$ (approximately) and plugged it into my filter. I wanted to verify my filter, so I passed an impulse function through it and recorded the output:
double result[128];
for (int i = 0; i < 128; ++i)
{
if (0 == i) result[i] = lowPassFilter(1.0);
else result[i] = lowPassFilter(0.0);
}
I then performed FFT on the result to find the frequency response:
complex_t fftResult* = fft(result);
for (int i = 0; i < 65; ++i)
{
double amplitude = fftResult[i].real * fftResult[i].real;
amplitude += fftResult[i].img * fftResult[i].img;
amplitude = sqrt(magnitude);
printf("f = %.3f: %f\n", i / 128.0, amplitude );
}
The result I saw was somewhat expected but also not exactly what I expected. In the DC bin ($f = 0$) I saw a amplitude very close to one and I saw the amplitude attenuate as it approached 0.5. However, I also expected that at $f = 0.25$ the amplitude should have been around $0.70795$ (i.e. -3db). but instead the amplitude was around $0.775$:
...
f = 0.242: 0.782501
f = 0.250: 0.774971
f = 0.258: 0.767380
...
Aside from the inefficient code, is there something wrong with the method I am using to obtain the frequency response or am I interpreting the cut-off frequency relation incorrectly or something else?