I am using Naudio open source library and I am trying to do some simple filtering. The problem is that I hear some "clicks", not too loud. The library offers me the possibility to use at least two buffers, so the computing time doesn't introduce a delay between them. Because in the most of the time I am dealing with a stereo signal, I have split it in two arrays and I compute each other independently. I would like to know if is there something special that I have to do with a filter when I used it on a buffer. I have first used a low pass biquad filter like the one below:
//generate coeff
//sincerely, I don't know what's up with q
//I have taken into consideration some values
//to see if the noise disappears
double w0 = 2 * Math.PI * cutoffFrequency / _sampleRate;
double cosw0 = Math.Cos(w0);
double alpha = Math.Sin(w0) / (2 * q);
_b0 = (1 - cosw0) / 2;
_b1 = 1 - cosw0;
_b2 = (1 - cosw0) / 2;
_a0 = 1 + alpha;
_a1 = -2 * cosw0;
_a2 = 1 - alpha;
for (int i = 2; i < length; i++)
{
output[i] = (float)((_b0 / _a0) * input[i] + (_b1 / _a0) * input[i - 1] + (_b2 / _a0) * input[i - 2]- (_a1 / _a0) * output[i - 1] - (_a2 / _a0) * output[i - 2]);
}
output[1] = (float)(
(_b0 / _a0) * input[1] + (_b1 / _a0) * input[0] + (_b2 / _a0) * input[0]
- (_a1 / _a0) * output[0] - (_a2 / _a0) * output[0]);
output[0] = (float)(
(_b0 / _a0) * input[0] + (_b1 / _a0) * 0 + (_b2 / _a0) * 0
- (_a1 / _a0) * 0 - (_a2 / _a0) * 0);
I thought that all my problems came from the first two samples (output 0:1), I've tried all combinations: output[-1]=0,output[-1]=output[0], but nothing worked. What values output[i-1], output[i-2] should have when "i" is 0 or 1?
I have encountered the same noise (clicks) when I used a LowPass Windowed-Sinc Filter, just like this:
//calculate coeff
int i;
int m = length;
double PI = Math.PI;
length=101;
for (i = 0; i < length; i++)
{
if (i - m / 2 == 0)
{
_h[i] = 2 * PI * _cutOffFrecv;
}
else
{
//!=0
_h[i] = Math.Sin(2 * PI * _cutOffFrecv * (i - m / 2)) / (i - m / 2);
}
_h[i] = _h[i] * (0.54 - 0.46 * Math.Cos(2 * PI * i / m));
}
//normalize the low-pass filter kernel for unity gain at DC
double s = 0;
for (i = 0; i < m; i++)
{
s = s + _h[i];
}
for (i = 0; i < m; i++)
{
_h[i] = _h[i] / s;
}
//convolve the input & kernel
//_kernelSize=101
//most often length is 6615 or 6614 for each channel
//in these examples I compute only one channel
for (j = 0; j < length; j++)
{
output[j]=0;
for (i = 0; i < _kernelSize; i++)
{
if (j >= i)
{
output[j] =(float)(output[j]+ _h[i] * input[j - i]);
}
}
}
The problem surely is not from splitting the signal, or from combining channels, because I have tested this without any filter and everything is ok. I have also tried to simulate some delays created by a processing algorithm (but without changing the signal) and nothing went wrong. I am very sure that the problem comes from filtering. Everything I wrote is used on a buffer.
No comments:
Post a Comment