|
1 | 1 | from __future__ import division, print_function |
2 | 2 | import numpy as np |
3 | | -import pylab |
| 3 | +import pylab as plt |
4 | 4 | from scipy.spatial.distance import pdist, squareform |
5 | 5 |
|
| 6 | +from utils import calculate_convolve |
6 | 7 |
|
7 | | -def calculate_convolve(signal, n=5): |
8 | | - return np.convolve(signal, np.ones((n,)) / n, mode='valid') |
9 | 8 |
|
| 9 | +# ----------------- Plot config ------------------------------------------- |
| 10 | +SMALL = 8 |
| 11 | +MEDIUM = 10 |
| 12 | +BIGGER = 11 |
10 | 13 |
|
11 | | -def recurrence_plot(signal, eps=0.10, steps=3): |
12 | | - _2d_array = signal[:, None] |
13 | | - # Pairwise distances |
14 | | - distance = pdist(_2d_array) |
15 | | - print(distance) |
16 | | - distance = np.floor(distance / eps) |
17 | | - print(distance) |
18 | | - distance[distance > steps] = steps |
19 | | - print(distance.shape) |
20 | | - return squareform(distance) |
| 14 | +plt.rc('font', size=SMALL) # controls default text sizes |
| 15 | +plt.rc('axes', titlesize=SMALL) # font size of the axes title |
| 16 | +plt.rc('axes', labelsize=MEDIUM) # font size of the x and y labels |
| 17 | +plt.rc('xtick', labelsize=SMALL) # font size of the tick labels |
| 18 | +plt.rc('ytick', labelsize=SMALL) # font size of the tick labels |
| 19 | +plt.rc('legend', fontsize=SMALL) # legend font size |
| 20 | +plt.rc('figure', titlesize=BIGGER) # font size of the figure title |
| 21 | +# ------------------------------------------------------------------------- |
21 | 22 |
|
22 | 23 |
|
23 | | -if __name__ == "__main__": |
24 | | - raw_signal = np.random.uniform(-1, 1, 1000) |
25 | | - convolved_signal = calculate_convolve(raw_signal) |
| 24 | +class RecurrencePlot(object): |
| 25 | + def __init__(self, signal, row=2, col=2): |
| 26 | + self.signal = signal |
| 27 | + self.size = '%d%d' % (row, col) |
| 28 | + |
| 29 | + def recurrence_plot(self, eps=0.10, steps=3): |
| 30 | + _2d_array = self.signal[:, None] |
| 31 | + |
| 32 | + # Pairwise distances |
| 33 | + distance = pdist(_2d_array) |
| 34 | + distance = np.floor(distance / eps) |
| 35 | + distance[distance > steps] = steps |
| 36 | + return squareform(distance) |
26 | 37 |
|
27 | | - pylab.title("Raw Signal") |
28 | | - pylab.subplot(211) |
29 | | - pylab.plot(convolved_signal) |
| 38 | + def subplot(self, x, is_signal=True, index=1, title=None, grid=True): |
| 39 | + plt.subplot(int('%s%d' % (self.size, index))) |
| 40 | + plt.plot(x) if is_signal else plt.imshow(x) |
| 41 | + plt.title(title) |
| 42 | + plt.grid(grid) |
30 | 43 |
|
31 | | - # pylab.title("2D Image") |
32 | | - pylab.subplot(212) |
| 44 | + def save(self): |
| 45 | + # plot with various axes scales |
| 46 | + plt.figure() |
| 47 | + self.subplot(self.signal, index=1, title='Raw Signal') |
| 48 | + self.subplot(self.recurrence_plot(), is_signal=False, index=2, title='2D Image') |
| 49 | + self.subplot(self.signal, index=3, title='Raw Signal') |
| 50 | + self.subplot(self.recurrence_plot(), is_signal=False, index=4, title='2D Image') |
| 51 | + plt.savefig('result.jpg') |
33 | 52 |
|
34 | | - pylab.imshow(recurrence_plot(convolved_signal)) |
35 | | - pylab.savefig('result.jpg') |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + raw_signal = np.random.uniform(-1, 1, 500) |
| 56 | + convolved_signal = calculate_convolve(raw_signal) |
| 57 | + rp = RecurrencePlot(convolved_signal) |
| 58 | + rp.save() |
0 commit comments