def fftvec(vec): """ performs a fft on a vector with 3 components in the first index position This is really just a wrapper for fft, fftn and their inverses """ try: from anfft import fft, fftn 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 fft, fftn 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 complex64, shape, array, empty 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([fftn(i,measure=True) for i in vec]).astype(complex64) # result = empty(vec.shape, dtype=complex64) # result[0] = fftn(vec[0], measure=True).astype(complex64) # result[1] = fftn(vec[1], measure=True).astype(complex64) # result[2] = fftn(vec[2], measure=True).astype(complex64) # return result elif fft_type==0: return fftn(vec, axes=range(1,vec.ndim)).astype(complex64) elif fft_type==2: # return array([gpu_fft(i) for i in vec.astype(complex64)]) result = empty(vec.shape, dtype=complex64) result[0] = gpu_fft(vec[0].copy()) result[1] = gpu_fft(vec[1].copy()) result[2] = gpu_fft(vec[2].copy()) return result else: # "Scalar", fft the whole thing if fft_type==1: return fftn(vec,measure=True).astype(complex64) elif fft_type==0: return fftn(vec).astype(complex64) elif fft_type==2: return gpu_fft(vec.copy()) elif vec.ndim == 1: #Not a vector, so use fft if fft_type==1: return fft(vec,measure = True).astype(complex64) elif fft_type==0: return fft(vec).astype(complex64) elif fft_type==2: return gpu_fft(vec.astype(complex64)) else: #0th index is 3, so its a vector #return fft(vec, axis=1).astype(complex64) return array([fft(i) for i in vec])
def plot_tada_spec(): plt.close('all') rate, sig = wavfile.read('tada.wav') sig = sp.float32(sig) fsig = anfft.fft(sig.T).T plt.figure() plt.plot(sp.absolute(fsig)) plt.savefig('tadaspec.pdf')
def plot_noise_spec(): plt.close('all') rate, sig = wavfile.read('Noisysignal1.wav') sig = sp.float32(sig) fsig = anfft.fft(sig.T).T f = sp.absolute(fsig) plt.figure() plt.plot(f[0:f.shape[0] / 2]) plt.savefig('noisyspec.pdf')
def plot_noise_spec(): plt.close('all') rate, sig = wavfile.read('Noisysignal1.wav') sig = sp.float32(sig) fsig = anfft.fft(sig.T).T f = sp.absolute(fsig) plt.figure() plt.plot(f[0:f.shape[0]/2]) plt.savefig('noisyspec.pdf')
def plot_tada_spec_left(): plt.close('all') rate, sig = wavfile.read('tada.wav') sig = sp.float32(sig) fsig = anfft.fft(sig.T).T f = sp.absolute(fsig) plt.figure() plt.plot(f[0:f.shape[0]/2,:]) plt.savefig('tadaspec2.pdf')
def plot_sine_spec(): plt.close('all') samplerate = 44100 # 44100 samples per second freq = 1760 # We’re going to produce a 1760 Hz sine wave ... length = 2 # ... which will last for 2 seconds. stepsize = freq*2*sp.pi/samplerate sig = sp.sin(sp.arange(0,stepsize*length*samplerate ,stepsize)) sig = sp.float32(sig) fsig = anfft.fft(sig) plt.plot(sp.absolute(fsig)) plt.savefig('sinespec.pdf')
def plot_down_saw_spec_correct(): plt.close('all') rate, in_sig = wavfile.read('saw.wav') old_rate = 44100 new_rate = 22050 in_sig = sp.float32(in_sig) fin = anfft.fft(in_sig) nsiz = sp.floor(in_sig.size*new_rate/old_rate) nsizh = sp.floor(nsiz/2) fout = sp.zeros(nsiz) fout = fout + 0j fout[0:nsizh] = fin[0:nsizh] fout[nsiz-nsizh+1:] = sp.conj(sp.flipud(fout[1:nsizh])) f = sp.absolute(fout) plt.plot(f[0:f.shape[0]/2]) plt.savefig('sawdownspec.pdf')
print(np.sum(c)) 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"])
import datetime import os from weakref import ref from numpy import array_split, short, fromstring from numpy.random import rand as rand_array import anfft import RPi.GPIO as GPIO pins = [0, 1, 4, 7, 8, 9, 10, 11, 14, 15, 17, 18, 21, 22, 23, 24, 25] # Warm up ANFFT, allowing it to determine which FFT algorithm will work fastest on this machine. anfft.fft(rand_array(1024), measure=True) class SpectrumAnalyzer(object): def __init__(self, messageQueue, config): self.messageQueue = messageQueue self._dataSinceLastSpectrum = [] self.loadSettings(config) self.lights = LightController(self, config) def loadSettings(self, gcp): # The number of spectrum analyzer bands (also light output channels) to use. self.frequencyBands = int(gcp.get('main', 'frequencyBands', 16))
print np.sum(c) 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)