Skip to content

Instantly share code, notes, and snippets.

@mick001
Last active August 29, 2015 09:40
Show Gist options
  • Save mick001/ce494f9605eae22900dd to your computer and use it in GitHub Desktop.
Save mick001/ce494f9605eae22900dd to your computer and use it in GitHub Desktop.
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