1
votes

I am trying to read a wav file in Python. I 25MB file which when I read using wavfile.read and I obtain the sample_rate and data they are of the following dimensions:

files[:1][0] is the .wav file.

sample_rate, samples = wavfile.read(files[:1][0])
print(sample_rate)
print(len(samples))
print(samples)

48000
14466512
[157 150 141 ...,  33  37  42]

Now I want to output it's wave and spectrogram and I'm using the following code to do that:

freqs, times, spectrogram = log_specgram(samples, sample_rate)

fig = plt.figure(figsize=(14,8))
ax1 = fig.add_subplot(211)
ax1.set_title('Raw wave of ' + files[:1][0])
ax1.set_ylabel('Amplitude')
ax1.plot(np.linspace(0, sample_rate/len(samples), sample_rate), samples)

ax2 = fig.add_subplot(212)
ax2.imshow(spectrogram.T, aspect='auto', origin='lower', 
           extent=[times.min(), times.max(), freqs.min(), freqs.max()])
ax2.set_yticks(freqs[::16])
ax2.set_xticks(times[::16])
ax2.set_title('Spectrogram of ' + files[:1][0])
ax2.set_ylabel('Freqs in Hz')
ax2.set_xlabel('Seconds')

However, I get the following error:

ValueError: x and y must have same first dimension, but have shapes (48000,) and (14466512,)

Any ideas how to go around this?

1

1 Answers

2
votes

Replace

ax1.plot(np.linspace(0, sample_rate/len(samples), sample_rate), samples)

with

ax1.plot(np.linspace(0, len(samples)/sample_rate, len(samples)), samples)

The sample rate is how many samples per second. If you are trying to plot all of your samples, the number of x values should be the number of samples, not the number of samples per second.

For the second change I am assuming that you are trying to make your x-axis in seconds. number of samples/number of samples per second will give you the number of seconds, not the other way around.