我需要在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的值(value)更少。看起来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 ]
关于python - 了解netlib fftpack的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60600017/