I have implemented a rudimentary low-pass filter using a time based value. This is ok, but trying to find the correct time slice is guess work, and gives different results based on different input audio files. Here is what I have now:
- (void)processDataWithInBuffer:(const int16_t *)buffer outBuffer:(int16_t *)outBuffer sampleCount:(int)len {
BOOL positive;
for(int i = 0; i < len; i++) {
positive = (buffer[i] >= 0);
currentFilteredValueOfSampleAmplitude = LOWPASSFILTERTIMESLICE * (float)abs(buffer[i]) + (1.0 - LOWPASSFILTERTIMESLICE) * previousFilteredValueOfSampleAmplitude;
previousFilteredValueOfSampleAmplitude = currentFilteredValueOfSampleAmplitude;
outBuffer[i] = currentFilteredValueOfSampleAmplitude * (positive ? 1 : -1);
}
}
What can I do to convert this code into code that will allow me to cut frequencies over a certain hz by a certain db level?
I strongly recommend numerical recipes in c. Outside of that, I'm not sure I can help you.
When you design a filter, you need to calculate the coefficients of that filter based on the frequency so you almost need a class to handle it, not just a function.
This is in C++ but it should get you started. Sorry I can't provide a concrete answer.
I implemented a filter using the interactive filter designer.
Here's some sample code, with it integrated: https://github.com/davidcairns/MediaPlayerDemo
What you have is an IIR filter, and for more control I'd suggest using a FIR filter, which is easier to calculate the coefficients for. I create a window function that is:
y = sin (x * bandwidth) / (sin (x) * windowWidth)
where windowWidth is how many samples wide your window is, x ranges from -2 * PI to 2 * PI, and bandwidth:
bandwidth = 2 * frequency * n / sampleRate;
This creates an array of numbers which you apply to a range of samples centered around the one you want to output. You iterate this over every sample.
I've summarized my own code for doing this, since the original code is rather crufty.