python - Python特定频率删除(陷波滤波器)?

标签 python audio filter signal-processing

#complie by python3 only_test.py

import pyaudio
import numpy as np
import wave
import time
import math
#from pydub import AudioSegment
#from pydub.playback import play
#from scipy.signal import iirfilter
from scipy import signal

RATE = 48000
CHUNK = 4096
WIDTH = 2
volume = 0.0
duration = 1.0
#SHORT_NORMALIZE = (1.0/32768.0)
#INPUT_BLOCK_TIME = 1
#INPUT_BLOCK_PER_BLOCK = int(RATE*INPUT_BLOCK_TIME)

while True:

    #use a blackman window
    window = np.blackman(CHUNK)

    #load audio stream
    p = pyaudio.PyAudio()

    player = p.open(format=pyaudio.paInt16,
                    channels=1,
                    rate=RATE,
                    output=True,
                    frames_per_buffer=CHUNK)

    stream = p.open(format=pyaudio.paInt16,
                    channels=1,
                    rate=RATE,
                    input=True,
                    frames_per_buffer=CHUNK)

    #errorcount = 0

    for i in range(int(20*RATE/CHUNK)):

        sound = stream.read(CHUNK)

        #imp_ff = signal.filtfilt(b,a,sound)

        #playback microphone sound 
        #player.write(np.fromstring(sound,dtype=np.int16),CHUNK)

        #generate samples with return frequency to array
        #samples= (np.sin(2*np.pi*np.arange(RATE*duration)*freq/RATE)).astype(np.int16)

        #inverse frequency samples
        #inverse_samples = -samples

        #return frequency sound stream      
        #player.write(np.fromstring((volume*inverse_samples)\
        ,dtype=np.int16),CHUNK)

        #unpack the data and times by hamming window
        indata = np.array(wave.struct.unpack("%dh"%(len(sound)/WIDTH),\
                                             sound))*window

        #take the fft and square each value
        fftData = abs(np.fft.rfft(indata))*2

        #ifftData = abs(np.fft.irfft(indata))*2

        #find the maxium
        which = fftData[1:].argmax() + 1

        #use quadratic interpolation around the max
        if which != len(fftData)-1:
            y0,y1,y2 = np.log(fftData[which-1:which+2:])
            x1 = (y2-y0)*.5 / (2*y1-y2-y0)

            #find the frequency and output it
            freq = (which+x1)*RATE/CHUNK
            print("the freq is %d hz." % (freq))

        else:
            freq = which*RATE/CHUNK
            print("the freq is %d hz." % (freq))

        #playback the mic sound
        player.write(np.fromstring(sound,dtype=np.int16),CHUNK)

        if freq < 65:
           freq = 0

        #generate samples, note conversion to array
        #samples = 
        (np.sin(2*np.pi*np.arange(RATE*duration)*freq/RATE)).astype(np.int16)

        #invert phase of samples
        #result_samples = samples 

        #playback the invert_mic sound
        #player.write(np.fromstring(result_samples,dtype=np.int16),CHUNK)

    stream.stop_stream()
    stream.close()
    p.terminate()

我们目前正在实时处理麦克风。
它旨在通过该频率获得频率,并通过陷波滤波器(带阻滤波器)消除输出频率的正弦波声音。
我不知道写什么代码来做陷波滤波器(带阻滤波器)。
您有任何代码或库可以帮助您吗?

最佳答案

由于您已经在使用scipy.signal,因此可以使用scipy.signal.iirnotch。也许您还想阅读有关IIR滤波器的一些背景知识,例如质量因数。

如下使用它:

b, a = signal.iirnotch( w0, Q )

w0是归一化的频率。

Q是表征陷波滤波器相对于其中心频率的-3 dB带宽的品质因数。

该函数返回IIR滤波器的分子b和分母a多项式。

例:
fs = 200.0  # Sample frequency (Hz)
f0 = 60.0  # Frequency to be removed from signal (Hz)
Q = 30.0  # Quality factor
w0 = f0 / (fs / 2 )  # Normalized Frequency
b, a = signal.iirnotch( w0, Q )
# Look at frequency response
w, h = signal.freqz( b, a )
freq = w * fs / ( 2 * np.pi )
plt.plot( freq, 20*np.log10( abs( h ) ) )

关于python - Python特定频率删除(陷波滤波器)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50247517/

相关文章:

python - 如何使用 beautifulsoup 和 selenium 在 Python Scraping 中识别类名或 id

java - 保持 JVM 运行

javascript - 使用play()时HTML Audio停止IOS音乐播放器

Magento (CE 1.3) - 过滤产品集合,其中库存数量为 0

python - 解释 sys.stdin 的 python3 与 python 行为

python - 在 Python 中从另一个文件导入变量

javascript - 在播放器上的浏览器上播放 wav 字节数据

audio - 在LabView中使用过滤器后如何保存wav文件?

shell - hbase shell 命令 - 扫描和过滤

filter - 陀螺仪、加速度计、磁力计和卡尔曼滤波器