How does the inverse Fourier transform go in the process of active noise cancellation?

198 Views Asked by At

Sound was input to the microphone through the sound card, and the sound was read in real time, and the frequency was read by the frequency analysis through the FFT. I want to generate the reverse frequency through this, but I do not know how.

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

There are 0 best solutions below