How to plot a wav file

前端 未结 8 1050
时光取名叫无心
时光取名叫无心 2020-12-12 10:51

I have just read a wav file with scipy and now I want to make the plot of the file using matplotlib, on the \"y scale\" I want to see the aplitude and over the \"x scale\" I

8条回答
  •  星月不相逢
    2020-12-12 11:22

    Here's a version that will also handle stereo inputs, based on the answer by @ederwander

    import matplotlib.pyplot as plt
    import numpy as np
    import wave
    
    file = 'test.wav'
    
    with wave.open(file,'r') as wav_file:
        #Extract Raw Audio from Wav File
        signal = wav_file.readframes(-1)
        signal = np.fromstring(signal, 'Int16')
    
        #Split the data into channels 
        channels = [[] for channel in range(wav_file.getnchannels())]
        for index, datum in enumerate(signal):
            channels[index%len(channels)].append(datum)
    
        #Get time from indices
        fs = wav_file.getframerate()
        Time=np.linspace(0, len(signal)/len(channels)/fs, num=len(signal)/len(channels))
    
        #Plot
        plt.figure(1)
        plt.title('Signal Wave...')
        for channel in channels:
            plt.plot(Time,channel)
        plt.show()
    

提交回复
热议问题