2 # Copyright 2004,2005,2009 Free Software Foundation, Inc.
4 # This file is part of GNU Radio
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING. If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
23 Routines for designing window functions.
27 from gnuradio import gr
39 if u >= IzeroEPSILON*sum:
44 return (fft_size - 1)/2
50 return 2.0*math.pi/fft_size
53 return 1.0/(fft_size >> 1)
56 math.log(2.0)/(midn(fft_size) + 1.0)
58 def hamming(fft_size):
60 for index in xrange(fft_size):
61 window.append(0.54 - 0.46 * math.cos (2 * math.pi / fft_size * index)) # Hamming window
64 def hanning(fft_size):
66 for index in xrange(fft_size):
67 window.append(0.5 - 0.5 * math.cos (2 * math.pi / fft_size * index)) # von Hann window
71 window = [0 for i in range(fft_size)]
73 for index in xrange(midn(fft_size)+1):
74 window[j] = window[index] = (1.0 - math.sqrt((index - midm1(fft_size)) / midp1(fft_size)))
79 window = [0 for i in range(fft_size)]
81 for index in xrange(midn(fft_size)+1):
82 window[j] = window[index] = (1.0 - math.abs((index - midm1(fft_size)) / midp1(fft_size)))
86 def bartlett(fft_size):
89 window = [0 for i in range(fft_size)]
91 for index in xrange(midn(fft_size)+1):
92 window[j] = window[index] = angle
97 def blackman2(fft_size):
100 window = [0 for i in range(fft_size)]
102 for index in xrange(midn(fft_size)+1):
104 window[j] = window[index] = (.34401 + (cx * (-.49755 + (cx * .15844))))
109 def blackman3(fft_size):
110 mfrq = freq(fft_size)
112 window = [0 for i in range(fft_size)]
114 for index in xrange(midn(fft_size)+1):
116 window[j] = window[index] = (.21747 + (cx * (-.45325 + (cx * (.28256 - (cx * .04672))))))
121 def blackman4(fft_size):
122 mfrq = freq(fft_size)
124 window = [0 for i in range(fft_size)]
126 for index in xrange(midn(fft_size)+1):
128 window[j] = window[index] = (.084037 + (cx * (-.29145 + (cx * (.375696 + (cx * (-.20762 + (cx * .041194))))))))
133 def exponential(fft_size):
135 window = [0 for i in range(fft_size)]
137 for index in xrange(midn(fft_size)+1):
138 window[j] = window[i] = (expsum - 1.0)
139 expsum *= expn(fft_size)
143 def riemann(fft_size):
145 window = [0 for i in range(fft_size)]
147 for index in xrange(midn(fft_size)):
148 if index == midn(fft_size):
149 window[index] = window[j] = 1.0
151 cx = sr1*midn(fft_size) - index
152 window[index] = window[j] = math.sin(cx)/cx
156 def kaiser(fft_size,beta):
157 ibeta = 1.0/izero(beta)
158 inm1 = 1.0/(fft_size)
159 window = [0 for i in range(fft_size)]
160 for index in xrange(fft_size):
161 window[index] = izero(beta*math.sqrt(1.0 - (index * inm1)*(index * inm1))) * ibeta
164 # Closure to generate functions to create cos windows
166 def coswindow(coeffs):
167 def closure(fft_size):
168 window = [0] * fft_size
169 #print list(enumerate(coeffs))
170 for w_index in range(fft_size):
171 for (c_index, coeff) in enumerate(coeffs):
172 window[w_index] += (-1)**c_index * coeff * math.cos(2.0*c_index*math.pi*(w_index+0.5)/(fft_size-1))
176 blackmanharris = coswindow((0.35875,0.48829,0.14128,0.01168))
177 nuttall = coswindow((0.3635819,0.4891775,0.1365995,0.0106411)) # Wikipedia calls this Blackman-Nuttall
178 nuttall_cfd = coswindow((0.355768,0.487396,0.144232,0.012604)) # Wikipedia calls this Nuttall, continuous first deriv
179 flattop = coswindow((1.0,1.93,1.29,0.388,0.032)) # Flat top window, coeffs from Wikipedia
180 rectangular = lambda fft_size: [1]*fft_size