Python声音的数字化
时间: 2023-12-05 22:39:43 浏览: 135
python+pygame+可视化简易计算器
Python中可以使用wave模块来读取和处理声音文件,将声音转化为数字信号。下面是Python声音数字化的步骤:
1.导入wave模块和numpy模块
```python
import wave
import numpy as np
```
2.打开声音文件
```python
f = wave.open(r"lantian.wav", "rb")
```
3.获取声音参数
```python
params = f.getparams()
nchannels, sampwidth, framerate, nframes = params[:4]
```
4.读取声音数据
```python
str_data = f.readframes(nframes)
```
5.将声音数据转化为数组
```python
wave_data = np.fromstring(str_data, dtype=np.short)
```
6.将数组归一化
```python
wave_data = wave_data*1.0/(max(abs(wave_data)))
```
7.绘制声音波形图
```python
time = np.arange(0, nframes)*(1.0/framerate)
plt.plot(time, wave_data)
plt.xlabel("Time(s)")
plt.ylabel("Amplitude")
plt.title("Single channel wavedata")
plt.grid('on')
plt.show()
```
阅读全文