为什么我的C++ conv函数的输出与conv Matlab调用中的输出不同
Why is the output of my C++ conv-function not the same as in the conv Matlab call?
因此,我实现了自己的卷积函数,并将其输出与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];
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- C++格式化输出问题
- 将值从二维数组输出到文本文件
- 集合上的输出迭代器:assign和increment迭代器
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 为什么rk4.do_step不输出C++中的初始条件
- 输出没有重复元素的动态数组(收缩数组)C++
- 为什么我的C++ conv函数的输出与conv Matlab调用中的输出不同