from numpy.fft import fft
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import (MultipleLocator, AutoMinorLocator)
signalFrequency1 = 550
signalFrequency2 = 650
signalFrequency3 = 750
sampleFrequency = 20000
numberOfSamples = 2000
samplingInterval = 1 / sampleFrequency
endTime = numberOfSamples/sampleFrequency
timeAxis=np.arange(0, endTime, samplingInterval)
frequencyAxis = np.arange(0, sampleFrequency, 1/endTime)
signal1 = np.sin(2*np.pi*signalFrequency1*timeAxis)
signal1 = 10*signal1;
signal2 = np.sin(2*np.pi*signalFrequency2*timeAxis)
signal2 = 5*signal2;
signal3 = np.sin(2*np.pi*signalFrequency3*timeAxis)
signal3 = 2*signal3;
compositeSignal = signal1 + signal2 + signal3
transform = fft(compositeSignal)
fig1 = plt.figure(figsize=(15,4))
plt.plot(timeAxis, compositeSignal)
fig2, ax = plt.subplots(figsize=(15,3))
ax.bar(frequencyAxis[50:80], abs(transform)[50:80], width=2)
ax.xaxis.set_minor_locator(AutoMinorLocator())
ax.tick_params(which='both', width=2)
ax.tick_params(which='major', length=10)
ax.tick_params(which='minor', length=5, color='gray')
ax.xaxis.set_minor_locator(MultipleLocator(10))
ax.xaxis.set_major_locator(MultipleLocator(50))
plt.show()
plt.savefig('fft.png')
No comments:
Post a Comment