我正在尝试了解如何利用 fft 来处理我使用软件无线电捕获的数据。我发现了 C++ 的 fftw3 库,并试图查找一些文档或教程,但发现没有太多可取之处。
但是我确实找到了这个tutorial。执行 fft/ifft/等的代码。编译和工作对我来说很好,但是当我试图实现 fftShift 函数时,我得到以下编译器错误:
In file included from /usr/include/c++/9/algorithm:62,
from CppFFTW.cpp:13:
/usr/include/c++/9/bits/stl_algo.h: In instantiation of ‘_RandomAccessIterator std::_V2::__rotate(_RandomAccessIterator, _RandomAccessIterator, _RandomAccessIterator, std::random_access_iterator_tag) [with _RandomAccessIterator = double (*)[2]]’:
/usr/include/c++/9/bits/stl_algo.h:1449:27: required from ‘_FIter std::_V2::rotate(_FIter, _FIter, _FIter) [with _FIter = double (*)[2]]’
CppFFTW.cpp:76:54: required from here
/usr/include/c++/9/bits/stl_algo.h:1371:16: error: array must be initialized with a brace-enclosed initializer
1371 | _ValueType __t = _GLIBCXX_MOVE(*__p);
| ^~~
/usr/include/c++/9/bits/stl_algo.h:1373:22: error: invalid array assignment
1373 | *(__p + __n - 1) = _GLIBCXX_MOVE(__t);
| ^
/usr/include/c++/9/bits/stl_algo.h:1394:16: error: array must be initialized with a brace-enclosed initializer
1394 | _ValueType __t = _GLIBCXX_MOVE(*(__p + __n - 1));
| ^~~
/usr/include/c++/9/bits/stl_algo.h:1396:10: error: invalid array assignment
1396 | *__p = _GLIBCXX_MOVE(__t);
| ^
这是我用来编译代码的行:
g++ CppFFTW.cpp -lfftw3 -lm
此错误消息对我来说意义不大,但我认为这意味着算法的 std::rotate 函数由于某种原因不再适用于 fftw_complex 数据类型。
这给我留下了几个问题
fftShift 应该做的是像本例中那样将零分量移动到中心
When # elements odd:
{a, b, c, d, e, f, g} -> {e, f, g, a, b, c, d}
When # elements even:
{a, b, c, d, e, f, g, h} -> {e, f, g, h, a, b, c, d}
这里是 fftShift 定义定义:
// FFT shift for complex data
void fftShift(fftw_complex *data)
{
// even number of elements
if (N % 2 == 0) {
std::rotate(&data[0], &data[N >> 1], &data[N]);
}
// odd number of elements
else {
std::rotate(&data[0], &data[(N >> 1) + 1], &data[N]);
}
}
这是其余代码(省略了与问题无关的函数/调用):
*
Example code for how to utilize the FFTW libs
Adapted from damian-dz C++ Tutorial
https://github.com/damian-dz/CppFFTW/tree/master/CppFFTW
(https://www.youtube.com/watch?v=geYbCA137PU&t=0s)
To compile, run: g++ CppFFTW.cpp -lfftw3 -lm
-lfftw3 -lm links the code to the fftw3 library
*/
#include <fftw3.h>
#include <iostream>
#include <cmath>
#include <algorithm>
// macros for real & imaginary parts
#define REAL 0
#define IMAG 1
// length of the complex arrays
#define N 8
/* Computres the 1-D Fast Fourier Transform. */
void fft(fftw_complex *in, fftw_complex *out)
{
// create a DFT plan
fftw_plan plan = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
// execute the plan
fftw_execute(plan);
// clean up
fftw_destroy_plan(plan);
fftw_cleanup();
}
/* Displays complex numbers in the form a +/- bi */
void displayComplex(fftw_complex *y)
{
for (int idx = 0; idx < N; ++idx) {
if (y[idx][IMAG] < 0) {
std::cout << y[idx][REAL] << " - " << abs(y[idx][IMAG]) << "i" << std::endl;
} else {
std::cout << y[idx][REAL] << " + " << abs(y[idx][IMAG]) << "i" << std::endl;
}
}
}
// FFT shift for complex data
void fftShift(fftw_complex *data)
{
// even number of elements
if (N % 2 == 0) {
std::rotate(&data[0], &data[N >> 1], &data[N]);
}
// odd number of elements
else {
std::rotate(&data[0], &data[(N >> 1) + 1], &data[N]);
}
}
/* Test */
int main()
{
// input array
fftw_complex x[N];
// output array
fftw_complex y[N];
// fill the first array with some numbers
for (int idx = 0; idx < N; ++idx) {
x[idx][REAL] = idx + 1;
x[idx][IMAG] = 0;
}
// compute the FFT of x and store results in y
fft(x, y);
// display the results
std::cout << "FFT =" << std::endl;
displayComplex(y);
// "shifted" results
fftShift(y);
std::cout << "\nFFT shifted =" << std::endl;
displayComplex(y);
return 0;
}
没有看到任何明显的现有问题可以应用于我的案例(可能是错误的,我还在学习 C++),所以我做了一个帐户,这是我在这里的第一个问题。请随时提供反馈,以便我可以使我的问题更容易理解。
std::complex
:
对于
类型的任何对象z
,std::complex<T>
是reinterpret_cast<T(&)[2]>(z)[0]
的实部,z
是reinterpret_cast<T(&)[2]>(z)[1]
的虚部。z
此要求的目的是保持 C++ 库复数类型与 C 语言复数类型(及其数组)之间的二进制兼容性,它们具有相同的对象表示要求。
现在,
fftw_complex
不一定是用 C 语言定义的复数类型 - 但您可能会通过执行类似的强制转换而逃脱。事实上,fftw3.h
标题中的评论表明它很可能没问题:
/* If <complex.h> is included, use the C99 complex type. Otherwise
define a type bit-compatible with C99 complex */
#if !defined(FFTW_NO_Complex) && defined(_Complex_I) && defined(complex) && defined(I)
# define FFTW_DEFINE_COMPLEX(R, C) typedef R _Complex C
#else
# define FFTW_DEFINE_COMPLEX(R, C) typedef R C[2]
#endif
fftw_complex
的 C++ 定义变成了 double[2]
,这也是 std::rotate
失败的原因——您不能将数组分配给数组。
所以我会用这样的
fftw_complex*
定义我的函数:
void fftShift(std::complex<double>* data) {
static_assert(sizeof(fftw_complex) == sizeof(std::complex<double>));
// even number of elements
if(N % 2 == 0) {
std::rotate(&data[0], &data[N >> 1], &data[N]);
}
// odd number of elements
else {
std::rotate(&data[0], &data[(N >> 1) + 1], &data[N]);
}
}
void fftShift(fftw_complex* data) {
fftShift(reinterpret_cast<std::complex<double>*>(data));
}
int main() {
fftw_complex data[N];
fftShift(data);
}
更好的选择是在您的 C++ 代码中使用
std::complex<double>
s,并且仅在调用 fftw_complex*
函数时转换为 fftw
。
std::vector<std::complex<double>> wave;
fftw_function( reinterpret_cast<fftw_complex*>(wave.data()), wave.size() );
- 为什么这个方法在教程视频中有效,但对我却不起作用?
你没有链接到教程所以不可能说。
- 有哪些方法可以理解此错误消息的含义?
这意味着
std::rotate
尝试将一个 double[2]
分配给另一个 double[2]
,as-if 通过做:
double a[2]{};
double b[2]{};
a = b; // error: invalid array assignment
- 如何实现有效的
功能?fftShift
见上文。
- 我应该使用不同的编译器版本吗?
不,即使是
g++
和 clang++
的更新版本也会抱怨同样的问题。