frequency spectrum python code example

Example: frequency spectrum signal python

#A short example to extract the frequency spectrum of a signal
import numpy as np
import matplotlib.pyplot as plot
samplingFrequency  = 100
t=np.arange(0,5,1/samplingFrequency)
signal=np.sin(2*np.pi*t)
fourierTransform = np.fft.fft(signal)/len(signal)
fourierTransform = fourierTransform[range(int(len(signal)/2))]
tpCount     = len(signal)
values      = np.arange(int(tpCount/2))
timePeriod  = tpCount/samplingFrequency
frequencies = values/timePeriod
plt.plot(frequencies, abs(fourierTransform))