Hướng dẫn plot wav file python - cốt truyện tập tin wav python

Bạn có thể gọi Wave lib để đọc một tệp âm thanh.

Để vẽ biểu đồ dạng sóng, hãy sử dụng hàm "sơ đồ" từ matplotlib

import matplotlib.pyplot as plt
import numpy as np
import wave
import sys


spf = wave.open("wavfile.wav", "r")

# Extract Raw Audio from Wav File
signal = spf.readframes(-1)
signal = np.fromstring(signal, "Int16")


# If Stereo
if spf.getnchannels() == 2:
    print("Just mono files")
    sys.exit(0)

plt.figure(1)
plt.title("Signal Wave...")
plt.plot(signal)
plt.show()

Bạn sẽ có một cái gì đó như:

Hướng dẫn plot wav file python - cốt truyện tập tin wav python

Để vẽ trục x trong vài giây, bạn cần lấy tốc độ khung hình và chia theo kích thước tín hiệu của mình, bạn có thể sử dụng hàm linspace từ numpy để tạo vectơ thời gian cách nhau tuyến tính với kích thước của tệp âm thanh và cuối cùng bạn có thể sử dụng lại lô như

pip install numpy
0

import matplotlib.pyplot as plt
import numpy as np
import wave
import sys


spf = wave.open("Animal_cut.wav", "r")

# Extract Raw Audio from Wav File
signal = spf.readframes(-1)
signal = np.fromstring(signal, "Int16")
fs = spf.getframerate()

# If Stereo
if spf.getnchannels() == 2:
    print("Just mono files")
    sys.exit(0)


Time = np.linspace(0, len(signal) / fs, num=len(signal))

plt.figure(1)
plt.title("Signal Wave...")
plt.plot(Time, signal)
plt.show()

Trục x nếu trục x trong vài giây:

Hướng dẫn plot wav file python - cốt truyện tập tin wav python


To plot a .wav file using matplotlib, we can take following the steps −

  • To read a .wav file, we can use the read() method..wav file, we can use the read() method.

  • After reading the .wav file, we will get a tuple. At the 0 th index, rate would be there and at the 1st index, array sample data..wav file, we will get a tuple. At the 0 th index, rate would be there and at the 1st index, array sample data.

  • Use the plot() method to plot the .wav file.plot() method to plot the .wav file.

  • Set y and x labels using ylabel and xlabel with “Amplitude” and “Time” label, respectively.ylabel and xlabel with “Amplitude” and “Time” label, respectively.

  • To display the figure, use the show() method.show() method.

Example

from scipy.io.wavfile import read
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
input_data = read("my_audio.wav")
audio = input_data[1]
plt.plot(audio[0:1024])
plt.ylabel("Amplitude")
plt.xlabel("Time")
plt.show()

Output

Hướng dẫn plot wav file python - cốt truyện tập tin wav python

Hướng dẫn plot wav file python - cốt truyện tập tin wav python

Updated on 09-Apr-2021 12:31:42

  • Related Questions & Answers
  • Plot data from a .txt file using matplotlib
  • How to convert a .wav file to a spectrogram in Python3?
  • Playing a WAV file on iOS Safari
  • How to plot a very simple bar chart (Python, Matplotlib) using input *.txt file?
  • How to get a Gantt plot using matplotlib?
  • How to plot collections.Counter histogram using Matplotlib?
  • Make a multiline plot from .CSV file in matplotlib
  • Plot a bar using matplotlib using a dictionary
  • Plot data from CSV file with Matplotlib
  • How to plot a kernel density plot of dates in Pandas using Matplotlib?
  • How to a plot stem plot in Matplotlib Python?
  • How to plot a rectangle on a datetime axis using Matplotlib?
  • How to plot vectors in Python using Matplotlib?
  • How to plot 3D graphs using Python Matplotlib?
  • How to plot MFCC in Python using Matplotlib?

Xem thảo luận

Nội dung chính ShowShow

  • Bàn luận
  • 2. Numpy: Numpy được cài đặt tự động được cài đặt với matplotlib. Mặc dù, nếu bạn phải đối mặt với bất kỳ lỗi nhập nào, hãy sử dụng lệnh bên dưới để cài đặt Numpy
  • Làm thế nào để bạn vẽ âm thanh trong Python?
  • Làm cách nào để hình dung một tệp wav trong Python?
  • Làm thế nào để bạn đồ thị dữ liệu trong Python?
  • Python có thể tạo ra đồ thị không?

Bạn có thể tạo ra nhiều loại lô và biểu đồ khác nhau với matplotlib.Nó cũng tích hợp tốt với các thư viện khoa học dữ liệu và toán học khác như Numpy và Pandas.Bạn cũng sẽ thấy rằng Matplotlib hoạt động với hầu hết các bộ công cụ GUI của Python, như Tkinter, Wxpython và PyQT.. It also integrates well with other data science and math libraries like NumPy and pandas. You will also find that Matplotlib works with most of Python's GUI toolkits, such as Tkinter, wxPython and PyQt.

Xem thảo luận

  • Nội dung chính Show
  • Bàn luận
  • Xem thảo luận

    Bạn có thể tạo ra nhiều loại lô và biểu đồ khác nhau với matplotlib.Nó cũng tích hợp tốt với các thư viện khoa học dữ liệu và toán học khác như Numpy và Pandas.Bạn cũng sẽ thấy rằng Matplotlib hoạt động với hầu hết các bộ công cụ GUI của Python, như Tkinter, Wxpython và PyQT.. It also integrates well with other data science and math libraries like NumPy and pandas. You will also find that Matplotlib works with most of Python's GUI toolkits, such as Tkinter, wxPython and PyQt.

    Xem thảo luận

    Nội dung chính Show

    Bàn luận

    2. Numpy: Numpy được cài đặt tự động được cài đặt với matplotlib. Mặc dù, nếu bạn phải đối mặt với bất kỳ lỗi nhập nào, hãy sử dụng lệnh bên dưới để cài đặt Numpy1. Matplotlib: Install Matplotlib using the below command:

    pip install matplotlib

    Làm thế nào để bạn vẽ âm thanh trong Python?Numpy: Numpy gets installed automatically installed with Matplotlib. Although, if you face any import error, use the below command to install Numpy

    pip install numpy

    Làm cách nào để hình dung một tệp wav trong Python? If you are on Linux like me, then you might need to use pip3 instead of pip or you might create a virtual environment and run the above command.

    2. Numpy: Numpy được cài đặt tự động được cài đặt với matplotlib. Mặc dù, nếu bạn phải đối mặt với bất kỳ lỗi nhập nào, hãy sử dụng lệnh bên dưới để cài đặt Numpy

    • Làm thế nào để bạn vẽ âm thanh trong Python?
    • Làm cách nào để hình dung một tệp wav trong Python?
    • Làm thế nào để bạn đồ thị dữ liệu trong Python?
    • Cải thiện bài viết
    • Lưu bài viết
    • Đọc
    • Trong bài viết này, chúng tôi sẽ khám phá cách trực quan hóa sóng âm thanh bằng cách sử dụng Python và Matplotlib.1. Matplotlib: Install Matplotlib using the below command:

    Các mô -đun cần thiếtNumpy: Numpy gets installed automatically installed with Matplotlib. Although, if you face any import error, use the below command to install Numpy
     

    Python3

    & nbsp; 1. Matplotlib: Cài đặt matplotlib bằng lệnh bên dưới: If you are on Linux like me, then you might need to use pip3 instead of pip or you might create a virtual environment and run the above command.

    Lưu ý: Nếu bạn đang ở trên Linux như tôi, thì bạn có thể cần sử dụng PIP3 thay vì PIP hoặc bạn có thể tạo môi trường ảo và chạy lệnh trên.

    Cách tiếp cận

    Nhập mô -đun matplotlib, numpy, wave và sys.

    pip install numpy
    1

    Mở tệp âm thanh bằng phương thức wave.open ().

    Đọc tất cả các khung của sóng âm thanh đã mở bằng hàm readframes ().

    Lưu trữ tốc độ khung hình trong một biến bằng cách sử dụng hàm getFramrate ().

    Output:

    Cuối cùng, vẽ trục x trong giây bằng tốc độ khung hình.

    Sử dụng hàm matplotlib.figure () để vẽ đồ thị dẫn xuất wav file. If you have some other file type then you can use ffmpeg to convert it to wav file. If you want then feel free to download the audio file we will be using. You can download it using this link, but do try out other files too.
    To run the code, you need to pass the path of the audio file in the command line. To do that type the following in your terminal:
    import matplotlib.pyplot as plt
    import numpy as np
    import wave
    import sys
    
    
    spf = wave.open("Animal_cut.wav", "r")
    
    # Extract Raw Audio from Wav File
    signal = spf.readframes(-1)
    signal = np.fromstring(signal, "Int16")
    fs = spf.getframerate()
    
    # If Stereo
    if spf.getnchannels() == 2:
        print("Just mono files")
        sys.exit(0)
    
    
    Time = np.linspace(0, len(signal) / fs, num=len(signal))
    
    plt.figure(1)
    plt.title("Signal Wave...")
    plt.plot(Time, signal)
    plt.show()
    
    5
    pip install numpy
    5
    pip install numpy
    41
    pip install numpy
    7
    pip install numpy
    43
    import matplotlib.pyplot as plt
    import numpy as np
    import wave
    import sys
    
    
    spf = wave.open("Animal_cut.wav", "r")
    
    # Extract Raw Audio from Wav File
    signal = spf.readframes(-1)
    signal = np.fromstring(signal, "Int16")
    fs = spf.getframerate()
    
    # If Stereo
    if spf.getnchannels() == 2:
        print("Just mono files")
        sys.exit(0)
    
    
    Time = np.linspace(0, len(signal) / fs, num=len(signal))
    
    plt.figure(1)
    plt.title("Signal Wave...")
    plt.plot(Time, signal)
    plt.show()
    
    56
    pip install numpy
    45soundwave.py and the name of the audio file is sample_audio.wav. You need to change these according to your system. Now, a new window should have popped up and should be seeing a sound wave plot. If you have used my audio, then your plot should look something like this.

    Sử dụng nhãn theo yêu cầu.

    Dưới đây là việc thực hiện. & NBSP;

    import matplotlib.pyplot as plt
    import numpy as np
    import wave
    import sys
    
    
    spf = wave.open("wavfile.wav", "r")
    
    # Extract Raw Audio from Wav File
    signal = spf.readframes(-1)
    signal = np.fromstring(signal, "Int16")
    
    
    # If Stereo
    if spf.getnchannels() == 2:
        print("Just mono files")
        sys.exit(0)
    
    plt.figure(1)
    plt.title("Signal Wave...")
    plt.plot(signal)
    plt.show()
    

    pip install numpy
    1
    pip install numpy
    2

    pip install numpy
    1
    pip install numpy
    4

    import matplotlib.pyplot as plt
    import numpy as np
    import wave
    import sys
    
    
    spf = wave.open("Animal_cut.wav", "r")
    
    # Extract Raw Audio from Wav File
    signal = spf.readframes(-1)
    signal = np.fromstring(signal, "Int16")
    fs = spf.getframerate()
    
    # If Stereo
    if spf.getnchannels() == 2:
        print("Just mono files")
        sys.exit(0)
    
    
    Time = np.linspace(0, len(signal) / fs, num=len(signal))
    
    plt.figure(1)
    plt.title("Signal Wave...")
    plt.plot(Time, signal)
    plt.show()
    

    pip install numpy
    0
    pip install numpy
    1
    pip install numpy
    2
    pip install numpy
    3
    pip install numpy
    4
    pip install numpy
    5
    pip install numpy
    6
    pip install numpy
    7
    pip install numpy
    8
    pip install numpy
    9
    import matplotlib.pyplot as plt
    import numpy as np
    import wave
    import sys
    
    
    spf = wave.open("Animal_cut.wav", "r")
    
    # Extract Raw Audio from Wav File
    signal = spf.readframes(-1)
    signal = np.fromstring(signal, "Int16")
    fs = spf.getframerate()
    
    # If Stereo
    if spf.getnchannels() == 2:
        print("Just mono files")
        sys.exit(0)
    
    
    Time = np.linspace(0, len(signal) / fs, num=len(signal))
    
    plt.figure(1)
    plt.title("Signal Wave...")
    plt.plot(Time, signal)
    plt.show()
    
    50
    pip install numpy
    5
    import matplotlib.pyplot as plt
    import numpy as np
    import wave
    import sys
    
    
    spf = wave.open("Animal_cut.wav", "r")
    
    # Extract Raw Audio from Wav File
    signal = spf.readframes(-1)
    signal = np.fromstring(signal, "Int16")
    fs = spf.getframerate()
    
    # If Stereo
    if spf.getnchannels() == 2:
        print("Just mono files")
        sys.exit(0)
    
    
    Time = np.linspace(0, len(signal) / fs, num=len(signal))
    
    plt.figure(1)
    plt.title("Signal Wave...")
    plt.plot(Time, signal)
    plt.show()
    
    522
    pip install numpy
    7
    import matplotlib.pyplot as plt
    import numpy as np
    import wave
    import sys
    
    
    spf = wave.open("Animal_cut.wav", "r")
    
    # Extract Raw Audio from Wav File
    signal = spf.readframes(-1)
    signal = np.fromstring(signal, "Int16")
    fs = spf.getframerate()
    
    # If Stereo
    if spf.getnchannels() == 2:
        print("Just mono files")
        sys.exit(0)
    
    
    Time = np.linspace(0, len(signal) / fs, num=len(signal))
    
    plt.figure(1)
    plt.title("Signal Wave...")
    plt.plot(Time, signal)
    plt.show()
    
    54
    import matplotlib.pyplot as plt
    import numpy as np
    import wave
    import sys
    
    
    spf = wave.open("Animal_cut.wav", "r")
    
    # Extract Raw Audio from Wav File
    signal = spf.readframes(-1)
    signal = np.fromstring(signal, "Int16")
    fs = spf.getframerate()
    
    # If Stereo
    if spf.getnchannels() == 2:
        print("Just mono files")
        sys.exit(0)
    
    
    Time = np.linspace(0, len(signal) / fs, num=len(signal))
    
    plt.figure(1)
    plt.title("Signal Wave...")
    plt.plot(Time, signal)
    plt.show()
    
    55
    import matplotlib.pyplot as plt
    import numpy as np
    import wave
    import sys
    
    
    spf = wave.open("Animal_cut.wav", "r")
    
    # Extract Raw Audio from Wav File
    signal = spf.readframes(-1)
    signal = np.fromstring(signal, "Int16")
    fs = spf.getframerate()
    
    # If Stereo
    if spf.getnchannels() == 2:
        print("Just mono files")
        sys.exit(0)
    
    
    Time = np.linspace(0, len(signal) / fs, num=len(signal))
    
    plt.figure(1)
    plt.title("Signal Wave...")
    plt.plot(Time, signal)
    plt.show()
    
    56
    import matplotlib.pyplot as plt
    import numpy as np
    import wave
    import sys
    
    
    spf = wave.open("Animal_cut.wav", "r")
    
    # Extract Raw Audio from Wav File
    signal = spf.readframes(-1)
    signal = np.fromstring(signal, "Int16")
    fs = spf.getframerate()
    
    # If Stereo
    if spf.getnchannels() == 2:
        print("Just mono files")
        sys.exit(0)
    
    
    Time = np.linspace(0, len(signal) / fs, num=len(signal))
    
    plt.figure(1)
    plt.title("Signal Wave...")
    plt.plot(Time, signal)
    plt.show()
    
    57

    Làm thế nào để bạn vẽ âm thanh trong Python?

    Approach.. .

    Nhập mô -đun matplotlib, numpy, wave và sys ..

    Mở tệp âm thanh bằng sóng. ....

    Đọc tất cả các khung của sóng âm thanh đã mở bằng hàm readframes () ..

    Lưu trữ tốc độ khung hình trong một biến bằng hàm getFramrate () ..

    Cuối cùng, hãy vẽ trục x trong vài giây bằng tốc độ khung hình ..

    Làm cách nào để hình dung một tệp wav trong Python?

    Matplotlib với Python...

    Để đọc a. Tệp WAV, chúng ta có thể sử dụng phương thức Read () ..

    Sau khi đọc. Tệp WAV, chúng tôi sẽ nhận được một tuple. ....

    Sử dụng phương thức cốt truyện () để vẽ biểu đồ. Tệp wav ..

    Đặt các nhãn Y và X bằng cách sử dụng nhãn YLabel và XLabel với nhãn Biên độ và thời gian, tương ứng ..

    Để hiển thị hình, hãy sử dụng phương thức show () ..

    Làm thế nào để bạn đồ thị dữ liệu trong Python?

    Các bước sau đây đã được theo dõi:...

    Xác định trục x và giá trị trục y tương ứng như danh sách ..

    Vẽ chúng trên canvas bằng hàm .plot () ..

    Đặt tên cho trục x và trục y bằng các hàm .xlabel () và .ylabel () ..

    Đưa ra một tiêu đề cho cốt truyện của bạn bằng hàm .title () ..

    Cuối cùng, để xem cốt truyện của bạn, chúng tôi sử dụng chức năng .show () ..

    Python có thể tạo ra đồ thị không?

    Bạn có thể tạo ra nhiều loại lô và biểu đồ khác nhau với matplotlib.Nó cũng tích hợp tốt với các thư viện khoa học dữ liệu và toán học khác như Numpy và Pandas.Bạn cũng sẽ thấy rằng Matplotlib hoạt động với hầu hết các bộ công cụ GUI của Python, như Tkinter, Wxpython và PyQT.. It also integrates well with other data science and math libraries like NumPy and pandas. You will also find that Matplotlib works with most of Python's GUI toolkits, such as Tkinter, wxPython and PyQt.. It also integrates well with other data science and math libraries like NumPy and pandas. You will also find that Matplotlib works with most of Python's GUI toolkits, such as Tkinter, wxPython and PyQt.