/
wavelets.py
367 lines (279 loc) · 9.91 KB
/
wavelets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
from __future__ import division
import numpy as np
import scipy
import scipy.signal
import scipy.optimize
import scipy.special
from scipy.misc import factorial
__all__ = ['Morlet', 'Paul', 'DOG', 'Ricker', 'Marr', 'Mexican_hat']
class Morlet(object):
def __init__(self, w0=6):
"""w0 is the nondimensional frequency constant. If this is
set too low then the wavelet does not sample very well: a
value over 5 should be ok; Terrence and Compo set it to 6.
"""
self.w0 = w0
if w0 == 6:
# value of C_d from TC98
self.C_d = 0.776
def __call__(self, *args, **kwargs):
return self.time(*args, **kwargs)
def time(self, t, s=1.0, complete=True):
"""
Complex Morlet wavelet, centred at zero.
Parameters
----------
t : float
Time. If s is not specified, this can be used as the
non-dimensional time t/s.
s : float
Scaling factor. Default is 1.
complete : bool
Whether to use the complete or the standard version.
Returns
-------
out : complex
Value of the Morlet wavelet at the given time
See Also
--------
scipy.signal.gausspulse
Notes
-----
The standard version::
pi**-0.25 * exp(1j*w*x) * exp(-0.5*(x**2))
This commonly used wavelet is often referred to simply as the
Morlet wavelet. Note that this simplified version can cause
admissibility problems at low values of `w`.
The complete version::
pi**-0.25 * (exp(1j*w*x) - exp(-0.5*(w**2))) * exp(-0.5*(x**2))
The complete version of the Morlet wavelet, with a correction
term to improve admissibility. For `w` greater than 5, the
correction term is negligible.
Note that the energy of the return wavelet is not normalised
according to `s`.
The fundamental frequency of this wavelet in Hz is given
by ``f = 2*s*w*r / M`` where r is the sampling rate.
"""
w = self.w0
x = t / s
output = np.exp(1j * w * x)
if complete:
output -= np.exp(-0.5 * (w ** 2))
output *= np.exp(-0.5 * (x ** 2)) * np.pi ** (-0.25)
return output
# Fourier wavelengths
def fourier_period(self, s):
"""Equivalent Fourier period of Morlet"""
return 4 * np.pi * s / (self.w0 + (2 + self.w0 ** 2) ** .5)
def scale_from_period(self, period):
"""
Compute the scale from the fourier period.
Returns the scale
"""
# Solve 4 * np.pi * scale / (w0 + (2 + w0 ** 2) ** .5)
# for s to obtain this formula
coeff = np.sqrt(self.w0 * self.w0 + 2)
return (period * (coeff + self.w0)) / (4. * np.pi)
# Frequency representation
def frequency(self, w, s=1.0):
"""Frequency representation of Morlet.
Parameters
----------
w : float
Angular frequency. If `s` is not specified, i.e. set to 1,
this can be used as the non-dimensional angular
frequency w * s.
s : float
Scaling factor. Default is 1.
Returns
-------
out : complex
Value of the Morlet wavelet at the given frequency
"""
x = w * s
# Heaviside mock
Hw = np.array(w)
Hw[w <= 0] = 0
Hw[w > 0] = 1
return np.pi ** -.25 * Hw * np.exp((-(x - self.w0) ** 2) / 2)
def coi(self, s):
"""The e folding time for the autocorrelation of wavelet
power at each scale, i.e. the timescale over which an edge
effect decays by a factor of 1/e^2.
This can be worked out analytically by solving
|Y_0(T)|^2 / |Y_0(0)|^2 = 1 / e^2
"""
return 2 ** .5 * s
class Paul(object):
def __init__(self, m=4):
"""Initialise a Paul wavelet function of order `m`.
"""
self.m = m
def __call__(self, *args, **kwargs):
return self.time(*args, **kwargs)
def time(self, t, s=1.0):
"""
Complex Paul wavelet, centred at zero.
Parameters
----------
t : float
Time. If `s` is not specified, i.e. set to 1, this can be
used as the non-dimensional time t/s.
s : float
Scaling factor. Default is 1.
Returns
-------
out : complex
Value of the Paul wavelet at the given time
The Paul wavelet is defined (in time) as::
(2 ** m * i ** m * m!) / (pi * (2 * m)!) \
* (1 - i * t / s) ** -(m + 1)
"""
m = self.m
x = t / s
const = (2 ** m * 1j ** m * factorial(m)) \
/ (np.pi * factorial(2 * m)) ** .5
functional_form = (1 - 1j * x) ** -(m + 1)
output = const * functional_form
return output
# Fourier wavelengths
def fourier_period(self, s):
"""Equivalent Fourier period of Paul"""
return 4 * np.pi * s / (2 * self.m + 1)
def scale_from_period(self, period):
raise NotImplementedError()
# Frequency representation
def frequency(self, w, s=1.0):
"""Frequency representation of Paul.
Parameters
----------
w : float
Angular frequency. If `s` is not specified, i.e. set to 1,
this can be used as the non-dimensional angular
frequency w * s.
s : float
Scaling factor. Default is 1.
Returns
-------
out : complex
Value of the Paul wavelet at the given frequency
"""
m = self.m
x = w * s
# Heaviside mock
Hw = 0.5 * (np.sign(x) + 1)
# prefactor
const = 2 ** m / (m * factorial(2 * m - 1)) ** .5
functional_form = Hw * (x) ** m * np.exp(-x)
output = const * functional_form
return output
def coi(self, s):
"""The e folding time for the autocorrelation of wavelet
power at each scale, i.e. the timescale over which an edge
effect decays by a factor of 1/e^2.
This can be worked out analytically by solving
|Y_0(T)|^2 / |Y_0(0)|^2 = 1 / e^2
"""
return s / 2 ** .5
class DOG(object):
def __init__(self, m=2):
"""Initialise a Derivative of Gaussian wavelet of order `m`."""
if m == 2:
# value of C_d from TC98
self.C_d = 3.541
elif m == 6:
self.C_d = 1.966
else:
pass
self.m = m
def __call__(self, *args, **kwargs):
return self.time(*args, **kwargs)
def time(self, t, s=1.0):
"""
Return a Derivative of Gaussian wavelet,
When m = 2, this is also known as the "Mexican hat", "Marr"
or "Ricker" wavelet.
It models the function::
``A d^m/dx^m exp(-x^2 / 2)``,
where ``A = (-1)^(m+1) / (gamma(m + 1/2))^.5``
and ``x = t / s``.
Note that the energy of the return wavelet is not normalised
according to `s`.
Parameters
----------
t : float
Time. If `s` is not specified, this can be used as the
non-dimensional time t/s.
s : scalar
Width parameter of the wavelet.
Returns
-------
out : float
Value of the DOG wavelet at the given time
Notes
-----
The derivative of the Gaussian has a polynomial representation:
from http://en.wikipedia.org/wiki/Gaussian_function:
"Mathematically, the derivatives of the Gaussian function can be
represented using Hermite functions. The n-th derivative of the
Gaussian is the Gaussian function itself multiplied by the n-th
Hermite polynomial, up to scale."
http://en.wikipedia.org/wiki/Hermite_polynomial
Here, we want the 'probabilists' Hermite polynomial (He_n),
which is computed by scipy.special.hermitenorm
"""
x = t / s
m = self.m
# compute the Hermite polynomial (used to evaluate the
# derivative of a Gaussian)
He_n = scipy.special.hermitenorm(m)
gamma = scipy.special.gamma
const = (-1) ** (m + 1) / gamma(m + 0.5) ** .5
function = He_n(x) * np.exp(-x ** 2 / 2)
return const * function
def fourier_period(self, s):
"""Equivalent Fourier period of derivative of Gaussian"""
return 2 * np.pi * s / (self.m + 0.5) ** .5
def scale_from_period(self, period):
raise NotImplementedError()
def frequency(self, w, s=1.0):
"""Frequency representation of derivative of Gaussian.
Parameters
----------
w : float
Angular frequency. If `s` is not specified, i.e. set to 1,
this can be used as the non-dimensional angular
frequency w * s.
s : float
Scaling factor. Default is 1.
Returns
-------
out : complex
Value of the derivative of Gaussian wavelet at the
given time
"""
m = self.m
x = s * w
gamma = scipy.special.gamma
const = -1j ** m / gamma(m + 0.5) ** .5
function = x ** m * np.exp(-x ** 2 / 2)
return const * function
def coi(self, s):
"""The e folding time for the autocorrelation of wavelet
power at each scale, i.e. the timescale over which an edge
effect decays by a factor of 1/e^2.
This can be worked out analytically by solving
|Y_0(T)|^2 / |Y_0(0)|^2 = 1 / e^2
"""
return 2 ** .5 * s
class Ricker(DOG):
def __init__(self):
"""The Ricker, aka Marr / Mexican Hat, wavelet is a
derivative of Gaussian order 2.
"""
DOG.__init__(self, m=2)
# value of C_d from TC98
self.C_d = 3.541
# aliases for DOG2
Marr = Ricker
Mexican_hat = Ricker