Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
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:
pip install matplotlib
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
pip install numpy
& 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.
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ư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.
- 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 [].
- 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
Python3
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
pip install numpy0
pip install numpy1
pip install numpy2
pip install numpy3
pip install numpy4
pip install numpy5
pip install numpy6
pip install numpy7
pip install numpy8
pip install numpy9
python soundwave.py sample_audio.wav0
pip install numpy5
python soundwave.py sample_audio.wav22
pip install numpy7
python soundwave.py sample_audio.wav4
python soundwave.py sample_audio.wav5
python soundwave.py sample_audio.wav6
python soundwave.py sample_audio.wav7
Các
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[]
3import 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[]
4import 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[]
5pip install numpy5
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[]
6pip install numpy7
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[]
8pip install numpy5
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[]
0pip install numpy7
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[]
2pip install numpy5
python soundwave.py sample_audio.wav7
pip install numpy5
import
9python soundwave.py sample_audio.wav6
python soundwave.py sample_audio.wav7
pip install numpy5
matplotlib.pyplot as plt
3matplotlib.pyplot as plt
4python soundwave.py sample_audio.wav7
pip install numpy5
matplotlib.pyplot as plt
7matplotlib.pyplot as plt
8python soundwave.py sample_audio.wav7
pip install numpy5
import
1pip install numpy5
import
3import 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[]
3import 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[]
7import 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[]
8import 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[]
9 import
0import 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[]
3import
22____17 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[]
7import
5pip install numpy5
numpy as np
7Output:
import
4 import
5
pip install numpy7
pip install numpy7
import
8import
9wav 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:
python soundwave.py sample_audio.wav
pip install numpy5
numpy as np
1pip install numpy7
numpy as np
3python soundwave.py sample_audio.wav6
numpy as np
5soundwave.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.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ư:
Để 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ư numpy as np
8
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: