了解 netlib fftpack 的结果
Understanding results of netlib fftpack
我需要在 C++ 中实现跨平台 STFT,它既可以从 python 调用,也可以在 iOS/Android 上编译为 运行。我选择使用 fftpack 因为它是轻量级的并且似乎可以非常快地进行 FFT。
我已经用C:
写了测试代码
int n = 8;
float wsave[2*n + 15];
int ifac[n+15];
float arr[8] = {11.0, 3.0, 4.05, 9.0, 10.3, 8.0, 4.934, 5.11};
// initialize rfftf and rfftb
__ogg_fdrffti(n, wsave, ifac);
// forward transform of a real periodic sequence
__ogg_fdrfftf(n, arr, wsave, ifac);
// Print result
std::cout << "[";
for(int k=0; k<n-1; k++){
std::cout<<arr[k] << ", ";
}
std::cout << arr[n-1] << "]" << std::endl;
// Result: [55.394, -5.58618, 1.66889, 12.316, 3.11, 6.98618, -0.0991108, 5.174]
为了尝试我的代码,我 运行 scipy rfft
在同一个数组上:
arr = np.array([11.0, 3.0, 4.05, 9.0, 10.3, 8.0, 4.934, 5.11])
res = np.real(rfft(arr, n=8))
print(res) # Prints [55.394 -5.58617928 12.316 6.98617928 5.174 ]
为什么 scipy 的值较少。看起来 scipy 因此从同一个结果数组中跳过了一些值,但为什么呢?如何在 C++ 中从 arr
读取正确的 FFT 值?
您从 C++ 程序中获得的值与您在 python 脚本中获得的值相同。只是你扔掉了python脚本中的虚部:
#!/usr/bin/python3
from scipy.fftpack import rfft
arr = [11.0, 3.0, 4.05, 9.0, 10.3, 8.0, 4.934, 5.11]
res = rfft(arr, n=8)
print(res)
输出
[55.394 -5.58617928 1.66888853 12.316 3.11 6.98617928
-0.09911147 5.174 ]
我需要在 C++ 中实现跨平台 STFT,它既可以从 python 调用,也可以在 iOS/Android 上编译为 运行。我选择使用 fftpack 因为它是轻量级的并且似乎可以非常快地进行 FFT。
我已经用C:
写了测试代码int n = 8;
float wsave[2*n + 15];
int ifac[n+15];
float arr[8] = {11.0, 3.0, 4.05, 9.0, 10.3, 8.0, 4.934, 5.11};
// initialize rfftf and rfftb
__ogg_fdrffti(n, wsave, ifac);
// forward transform of a real periodic sequence
__ogg_fdrfftf(n, arr, wsave, ifac);
// Print result
std::cout << "[";
for(int k=0; k<n-1; k++){
std::cout<<arr[k] << ", ";
}
std::cout << arr[n-1] << "]" << std::endl;
// Result: [55.394, -5.58618, 1.66889, 12.316, 3.11, 6.98618, -0.0991108, 5.174]
为了尝试我的代码,我 运行 scipy rfft
在同一个数组上:
arr = np.array([11.0, 3.0, 4.05, 9.0, 10.3, 8.0, 4.934, 5.11])
res = np.real(rfft(arr, n=8))
print(res) # Prints [55.394 -5.58617928 12.316 6.98617928 5.174 ]
为什么 scipy 的值较少。看起来 scipy 因此从同一个结果数组中跳过了一些值,但为什么呢?如何在 C++ 中从 arr
读取正确的 FFT 值?
您从 C++ 程序中获得的值与您在 python 脚本中获得的值相同。只是你扔掉了python脚本中的虚部:
#!/usr/bin/python3
from scipy.fftpack import rfft
arr = [11.0, 3.0, 4.05, 9.0, 10.3, 8.0, 4.934, 5.11]
res = rfft(arr, n=8)
print(res)
输出
[55.394 -5.58617928 1.66888853 12.316 3.11 6.98617928
-0.09911147 5.174 ]