1d linear convolution in ANSI C code?

前端 未结 4 1927
孤城傲影
孤城傲影 2021-02-01 08:49

Rather than reinvent the wheel, I wonder if anyone could refer me to a 1D linear convolution code snippet in ANSI C? I did a search on google and in stack overflow, but couldn\'

4条回答
  •  庸人自扰
    2021-02-01 09:39

    I used @Mehrdad's approach, and created the following anwer:

    void conv(const double v1[], size_t n1, const double v2[], size_t n2, double r[])
    {
        for (size_t n = 0; n < n1 + n2 - 1; n++)
            for (size_t k = 0; k < max(n1, n2) && n >= k; k++)
                r[n] += (k < n1 ? v1[k] : 0) * (n - k < n2 ? v2[n - k] : 0);
    }
    

    There's problem with index exceeding lower bound when in second loops k gets bigger than n, so, guess there should be extra condition to prevent that.

提交回复
热议问题