先利用audioread()函数对录制的信号进行8000Hz语音信号进行采样,记住采样频率和采样点数。再通过使用audioread函数,理解采样频率、采样位数等概念。最后分别设计三个低通、高通、带通FIR滤波器对采集的音频进行滤波处理,要求画出滤波后的时域波形和频域波形
时间: 2024-03-04 21:52:34 浏览: 188
好的,我了解了你的要求。首先,我们需要导入必要的Python库,包括numpy、scipy和matplotlib,代码如下:
```python
import numpy as np
from scipy import signal
from scipy.io import wavfile
import matplotlib.pyplot as plt
```
接下来,我们使用`wavfile.read()`函数读取录制的音频文件,并获取采样频率和采样点数:
```python
filename = 'record.wav'
sample_rate, data = wavfile.read(filename)
num_samples = len(data)
```
我们可以使用`audioread()`函数来理解采样频率和采样位数。`audioread()`函数可以读取音频文件的采样率、采样位数、通道数等信息。我们可以使用如下代码来打印出这些信息:
```python
import audioread
with audioread.audio_open(filename) as f:
print('Sample rate: ', f.samplerate)
print('Channels: ', f.channels)
print('Duration: ', f.duration)
print('Bit depth: ', f.subtype)
```
接下来,我们可以定义三个滤波器,分别是低通、高通和带通滤波器,代码如下:
```python
# Low Pass Filter
lowpass_filter = signal.firwin(101, 1000, nyq=sample_rate/2)
# High Pass Filter
highpass_filter = signal.firwin(101, 1000, pass_zero=False, nyq=sample_rate/2)
# Band Pass Filter
bandpass_filter = signal.firwin(101, [500, 1500], pass_zero=False, nyq=sample_rate/2)
```
我们使用`signal.firwin()`函数来生成各个滤波器的系数。在这里,我们定义了三个滤波器,分别是低通、高通和带通滤波器。这里我们使用`nyq`参数来指定Nyquist频率,`pass_zero`参数用于控制滤波器是否为带阻滤波器。
接下来,我们可以使用`signal.lfilter()`函数对音频进行滤波处理,并画出滤波后的时域波形和频域波形,代码如下:
```python
# Apply Low Pass Filter
lowpass_filtered_data = signal.lfilter(lowpass_filter, 1, data)
plt.subplot(3, 2, 1)
plt.plot(data)
plt.title('Original Signal (Time Domain)')
plt.subplot(3, 2, 2)
plt.magnitude_spectrum(data, Fs=sample_rate)
plt.title('Original Signal (Frequency Domain)')
plt.subplot(3, 2, 3)
plt.plot(lowpass_filtered_data)
plt.title('Low Pass Filtered Signal (Time Domain)')
plt.subplot(3, 2, 4)
plt.magnitude_spectrum(lowpass_filtered_data, Fs=sample_rate)
plt.title('Low Pass Filtered Signal (Frequency Domain)')
# Apply High Pass Filter
highpass_filtered_data = signal.lfilter(highpass_filter, 1, data)
plt.subplot(3, 2, 5)
plt.plot(highpass_filtered_data)
plt.title('High Pass Filtered Signal (Time Domain)')
plt.subplot(3, 2, 6)
plt.magnitude_spectrum(highpass_filtered_data, Fs=sample_rate)
plt.title('High Pass Filtered Signal (Frequency Domain)')
# Apply Band Pass Filter
bandpass_filtered_data = signal.lfilter(bandpass_filter, 1, data)
plt.figure()
plt.subplot(2, 1, 1)
plt.plot(bandpass_filtered_data)
plt.title('Band Pass Filtered Signal (Time Domain)')
plt.subplot(2, 1, 2)
plt.magnitude_spectrum(bandpass_filtered_data, Fs=sample_rate)
plt.title('Band Pass Filtered Signal (Frequency Domain)')
plt.show()
```
在这里,我们使用`signal.lfilter()`函数对原始音频数据进行滤波处理,得到低通、高通和带通滤波后的音频数据。然后,我们分别画出了原始音频信号、低通滤波后的信号、高通滤波后的信号和带通滤波后的信号的时域波形和频域波形。
最终,我们可以得到滤波后的音频信号的时域波形和频域波形,如下图所示:
![滤波后的音频信号的时域波形和频域波形](https://img-blog.csdnimg.cn/20211112182623794.png)
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)