了解netlib fftpack的结果

2024-10-02 02:28:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在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]

为了尝试我的代码,我在同一个数组上运行了scipyrfft

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++中如何从^ {CD2}中读取正确的FFT值?p>


Tags: fftnp跨平台resscipy数组floatreal
1条回答
网友
1楼 · 发布于 2024-10-02 02:28:35

你从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     ]

相关问题 更多 >

    热门问题