在python中使用低通滤波器执行音频信号处理的上采样时出错

问题描述

我想对在样本之间引入零的音频信号进行上采样,以创建一个更长的信号,其因子为 N=2 。 采样率为 44100Hz,时间 = 5sec

fs = 44100

def butter_lowpass(cutoff,fs,order=5):
    nyq = 2 * fs
    normal_cutoff = cuto[enter image description here][1]ff / nyq
    b,a = butter(order,normal_cutoff,btype='low',analog=False)
    return b,a


def butter_lowpass_filter(data,cutoff,order=5):
    b,a = butter_lowpass(cutoff,order=order)
    y = lfilter(b,a,data)
    return y


# Filter requirements.
order = 6
fs = 44100  # sample rate,Hz
cutoff = 8000  # desired cutoff frequency of the filter,Hz

y = butter_lowpass_filter(data,order)
# Get the filter coefficients so we can check its frequency response.
b,order)

plt.figure()
plt.subplot(211)
plt.plot(signal,color='green')
plt.grid(color='green',linestyle='--',linewidth=0.5)
plt.title('original signal')

plt.subplot(212)
plt.plot(y,color='red')
plt.title('Filtered')
plt.grid(color='green',linewidth=0.5)
plt.show(block=True)

我正在使用以下模块,从开始录制到停止录制和打开 .wav 文件的录制。

import numpy as np
import matplotlib.pyplot as plt
import pyaudio  # Soundcard audio I/O access library
import wave  # Python 3 module for reading / writing simple .wav files
from scipy.signal import butter,lfilter
from scipy.io import wavfile

FORMAT = pyaudio.paInt16  # data type formate
CHANNELS = 2  # Adjust to your number of channels
RATE = 44100  # Sample Rate

CHUNK = 512  # Block Size
RECORD_SECONDS = 5  # Record time
WAVE_OUTPUT_FILENAME = "file3.wav"

# Startup pyaudio instance
audio = pyaudio.PyAudio()

# start Recording
stream = audio.open(format=FORMAT,channels=CHANNELS,rate=RATE,input=True,frames_per_buffer=CHUNK)
print("recording...")
frames = []

# print(int(RATE / CHUNK * RECORD_SECONDS))
print()
# Record for RECORD_SECONDS
for i in range(0,int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)
print("finished recording")

# Stop Recording
stream.stop_stream()
stream.close()
audio.terminate()

waveFile = wave.open(WAVE_OUTPUT_FILENAME,'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
# Finished Recording Procedure


#    opening Wav file for Plotting 

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

# Extract Raw Audio from Wav File
signal = spf.readframes(-1)
signal = np.frombuffer(signal,dtype=np.int16)

我知道对于上采样,在将其传递给低通信号后,我需要 Nyquist = 2Fs。在设计低通滤波器和更新采样率时,我的结果率仍然下降到 22100Hz。 Morover 我无法计算出截止频率但在这个例子中我提到了 8000Hz。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)