Skip to content
This repository
Newer
Older
100644 506 lines (426 sloc) 13.475 kb
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
1 """The suite of window functions."""
2
3 import numpy as np
4 from scipy import special, linalg
5 from scipy.fftpack import fft
6
95a39a5e »
2011-03-08 ENH: add __all__ dicts to all scipy.signal submodules.
7 __all__ = ['boxcar', 'triang', 'parzen', 'bohman', 'blackman', 'nuttall',
8 'blackmanharris', 'flattop', 'bartlett', 'hanning', 'barthann',
9 'hamming', 'kaiser', 'gaussian', 'general_gaussian', 'chebwin',
5bdcfea8 »
2011-05-23 BUG: Add 'hann' to __all__ in windows.py (it is an alias for the func…
10 'slepian', 'hann', 'get_window']
95a39a5e »
2011-03-08 ENH: add __all__ dicts to all scipy.signal submodules.
11
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
12
13 def boxcar(M, sym=True):
14 """The M-point boxcar window.
15
16 """
17 return np.ones(M, float)
18
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
19
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
20 def triang(M, sym=True):
21 """The M-point triangular window.
22
23 """
24 if M < 1:
25 return np.array([])
26 if M == 1:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
27 return np.ones(1, 'd')
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
28 odd = M % 2
29 if not sym and not odd:
30 M = M + 1
e96fa5b3 »
2011-05-29 STY: signal: big PEP8 cleanup, plus a BUG FIX: there was a comma miss…
31 n = np.arange(1, int((M + 1) / 2) + 1)
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
32 if M % 2 == 0:
e96fa5b3 »
2011-05-29 STY: signal: big PEP8 cleanup, plus a BUG FIX: there was a comma miss…
33 w = (2 * n - 1.0) / M
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
34 w = np.r_[w, w[::-1]]
35 else:
e96fa5b3 »
2011-05-29 STY: signal: big PEP8 cleanup, plus a BUG FIX: there was a comma miss…
36 w = 2 * n / (M + 1.0)
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
37 w = np.r_[w, w[-2::-1]]
38
39 if not sym and not odd:
40 w = w[:-1]
41 return w
42
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
43
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
44 def parzen(M, sym=True):
45 """The M-point Parzen window.
46
47 """
48 if M < 1:
49 return np.array([])
50 if M == 1:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
51 return np.ones(1, 'd')
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
52 odd = M % 2
53 if not sym and not odd:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
54 M = M + 1
e96fa5b3 »
2011-05-29 STY: signal: big PEP8 cleanup, plus a BUG FIX: there was a comma miss…
55 n = np.arange(-(M - 1) / 2.0, (M - 1) / 2.0 + 0.5, 1.0)
56 na = np.extract(n < -(M - 1) / 4.0, n)
57 nb = np.extract(abs(n) <= (M - 1) / 4.0, n)
58 wa = 2 * (1 - np.abs(na) / (M / 2.0)) ** 3.0
59 wb = (1 - 6 * (np.abs(nb) / (M / 2.0)) ** 2.0 +
60 6 * (np.abs(nb) / (M / 2.0)) ** 3.0)
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
61 w = np.r_[wa, wb, wa[::-1]]
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
62 if not sym and not odd:
63 w = w[:-1]
64 return w
65
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
66
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
67 def bohman(M, sym=True):
68 """The M-point Bohman window.
69
70 """
71 if M < 1:
72 return np.array([])
73 if M == 1:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
74 return np.ones(1, 'd')
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
75 odd = M % 2
76 if not sym and not odd:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
77 M = M + 1
78 fac = np.abs(np.linspace(-1, 1, M)[1:-1])
e96fa5b3 »
2011-05-29 STY: signal: big PEP8 cleanup, plus a BUG FIX: there was a comma miss…
79 w = (1 - fac) * np.cos(np.pi * fac) + 1.0 / np.pi * np.sin(np.pi * fac)
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
80 w = np.r_[0, w, 0]
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
81 if not sym and not odd:
82 w = w[:-1]
83 return w
84
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
85
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
86 def blackman(M, sym=True):
87 """The M-point Blackman window.
88
89 """
90 if M < 1:
91 return np.array([])
92 if M == 1:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
93 return np.ones(1, 'd')
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
94 odd = M % 2
95 if not sym and not odd:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
96 M = M + 1
97 n = np.arange(0, M)
e96fa5b3 »
2011-05-29 STY: signal: big PEP8 cleanup, plus a BUG FIX: there was a comma miss…
98 w = (0.42 - 0.5 * np.cos(2.0 * np.pi * n / (M - 1)) +
99 0.08 * np.cos(4.0 * np.pi * n / (M - 1)))
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
100 if not sym and not odd:
101 w = w[:-1]
102 return w
103
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
104
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
105 def nuttall(M, sym=True):
106 """A minimum 4-term Blackman-Harris window according to Nuttall.
107
108 """
109 if M < 1:
110 return np.array([])
111 if M == 1:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
112 return np.ones(1, 'd')
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
113 odd = M % 2
114 if not sym and not odd:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
115 M = M + 1
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
116 a = [0.3635819, 0.4891775, 0.1365995, 0.0106411]
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
117 n = np.arange(0, M)
e96fa5b3 »
2011-05-29 STY: signal: big PEP8 cleanup, plus a BUG FIX: there was a comma miss…
118 fac = n * 2 * np.pi / (M - 1.0)
119 w = (a[0] - a[1] * np.cos(fac) +
120 a[2] * np.cos(2 * fac) - a[3] * np.cos(3 * fac))
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
121 if not sym and not odd:
122 w = w[:-1]
123 return w
124
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
125
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
126 def blackmanharris(M, sym=True):
127 """The M-point minimum 4-term Blackman-Harris window.
128
129 """
130 if M < 1:
131 return np.array([])
132 if M == 1:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
133 return np.ones(1, 'd')
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
134 odd = M % 2
135 if not sym and not odd:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
136 M = M + 1
137 a = [0.35875, 0.48829, 0.14128, 0.01168]
138 n = np.arange(0, M)
e96fa5b3 »
2011-05-29 STY: signal: big PEP8 cleanup, plus a BUG FIX: there was a comma miss…
139 fac = n * 2 * np.pi / (M - 1.0)
140 w = (a[0] - a[1] * np.cos(fac) +
141 a[2] * np.cos(2 * fac) - a[3] * np.cos(3 * fac))
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
142 if not sym and not odd:
143 w = w[:-1]
144 return w
145
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
146
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
147 def flattop(M, sym=True):
148 """The M-point Flat top window.
149
150 """
151 if M < 1:
152 return np.array([])
153 if M == 1:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
154 return np.ones(1, 'd')
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
155 odd = M % 2
156 if not sym and not odd:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
157 M = M + 1
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
158 a = [0.2156, 0.4160, 0.2781, 0.0836, 0.0069]
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
159 n = np.arange(0, M)
e96fa5b3 »
2011-05-29 STY: signal: big PEP8 cleanup, plus a BUG FIX: there was a comma miss…
160 fac = n * 2 * np.pi / (M - 1.0)
161 w = (a[0] - a[1] * np.cos(fac) +
162 a[2] * np.cos(2 * fac) - a[3] * np.cos(3 * fac) +
163 a[4] * np.cos(4 * fac))
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
164 if not sym and not odd:
165 w = w[:-1]
166 return w
167
168
169 def bartlett(M, sym=True):
170 """The M-point Bartlett window.
171
172 """
173 if M < 1:
174 return np.array([])
175 if M == 1:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
176 return np.ones(1, 'd')
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
177 odd = M % 2
178 if not sym and not odd:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
179 M = M + 1
180 n = np.arange(0, M)
e96fa5b3 »
2011-05-29 STY: signal: big PEP8 cleanup, plus a BUG FIX: there was a comma miss…
181 w = np.where(np.less_equal(n, (M - 1) / 2.0),
182 2.0 * n / (M - 1), 2.0 - 2.0 * n / (M - 1))
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
183 if not sym and not odd:
184 w = w[:-1]
185 return w
186
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
187
3039101b »
2012-05-25 STY: signal: make hann() the function and hanning the alias
188 def hann(M, sym=True):
189 """The M-point Hann window.
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
190
191 """
192 if M < 1:
193 return np.array([])
194 if M == 1:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
195 return np.ones(1, 'd')
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
196 odd = M % 2
197 if not sym and not odd:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
198 M = M + 1
199 n = np.arange(0, M)
e96fa5b3 »
2011-05-29 STY: signal: big PEP8 cleanup, plus a BUG FIX: there was a comma miss…
200 w = 0.5 - 0.5 * np.cos(2.0 * np.pi * n / (M - 1))
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
201 if not sym and not odd:
202 w = w[:-1]
203 return w
204
3039101b »
2012-05-25 STY: signal: make hann() the function and hanning the alias
205 hanning = hann
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
206
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
207
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
208 def barthann(M, sym=True):
209 """Return the M-point modified Bartlett-Hann window.
210
211 """
212 if M < 1:
213 return np.array([])
214 if M == 1:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
215 return np.ones(1, 'd')
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
216 odd = M % 2
217 if not sym and not odd:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
218 M = M + 1
219 n = np.arange(0, M)
e96fa5b3 »
2011-05-29 STY: signal: big PEP8 cleanup, plus a BUG FIX: there was a comma miss…
220 fac = np.abs(n / (M - 1.0) - 0.5)
221 w = 0.62 - 0.48 * fac + 0.38 * np.cos(2 * np.pi * fac)
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
222 if not sym and not odd:
223 w = w[:-1]
224 return w
225
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
226
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
227 def hamming(M, sym=True):
228 """The M-point Hamming window.
229
230 """
231 if M < 1:
232 return np.array([])
233 if M == 1:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
234 return np.ones(1, 'd')
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
235 odd = M % 2
236 if not sym and not odd:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
237 M = M + 1
238 n = np.arange(0, M)
e96fa5b3 »
2011-05-29 STY: signal: big PEP8 cleanup, plus a BUG FIX: there was a comma miss…
239 w = 0.54 - 0.46 * np.cos(2.0 * np.pi * n / (M - 1))
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
240 if not sym and not odd:
241 w = w[:-1]
242 return w
243
244
245 def kaiser(M, beta, sym=True):
246 """Return a Kaiser window of length M with shape parameter beta.
247
248 """
249 if M < 1:
250 return np.array([])
251 if M == 1:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
252 return np.ones(1, 'd')
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
253 odd = M % 2
254 if not sym and not odd:
255 M = M + 1
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
256 n = np.arange(0, M)
e96fa5b3 »
2011-05-29 STY: signal: big PEP8 cleanup, plus a BUG FIX: there was a comma miss…
257 alpha = (M - 1) / 2.0
258 w = (special.i0(beta * np.sqrt(1 - ((n - alpha) / alpha) ** 2.0)) /
259 special.i0(beta))
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
260 if not sym and not odd:
261 w = w[:-1]
262 return w
263
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
264
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
265 def gaussian(M, std, sym=True):
266 """Return a Gaussian window of length M with standard-deviation std.
267
268 """
269 if M < 1:
270 return np.array([])
271 if M == 1:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
272 return np.ones(1, 'd')
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
273 odd = M % 2
274 if not sym and not odd:
275 M = M + 1
e96fa5b3 »
2011-05-29 STY: signal: big PEP8 cleanup, plus a BUG FIX: there was a comma miss…
276 n = np.arange(0, M) - (M - 1.0) / 2.0
277 sig2 = 2 * std * std
278 w = np.exp(-n ** 2 / sig2)
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
279 if not sym and not odd:
280 w = w[:-1]
281 return w
282
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
283
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
284 def general_gaussian(M, p, sig, sym=True):
285 """Return a window with a generalized Gaussian shape.
286
3d4e40f2 »
2012-05-31 add abs() to generalized Gaussian so it works for values like p = 0.5…
287 The Gaussian shape is defined as ``exp(-0.5*abs(x/sig)**(2*p))``, the
be46c83c »
2010-09-23 DOC: make docs of signal.windows/waveforms/wavelets conform to doc st…
288 half-power point is at ``(2*log(2)))**(1/(2*p)) * sig``.
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
289
290 """
291 if M < 1:
292 return np.array([])
293 if M == 1:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
294 return np.ones(1, 'd')
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
295 odd = M % 2
296 if not sym and not odd:
297 M = M + 1
e96fa5b3 »
2011-05-29 STY: signal: big PEP8 cleanup, plus a BUG FIX: there was a comma miss…
298 n = np.arange(0, M) - (M - 1.0) / 2.0
3d4e40f2 »
2012-05-31 add abs() to generalized Gaussian so it works for values like p = 0.5…
299 w = np.exp(-0.5 * np.abs(n / sig) ** (2 * p))
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
300 if not sym and not odd:
301 w = w[:-1]
302 return w
303
304
305 # `chebwin` contributed by Kumar Appaiah.
306
307 def chebwin(M, at, sym=True):
308 """Dolph-Chebyshev window.
309
be46c83c »
2010-09-23 DOC: make docs of signal.windows/waveforms/wavelets conform to doc st…
310 Parameters
311 ----------
312 M : int
313 Window size.
314 at : float
315 Attenuation (in dB).
316 sym : bool
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
317 Generates symmetric window if True.
318
319 """
320 if M < 1:
321 return np.array([])
322 if M == 1:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
323 return np.ones(1, 'd')
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
324
325 odd = M % 2
326 if not sym and not odd:
327 M = M + 1
328
329 # compute the parameter beta
330 order = M - 1.0
e96fa5b3 »
2011-05-29 STY: signal: big PEP8 cleanup, plus a BUG FIX: there was a comma miss…
331 beta = np.cosh(1.0 / order * np.arccosh(10 ** (np.abs(at) / 20.)))
332 k = np.r_[0:M] * 1.0
333 x = beta * np.cos(np.pi * k / M)
334 # Find the window's DFT coefficients
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
335 # Use analytic definition of Chebyshev polynomial instead of expansion
336 # from scipy.special. Using the expansion in scipy.special leads to errors.
337 p = np.zeros(x.shape)
338 p[x > 1] = np.cosh(order * np.arccosh(x[x > 1]))
e96fa5b3 »
2011-05-29 STY: signal: big PEP8 cleanup, plus a BUG FIX: there was a comma miss…
339 p[x < -1] = (1 - 2 * (order % 2)) * np.cosh(order * np.arccosh(-x[x < -1]))
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
340 p[np.abs(x) <= 1] = np.cos(order * np.arccos(x[np.abs(x) <= 1]))
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
341
342 # Appropriate IDFT and filling up
343 # depending on even/odd M
344 if M % 2:
345 w = np.real(fft(p))
346 n = (M + 1) / 2
347 w = w[:n] / w[0]
348 w = np.concatenate((w[n - 1:0:-1], w))
349 else:
e96fa5b3 »
2011-05-29 STY: signal: big PEP8 cleanup, plus a BUG FIX: there was a comma miss…
350 p = p * np.exp(1.j * np.pi / M * np.r_[0:M])
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
351 w = np.real(fft(p))
352 n = M / 2 + 1
353 w = w / w[1]
354 w = np.concatenate((w[n - 1:0:-1], w[1:n]))
355 if not sym and not odd:
356 w = w[:-1]
357 return w
358
359
360 def slepian(M, width, sym=True):
361 """Return the M-point slepian window.
362
363 """
e96fa5b3 »
2011-05-29 STY: signal: big PEP8 cleanup, plus a BUG FIX: there was a comma miss…
364 if (M * width > 27.38):
0d11a0cd »
2010-11-02 Remove an unused import. Update 'raise' syntax.
365 raise ValueError("Cannot reliably obtain slepian sequences for"
366 " M*width > 27.38.")
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
367 if M < 1:
368 return np.array([])
369 if M == 1:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
370 return np.ones(1, 'd')
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
371 odd = M % 2
372 if not sym and not odd:
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
373 M = M + 1
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
374
e96fa5b3 »
2011-05-29 STY: signal: big PEP8 cleanup, plus a BUG FIX: there was a comma miss…
375 twoF = width / 2.0
376 alpha = (M - 1) / 2.0
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
377 m = np.arange(0, M) - alpha
378 n = m[:, np.newaxis]
379 k = m[np.newaxis, :]
e96fa5b3 »
2011-05-29 STY: signal: big PEP8 cleanup, plus a BUG FIX: there was a comma miss…
380 AF = twoF * special.sinc(twoF * (n - k))
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
381 [lam, vec] = linalg.eig(AF)
382 ind = np.argmax(abs(lam), axis=-1)
383 w = np.abs(vec[:, ind])
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
384 w = w / max(w)
385
386 if not sym and not odd:
387 w = w[:-1]
388 return w
389
390
391 def get_window(window, Nx, fftbins=True):
bd16e5cd »
2010-09-02 DOC: merge doc wiki edits for scipy.signal.
392 """
393 Return a window of length `Nx` and type `window`.
394
395 Parameters
396 ----------
397 window : string, float, or tuple
398 The type of window to create. See below for more details.
399 Nx : int
400 The number of samples in the window.
401 fftbins : bool, optional
402 If True, create a "periodic" window ready to use with ifftshift
403 and be multiplied by the result of an fft (SEE ALSO fftfreq).
404
405 Notes
406 -----
407 Window types:
408
409 boxcar, triang, blackman, hamming, hanning, bartlett,
410 parzen, bohman, blackmanharris, nuttall, barthann,
411 kaiser (needs beta), gaussian (needs std),
412 general_gaussian (needs power, width),
413 slepian (needs width), chebwin (needs attenuation)
414
415
416 If the window requires no parameters, then `window` can be a string.
417
418 If the window requires parameters, then `window` must be a tuple
419 with the first argument the string name of the window, and the next
420 arguments the needed parameters.
421
422 If `window` is a floating point number, it is interpreted as the beta
423 parameter of the kaiser window.
424
425 Each of the window types listed above is also the name of
426 a function that can be called directly to create a window of
427 that type.
428
429 Examples
430 --------
431 >>> get_window('triang', 7)
432 array([ 0.25, 0.5 , 0.75, 1. , 0.75, 0.5 , 0.25])
433 >>> get_window(('kaiser', 4.0), 9)
434 array([ 0.08848053, 0.32578323, 0.63343178, 0.89640418, 1. ,
435 0.89640418, 0.63343178, 0.32578323, 0.08848053])
436 >>> get_window(4.0, 9)
437 array([ 0.08848053, 0.32578323, 0.63343178, 0.89640418, 1. ,
438 0.89640418, 0.63343178, 0.32578323, 0.08848053])
439
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
440 """
441
442 sym = not fftbins
443 try:
444 beta = float(window)
445 except (TypeError, ValueError):
446 args = ()
4d39a088 »
2010-09-12 3K: signal: fix types.* issues
447 if isinstance(window, tuple):
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
448 winstr = window[0]
449 if len(window) > 1:
450 args = window[1:]
4d39a088 »
2010-09-12 3K: signal: fix types.* issues
451 elif isinstance(window, str):
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
452 if window in ['kaiser', 'ksr', 'gaussian', 'gauss', 'gss',
453 'general gaussian', 'general_gaussian',
454 'general gauss', 'general_gauss', 'ggs',
0ac6ae80 »
2010-04-06 ENH: signal: Added 'chebwin' to the windows understood by get_window();
455 'slepian', 'optimal', 'slep', 'dss',
456 'chebwin', 'cheb']:
457 raise ValueError("The '" + window + "' window needs one or "
458 "more parameters -- pass a tuple.")
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
459 else:
460 winstr = window
461
462 if winstr in ['blackman', 'black', 'blk']:
463 winfunc = blackman
464 elif winstr in ['triangle', 'triang', 'tri']:
465 winfunc = triang
466 elif winstr in ['hamming', 'hamm', 'ham']:
467 winfunc = hamming
468 elif winstr in ['bartlett', 'bart', 'brt']:
469 winfunc = bartlett
470 elif winstr in ['hanning', 'hann', 'han']:
471 winfunc = hanning
3a8dd806 »
2011-05-21 STY: signal: a bit of whitespace cleanup
472 elif winstr in ['blackmanharris', 'blackharr', 'bkh']:
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
473 winfunc = blackmanharris
474 elif winstr in ['parzen', 'parz', 'par']:
475 winfunc = parzen
476 elif winstr in ['bohman', 'bman', 'bmn']:
477 winfunc = bohman
478 elif winstr in ['nuttall', 'nutl', 'nut']:
479 winfunc = nuttall
480 elif winstr in ['barthann', 'brthan', 'bth']:
481 winfunc = barthann
482 elif winstr in ['flattop', 'flat', 'flt']:
483 winfunc = flattop
484 elif winstr in ['kaiser', 'ksr']:
485 winfunc = kaiser
486 elif winstr in ['gaussian', 'gauss', 'gss']:
487 winfunc = gaussian
488 elif winstr in ['general gaussian', 'general_gaussian',
489 'general gauss', 'general_gauss', 'ggs']:
490 winfunc = general_gaussian
491 elif winstr in ['boxcar', 'box', 'ones']:
492 winfunc = boxcar
493 elif winstr in ['slepian', 'slep', 'optimal', 'dss']:
494 winfunc = slepian
0ac6ae80 »
2010-04-06 ENH: signal: Added 'chebwin' to the windows understood by get_window();
495 elif winstr in ['chebwin', 'cheb']:
496 winfunc = chebwin
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
497 else:
0d11a0cd »
2010-11-02 Remove an unused import. Update 'raise' syntax.
498 raise ValueError("Unknown window type.")
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
499
0ac6ae80 »
2010-04-06 ENH: signal: Added 'chebwin' to the windows understood by get_window();
500 params = (Nx,) + args + (sym,)
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
501 else:
502 winfunc = kaiser
0ac6ae80 »
2010-04-06 ENH: signal: Added 'chebwin' to the windows understood by get_window();
503 params = (Nx, beta, sym)
e4d5a37e »
2010-04-06 REF: signal: Moved the windows to their own file.
504
505 return winfunc(*params)
Something went wrong with that request. Please try again.