gpt4 book ai didi

c - 对音频信号应用滤波器时的不同输出

转载 作者:行者123 更新时间:2023-11-30 16:58:33 25 4
gpt4 key购买 nike

晚上好,我正在用 C 语言编写一个简单的软件,它使用 libsndfile 库( http://www.mega-nerd.com/libsndfile/api.html )读取 wav 音频文件,然后将样本转到处理函数,在该函数中我对信号应用滤波器(二阶巴特沃斯低通滤波器)滤波器,应用转置的直接形式 II)。之后我将结果写入一个新的 wav 文件。如果我应用简单的操作而不是过滤器(例如将样本乘以常数),它可以正常工作,但是当我应用过滤器时,它会产生大量噪声。我尝试在过滤器之后和将它们写入新文件之前打印样本的值,并且我得到了与从 Matlab 获得的相同的值(其中我得到的输出是完美的),但它们与我的值不同如果我读取了库编写的输出,就得到了。

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}。另外,不要对系数进行归一化,因为 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() 函数中,您可以将音频加载到缓冲区中并像这样处理每个 block :

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);
}

...
}

希望这有帮助。

关于c - 对音频信号应用滤波器时的不同输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38727339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com