我需要在C++中实现跨平台STFT,它既可以从python调用,也可以编译为在iOS/Android上运行。我选择使用弗特帕普,因为它是轻量级的,似乎做快速傅立叶变换。
我用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]为了尝试我的代码,我在同一个数组上运行了code 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 ]为什么枕木有较少的价值。因此,它看起来像是跳过了同一个结果数组中的一些值,但是为什么呢?如何从arr中读取正确的C++值?
发布于 2020-03-09 12:19:08
您从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 ]https://stackoverflow.com/questions/60600017
复制相似问题