plot_wave.py 798 B

1234567891011121314151617181920212223242526272829
  1. # Visualize wav file by matplotlib
  2. # Load the required libraries:
  3. # * scipy
  4. # * numpy
  5. # * matplotlib
  6. from scipy.io import wavfile
  7. from matplotlib import pyplot as plt
  8. import sys
  9. import numpy as np
  10. if len(sys.argv) < 2:
  11. print("Plays a wave file.\n\nUsage: %s filename.wav" % sys.argv[0])
  12. sys.exit(-1)
  13. # Load the data and calculate the time of each sample
  14. samplerate, data = wavfile.read(sys.argv[1])
  15. times = np.arange(len(data))/float(samplerate)
  16. # Make the plot
  17. # You can tweak the figsize (width, height) in inches
  18. plt.figure(figsize=(30, 4))
  19. plt.fill_between(times, data, color='k')
  20. plt.xlim(times[0], times[-1])
  21. plt.xlabel('time (s)')
  22. plt.ylabel('amplitude')
  23. # You can set the format by changing the extension
  24. # like .pdf, .svg, .eps
  25. plt.savefig('plot.png', dpi=100)
  26. plt.show()