def ifftvec(vec): """ performs a fft on a vector with 3 components in the last index position This is a wrapper for ifft and ifftn """ try: from anfft import ifft, ifftn fft_type = 1 except: # print "Could not import anfft, importing scipy instead." #Update 9/18/2013 -- numpy with mkl is way faster than scipy import mkl mkl.set_num_threads(8) from numpy.fft import ifft, ifftn fft_type = 0 if force_gpu: fft_type = 2 #set gpu fft's manually -- not sure what a automatic way would look like from numpy import float32, real, array, empty, complex64 if vec.ndim > 2: if vec.shape[0] == 3: # "Vector": first index has size 3 so fft the other columns if fft_type==1: return array([ifftn(i,measure=True) for i in vec]).astype(float32) # result = empty(vec.shape, dtype=float32) # result[0] = real(ifftn(vec[0], measure=True)).astype(float32) # result[1] = real(ifftn(vec[1], measure=True)).astype(float32) # result[2] = real(ifftn(vec[2], measure=True)).astype(float32) # return result elif fft_type==0: return ifftn(vec, axes=range(1,vec.ndim)).astype(float32) elif fft_type==2: # return array([gpu_ifft(i) for i in vec]).astype(float32) result = empty(vec.shape, dtype=float32) result[0] = gpu_ifft(vec[0].copy()).astype(float32) result[1] = gpu_ifft(vec[1].copy()).astype(float32) result[2] = gpu_ifft(vec[2].copy()).astype(float32) return result else: # "Scalar", fft the whole thing if fft_type==1: return ifftn(vec,measure=True).astype(float32) elif fft_type==0: return ifftn(vec).astype(float32) elif fft_type==2: return gpu_ifft(vec.copy()).astype(float32) elif vec.ndim == 1: #Not a vector, so use fft if fft_type==1: return ifft(vec,measure = True).astype(float32) elif fft_type==0: return ifft(vec).astype(float32) elif fft_type==2: return gpu_ifft(vec).astype(float32) else: #0th index is 3, so its a vector #return fft(vec, axis=1).astype(complex64) return array([ifft(i) for i in vec]).astype(float32)
t1 = time.time() for i in range(Ntest): b2 = scifft.fft(a0) a1 = scifft.ifft(b2) t1b = time.time() print(np.sum(a1)) t2 = time.time() for i in range(Ntest): b3 = anfft.fft(a0) a2 = anfft.ifft(b3) t2b = time.time() print(np.sum(a2)) t3 = time.time() # create a forward and backward fft plan a = fftw3.create_aligned_array(a0.shape, np.complex128, 16) b = fftw3.create_aligned_array(a0.shape, np.complex128, 16) c = fftw3.create_aligned_array(a0.shape, np.complex128, 16) a[:] = a0 ft3fft = fftw3.Plan(a, b, direction="forward", flags=["measure"]) ft3ifft = fftw3.Plan(b, c, direction="backward", flags=["measure"])
t1 = time.time() for i in range(Ntest): b2 = scifft.fft(a0) a1 = scifft.ifft(b2) t1b = time.time() print np.sum(a1) t2 = time.time() for i in range(Ntest): b3 = anfft.fft(a0) a2 = anfft.ifft(b3) t2b = time.time() print np.sum(a2) t3 = time.time() # create a forward and backward fft plan a = fftw3.create_aligned_array(a0.shape, np.complex128, 16) b = fftw3.create_aligned_array(a0.shape, np.complex128, 16) c = fftw3.create_aligned_array(a0.shape, np.complex128, 16) a[:] = a0 ft3fft = fftw3.Plan(a, b, direction='forward', flags=['measure']) ft3ifft = fftw3.Plan(b, c, direction='backward', flags=['measure'])
#rate2,sig2 = wavfile.read('balloon.wav') #m = sig2.shape[0] #sig1 = sp.append(sig1,sp.zeros((m,2)),axis = 0) #sig2 = sp.append(sig2,sp.zeros((sig1.shape[0] - m,2)),axis = 0) #f1 = anfft.fft(sig1.T).T #f2 = anfft.fft(sig2.T).T #out = anfft.ifft((f1*f2).T).T #out = sp.real(out) #scaled = sp.int16(out/sp.absolute(out).max() * 32767) #wavfile.write('test.wav',44100,scaled) #============================================================================== # PROBLEM 4 #============================================================================== #samplerate = 22050 #noise = sp.int16(sp.random.randint(-32767,32767,samplerate*10)) # Create 10 seconds of mono white noise #wavfile.write('white_noise.wav',22050,noise) #f = anfft.fft(sp.float32(noise)) #plt.plot(sp.absolute(f)) #plt.show() #============================================================================== # PROBLEM 5 #============================================================================== rate, sig = wavfile.read('tada.wav') sig = sp.float32(sig) noise = sp.float32(sp.random.randint(-32767,32767,sig.shape)) out = anfft.ifft(anfft.fft(sig.T)*anfft.fft(noise.T)).T out = sp.real(out) out = sp.int16(out/sp.absolute(out).max() * 32767) wavfile.write('white-conv.wav',rate,out)