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