Low pass filter in iPhone
I have code that implements a low-pass filter in order to detect a blow on the mic. I can't understand how to determine the frequency and pick my own ALPHA:
lowPassResults = ALPHA * peakPowerForChannel + (1.0 - ALPHA) * lowPassResults;
This should detect a blow on the mic.
I want to get a new ALPHA to implement a lowpass filter with fc = 1 khz or higher.
How do i pick another ALPHA to do that ?
They took ALPHA=0.05 and according to Wikipedia formula, with 开发者_Python百科sample rate of 30 times a second which they did, I got fc of 1.2 Hz.
Or should I take a sample rate of 44.1 kHz ? We sample the signal at 30 Hz.
This is a simple recursive low pass filter which is just being used to smooth the measured (instantaneous) power from the microphone. Typically you want a low cut-off frequency, e.g. 1 Hz, so that you filter out all the noise and just get a smoothed power measurement. Increasing the cut off frequency (increasing ALPHA) will make the output respond faster but will introduce more noise. Conversely decreasing the cut off frequency (reducing ALPHA) will make the output respond more slowly but will also reduce the amount of noise.
Note that if you're only updating the filter at 30 Hz then you can't set the cut-off frequency to 1 kHz. The Nyquist rate is 15 Hz so you can only filter at frequencies below this. Besides, it makes no sense to try and filter at such a high frequency if all you want to do is to detect blowing on the mic.
This page gives perhaps a clearer version of the formula, with code for choosing alpha:
void lowPassFrequency(double* input, double* output, int points)
{
double RC = 1.0/(CUTOFF*2*3.14);
double dt = 1.0/SAMPLE_RATE;
double alpha = dt/(RC+dt);
output[0] = input[0]
for(int i = 1; i < points; ++i)
{
output[i] = output[i-1] + (alpha*(input[i] - output[i-1]));
}
}
Note that it works on all sample points at once, rather than over time as in original question.
精彩评论