37154135
Fourier Transform Assignment
1. Fourier transform of sine wave (code): import numpy as np import matplotlib.pyplot as plt from scipy.fftpack import fft,fftfreq dt = 0.01 time = np.arange(0,5.,dt) f_1 = 3. a_1 = 2.3 y = a_1*np.sin(2.*np.pi*time*f_1) plt.plot(time,y) plt.xlabel("Time t [s]") plt.ylabel("Wave") plt.title("Wave Signal") plt.show() n = time.shape[-1] transform = (fft(y)[:n/2]) * 2./n frequency = fftfreq(n,time[1]-time[0])[:n/2] plt.plot(frequency,np.abs(transform)) plt.xlabel("Frequency f [Hz]") plt.ylabel("Amplitude") plt.title("Fourier Transform")
Input:
Output:
(code): import numpy as np import matplotlib.pyplot as plt from scipy.fftpack import fft,fftfreq dt = 0.01 time = np.arange(0,5.,dt) f_1 = 3. a_1 = 2.3 f_2 = 2.5 a_2 = 2 f_3 = 1.5 a_3 = 1.8 y = a_1*np.sin(2.*np.pi*time*f_1)+a_2*np.sin(2.*np.pi*time*f_2)+a_3*np.sin(2.*np.pi*time*f_3) plt.plot(time,y) plt.xlabel("Time t [s]") plt.ylabel("Wave") plt.title("Wave Signal") plt.show() n = time.shape[-1] transform = (fft(y)[:n/2]) * 2./n frequency = fftfreq(n,time[1]-time[0])[:n/2] plt.plot(frequency,np.abs(transform)) plt.xlabel("Frequency f [Hz]") plt.ylabel("Amplitude") plt.title("Fourier Transform")
Input:
Output:
Discussion:
There are three peaks one Fourier transform diagram that represent three waves with different frequencies. The height of each peak is the amplitude of each wave, while the position of each peak represents their frequencies.
2. Fourier transform of a square wave (code): import numpy as np import matplotlib.pyplot as plt from scipy.fftpack import fft,fftfreq data = np.loadtxt('squareWave.csv', delimiter=',') time = data[:,0] y = data[:,1] plt.plot(time,y) plt.xlabel("Time t [s]")
plt.ylabel("Wave")