Variations in matplotlib and audacity for plotting multichannel wav files

133 Views Asked by At

I was trying to plot time vs amplitude for all the channels of a wav file, using the code below. However I see that the matplotlib is plotting a non-existent sine wav on one of my channels. Audacity seems to be plotting the right thing, what am I missing?

import wave
import numpy as np
import matplotlib.pyplot as plt

f = wave.open('captured_Mic1_22_09_20_1738.wav')
sampleRate = f.getframerate()
totalFrames = f.getnframes()

print(f.getnchannels())

data = f.readframes(-1) # -1 is read all frames, can also use totalFrames
# print(data)  # Data in Hex

dataInt = np.frombuffer(data, np.int16)
print("All Samples -> {}".format(dataInt))

dataInt.shape = -1,2
print("L and R in 2 columns \n {}" .format(dataInt))

dataInt = dataInt.T
print("L and R in two separate rows \n {}" .format(dataInt))

print("total duration of file -> {}" .format(totalFrames/float(sampleRate)))
duration = 1/float(sampleRate)
print(duration)

timeSeq = np.arange(0, totalFrames/float(sampleRate), duration)

# plt.plot(timeSeq, dataInt[0])
# plt.show()

plt.plot(timeSeq, dataInt[1])
plt.show()

matplotlib vs audacity plots Code Credits

1

There are 1 best solutions below

0
On

I printed the entire contents of the dataInt buffer to see if there's actually any data using the 2 lines below.

np.set_printoptions(threshold=np.inf)
print(dataInt[1])

The amplitude is very small and hence looked like there was no data on audacity. Here's a screenshot with a lot of zoom on audacity.

enter image description here