Skip to content

Instantly share code, notes, and snippets.

@tjoen
Forked from leouieda/plot_wav.py
Created March 2, 2018 16:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tjoen/321528e0e0e441a9a16d4ff1806b00f8 to your computer and use it in GitHub Desktop.
Save tjoen/321528e0e0e441a9a16d4ff1806b00f8 to your computer and use it in GitHub Desktop.
Python code to plot a .wav file
# Load the required libraries:
# * scipy
# * numpy
# * matplotlib
from scipy.io import wavfile
from matplotlib import pyplot as plt
import numpy as np
# Load the data and calculate the time of each sample
samplerate, data = wavfile.read('Clapping.wav')
times = np.arange(len(data))/float(samplerate)
# Make the plot
# You can tweak the figsize (width, height) in inches
plt.figure(figsize=(30, 4))
plt.fill_between(times, data[:,0], data[:,1], color='k')
plt.xlim(times[0], times[-1])
plt.xlabel('time (s)')
plt.ylabel('amplitude')
# You can set the format by changing the extension
# like .pdf, .svg, .eps
plt.savefig('plot.png', dpi=100)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment