逆傅里叶变换如何进入有源噪声消除过程?

时间:2018-04-19 10:07:16

标签: python signal-processing fft ifft

通过声卡将声音输入麦克风,并实时读取声音,并通过FFT频率分析读取频率。我希望通过这个生成反向频率,但我不知道如何。

import pyaudio
import numpy as np
import wave
import time

RATE = 44100
CHUNK = 512
WIDTH = 2

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

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)

for i in range(int(20*RATE/CHUNK)): #do this for 10 seconds

    player.write(np.fromstring(stream.read(CHUNK),dtype=np.int16),CHUNK)

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

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

    #find the maximum
    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
        thefreq = (which+x1)*RATE/CHUNK
        print "The freq is %f Hz." % (thefreq)
        #print "The freq is %f Hz." % (-thefreq) 

    else:
        thefreq = which*RATE/CHUNK
        print "The freq is %f Hz." % (thefreq)
        #print "The freq is %f Hz." % (-thefreq)

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

0 个答案:

没有答案