Goodevening,我在C中编写一个简单的软件,使用libsndfile库(http://www.mega-nerd.com/libsndfile/api.html)读取一个wav音频文件,而不是样本转到一个过程函数,在那里我对信号应用滤波器(二阶巴特沃思)低通滤波器,适用于转置直接形式II)。之后,我将结果写入新的wav文件。 如果我代替过滤器,我应用简单的操作(比如多次采样常数),它可以正常工作,但是当我应用滤波器时会产生很多噪音。 我尝试在过滤器之后以及在新文件中写入之前打印样本的值,并且我获得了与Matlab相同的值(我获得的输出是完美的),但它们与值I不同如果我读了库写的输出,那就得到了。
static void processAudio (double *buffer, int length)
{
//arrays a and b are the coefficients
double a[] = { 1,
-1.799096409484668,
0.817512403384758};
double b[] = { 0.004603998475022,
0.009207996950045,
0.004603998475022};
//arrays a_ and b_ are the coefficients normalized
double a_[] = {1,a[1]/b[0],a[2]/b[0]};
double b_[] = {1,b[1]/b[0],b[2]/b[0]};
double gain = b[0]/a[0];
double reg[] ={0,0}; //memory registers
for(int i = 0; i<length; i++)
{
if(i%2==0) //just left channel is changed
{
//TRANSPOSED DIRECT FORM II
double input = *(buffer+i);
double output =(input + reg[0])*gain;
reg[0] = reg[1]+b_[1]*input-a_[1]*output;
reg[1] = b_[2]*input - a_[2]*output;
*(buffer+i) = output;
}
}
}
//that function is called in the main method inside this cycle
int main(void)
{
...
while ((framesRead = sf_read_double(inputFile, buffer, BUFFER_LENGTH)))
{
processAudio(buffer, framesRead);
sf_write_double(outputFile, buffer, framesRead);
}
...
}
如果我在过滤器之后打印结果:
0.00000000000000
0.00000000000000
0.00000000000000
0.00000000000000
0.00000000000000
0.00000000000000
0.00000000000000
0.00000000000000
0.00000000000000
0.00000000000000
0.00000000000000
0.00000000000000
0.00000000000000
0.00000000000000
0.00000000000000
-0.00000014050288
-0.00000025277823
0.00000050310774
0.00000209530885
0.00000420138311
0.00000725078112
0.00001115570320
0.00001554761090
0.00002053775981
0.00002550357112
0.00002796948679
0.00000727086200
-0.00006401853354
...
如果我读取输出文件,结果是
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
3.05175781250000e-05
3.05175781250000e-05
3.05175781250000e-05
3.05175781250000e-05
0
-6.10351562500000e-05
-0.000366210937500000
-0.00183105468750000
-0.00601196289062500
-0.0140075683593750
-0.0261840820312500
-0.0421142578125000
-0.0610656738281250
-0.0820617675781250
-0.104156494140625
-0.126342773437500
-0.147766113281250
-0.167663574218750
-0.185363769531250
-0.200378417968750
-0.212280273437500
...
他们非常不同。我真的不知道发生了什么。如果您有任何想法,请告诉我。提前谢谢!
答案 0 :(得分:0)
你得到的输出不同,因为你没有保持过滤器的状态。在帧循环的每次迭代中,您都将过滤器状态重置为初始状态{0.0, 0.0}
。另外,不要对系数进行标准化,因为MATLAB会为您执行此操作。只需从MATLAB中取出系数并将其应用于滤波器。
我会形成一个结构来保持过滤器的状态,如下所示:
struct biquad {
double b0, b1, b2, a1, a2; // Note: MATLAB will always normalize a0 to 1.0, so no need to process that.
double r0, r1;
};
然后,我会添加像biquad_process
这样的函数,如下所示:
void process_biquad(struct biquad *self, double *buffer, int length)
{
int i;
for (i = 0; i < length; i++) {
double x = buffer[i];
double y = (x * self->b0) + self->r0;
self->r0 = self->r1 + (x * self->b1) - (self->a1 * y);
self->r1 = (self->b2 * x) - (self->a2 * y);
buffer[i] = y;
}
}
然后,在您的main()
函数中,您可以在缓冲区中加载音频并像这样处理每个块:
int main(void)
{
struct biquad *bq = calloc(1, sizeof(struct biquad));
// Take coefficients from MATLAB and put them here
bq->b0 = ...
bq->b1 = ...
// Set initial state to 0.0
bq->r0 = bq->r1 = 0.0;
...
while ((framesRead = sf_read_double(inputFile, buffer, BUFFER_LENGTH)))
{
process_biquad(bq, buffer, framesRead);
sf_write_double(outputFile, buffer, framesRead);
}
...
}
希望这有帮助。