Reference
Python module for continuous wavelet spectral analysis.
This module includes a collection of routines for wavelet transform and statistical analysis via FFT algorithm. In addition, the module also includes cross-wavelet transforms, wavelet coherence tests and sample scripts.
Functions
- cwt : Continuous wavelet transform.
- icwt: Inverse continuous wavelet transform.
- significance : Significance test for the one dimensional wavelet transform.
- xwt : cross-wavelet transform.
- wct : Wavelet coherence transform.
- wct_significance : Wavelet coherence significance using Monte Carlo simulations.
Classes
- Morlet : Morlet wavelet
- Paul : Paul wavelet
- DOG : Derivative of a Guassian wavelet family
- MexicanHat : Mexican hat wavelet
Disclaimer
This module is based on routines provided by C. Torrence and G. P. Compo available at http://paos.colorado.edu/research/wavelets/, on routines provided by A. Grinsted, J. Moore and S. Jevrejeva available at http://noc.ac.uk/using-science/crosswavelet-wavelet-coherence, and on routines provided by A. Brazhe available at http://cell.biophys.msu.ru/static/swan/.
This software is released under a BSD-style open source license. Please read the license file for further information. This routine is provided as is without any express or implied warranties whatsoever.
Acknowledgements
We would like to thank Christopher Torrence, Gilbert P. Compo, Aslak Grinsted, John Moore, Svetlana Jevrejevaand and Alexey Brazhe for their code and also Jack Ireland and Renaud Dussurget for their attentive eyes, feedback and debugging.
Authors
Sebastian Krieger, Nabil Freij, Alexey Brazhe, Christopher Torrence, Gilbert P. Compo and contributors.
References
- Torrence, C. and Compo, G. P.. A Practical Guide to Wavelet Analysis. Bulletin of the American Meteorological Society, American Meteorological Society, 1998, 79, 61-78. http://dx.doi.org/10.1175/1520-0477(1998)079\<0061:APGTWA>2.0.CO;2>
- Torrence, C. and Webster, P. J.. Interdecadal changes in the ENSO-Monsoon system, Journal of Climate, 1999, 12(8), 2679-2690. http://dx.doi.org/10.1175/1520-0442(1999)012\<2679:ICITEM>2.0.CO;2>
- Grinsted, A.; Moore, J. C. & Jevrejeva, S. Application of the cross wavelet transform and wavelet coherence to geophysical time series. Nonlinear Processes in Geophysics, 2004, 11, 561-566. http://dx.doi.org/10.5194/npg-11-561-2004
- Mallat, S.. A wavelet tour of signal processing: The sparse way. Academic Press, 2008, 805.
- Addison, P. S. The illustrated wavelet transform handbook: introductory theory and applications in science, engineering, medicine and finance. IOP Publishing, 2002. http://dx.doi.org/10.1201/9781420033397
- Liu, Y., Liang, X. S. and Weisberg, R. H. Rectification of the bias in the wavelet power spectrum. Journal of Atmospheric and Oceanic Technology, 2007, 24, 2093-2102. http://dx.doi.org/10.1175/2007JTECHO511.1
DOG
Bases: object
Implements the derivative of a Guassian wavelet class.
Note that the input parameter f is the angular frequency and that for m=2 the DOG becomes the Mexican hat wavelet, which is then default.
Source code in pycwt/mothers.py
coi()
flambda()
psi(t)
DOG wavelet as described in Torrence and Compo (1998).
The derivative of a Gaussian of order n
can be determined using
the probabilistic Hermite polynomials. They are explicitly
written as:
Hn(x) = 2 ** (-n / s) * n! * sum ((-1) ** m) *
(2 ** 0.5 * x) ** (n - 2 * m) / (m! * (n - 2*m)!)
or in the recursive form:
Hn(x) = x * Hn(x) - nHn-1(x)
Source: http://www.ask.com/wiki/Hermite_polynomials
Source code in pycwt/mothers.py
psi_ft(f)
MexicanHat
Bases: DOG
Implements the Mexican hat wavelet class.
This class inherits the DOG class using m=2.
Source code in pycwt/mothers.py
Morlet
Bases: object
Implements the Morlet wavelet class.
Note that the input parameters f and f0 are angular frequencies. f0 should be more than 0.8 for this function to be correct, its default value is f0 = 6.
Source code in pycwt/mothers.py
coi()
flambda()
psi(t)
psi_ft(f)
smooth(W, dt, dj, scales)
Smoothing function used in coherence analysis.
Parameters
W : dt : dj : scales :
Returns
T :
Source code in pycwt/mothers.py
Paul
Bases: object
Implements the Paul wavelet class.
Note that the input parameter f is the angular frequency and that the default order for this wavelet is m=4.
Source code in pycwt/mothers.py
coi()
flambda()
psi(t)
Paul wavelet as described in Torrence and Compo (1998).
psi_ft(f)
cwt(signal, dt, dj=1 / 12, s0=-1, J=-1, wavelet='morlet', freqs=None)
Continuous wavelet transform of the signal at specified scales.
Parameters
signal : numpy.ndarray, list Input signal array. dt : float Sampling interval. dj : float, optional Spacing between discrete scales. Default value is 1/12. Smaller values will result in better scale resolution, but slower calculation and plot. s0 : float, optional Smallest scale of the wavelet. Default value is 2dt. J : float, optional Number of scales less one. Scales range from s0 up to s0 * 2*(J * dj), which gives a total of (J + 1) scales. Default is J = (log2(N * dt / so)) / dj. wavelet : instance of Wavelet class, or string Mother wavelet class. Default is Morlet wavelet. freqs : numpy.ndarray, optional Custom frequencies to use instead of the ones corresponding to the scales described above. Corresponding scales are calculated using the wavelet Fourier wavelength.
Returns
W : numpy.ndarray Wavelet transform according to the selected mother wavelet. Has (J+1) x N dimensions. sj : numpy.ndarray Vector of scale indices given by sj = s0 * 2**(j * dj), j={0, 1, ..., J}. freqs : array like Vector of Fourier frequencies (in 1 / time units) that corresponds to the wavelet scales. coi : numpy.ndarray Returns the cone of influence, which is a vector of N points containing the maximum Fourier period of useful information at that particular time. Periods greater than those are subject to edge effects. fft : numpy.ndarray Normalized fast Fourier transform of the input signal. fftfreqs : numpy.ndarray Fourier frequencies (in 1/time units) for the calculated FFT spectrum.
Example
mother = wavelet.Morlet(6.) wave, scales, freqs, coi, fft, fftfreqs = wavelet.cwt(signal, 0.25, 0.25, 0.5, 28, mother)
Source code in pycwt/wavelet.py
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 |
|
icwt(W, sj, dt, dj=1 / 12, wavelet='morlet')
Inverse continuous wavelet transform.
Parameters
W : numpy.ndarray
Wavelet transform, the result of the cwt
function.
sj : numpy.ndarray
Vector of scale indices as returned by the cwt
function.
dt : float
Sample spacing.
dj : float, optional
Spacing between discrete scales as used in the cwt
function. Default value is 0.25.
wavelet : instance of Wavelet class, or string
Mother wavelet class. Default is Morlet
Returns
iW : numpy.ndarray Inverse wavelet transform.
Example
mother = wavelet.Morlet() wave, scales, freqs, coi, fft, fftfreqs = wavelet.cwt(var, 0.25, 0.25, 0.5, 28, mother) iwave = wavelet.icwt(wave, scales, 0.25, 0.25, mother)
Source code in pycwt/wavelet.py
significance(signal, dt, scales, sigma_test=0, alpha=None, significance_level=0.95, dof=-1, wavelet='morlet')
Significance test for the one dimensional wavelet transform.
Parameters
signal : array like, float
Input signal array. If a float number is given, then the
variance is assumed to have this value. If an array is
given, then its variance is automatically computed.
dt : float
Sample spacing.
scales : array like
Vector of scale indices given returned by cwt
function.
sigma_test : int, optional
Sets the type of significance test to be performed.
Accepted values are 0 (default), 1 or 2. See notes below for
further details.
alpha : float, optional
Lag-1 autocorrelation, used for the significance levels.
Default is 0.0.
significance_level : float, optional
Significance level to use. Default is 0.95.
dof : variant, optional
Degrees of freedom for significance test to be set
according to the type set in sigma_test.
wavelet : instance of Wavelet class, or string
Mother wavelet class. Default is Morlet
Returns
signif : array like Significance levels as a function of scale. fft_theor (array like): Theoretical red-noise spectrum as a function of period.
Notes
If sigma_test is set to 0, performs a regular chi-square test, according to Torrence and Compo (1998) equation 18.
If set to 1, performs a time-average test (equation 23). In this case, dof should be set to the number of local wavelet spectra that where averaged together. For the global wavelet spectra it would be dof=N, the number of points in the time-series.
If set to 2, performs a scale-average test (equations 25 to 28). In this case dof should be set to a two element vector [s1, s2], which gives the scale range that were averaged together. If, for example, the average between scales 2 and 8 was taken, then dof=[2, 8].
Source code in pycwt/wavelet.py
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 |
|
wct(y1, y2, dt, dj=1 / 12, s0=-1, J=-1, sig=True, significance_level=0.95, wavelet='morlet', normalize=True, **kwargs)
Wavelet coherence transform (WCT).
The WCT finds regions in time frequency space where the two time series co-vary, but do not necessarily have high power.
Parameters
y1, y2 : numpy.ndarray, list Input signals. dt : float Sample spacing. dj : float, optional Spacing between discrete scales. Default value is 1/12. Smaller values will result in better scale resolution, but slower calculation and plot. s0 : float, optional Smallest scale of the wavelet. Default value is 2dt. J : float, optional Number of scales less one. Scales range from s0 up to s0 * 2(J * dj), which gives a total of (J + 1) scales. Default is J = (log2(Ndt/so))/dj. sig : bool set to compute signficance, default is True significance_level (float, optional) : Significance level to use. Default is 0.95. normalize (boolean, optional) : If set to true, normalizes CWT by the standard deviation of the signals.
Returns
WCT : magnitude of coherence
aWCT : phase angle of coherence
coi (array like):
Cone of influence, which is a vector of N points containing
the maximum Fourier period of useful information at that
particular time. Periods greater than those are subject to
edge effects.
freq (array like):
Vector of Fourier equivalent frequencies (in 1 / time units) coi :
sig : Significance levels as a function of scale
if sig=True when called, otherwise zero.
See also
cwt, xwt
Source code in pycwt/wavelet.py
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 |
|
wct_significance(al1, al2, dt, dj, s0, J, significance_level=0.95, wavelet='morlet', mc_count=300, progress=True, cache=True)
Wavelet coherence transform significance.
Calculates WCT significance using Monte Carlo simulations with 95% confidence.
Parameters
al1, al2: float
Lag-1 autoregressive coeficients of both time series.
dt : float
Sample spacing.
dj : float, optional
Spacing between discrete scales. Default value is 1/12.
Smaller values will result in better scale resolution, but
slower calculation and plot.
s0 : float, optional
Smallest scale of the wavelet. Default value is 2dt.
J : float, optional
Number of scales less one. Scales range from s0 up to
s0 * 2(J * dj), which gives a total of (J + 1) scales.
Default is J = (log2(Ndt/so))/dj.
significance_level : float, optional
Significance level to use. Default is 0.95.
wavelet : instance of a wavelet class, optional
Mother wavelet class. Default is Morlet wavelet.
mc_count : integer, optional
Number of Monte Carlo simulations. Default is 300.
progress : bool, optional
If True
(default), shows progress bar on screen.
cache : bool, optional
If True
(default) saves cache to file.
Returns
TODO
Source code in pycwt/wavelet.py
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 |
|
xwt(y1, y2, dt, dj=1 / 12, s0=-1, J=-1, significance_level=0.95, wavelet='morlet', normalize=True)
Cross wavelet transform (XWT) of two signals.
The XWT finds regions in time frequency space where the time series show high common power.
Parameters
y1, y2 : numpy.ndarray, list Input signal array to calculate cross wavelet transform. dt : float Sample spacing. dj : float, optional Spacing between discrete scales. Default value is 1/12. Smaller values will result in better scale resolution, but slower calculation and plot. s0 : float, optional Smallest scale of the wavelet. Default value is 2dt. J : float, optional Number of scales less one. Scales range from s0 up to s0 * 2(J * dj), which gives a total of (J + 1) scales. Default is J = (log2(Ndt/so))/dj. wavelet : instance of a wavelet class, optional Mother wavelet class. Default is Morlet wavelet. significance_level : float, optional Significance level to use. Default is 0.95. normalize : bool, optional If set to true, normalizes CWT by the standard deviation of the signals.
Returns
xwt (array like): Cross wavelet transform according to the selected mother wavelet. x (array like): Intersected independent variable. coi (array like): Cone of influence, which is a vector of N points containing the maximum Fourier period of useful information at that particular time. Periods greater than those are subject to edge effects. freqs (array like): Vector of Fourier equivalent frequencies (in 1 / time units) that correspond to the wavelet scales. signif (array like): Significance levels as a function of scale.
Notes
Torrence and Compo (1998) state that the percent point function (PPF) -- inverse of the cumulative distribution function -- of a chi-square distribution at 95% confidence and two degrees of freedom is Z2(95%)=3.999. However, calculating the PPF using chi2.ppf gives Z2(95%)=5.991. To ensure similar significance intervals as in Grinsted et al. (2004), one has to use confidence of 86.46%.
Source code in pycwt/wavelet.py
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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
|