Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / window.py
1 #
2 # Copyright 2004,2005 Free Software Foundation, Inc.
3
4 # This file is part of GNU Radio
5
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 2, or (at your option)
9 # any later version.
10
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.
15
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.
20
21
22 '''
23 Routines for designing window functions.
24 '''
25
26 import math
27 from gnuradio import gr
28
29 def izero(x):
30     izeroepsilon = 1e-21
31     halfx = x/2.0
32     accum = u = n = 1
33     while 1:
34         temp = halfx/n
35         n += 1
36         temp *= temp
37         u *= temp
38         accum += u
39         if u >= IzeroEPSILON*sum:
40             break
41     return accum
42
43 def midm1(fft_size):
44     return (fft_size - 1)/2
45
46 def midp1(fft_size):
47     return (fft_size+1)/2
48
49 def freq(fft_size):
50     return 2.0*math.pi/fft_size
51
52 def rate(fft_size):
53     return 1.0/(fft_size >> 1)
54
55 def expn(fft_size):
56     math.log(2.0)/(midn(fft_size) + 1.0)
57     
58 def hamming(fft_size):
59     window = []
60     for index in xrange(fft_size):
61         window.append(0.54 - 0.46 * math.cos (2 * math.pi / fft_size * index))  # Hamming window
62     return window
63
64 def hanning(fft_size):
65     window = []
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
68     return window
69
70 def welch(fft_size):
71     window = [0 for i in range(fft_size)]
72     j = fft_size-1
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)))
75         j -= 1
76     return window
77
78 def parzen(fft_size):
79     window = [0 for i in range(fft_size)]
80     j = fft_size-1
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)))
83         j -= 1
84     return window
85
86 def bartlett(fft_size):
87     mfrq = freq(fft_size)
88     angle = 0
89     window = [0 for i in range(fft_size)]
90     j = fft_size-1
91     for index in xrange(midn(fft_size)+1):
92         window[j] = window[index] = angle
93         angle += freq
94         j -= 1
95     return window
96     
97 def blackman2(fft_size):
98     mfrq = freq(fft_size)
99     angle = 0
100     window = [0 for i in range(fft_size)]
101     j = fft_size-1
102     for index in xrange(midn(fft_size)+1):
103         cx = math.cos(angle)
104         window[j] = window[index] = (.34401 + (cx * (-.49755 + (cx * .15844))))
105         angle += freq
106         j -= 1
107     return window
108     
109 def blackman3(fft_size):
110     mfrq = freq(fft_size)
111     angle = 0
112     window = [0 for i in range(fft_size)]
113     j = fft_size-1
114     for index in xrange(midn(fft_size)+1):
115         cx = math.cos(angle)
116         window[j] = window[index] = (.21747 + (cx * (-.45325 + (cx * (.28256 - (cx * .04672))))))
117         angle += freq
118         j -= 1
119     return window
120     
121 def blackman4(fft_size):
122     mfrq = freq(fft_size)
123     angle = 0
124     window = [0 for i in range(fft_size)]
125     j = fft_size-1
126     for index in xrange(midn(fft_size)+1):
127         cx = math.cos(angle)
128         window[j] = window[index] = (.084037 + (cx * (-.29145 + (cx * (.375696 + (cx * (-.20762 + (cx * .041194))))))))
129         angle += freq
130         j -= 1
131     return window
132     
133 def exponential(fft_size):
134     expsum = 1.0
135     window = [0 for i in range(fft_size)]
136     j = fft_size-1
137     for index in xrange(midn(fft_size)+1):
138       window[j] = window[i] = (expsum - 1.0)
139       expsum *= expn(fft_size)
140       j -= 1
141     return window
142
143 def riemann(fft_size):
144     sr1 = freq(fft_size)
145     window = [0 for i in range(fft_size)]
146     j = fft_size-1
147     for index in xrange(midn(fft_size)):
148         if index == midn(fft_size):
149             window[index] = window[j] = 1.0
150         else:
151             cx = sr1*midn(fft_size) - index
152             window[index] = window[j] = math.sin(cx)/cx
153         j -= 1
154     return window
155
156 def blackmanharris(fft_size):
157     a0 = 0.35875
158     a1 = 0.48829
159     a2 = 0.14128
160     a3 = 0.01168
161     window = [0 for i in range(fft_size)]
162     for index in xrange(fft_size):
163         window[index] = a0 
164         window[index] -= a1*math.cos(2.0*math.pi*(index+0.5)/(fft_size - 1))
165         window[index] += a2*math.cos(4.0*math.pi*(index+0.5)/(fft_size - 1))
166         window[index] -= a3*math.cos(6.0*math.pi*(index+0.5)/(fft_size - 1))
167     return window
168
169 def nuttall(fft_size):
170     a0 = 0.3635819
171     a1 = 0.4891775
172     a2 = 0.1365995
173     a3 = 0.0106411
174     window = [0 for i in range(fft_size)]
175     for index in xrange(fft_size):
176         window[index] = a0 
177         window[index] -= a1*math.cos(2.0*math.pi*(index+0.5)/(fft_size - 1))
178         window[index] += a2*math.cos(4.0*math.pi*(index+0.5)/(fft_size - 1))
179         window[index] -= a3*math.cos(6.0*math.pi*(index+0.5)/(fft_size - 1))
180     return window
181
182 def kaiser(fft_size,beta):
183     ibeta = 1.0/izero(beta)
184     inm1 = 1.0/(fft_size)
185     window = [0 for i in range(fft_size)]
186     for index in xrange(fft_size):
187         window[index] = izero(beta*math.sqrt(1.0 - (index * inm1)*(index * inm1))) * ibeta
188     return window
189
190