Using Python, I am trying to plot the signal amplitude of a wav file, however I am getting the following error "ValueError: x and y must have same first dimension". Here is my code:
import wave
import matplotlib.pyplot as plt
import numpy as np
wav_obj = wave.open("loop.wav", "rb")
sample_freq = wav_obj.getframerate()
n_samples = wav_obj.getnframes()
t_audio = n_samples/sample_freq
n_channels = wav_obj.getnchannels()
signal_wave = wav_obj.readframes(n_samples)
signal_array = np.frombuffer(signal_wave, dtype=np.int16)
l_channel = signal_array[0::2]
r_channel = signal_array[1::2]
times = np.linspace(0, t_audio, num=n_samples)
plt.figure(figsize=(15, 5))
plt.plot(times, l_channel)
plt.title('Left Channel')
plt.ylabel("Signal Value")
plt.xlabel("Time in seconds")
plt.xlim(0, t_audio)
plt.show
I know that the shape of my signal_array should be equal to (n_samples * n_channels), but that is not the case and I don't know why. Right now the shape of signal_array is 1076340, and (n_samples * n_channels) is 717560.
I tried using a different wav file and I got the same error.
UPDATE: I have some more insight, my wav file is stereo, so it has 2 channels. The "signal_array" shape is actually (n_samples * 3) which is because the sample width of the wav file is 3. Therefore the shape of my "l_channel" is actually (times * 1.5).
So my question now is, how do I take into account that my sample width is 3? What should I do to my arrays so that they end up being equal to the shape of my "times" array?