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\'
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.