C ++中的抽取(Decimation in C++)

亲爱的Stack社区,

我正在做一个DSP练习来补充我的C ++ FIR低通滤波器,滤波器系数设计在Matlab中并从Matlab导出。 所讨论的DSP练习是将FIR低通滤波器的输出阵列抽取到较低采样率的因子为“M”。 在C ++中,我在.cpp文件中做了一个成功但非常简单的实现,我一直在努力将它转换为我可以给FIR滤波器的输出数组的函数。 这是代码的最基本版本:

int n = 0; int length = 50; int M = 12; float array[length]; float array2[n]; for (int i = 0 ; i<length; i++) { array[i] = std::rand(); } for (int i = 0; i<length; i=i+M) { array2[n++] = array[i]; } for (int i = 0; i<n; i++) { std::cout << i << " " << array2[i] << std::endl; }

你可以看到非常简单。 遗憾的是,我尝试将此转换为函数使用不起作用。 这是函数:

std::vector<float> decimated_array(int M,std::vector<float> arr){ size_t n_idx = 0; std::vector<float> decimated(n_idx); for (int i = 0; i<(int)arr.size(); i = i + M) { decimated[n_idx++] = arr[i]; } return decimated; }

在.cpp文件中使用此部分代码时,这会产生一个非常常见的EXC_BAD_ACCESS Xcode错误。 错误发生在'decimated [n_idx ++] = arr [i];'行中 特别:

int length = 50; int M = 3; std::vector<float> fct_array(length); for (int i = 0 ; i<length; i++) { fct_array[i] = std::rand(); } FIR_LPF test; std::vector<float> output; output = test.decimated_array(M,fct_array);

我试图理解我的应用程序或者可能只是将我的算法转换为更一般的设置是不正确的。 任何有关此事的帮助都将受到高度赞赏,并希望这一点足以让社区了解。

此致,Vhaanzeit

Dear Stack Community,

I'm doing a DSP exercise to complement my C++ FIR lowpass filter with filter coefficients designed in and exported from Matlab. The DSP exercise in question is the act of decimating the output array of the FIR lowpass filter to a lower sample rate by a factor of 'M'. In C++ I made a successful but extremely simple implementation within a .cpp file and I've been trying hard to convert it to a function to which I can give the output array of the FIR filter. Here is the very basic version of the code:

int n = 0; int length = 50; int M = 12; float array[length]; float array2[n]; for (int i = 0 ; i<length; i++) { array[i] = std::rand(); } for (int i = 0; i<length; i=i+M) { array2[n++] = array[i]; } for (int i = 0; i<n; i++) { std::cout << i << " " << array2[i] << std::endl; }

As you can see very simple. My attempt to convert this to a function using is unfortunately not working. Here is the function as is:

std::vector<float> decimated_array(int M,std::vector<float> arr){ size_t n_idx = 0; std::vector<float> decimated(n_idx); for (int i = 0; i<(int)arr.size(); i = i + M) { decimated[n_idx++] = arr[i]; } return decimated; }

This produces a very common Xcode error of EXC_BAD_ACCESS when using this section of code in the .cpp file. The error occurs in the line 'decimated[n_idx++] = arr[i];' specifically:

int length = 50; int M = 3; std::vector<float> fct_array(length); for (int i = 0 ; i<length; i++) { fct_array[i] = std::rand(); } FIR_LPF test; std::vector<float> output; output = test.decimated_array(M,fct_array);

I'm trying to understand what is incorrect with my application of or perhaps just my translation of the algorithm into a more general setting. Any help with this matter would be greatly appreciated and hopefully this is clear enough for the community to understand.

Regards, Vhaanzeit

最满意答案

问题:

size_t n_idx = 0; std::vector<float> decimated(n_idx);

在使用向量之前,没有对向量进行大小调整,因此在分配给decimated向量的元素0,1等时,您正在调用未定义的行为。

你可以做的是在循环中,调用push_back :

std::vector<float> decimated_array(int M,std::vector<float> arr) { std::vector<float> decimated; for (size_t i = 0; i < arr.size(); i = i + M) { decimated.push_back(arr[i]); } return decimated; }

decimated向量开始为空,但是使用push_back调用添加了一个新项。

此外,您应该通过const引用传递arr向量,而不是通过值传递。

std::vector<float> decimated_array(int M, const std::vector<float>& arr);

传递(const)引用不会调用副本。

编辑:更改循环计数器以更正类型,因此不需要强制转换。

The issue:

size_t n_idx = 0; std::vector<float> decimated(n_idx);

You did not size the vector before you used it, thus you were invoking undefined behavior when assigning to element 0, 1, etc. of the decimated vector.

What you could have done is in the loop, call push_back:

std::vector<float> decimated_array(int M,std::vector<float> arr) { std::vector<float> decimated; for (size_t i = 0; i < arr.size(); i = i + M) { decimated.push_back(arr[i]); } return decimated; }

The decimated vector starts out empty, but a new item is added with the push_back call.

Also, you should pass the arr vector by const reference, not by value.

std::vector<float> decimated_array(int M, const std::vector<float>& arr);

Passing by (const) reference does not invoke a copy.

Edit: Changed loop counter to correct type, thus not needing the cast.

更多推荐