Mercurial > hg > aimc
comparison matlab/bmm/carfac/Carfac.py @ 471:40cc1c0f20a8
Added a calculation (last plot) of the group delay. The previous check in fixed a couple of bugs.
author | alan.strelzoff |
---|---|
date | Sun, 11 Mar 2012 22:45:36 +0000 |
parents | 8e8381785b2b |
children |
comparison
equal
deleted
inserted
replaced
470:8e8381785b2b | 471:40cc1c0f20a8 |
---|---|
2 # Author: Al Strelzoff: strelz@mit.edu | 2 # Author: Al Strelzoff: strelz@mit.edu |
3 | 3 |
4 # The point of this file is to extract some of the formulas from the book and practice with them so as to better understand the filters. | 4 # The point of this file is to extract some of the formulas from the book and practice with them so as to better understand the filters. |
5 # The file has been written and tested for python 2.7 | 5 # The file has been written and tested for python 2.7 |
6 | 6 |
7 from numpy import cos, sin, pi, e, real,imag,arctan2 | 7 from numpy import cos, sin, pi, e, real,imag,arctan2,log10 |
8 | 8 |
9 | 9 |
10 from pylab import figure, plot,loglog, title, axis, show | 10 from pylab import figure, plot,loglog, title, axis, show |
11 | 11 |
12 fs = 22050.0 # sampling rate | 12 fs = 22050.0 # sampling rate |
161 return g * (1.0 + (h*c*z)/(z**2 - 2.0*a*z + r**2 )) | 161 return g * (1.0 + (h*c*z)/(z**2 - 2.0*a*z + r**2 )) |
162 | 162 |
163 | 163 |
164 | 164 |
165 ######################################################End of Carfac filter class######################################################################## | 165 ######################################################End of Carfac filter class######################################################################## |
166 | 166 |
167 | |
167 # instantiate the filters | 168 # instantiate the filters |
168 | 169 |
169 # n_ch is the number of filters as determined above | 170 # n_ch is the number of filters as determined above |
170 | 171 |
171 Filters = [] # the list of all filters, the zeroth is the top frequency | 172 Filters = [] # the list of all filters, the zeroth is the top frequency |
255 | 256 |
256 # calculate and plot the phase responses of the cascaded filters | 257 # calculate and plot the phase responses of the cascaded filters |
257 | 258 |
258 figure(5) | 259 figure(5) |
259 title('Carfac cascaded filter Phase lag') | 260 title('Carfac cascaded filter Phase lag') |
260 | |
261 | 261 |
262 for i in range(n_ch): | 262 for i in range(n_ch): |
263 | 263 |
264 filter = Filters[i] | 264 filter = Filters[i] |
265 | 265 |
290 elif diff < -pi: | 290 elif diff < -pi: |
291 for j in range(i,len(w)): | 291 for j in range(i,len(w)): |
292 w[j] += 2.0 * pi | 292 w[j] += 2.0 * pi |
293 | 293 |
294 else: continue | 294 else: continue |
295 | 295 |
296 # convert delay to cycles | |
297 for i in range(len(w)): | |
298 w[i] /= 2.0 * pi | |
299 | |
296 | 300 |
297 plot(u,w) | 301 plot(u,w) |
298 axis([0.0,pi,-25.0,0.05]) | 302 axis([0.0,pi,-5.0,0.05]) |
303 | |
304 # calculate group delay as cascaded lag/filter center frequency | |
305 | |
306 figure(6) | |
307 title('Carfac group delay') | |
308 | |
309 for i in range(n_ch): | |
310 | |
311 filter = Filters[i] | |
312 | |
313 | |
314 u = [] | |
315 v = [] # store list of phases | |
316 w = [] # second copy of phases needed for phase unwrapping | |
317 for j in range(1,steps): | |
318 x = pi * float(j)/steps | |
319 | |
320 u.append(x) | |
321 mag = filter.HT[j] | |
322 a = imag(mag) | |
323 b = real(mag) | |
324 phase = arctan2(a,b) | |
325 | |
326 v.append(phase) | |
327 w.append(phase) | |
328 | |
329 | |
330 for i in range(1,len(v)): | |
331 diff = v[i]-v[i-1] | |
332 if diff > pi: | |
333 for j in range(i,len(w)): | |
334 w[j] -= 2.0 * pi | |
335 elif diff < -pi: | |
336 for j in range(i,len(w)): | |
337 w[j] += 2.0 * pi | |
338 | |
339 else: continue | |
340 | |
341 # calculate the group delay from: GroupDelay(n)=-[((ph(n+1)-ph(n-1))/(w(n+1)-w(n-1))]/2pi # w in radians | |
342 # or gd[n] = - ((delta ph)/(delta w))/2pi | |
343 # delta ph = w[n+1] - w[n-1] | |
344 # delta w = pi* fs/steps # we divide up the frequency range fs into fractions of pi radians. | |
345 # gd[n] = - steps(w[n+1] - w[n-1])/fs | |
346 | |
347 A = -(float(steps))/(2 *fs) # note. could have calculated this once outside the filter loop | |
348 | |
349 # gd[n] = A *(w[n+1] - w[n-1]) | |
350 | |
351 gd = [] # in radians | |
352 gd.append(0.0) # pad to equalize size of u array, end points are then meaningless | |
353 for n in range(1,len(w)-1): | |
354 gd.append(A * (w[n+1] - w[n-1])) | |
355 | |
356 gd.append(0.0) | |
357 | |
358 plot(u,gd) | |
359 axis([0.1,pi,-0.01,0.05]) | |
360 | |
299 show() | 361 show() |
300 | 362 |