Debian Codegraph @

demo/mra_vs_swt.py

raw symbols SCIP
1 #!/usr/bin/env python
2
3 import numpy as np
4 import matplotlib.pyplot as plt
5
6 import pywt
7 import pywt.data
8
9 ecg = pywt.data.ecg()
10
11 wavelet = pywt.Wavelet('db8') #'db16')
12 level = 4
13 coeffs = pywt.mra(ecg, wavelet=wavelet, level=level)
14 ca = coeffs[0]
15 details = coeffs[1:]
16
17 # Create a plot using the same axis limits for all coefficient arrays to
18 # illustrate the preservation of alignment across decomposition levels.
19 ylim = [ecg.min(), ecg.max()]
20
21 def mark_peaks(ax):
22 # add dashed lines at the locations of the ECG peaks for reference
23 ylim = ax.get_ylim()
24 ax.plot([190, 190], ylim, 'k--')
25 ax.plot([518, 518], ylim, 'k--')
26 ax.plot([848, 848], ylim, 'k--')
27
28 fig, axes = plt.subplots(len(coeffs) + 1, 2, figsize=[12, 4])
29 axes[0][0].set_title("MRA decomposition")
30 axes[0][0].plot(ecg)
31 axes[0][0].set_ylabel('ECG Signal')
32 axes[0][0].set_xlim(0, len(ecg) - 1)
33 axes[0][0].set_ylim(ylim[0], ylim[1])
34 mark_peaks(axes[0][0])
35
36 for i, x in enumerate(coeffs):
37 ax = axes[-i - 1][0]
38 ax.plot(coeffs[i], 'g')
39 if i == 0:
40 ax.set_ylabel("A%d" % (len(coeffs) - 1))
41 else:
42 ax.set_ylabel("D%d" % (len(coeffs) - i))
43 # Scale axes
44 ax.set_xlim(0, len(ecg) - 1)
45 ax.set_ylim(ylim[0], ylim[1])
46 mark_peaks(ax)
47
48 """
49 repeat using the SWT instead of MRA as the decomposition
50 """
51 coeffs = pywt.swt(ecg, wavelet=wavelet, level=level, norm=True, trim_approx=True)
52 ca = coeffs[0]
53 details = coeffs[1:]
54
55 axes[0][1].set_title("normalized SWT decomposition")
56 axes[0][1].plot(ecg)
57 axes[0][1].set_ylabel('ECG Signal')
58 axes[0][1].set_xlim(0, len(ecg) - 1)
59 axes[0][1].set_ylim(ylim[0], ylim[1])
60 mark_peaks(axes[0][1])
61
62 for i, x in enumerate(coeffs):
63 ax = axes[-i - 1][1]
64 ax.plot(coeffs[i], 'g')
65 if i == 0:
66 ax.set_ylabel("A%d" % (len(coeffs) - 1))
67 else:
68 ax.set_ylabel("D%d" % (len(coeffs) - i))
69 # Scale axes
70 ax.set_xlim(0, len(ecg) - 1)
71 ax.set_ylim(ylim[0], ylim[1])
72 mark_peaks(ax)