Debian Codegraph @

demo/wp_scalogram.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
10 x = np.linspace(0, 1, num=512)
11 data = np.sin(250 * np.pi * x**2)
12
13 wavelet = 'db2'
14 level = 4
15 order = "freq" # other option is "normal"
16 interpolation = 'nearest'
17 cmap = plt.cm.cool
18
19 # Construct wavelet packet
20 wp = pywt.WaveletPacket(data, wavelet, 'symmetric', maxlevel=level)
21 nodes = wp.get_level(level, order=order)
22 labels = [n.path for n in nodes]
23 values = np.array([n.data for n in nodes], 'd')
24 values = abs(values)
25
26 # Show signal and wavelet packet coefficients
27 fig = plt.figure()
28 fig.subplots_adjust(hspace=0.2, bottom=.03, left=.07, right=.97, top=.92)
29 ax = fig.add_subplot(2, 1, 1)
30 ax.set_title("linchirp signal")
31 ax.plot(x, data, 'b')
32 ax.set_xlim(0, x[-1])
33
34 ax = fig.add_subplot(2, 1, 2)
35 ax.set_title("Wavelet packet coefficients at level %d" % level)
36 ax.imshow(values, interpolation=interpolation, cmap=cmap, aspect="auto",
37 origin="lower", extent=[0, 1, 0, len(values)])
38 ax.set_yticks(np.arange(0.5, len(labels) + 0.5), labels)
39
40 # Show spectrogram and wavelet packet coefficients
41 fig2 = plt.figure()
42 ax2 = fig2.add_subplot(211)
43 ax2.specgram(data, NFFT=64, noverlap=32, Fs=2, cmap=cmap,
44 interpolation='bilinear')
45 ax2.set_title("Spectrogram of signal")
46 ax3 = fig2.add_subplot(212)
47 ax3.imshow(values, origin='upper', extent=[-1, 1, -1, 1],
48 interpolation='nearest')
49 ax3.set_title("Wavelet packet coefficients")
50
51
52 plt.show()