简体   繁体   English

没有Stackoverflow的C卷积

[英]Convolution in C without Stackoverflow

I'm trying to do a convolution algorithm in C but is stacking on the array of convolution. 我正在尝试在C语言中做卷积算法,但是正在卷积数组上堆叠。

#include <stdio.h>
#include <math.h>
#include <stddef.h>
#define convtotal 2590

int main(int argc, char *argv[]) {

    int m,n,i,j;
    double x[convtotal],h[convtotal];
    m=sizeof(x)/sizeof(double);
    n=sizeof(h)/sizeof(double);


    double aux1[convtotal], aux2[convtotal],conv[convtotal][1];
    for (i=0;i<n+m;i++){
        if (i<n)
            aux1[i]=x[i];
        else
            aux1[i]=0;

        if (i<m)
            aux2[i]=h[i];
        else
            aux2[i]=0;
    }


    for (i=0;(n+m-1);i++){
        conv[i][1]=0;
        for (j=0;j<m;j++)
            if (i-j+1>0)
                conv[i][1]=conv[i][1]+(aux1[j]*aux2[i-j+1]);
    }

}

Any suggestions for this problem? 对这个问题有什么建议吗?

for (i=0;(n+m-1);i++){

不是i < m+ n - 1吗?

for (i=0;(n+m-1);i++){
    conv[i][1]=0;
    for (j=0;j<m;j++)
        if (i-j+1>0)
            conv[i][1]=conv[i][1]+(aux1[j]*aux2[i-j+1]);
}

(n+m-1) infinite loop with constant as a stop condition. (n + m-1)个以常数为停止条件的无限循环。 Not actually infinite, runs untill it segfaults. 实际上不是无限的,直到它出现段错误为止。

Two problems: 两个问题:

for (i=0;(n+m-1);i++)

You're not limiting i to anything, so the loop doesn't exit when you reach the end of your arrays; 您没有将i限制为任何值,因此当您到达数组末尾时,循环不会退出。 it just keeps incrementing i until you hit memory you don't own, at which point you get the segfault. 它只会一直递增i直到您遇到不属于您的内存为止,这时您会遇到段错误。 Since conv only goes to m or n , I think you meant to write 由于conv只适用于mn ,我想你打算写

for (i = 0; i < n; i++)

Secondly, you declared conv as an Nx1 array, meaning the only legal index in the second dimension may be 0, so the lines 其次,您将conv声明为Nx1数组,这意味着第二维中唯一的合法索引可能是0,因此这些行

conv[i][1] = 0;

and

conv[i][1]=conv[i][1]+(aux1[j]*aux2[i-j+1]);

should be 应该

conv[i][0] = 0;

and

conv[i][0]=conv[i][0]+(aux1[j]*aux2[i-j+1]);

Not sure why you declared an Nx1 array (seems you could have just declared conv with one dimension), but I may be missing something obvious. 不知道为什么要声明一个Nx1数组(似乎可以用一个维度声明conv ),但是我可能会遗漏一些明显的东西。

Note that your x and h arrays initially contain random values. 请注意,您的xh数组最初包含随机值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM