Skip to content

Instantly share code, notes, and snippets.

@leouieda
Created February 17, 2014 01:33
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save leouieda/9043213 to your computer and use it in GitHub Desktop.
Save leouieda/9043213 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()
@mrahimygk
Copy link

It throws:

IndexError: too many indices for array

in line 16:

plt.fill_between(times, data[:,0], data[:,1], color='k')

@yashLadha
Copy link

yashLadha commented Aug 22, 2017

This is because the above code is for dual channel wav file not for a single channel.

Code for this is as follows:

plt.fill_between(times, data)

@andrewphillipdoss
Copy link

I'm getting:

OverflowError: Allocated too many blocks

@amrapali2801
Copy link

how do i represent single channel with two different colors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment