Debian Codegraph @

demo/cwt_analysis.py

raw symbols SCIP
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import numpy as np
5 import matplotlib.pyplot as plt
6
7 import pywt
8
9 time, sst = pywt.data.nino()
10 dt = time[1] - time[0]
11
12 # Taken from http://nicolasfauchereau.github.io/climatecode/posts/wavelet-analysis-in-python/
13 wavelet = 'cmor1.5-1.0'
14 scales = np.arange(1, 128)
15
16 [cfs, frequencies] = pywt.cwt(sst, scales, wavelet, dt)
17 power = (abs(cfs)) ** 2
18
19 period = 1. / frequencies
20 levels = [0.0625, 0.125, 0.25, 0.5, 1, 2, 4, 8]
21 f, ax = plt.subplots(figsize=(15, 10))
22 ax.contourf(time, np.log2(period), np.log2(power), np.log2(levels),
23 extend='both')
24
25 ax.set_title('%s Wavelet Power Spectrum (%s)' % ('Nino1+2', wavelet))
26 ax.set_ylabel('Period (years)')
27 Yticks = 2 ** np.arange(np.ceil(np.log2(period.min())),
28 np.ceil(np.log2(period.max())))
29 ax.set_yticks(np.log2(Yticks))
30 ax.set_yticklabels(Yticks)
31 ax.invert_yaxis()
32 ylim = ax.get_ylim()
33 ax.set_ylim(ylim[0], -1)
34
35 plt.show()