C语言 在 C 中实现简单的高通和低通滤波器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13882038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:44:35  来源:igfitidea点击:

Implementing simple high and low pass filters in C

cfiltersignal-processingportaudio

提问by Slater Victoroff

Trying to use portaudio to record some data, then use an algorithmic filter to change the recorded voice and then play it back. I've verified a lot of it (coming from example) but I'm quite new to C and I think in my filter implementation I've done something silly.

尝试使用 portaudio 录制一些数据,然后使用算法过滤器更改录制的语音,然后播放。我已经验证了很多(来自示例),但我对 C 很陌生,我认为在我的过滤器实现中我做了一些愚蠢的事情。

#if LOW_PASS 
{
    float RC = 1.0/(CUTOFF*2*3.14);
    float dt = 1.0/SAMPLE_RATE;
    float alpha = dt/(RC+dt);
    float filteredArray[numSamples];
    filteredArray[0] = data.recordedSamples[0];
    for(i=1; i<numSamples; i++){
        filteredArray[i] = filteredArray[i-1] + (alpha*(data.recordedSamples[i] - filteredArray[i-1]));
    }
    data.recordedSamples = filteredArray;
}
#endif
#if HIGH_PASS
{
    float RC = 1.0/(CUTOFF*2*3.14);
    float dt = 1.0/SAMPLE_RATE;
    float alpha = RC/(RC + dt);
    float filteredArray[numSamples];
    filteredArray[0] = data.recordedSamples[0];
    for (i = 1; i<numSamples; i++){
        filteredArray[i] = alpha * (filteredArray[i-1] + data.recordedSamples[i] - data.recordedSamples[i-1]);
    }
    data.recordedSamples = filteredArray;
}
#endif

When the recorded signal tries to go through these filters I get something the following error:

当记录的信号尝试通过这些过滤器时,我收到以下错误消息:

*** glibc detected *** ./paex_record: free(): invalid pointer: 0xbfd68600 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb75e2ee2]
./paex_record[0x8048fe5]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb75864d3]
./paex_record[0x80487f1]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 08:05 2363767    /home/svictoroff/Documents/CompArch/portaudio/examples/paex_record
...
bfd68000-bff1a000 rw-p 00000000 00:00 0          [stack]
Aborted (core dumped)

I'm just really not sure what's going on here. Any thoughts? Free is called from the end of the script at terminate here:

我真的不确定这里发生了什么。有什么想法吗?从脚本末尾处调用 Free 在此处终止:

Pa_Terminate();
    if( data.recordedSamples )       /* Sure it is NULL or valid. */
        free( data.recordedSamples );
    if( err != paNoError )
    {
        fprintf( stderr, "An error occured while using the portaudio stream\n" );
        fprintf( stderr, "Error number: %d\n", err );
        fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
        err = 1;          /* Always return 0 or 1, but no other return codes. */
    }
    return err;

采纳答案by user1284631

The problem is that data.recordedSamples now (at the time of free()) points towards a structure allocated on the stack, not on the heap!

问题是 data.recordedSamples 现在(在 时free())指向分配在堆栈上的结构,而不是在堆上!

Since you had this instruction:

既然你有这个指令:

data.recordedSamples = filteredArray;

The

if( data.recordedSamples )

is of no use, since the adress id valid, but not consistent: it is never allocated with malloc()and it is not on the heap, but on the stack!

没有用,因为地址 id 有效,但不一致:它永远不会被分配,malloc()它不在堆上,而是在堆栈上!

At the moment when you are calling free(), that adress could well point towards the stack of another function.

在您调用 的那一刻free(),该地址很可能指向另一个函数的堆栈。

Copy your filtered data back over the original recordedSamplesif you want, just do not re-assign that pointer.

recordedSamples如果需要,将过滤后的数据复制回原始数据,只是不要重新分配该指针。

edit:

编辑:

use this:

用这个:

for(i = 0; i<numSamples; i++) {
    data.recordedSamples[i] = filteredArray[i];
}

回答by nmichaels

It looks like you're trying to free a stack variable. The only time you have to call freeis when you've previously called malloc(or one of its friends like calloc) or when the documentation for a library function you're calling says you need to free a pointer that it returns.

看起来您正在尝试释放堆栈变量。您必须调用的唯一时间free是您以前调用过malloc(或它的朋友之一,例如calloc),或者当您调用的库函数的文档说您需要释放它返回的指针时。

Incidentally, any time you do free a pointer, a good practice is to set it to NULL immediately afterwards.

顺便说一下,任何时候你释放一个指针,一个好的做法是在之后立即将它设置为 NULL。

Stack variables go away as soon as they're out of scope. Thismight help you understand better.

一旦超出范围,堆栈变量就会消失。可能会帮助您更好地理解。