Harry
Harry

Reputation: 21

Plot Spectrogram of a wav audio file

I want to plot Spectrogram of 30s of a audio file in wav. But I encountered error while doing so in python. How can I achieve my goal?

    import scipy
    import matplotlib.pyplot as plt
    import scipy.io.wavfile
    sample_rate, X = scipy.io.wavfile.read('595.wav')
    print (sample_rate, X.shape )
    plt.specgram(X, Fs=sample_rate, xextent=(0,30))

And error

    ValueError: only 1-dimensional arrays can be used

Upvotes: 2

Views: 4513

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339112

The error is pretty clear: ValueError: only 1-dimensional arrays can be used. In your case X is not 1-dimensional. You would find out by printing X.shape.

While I can't be certain without a complete example here, the best guess would be that you're having a stereo wav file, which has 2 channels. So you need to select if you want to plot the spectrogram for the left or the right channel. E.g. for the left channel:

plt.specgram(X[:,0], Fs=sample_rate, xextent=(0,30))

Upvotes: 7

Related Questions