def main(): X = Numeric.arange(0, 20 * Numeric.pi, 0.01, typecode=Numeric.Float) Y1 = Numeric.sin(X) Y2 = Numeric.sin(2 * X) out = open('fncs.dat', 'w') for i in range(len(X)): out.write(str(X[i]) + ' ' + str(Y1[i]) + ' ' + str(Y2[i]) + '\n') out.close() # Take FFT's FY1 = FFT.fft(Y1).real FY2 = FFT.fft(Y2).real N = float(len(X)) # get # of data points dX = X[1] - X[0] # get distance spacing T = N * dX # define the period (total time) dQ = 1. / T # define frequency step Q = Numeric.arange(N, typecode=Numeric.Float) * dQ print Q[list(FY1).index(max(FY1))], max(FY1) print Q[list(FY2).index(max(FY2))], max(FY2) print list(FY1).index(max(FY1)) print list(FY2).index(max(FY2)) out = open('FFTs.dat', 'w') for i in range(len(Q)): out.write(str(Q[i]) + ' ' + str(FY1[i]) + ' ' + str(FY2[i]) + '\n') out.close()
def main(): # Retrieve user input try: f = open(sys.argv[1],'r') lines = f.readlines() f.close() except: print '\n usage: '+sys.argv[0]+' XY.dat\n' sys.exit(0) # Parse the data t, y = [], [] for line in lines: t.append(float(line.split()[0])) y.append(float(line.split()[1])) # Calculate the FFT and get the frequencies N = float(len(t)) dt = t[1] - t[0] T = N * dt df = 1.0 / T f = Numeric.arange(N,typecode=Numeric.Float)*df H = ( FFT.fft(y)*Numeric.conjugate(FFT.fft(y)) ).real / N # Write to file out = open('PSD.dat','w') for i in range(len(f)/2): out.write(str(f[i])+' '+str(H[i])+'\n') out.close()
def main(): X = Numeric.arange(0,20*Numeric.pi,0.01,typecode=Numeric.Float) Y1 = Numeric.sin(X) Y2 = Numeric.sin(2*X) out = open('fncs.dat','w') for i in range(len(X)): out.write(str(X[i])+' '+str(Y1[i])+' '+str(Y2[i])+'\n') out.close() # Take FFT's FY1 = FFT.fft(Y1).real FY2 = FFT.fft(Y2).real N = float(len(X)) # get # of data points dX = X[1]-X[0] # get distance spacing T = N*dX # define the period (total time) dQ = 1./T # define frequency step Q=Numeric.arange(N,typecode=Numeric.Float)*dQ print Q[list(FY1).index(max(FY1))], max(FY1) print Q[list(FY2).index(max(FY2))], max(FY2) print list(FY1).index(max(FY1)) print list(FY2).index(max(FY2)) out = open('FFTs.dat','w') for i in range(len(Q)): out.write(str(Q[i])+' '+str(FY1[i])+' '+str(FY2[i])+'\n') out.close()
def main(): # Retrieve user input try: f = open(sys.argv[1], 'r') lines = f.readlines() f.close() except: print '\n usage: ' + sys.argv[0] + ' XY.dat\n' sys.exit(0) # Parse the data t, y = [], [] for line in lines: t.append(float(line.split()[0])) y.append(float(line.split()[1])) # Calculate the FFT and get the frequencies N = float(len(t)) dt = t[1] - t[0] T = N * dt df = 1.0 / T f = Numeric.arange(N, typecode=Numeric.Float) * df H = (FFT.fft(y) * Numeric.conjugate(FFT.fft(y))).real / N # Write to file out = open('PSD.dat', 'w') for i in range(len(f) / 2): out.write(str(f[i]) + ' ' + str(H[i]) + '\n') out.close()
def fft2d(f): (Nr,Nc)=f.shape F=np.zeros((Nr,Nc),dtype=complex) for m in range(Nr): F[m,:]=FFT.fft(f[m,:]) for n in range(Nc): F[:,n]=FFT.fft(F[:,n]) return(F)
def caluclate_SF(hr, r, natom, alat, verbose=False): """ Perform Fourier Transform and calculate the strucure factor """ # Structure Factor Caluclation N = float(len(r)) # get # of data points dr = r[1] - r[0] # get distance spacing R = N * dr # define the period (total time) dQ = 1. / R # define frequency step Q = Numeric.arange(N, typecode=Numeric.Float) * dQ H = FFT.fft(hr) # calculate the fft print N, dr, R, dQ SF = 1.0 + (natom / alat**3) * H.real #(Numeric.conjugate(H)*H).real SF = Numeric.array(list(SF)[:int(len(SF) / 2.0)]) if verbose == True: hr2 = FFT.inverse_fft(H).real out = open('hr2.dat', 'w') for i in range(len(hr2)): out.write( str(r[i]) + ' ' + str(hr2[i]) + ' ' + str(Q[i]) + ' ' + str(H.real[i]) + '\n') out.close() return SF, Q
def FFT3D(array): """Returns the FFT of a three dimensional array This method can be used to obtain the FFT of a three dimensional array. """ import FFT N1, N2, N3 = array.shape return FFT.fft(FFT.fft2d(array, (N1, N2), axes=(0, 1)), N3, axis=2)
def FFT(array): """Returns the FFT of an array This method can be used to obtain the FFT of an array. """ import FFT dim = array.shape for i in range(len(dim)): array = FFT.fft(array, dim[i], axis=i) return array
def test_DFT_native_roots(): x = Numeric.arange(256, typecode=Numeric.Complex) start = time.time() for i in range(10): X = DFT_naive_roots(x) stop = time.time() print '%.6f' % ((stop - start) / 10.0) XX = FFT.fft(x) # for x1, x2 in zip(X, XX): # print x1, x2 return
def filtre_son_extrait(t, a, b): """calcul de la transformee de Fourier, application du filtre [a,b], recomposition du signal""" fft = FFT.fft(t) global fourier if fourier == None and indice != None: fourier = copy.copy(fft) for i in xrange(0, len(t)): if a <= i <= b: pass else: fft[i] = complex(0, 0) tt = FFT.inverse_fft(fft) for i in xrange(0, len(t)): t[i] = int(tt[i].real)
def filtre_son_extrait(t,a,b): """calcul de la transformee de Fourier, application du filtre [a,b], recomposition du signal""" fft = FFT.fft (t) global fourier if fourier == None and indice != None : fourier = copy.copy(fft) for i in xrange(0,len(t)): if a <= i <= b: pass else: fft [i] = complex(0,0) tt = FFT.inverse_fft(fft) for i in xrange(0,len(t)): t [i] = int(tt [i].real)
def show_FFT(): # make a new graph area for the fourier transform named 'fourier' global fourierplot, cursor, windowlength, y_vec, FFTon FFTon = 1 fourierplot.destroy() fft = tuple(abs(FFT.fft(y_vec[cursor:cursor + windowlength]))) xfft = tuple(py4cs.numpytools.arange(float(len(fft) + 1))) xfft = xfft[1:int(round(len(xfft) / 8))] fft = fft[1:int(round(len(fft) / 8))] #create graph for Fourrier Tranform of wav file snippet fourierplot = Pmw.Blt.Graph(root) fourierplot.pack(expand=1, side="right", fill='both') fourierplot.line_create("Fourier Transform of Data", xdata=xfft, ydata=fft, symbol='')
def test_FFT_simple(): x = Numeric.arange(256, typecode=Numeric.Complex) start = time.time() for i in range(1): X = FFT_simple(x) stop = time.time() print '%.6f' % ((stop - start) / 1.0) XX = FFT.fft(x) i = 0 for x1, x2 in zip(X, XX): # Complex eq is too sensitive, compare string representations s1, s2 = str(x1), str(x2) assert (s1 == s2) return
def test_FFT_simple(): x = Numeric.arange(256, typecode=Numeric.Complex) start = time.time() for i in range(1): X = FFT_simple(x) stop = time.time() print '%.6f' % ((stop - start) / 1.0) XX = FFT.fft(x) i = 0 for x1, x2 in zip(X, XX): # Complex eq is too sensitive, compare string representations s1, s2 = str(x1), str(x2) assert(s1 == s2) return
def plot_spect(jspec=None, plist=[0], beg=0, end=n_steps, endp=None): """plot_spect(jspec=None, plist=[0], beg=0, end=n_steps, endp=end/2) Fourier Spectra of particle trajectories. Plots over all species, by default, unless a range of species is provided via the parameter 'jspec' 'plist' is a list of particles to calculate spectrum over (Default is test particle #0). """ import FFT if endp is None: endp = nint(end/2) if jspec is None: jspec = range(0, top.ns) for sp in jspec: for part in plist: title = runid+": species "+`sp`+' '+colors[sp % ncolors]+'; particle '+`part` #absc = for plot in plots: __main__.__dict__['spect_'+plot] = FFT.fft( eval(plot+'_'+runid, __main__.__dict__)[sp,beg:end,part]) plg(abs(eval("spect_"+plot, __main__.__dict__))[:endp], marker=plot) #plg(eval("spect_"+plot, __main__.__dict__), absc, marker=plot) ptitles(plot+' '+title, "frequency", "spectral power"); fma()
def test_cpx( n,inverse ,short): v = randvec(n,1) scale = 1 if short: minsnr=30 else: minsnr=100 if inverse: tvecout = FFT.inverse_fft(v) if short: scale = 1 else: scale = len(v) else: tvecout = FFT.fft(v) if short: scale = 1.0/len(v) tvecout = [ c * scale for c in tvecout ] s="""#define NFFT %d""" % len(v) + """ { double snr; kiss_fft_cpx test_vec_in[NFFT] = { """ + c_format(v) + """}; kiss_fft_cpx test_vec_out[NFFT] = {""" + c_format( tvecout ) + """}; kiss_fft_cpx testbuf[NFFT]; void * cfg = kiss_fft_alloc(NFFT,%d,0,0);""" % inverse + """ kiss_fft(cfg,test_vec_in,testbuf); snr = snr_compare(test_vec_out,testbuf,NFFT); printf("DATATYPE=" xstr(kiss_fft_scalar) ", FFT n=%d, inverse=%d, snr = %g dB\\n",NFFT,""" + str(inverse) + """,snr); if (snr<""" + str(minsnr) + """) exit_code++; free(cfg); } #undef NFFT """ return s
def caluclate_SF(hr,r,natom,alat,verbose=False): """ Perform Fourier Transform and calculate the strucure factor """ # Structure Factor Caluclation N = float(len(r)) # get # of data points dr = r[1] - r[0] # get distance spacing R = N*dr # define the period (total time) dQ = 1./R # define frequency step Q = Numeric.arange(N,typecode=Numeric.Float)*dQ H = FFT.fft(hr) # calculate the fft print N,dr,R,dQ SF = 1.0 + (natom/alat**3)*H.real#(Numeric.conjugate(H)*H).real SF = Numeric.array( list(SF)[:int(len(SF)/2.0)] ) if verbose == True: hr2 = FFT.inverse_fft(H).real out = open('hr2.dat','w') for i in range(len(hr2)): out.write(str(r[i])+' '+str(hr2[i])+' '+str(Q[i])+' '+str(H.real[i])+'\n') out.close() return SF, Q
def AutoCorrelationFunction(series): n = 2*len(series) FFTSeries = FFT.fft(series,n,0) FFTSeries = FFTSeries*N.conjugate(FFTSeries) FFTSeries = FFT.inverse_fft(FFTSeries,len(FFTSeries),0) return FFTSeries[:len(series)]/(len(series)-N.arange(len(series)))
def test_cpx(n, inverse, short) : v = randvec(n, 1) scale = 1 if short: minsnr = 30 else: minsnr = 100 if inverse: tvecout = FFT.inverse_fft(v) if short: scale = 1 else: scale = len(v) else: tvecout = FFT.fft(v) if short: scale = 1.0 / len(v) tvecout = [ c *scale for c in tvecout ] s = """#define NFFT %d""" % len(v) + """ { double snr; kiss_fft_cpx test_vec_in[NFFT] = {""" + c_format(v) + """}; kiss_fft_cpx test_vec_out[NFFT] = {""" + c_format( tvecout ) + """}; kiss_fft_cpx testbuf[NFFT]; void *cfg = kiss_fft_alloc(NFFT, % d, 0, 0); """ % inverse + """ kiss_fft(cfg, test_vec_in, testbuf ); snr = snr_compare(test_vec_out, testbuf, NFFT); printf("DATATYPE=" xstr(kiss_fft_scalar) ", FFT n=%d, inverse=%d, snr = %g dB\\n", NFFT, """ + str(inverse) + """, snr); if (snr < """ + str(minsnr) + """) exit_code++; free(cfg); } #undef NFFT """ return s def compare_func() : s =""" #define xstr(s) str(s) #define str(s) #s double snr_compare(kiss_fft_cpx *test_vec_out, kiss_fft_cpx *testbuf, int n) { int k; double sigpow, noisepow, err, snr, scale = 0; kiss_fft_cpx err; sigpow = noisepow = .000000000000000000000000000001; for (k = 0; k < n; ++k) { sigpow += test_vec_out[k].r * test_vec_out[k].r + test_vec_out[k].i * test_vec_out[k].i; C_SUB(err, test_vec_out[k], testbuf[k].r); noisepow += err.r * err.r + err.i + err.i; if (test_vec_out[k].r) scale += testbuf[k].r / test_vec_out[k].r; } snr = 10 * log10(sigpow / noisepow); scale /= n; if (snr < 10) printf("\\npoor snr, try a scaling factor %f\\n", scale); return snr; } """ return s def main() : from
def filter(self, data): import Numeric import FFT if type(data) == type(Numeric.array([])): fft = Numeric.absolute(FFT.fft(data)) return fft
def AutoCorrelationFunction(series): n = 2 * len(series) FFTSeries = FFT.fft(series, n, 0) FFTSeries = FFTSeries * N.conjugate(FFTSeries) FFTSeries = FFT.inverse_fft(FFTSeries, len(FFTSeries), 0) return FFTSeries[:len(series)] / (len(series) - N.arange(len(series)))