Reputation: 3533
I need to implement a cross platform STFT in C++ that can be called both from python and can be compiled to run on iOS/Android. I have chosen to use fftpack since it is lightweight and seems to do the FFT quite fast.
I have written test code in 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]
To try my code I ran the scipy rfft
on the same array:
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 ]
Why does scipy have fewer values. It looks like scipy is consequently skipping some values from the same result array, but why? How to read the correct FFT values from arr
in C++?
Upvotes: 1
Views: 174
Reputation: 117861
You are getting the same values from your C++ program as you do in your python script. It's just that you throw away the imaginary parts in the python script:
#!/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)
Output
[55.394 -5.58617928 1.66888853 12.316 3.11 6.98617928
-0.09911147 5.174 ]
Upvotes: 2