c@409
|
1 #!/usr/bin/env python
|
c@409
|
2
|
c@409
|
3 import math
|
c@409
|
4 import sys
|
c@409
|
5 import random
|
c@409
|
6
|
c@409
|
7 pi=math.pi
|
c@409
|
8 e=math.e
|
c@409
|
9 j=complex(0,1)
|
c@409
|
10
|
c@409
|
11 def fft(f,inv):
|
c@409
|
12 n=len(f)
|
c@409
|
13 if n==1:
|
c@409
|
14 return f
|
c@409
|
15
|
c@409
|
16 for p in 2,3,5:
|
c@409
|
17 if n%p==0:
|
c@409
|
18 break
|
c@409
|
19 else:
|
c@409
|
20 raise Exception('%s not factorable ' % n)
|
c@409
|
21
|
c@409
|
22 m = n/p
|
c@409
|
23 Fout=[]
|
c@409
|
24 for q in range(p): # 0,1
|
c@409
|
25 fp = f[q::p] # every p'th time sample
|
c@409
|
26 Fp = fft( fp ,inv)
|
c@409
|
27 Fout.extend( Fp )
|
c@409
|
28
|
c@409
|
29 for u in range(m):
|
c@409
|
30 scratch = Fout[u::m] # u to end in strides of m
|
c@409
|
31 for q1 in range(p):
|
c@409
|
32 k = q1*m + u # indices to Fout above that became scratch
|
c@409
|
33 Fout[ k ] = scratch[0] # cuz e**0==1 in loop below
|
c@409
|
34 for q in range(1,p):
|
c@409
|
35 if inv:
|
c@409
|
36 t = e ** ( j*2*pi*k*q/n )
|
c@409
|
37 else:
|
c@409
|
38 t = e ** ( -j*2*pi*k*q/n )
|
c@409
|
39 Fout[ k ] += scratch[q] * t
|
c@409
|
40
|
c@409
|
41 return Fout
|
c@409
|
42
|
c@409
|
43 def rifft(F):
|
c@409
|
44 N = len(F) - 1
|
c@409
|
45 Z = [0] * (N)
|
c@409
|
46 for k in range(N):
|
c@409
|
47 Fek = ( F[k] + F[-k-1].conjugate() )
|
c@409
|
48 Fok = ( F[k] - F[-k-1].conjugate() ) * e ** (j*pi*k/N)
|
c@409
|
49 Z[k] = Fek + j*Fok
|
c@409
|
50
|
c@409
|
51 fp = fft(Z , 1)
|
c@409
|
52
|
c@409
|
53 f = []
|
c@409
|
54 for c in fp:
|
c@409
|
55 f.append(c.real)
|
c@409
|
56 f.append(c.imag)
|
c@409
|
57 return f
|
c@409
|
58
|
c@409
|
59 def real_fft( f,inv ):
|
c@409
|
60 if inv:
|
c@409
|
61 return rifft(f)
|
c@409
|
62
|
c@409
|
63 N = len(f) / 2
|
c@409
|
64
|
c@409
|
65 res = f[::2]
|
c@409
|
66 ims = f[1::2]
|
c@409
|
67
|
c@409
|
68 fp = [ complex(r,i) for r,i in zip(res,ims) ]
|
c@409
|
69 print 'fft input ', fp
|
c@409
|
70 Fp = fft( fp ,0 )
|
c@409
|
71 print 'fft output ', Fp
|
c@409
|
72
|
c@409
|
73 F = [ complex(0,0) ] * ( N+1 )
|
c@409
|
74
|
c@409
|
75 F[0] = complex( Fp[0].real + Fp[0].imag , 0 )
|
c@409
|
76
|
c@409
|
77 for k in range(1,N/2+1):
|
c@409
|
78 tw = e ** ( -j*pi*(.5+float(k)/N ) )
|
c@409
|
79
|
c@409
|
80 F1k = Fp[k] + Fp[N-k].conjugate()
|
c@409
|
81 F2k = Fp[k] - Fp[N-k].conjugate()
|
c@409
|
82 F2k *= tw
|
c@409
|
83 F[k] = ( F1k + F2k ) * .5
|
c@409
|
84 F[N-k] = ( F1k - F2k ).conjugate() * .5
|
c@409
|
85 #F[N-k] = ( F1kp + e ** ( -j*pi*(.5+float(N-k)/N ) ) * F2kp ) * .5
|
c@409
|
86 #F[N-k] = ( F1k.conjugate() - tw.conjugate() * F2k.conjugate() ) * .5
|
c@409
|
87
|
c@409
|
88 F[N] = complex( Fp[0].real - Fp[0].imag , 0 )
|
c@409
|
89 return F
|
c@409
|
90
|
c@409
|
91 def main():
|
c@409
|
92 #fft_func = fft
|
c@409
|
93 fft_func = real_fft
|
c@409
|
94
|
c@409
|
95 tvec = [0.309655,0.815653,0.768570,0.591841,0.404767,0.637617,0.007803,0.012665]
|
c@409
|
96 Ftvec = [ complex(r,i) for r,i in zip(
|
c@409
|
97 [3.548571,-0.378761,-0.061950,0.188537,-0.566981,0.188537,-0.061950,-0.378761],
|
c@409
|
98 [0.000000,-1.296198,-0.848764,0.225337,0.000000,-0.225337,0.848764,1.296198] ) ]
|
c@409
|
99
|
c@409
|
100 F = fft_func( tvec,0 )
|
c@409
|
101
|
c@409
|
102 nerrs= 0
|
c@409
|
103 for i in range(len(Ftvec)/2 + 1):
|
c@409
|
104 if abs( F[i] - Ftvec[i] )> 1e-5:
|
c@409
|
105 print 'F[%d]: %s != %s' % (i,F[i],Ftvec[i])
|
c@409
|
106 nerrs += 1
|
c@409
|
107
|
c@409
|
108 print '%d errors in forward fft' % nerrs
|
c@409
|
109 if nerrs:
|
c@409
|
110 return
|
c@409
|
111
|
c@409
|
112 trec = fft_func( F , 1 )
|
c@409
|
113
|
c@409
|
114 for i in range(len(trec) ):
|
c@409
|
115 trec[i] /= len(trec)
|
c@409
|
116
|
c@409
|
117 for i in range(len(tvec) ):
|
c@409
|
118 if abs( trec[i] - tvec[i] )> 1e-5:
|
c@409
|
119 print 't[%d]: %s != %s' % (i,tvec[i],trec[i])
|
c@409
|
120 nerrs += 1
|
c@409
|
121
|
c@409
|
122 print '%d errors in reverse fft' % nerrs
|
c@409
|
123
|
c@409
|
124
|
c@409
|
125 def make_random(dims=[1]):
|
c@409
|
126 import Numeric
|
c@409
|
127 res = []
|
c@409
|
128 for i in range(dims[0]):
|
c@409
|
129 if len(dims)==1:
|
c@409
|
130 r=random.uniform(-1,1)
|
c@409
|
131 i=random.uniform(-1,1)
|
c@409
|
132 res.append( complex(r,i) )
|
c@409
|
133 else:
|
c@409
|
134 res.append( make_random( dims[1:] ) )
|
c@409
|
135 return Numeric.array(res)
|
c@409
|
136
|
c@409
|
137 def flatten(x):
|
c@409
|
138 import Numeric
|
c@409
|
139 ntotal = Numeric.product(Numeric.shape(x))
|
c@409
|
140 return Numeric.reshape(x,(ntotal,))
|
c@409
|
141
|
c@409
|
142 def randmat( ndims ):
|
c@409
|
143 dims=[]
|
c@409
|
144 for i in range( ndims ):
|
c@409
|
145 curdim = int( random.uniform(2,4) )
|
c@409
|
146 dims.append( curdim )
|
c@409
|
147 return make_random(dims )
|
c@409
|
148
|
c@409
|
149 def test_fftnd(ndims=3):
|
c@409
|
150 import FFT
|
c@409
|
151 import Numeric
|
c@409
|
152
|
c@409
|
153 x=randmat( ndims )
|
c@409
|
154 print 'dimensions=%s' % str( Numeric.shape(x) )
|
c@409
|
155 #print 'x=%s' %str(x)
|
c@409
|
156 xver = FFT.fftnd(x)
|
c@409
|
157 x2=myfftnd(x)
|
c@409
|
158 err = xver - x2
|
c@409
|
159 errf = flatten(err)
|
c@409
|
160 xverf = flatten(xver)
|
c@409
|
161 errpow = Numeric.vdot(errf,errf)+1e-10
|
c@409
|
162 sigpow = Numeric.vdot(xverf,xverf)+1e-10
|
c@409
|
163 snr = 10*math.log10(abs(sigpow/errpow) )
|
c@409
|
164 if snr<80:
|
c@409
|
165 print xver
|
c@409
|
166 print x2
|
c@409
|
167 print 'SNR=%sdB' % str( snr )
|
c@409
|
168
|
c@409
|
169 def myfftnd(x):
|
c@409
|
170 import Numeric
|
c@409
|
171 xf = flatten(x)
|
c@409
|
172 Xf = fftndwork( xf , Numeric.shape(x) )
|
c@409
|
173 return Numeric.reshape(Xf,Numeric.shape(x) )
|
c@409
|
174
|
c@409
|
175 def fftndwork(x,dims):
|
c@409
|
176 import Numeric
|
c@409
|
177 dimprod=Numeric.product( dims )
|
c@409
|
178
|
c@409
|
179 for k in range( len(dims) ):
|
c@409
|
180 cur_dim=dims[ k ]
|
c@409
|
181 stride=dimprod/cur_dim
|
c@409
|
182 next_x = [complex(0,0)]*len(x)
|
c@409
|
183 for i in range(stride):
|
c@409
|
184 next_x[i*cur_dim:(i+1)*cur_dim] = fft(x[i:(i+cur_dim)*stride:stride],0)
|
c@409
|
185 x = next_x
|
c@409
|
186 return x
|
c@409
|
187
|
c@409
|
188 if __name__ == "__main__":
|
c@409
|
189 try:
|
c@409
|
190 nd = int(sys.argv[1])
|
c@409
|
191 except:
|
c@409
|
192 nd=None
|
c@409
|
193 if nd:
|
c@409
|
194 test_fftnd( nd )
|
c@409
|
195 else:
|
c@409
|
196 sys.exit(0)
|