1

[I am having trouble coding this in Python, I don't really know where to start. The DFT Version 3 is the one pasted below][1]

![1]: https://i.stack.imgur.com/0J7T0.png

![1]: https://i.stack.imgur.com/JXR4i.png

  • u may want to check this C code it's well explained https://batchloaf.wordpress.com/2013/12/07/simple-dft-in-c/ oderwise take a look here : https://www.bogotobogo.com/python/OpenCV_Python/python_opencv3_Signal_Processing_with_NumPy_Fourier_Transform_FFT_DFT.php well to stackoverflow! you should try something before asking, and show ur effort ! good luck! – Engine Oct 07 '21 at 20:20
  • 3
    you're in luck, it's already been made: https://numpy.org/doc/stable/reference/routines.fft.html – Mahrkeenerh Oct 07 '21 at 20:21

1 Answers1

0
import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline

def gen_wave(freq, amp, T, shift, sr):
    time = np.arange(0, T, T/sr)
    X = amp * np.sin(2*np.pi*freq*time+shift)
    return time, X

N = 100

time, amplitude = gen_wave(2, 3, 1, 0, N)
_, amplitude2 = gen_wave(5, 2, 1, 0, N)

amplitude3 = amplitude + amplitude2

plt.figure(figsize=[15, 5])
plt.ylim([-5, 5])
plt.grid(True, which='both')
plt.plot(time, amplitude, c="b")

plt.figure(figsize=[15, 5])
plt.ylim([-5, 5])
plt.grid(True, which='both')
plt.plot(time, amplitude2, c="b")

plt.figure(figsize=[15, 5])
plt.ylim([-5, 5])
plt.grid(True, which='both')
plt.plot(time, amplitude3, c="b")


k = np.arange(0, N)
n = np.arange(0, N)

k = k.reshape((N,1))

M = 2 * np.pi * k * n / N
Xa = np.cos(M)
Xb = 1j * np.sin(M)
X = Xa - Xb
print(X.shape)
Y = X.dot(amplitude3)
print(Y.shape)

plt.plot(k, Y.real, k, Y.imag)

Using Numpy:

gabarito = np.fft.fft(amplitude3, N)
plt.plot(k, gabarito.real, k, gabarito.imag)

REFER TO:

https://github.com/gnascimento/dft-python/blob/master/DFT.ipynb

Guinther Kovalski
  • 1,629
  • 1
  • 7
  • 15