DFT coefficients are totally different from FFT

33 Views Asked by At

I found the Fourier Transform coefficients using the following formula:

enter image description here

I used the following values as a signal:

import matplotlib.pyplot as plt
from math import tau
from scipy.integrate import quad_vec
import numpy as np

signal_x = np.array([1, 2, 3, 4, 5, 5, 5, 4, 3, 2])
signal_y = np.array([5, 5, 5, 5, 5, 4, 3, 3, 3, 3])

plt.plot(signal_x, signal_y)

enter image description here

After that I found the coefficients by transcribing the DFT formula of complex numbers in python

enter image description here

new_x = np.linspace(0, 2*np.pi, len(signal_x))

# LINEAR INTERPOLATION
def f(t, new_x, x, y):
    return np.interp(t, new_x, np.array(x) + 1j*np.array(y))

# DFT Complex Number
qt_coeff = 5
coeffs = []
for n in range(-qt_coeff, qt_coeff): # -5 +4 = 10 coeffs
    coef = 1/tau * quad_vec(lambda t: f(t, new_x, signal_x, signal_y)*np.exp(t*1j*n), 0, tau)[0]
    coeffs.append(coef)

coeffs = np.array(coeffs)

print(coeffs)

# [ 0.04353136+0.02109635j  0.04473849+0.07575828j  0.09004453-0.00688048j
#  -0.0256607 +0.13901032j -0.19044464+0.17837592j  3.61111111+4.11111111j
#  -1.37786713-0.37885172j -0.17198653+0.00530144j -0.16603542-0.06911041j
#  -0.11103156-0.05656161j]

But when using the FFT to gain speed the values of the coefficients are totally different.

# FFT - Fast Fourier transform
signal_complex = signal_x + 1j*signal_y
coeffs_fft = np.fft.fft(signal_complex)
print(coeffs_fft)

#[ 3.40000000e+01+41.j -3.31676888e+00 +1.j -1.00000000e+00 +1.j
#  9.25221011e-01 +1.j -1.00000000e+00 +1.j -4.44089210e-16 +1.j
# -1.00000000e+00 +1.j -1.98094910e+00 +1.j -1.00000000e+00 +1.j
# -1.56275030e+01 +1.j]

Could someone tell me what I'm doing wrong, to be able to use the FFT correctly?