I encountered an odd situation in Python (version 3.9.8) and numpy (version 1.20.0)trying to convert float64 numbers to int. It started with calculating the fft coefficients with numpy and then using inverse DFT with
x = np.array([1, -4, 2, 2, 1, -4, 6, 2]) # test vector
X = np.fft.fft(x) # calc fft coefficients
The result is fine so far:
print(X)
[ 6. +0.j 0. +4.j -6.+12.j 0. -4.j 14. +0.j 0. +4.j -6.-12.j 0. -4.j]
Now I've written my own inverse DFT and get correct results:
N = len(X) # number of samples N
y = np.zeros(N) # create empty result vector with N elements
# calc inverse DFT
for n in np.arange(N):
sum = 0
for k in np.arange(N):
sum = sum + X[k] * np.exp(1j * 2* np.pi * k * n/N)
y[n] = sum.real/N
# print results
print(y)
[ 1. -4. 2. 2. 1. -4. 6. 2.]
The resulting vector is also fine.
But when I convert this vector to int
strange things happen. IIn the second half of the vector, values change in ways that are inexplicable to me.
print(y.astype('int'))
[ 1 -4 2 2 0 -3 6 1]
Any idea?