我正在使用 Naudio 开源库,并且正在尝试进行一些简单的过滤。问题是我听到一些“咔哒”声,不是太大声。该库为我提供了使用至少两个缓冲区的可能性,因此计算时间不会在它们之间引入延迟。因为在大多数情况下,我都在处理立体声信号,我将它分成两个数组,并且我彼此独立计算。我想知道当我在缓冲区上使用过滤器时是否有什么特别之处。我首先使用了一个低通双二阶滤波器,如下所示:
//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);
我认为我所有的问题都来自前两个样本(输出 0:1),我尝试了所有组合:输出[-1]=0,输出[-1]=输出[0],但没有任何效果。当 "i" 为 0 或 1 时 output[i-1], output[i-2] 应该有什么值?
当我使用 LowPass Windowed-Sinc 滤波器时,我遇到了同样的噪音(咔哒声),就像这样:
//计算系数
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]);
}
}
}
问题肯定不在于拆分信号或组合通道,因为我已经在没有任何过滤器的情况下对此进行了测试,一切正常。我还尝试模拟由处理算法产生的一些延迟(但不改变信号)并且没有出错。我非常确定问题来自过滤。我写的所有东西都用在缓冲区上。