简体   繁体   中英

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

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. Eg for the left channel:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM