I\'m trying to solve a problem with python+numpy in which I\'ve some functions of type that I need to convolve with another function . In order to optimize code, I performed th
The problem in my code was both on input line 6, due to an incorrect (my fault) interpretation of the return value of np.fft.fftfreq()
, and on the necessity to pad arrays in order to obtain sound results.
The following code works great and could be extended to multidimension.
In [1]: import numpy as np
In [2]: shift = 1
In [3]: dx = 0.5
In [4]: pad = 20
In [5]: x = np.arange(-10, 11, dx)
In [6]: y = np.cos(x)
In [7]: y = np.pad(y, (0,pad), 'constant')
In [8]: y_shift = np.cos(x-shift)
In [9]: y_fft = np.fft.fft(y)
In [10]: w = np.fft.fftfreq(y.size, dx)
In [11]: phase = np.exp(-2.0*np.pi*1.0j*w*shift)
In [12]: test = phase * y_fft
In [13]: # we use np.real since the resulting inverse fft has small imaginary part values that are zero
In [14]: inv_test = np.real(np.fft.ifft(test))
In [15]: np.allclose(y[:-pad-2],inv_test[2:-pad])
Out[15]: True
Nice, thank you very much for sharing! I've implemented something in this lines some time ago, but couldn't really grasp the mathematics of it (I've blindly ported a whitepaper that described the algorithm). FWIW, this it it: https://github.com/creaktive/flare/blob/master/nrf905_demod.c#L376