为什么我的C++ conv函数的输出与conv Matlab调用中的输出不同

Why is the output of my C++ conv-function not the same as in the conv Matlab call?

本文关键字:conv 输出 调用 Matlab 函数 我的 为什么 C++      更新时间:2023-10-16

因此,我实现了自己的卷积函数,并将其输出与Matlab conv函数之一进行了比较。

具体来说,我希望conv( [0.1, 0.23, 0.25, 0.18, 0.09], [0, 0, 1, 2, 3, 4, 5, 0, 0], 'same')调用的输出与调用conv({0, 0, 1, 2, 3, 4, 5, 0, 0}, 2, 6, {0.1, 0.23, 0.25, 0.18, 0.09}, 5, 5, output);后用output写的内容相同。

这是我的代码(它假设信号已经填充,这就是为什么我有_start和_stop的东西)

void conv(double* signal, int conv_start, int conv_stop, double* kernel, int kernel_len, int output_len, double* output){   
    int halfKernel = floor(kernel_len/2.0);
    for (int i = 0; i<output_len; i++) output[i] = 0;
    for (int c = conv_start; c<=conv_stop; c++){
        for (int k = -halfKernel; k <=halfKernel; k++){ 
            output[c-conv_start] += kernel[k+halfKernel]*signal[c+k];
        }
    }
}

Matlab函数的输出是: 1.0100 1.7700 2.6200 2.8700 2.2400

而我的是: 0.880000 1.630000 2.480000 2.790000 2.470000 .

我也用上面的输入手动完成了计算,然后我得到了与我自己的实现相同的结果。那么,这是一个概念错误,还是 Matlab 函数没有做我认为它应该做的事情?

首先,由于您编写了函数,因此您的手牌计算结果相同是有道理的。此外,这暗示这确实是一个概念错误。

在卷积中,内核应该反映在中间(或者如果你愿意,可以翻转)。因此,您可以执行以下操作:

output[c-conv_start] += kernel[(kernel_len -1) - (k+halfKernel)]*signal[c+k];