How to plot a wav file

前端 未结 8 1055
时光取名叫无心
时光取名叫无心 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:23

    Here is the code to draw a waveform and a frequency spectrum of a wavefile

    import wave
    import numpy as np
    import matplotlib.pyplot as plt
    
    signal_wave = wave.open('voice.wav', 'r')
    sample_rate = 16000
    sig = np.frombuffer(signal_wave.readframes(sample_rate), dtype=np.int16)
    

    For the whole segment of the wave file

    sig = sig[:]
    

    For partial segment of the wave file

    sig = sig[25000:32000]
    

    Separating stereo channels

    left, right = data[0::2], data[1::2]
    

    Plot the waveform (plot_a) and the frequency spectrum (plot_b)

    plt.figure(1)
    
    plot_a = plt.subplot(211)
    plot_a.plot(sig)
    plot_a.set_xlabel('sample rate * time')
    plot_a.set_ylabel('energy')
    
    plot_b = plt.subplot(212)
    plot_b.specgram(sig, NFFT=1024, Fs=sample_rate, noverlap=900)
    plot_b.set_xlabel('Time')
    plot_b.set_ylabel('Frequency')
    
    plt.show()
    

提交回复
热议问题