Last active
August 29, 2015 09:40
-
-
Save mick001/ce494f9605eae22900dd to your computer and use it in GitHub Desktop.
Fourier Transform: first trials. Full article at: http://www.firsttimeprogrammer.blogspot.com/2015/05/fourier-transform-first-trials.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
import numpy as np | |
import scipy as sc | |
plt.style.use("dark_background") | |
fs = 1000 | |
# Time | |
t = np.linspace(0,1.5-1/fs,1500) | |
# Frequencies in the signal | |
f1 = 85 | |
f2 = 50 | |
f3 = 75 | |
# Signal | |
x = 5*np.sin(2*np.pi*f1*t-0.5) + 2*np.cos(2*np.pi*f2*t-0.75) + 2.5*np.cos(2*np.pi*f3*t+1.2) | |
# Take the FFT of the signal | |
X = sc.fft(x) | |
# The time domain representation of the signal | |
# (use 350 to get the picture I got) | |
def timeDomain(n=len(t)): | |
plt.plot(t[:n],x[:n]) | |
plt.title("Time domain signal representation") | |
plt.xlabel("Time") | |
plt.grid(True) | |
plt.show() | |
# Plot the magnitude of the signal | |
def magnitude(): | |
plt.plot(abs(X)) | |
plt.xlabel("Bins") | |
plt.ylabel("Magnitude") | |
plt.grid(True) | |
plt.show() | |
# Plot the frequency domain signal representation | |
def frequencyDomain(): | |
freq = np.linspace(0,500,751) | |
plt.plot(freq,abs(X)[:751],'r') | |
plt.xlabel("Frequency Hz") | |
plt.ylabel("Magnitude") | |
plt.title("Frequency domain signal representation") | |
plt.grid(True) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment