Show Sidebar Hide Sidebar

Fast Fourier Transform in matplotlib

An example of FFT audio analysis in matplotlib and the fft function.

import matplotlib.pyplot as plt
import plotly.plotly as py
import numpy as np
# Learn about API authentication here:
# Find your api_key here:

Fs = 150.0;  # sampling rate
Ts = 1.0/Fs; # sampling interval
t = np.arange(0,1,Ts) # time vector

ff = 5;   # frequency of the signal
y = np.sin(2*np.pi*ff*t)

n = len(y) # length of the signal
k = np.arange(n)
T = n/Fs
frq = k/T # two sides frequency range
frq = frq[range(n/2)] # one side frequency range

Y = np.fft.fft(y)/n # fft computing and normalization
Y = Y[range(n/2)]

fig, ax = plt.subplots(2, 1)
ax[1].plot(frq,abs(Y),'r') # plotting the spectrum
ax[1].set_xlabel('Freq (Hz)')

plot_url = py.plot_mpl(fig, filename='mpl-basic-fft')
Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.