Shafayat__

Untitled

Jul 1st, 2023
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. def fourier_transform(data):
  5.     N = len(data)
  6.     n = np.arange(N)
  7.     k = n.reshape((N, 1))
  8.     e = np.exp(-2j * np.pi * k * n / N)
  9.     return np.dot(e, data)
  10.  
  11. def inverse_fourier_transform(result):
  12.     N = len(result)
  13.     n = np.arange(N)
  14.     k = n.reshape((N, 1))
  15.     e = np.exp(2j * np.pi * k * n / N)
  16.     return np.dot(e, result)
  17.  
  18. data = np.array([1,1,1,0])
  19.  
  20. result = fourier_transform(data)
  21.  
  22. inverse_result = (inverse_fourier_transform(result)) / len(data)
  23. # print(result)
  24.  
  25. fig, (ax1, ax2) = plt.subplots(1, 2)
  26.  
  27. ax1.plot(np.abs(result), marker='o', linestyle='', color='b')
  28. print(np.angle(result))
  29. ax1.set_xlabel('Frequency')
  30. ax1.set_ylabel('Magnitude')
  31. ax1.set_title('Discrete Fourier Transform')
  32.  
  33. ax2.plot(np.abs(inverse_result), marker='o', linestyle='', color='b')
  34. ax2.set_xlabel('Time')
  35. ax2.set_ylabel('Magnitude')
  36. ax2.set_title('Discrete Time Signal')
  37.  
  38. plt.show()
  39.  
Advertisement
Add Comment
Please, Sign In to add comment