|
2 | 2 | """ |
3 | 3 | Compute the coherence of two signals |
4 | 4 | """ |
5 | | -import numpy as n |
| 5 | +import numpy as np |
| 6 | +import matplotlib.pyplot as plt |
6 | 7 |
|
7 | | -from pylab import figure, show |
| 8 | +# make a little extra space between the subplots |
| 9 | +plt.subplots_adjust(wspace=0.5) |
8 | 10 |
|
9 | 11 | dt = 0.01 |
10 | | -t = n.arange(0, 30, dt) |
11 | | -Nt = len(t) |
12 | | -nse1 = n.random.randn(Nt) # white noise 1 |
13 | | -nse2 = n.random.randn(Nt) # white noise 2 |
14 | | -r = n.exp(-t/0.05) |
| 12 | +t = np.arange(0, 30, dt) |
| 13 | +nse1 = np.random.randn(len(t)) # white noise 1 |
| 14 | +nse2 = np.random.randn(len(t)) # white noise 2 |
| 15 | +r = np.exp(-t/0.05) |
15 | 16 |
|
16 | | -cnse1 = n.convolve(nse1, r)*dt # colored noise 1 |
17 | | -cnse1 = cnse1[:Nt] |
18 | | -cnse2 = n.convolve(nse2, r)*dt # colored noise 2 |
19 | | -cnse2 = cnse2[:Nt] |
| 17 | +cnse1 = np.convolve(nse1, r, mode='same')*dt # colored noise 1 |
| 18 | +cnse2 = np.convolve(nse2, r, mode='same')*dt # colored noise 2 |
20 | 19 |
|
21 | 20 | # two signals with a coherent part and a random part |
22 | | -s1 = 0.01*n.sin(2*n.pi*10*t) + cnse1 |
23 | | -s2 = 0.01*n.sin(2*n.pi*10*t) + cnse2 |
24 | | - |
25 | | -fig = figure() |
26 | | -ax = fig.add_subplot(211) |
27 | | -ax.plot(t, s1, 'b-', t, s2, 'g-') |
28 | | -ax.set_xlim(0,5) |
29 | | -ax.set_xlabel('time') |
30 | | -ax.set_ylabel('s1 and s2') |
31 | | - |
32 | | -ax = fig.add_subplot(212) |
33 | | -cxy, f = ax.cohere(s1, s2, 256, 1./dt) |
34 | | - |
35 | | -show() |
| 21 | +s1 = 0.01*np.sin(2*np.pi*10*t) + cnse1 |
| 22 | +s2 = 0.01*np.sin(2*np.pi*10*t) + cnse2 |
| 23 | + |
| 24 | +plt.subplot(211) |
| 25 | +plt.plot(t, s1, 'b-', t, s2, 'g-') |
| 26 | +plt.xlim(0,5) |
| 27 | +plt.xlabel('time') |
| 28 | +plt.ylabel('s1 and s2') |
| 29 | +plt.grid(True) |
| 30 | + |
| 31 | +plt.subplot(212) |
| 32 | +cxy, f = plt.cohere(s1, s2, 256, 1./dt) |
| 33 | +plt.ylabel('coherence') |
| 34 | +plt.show() |
36 | 35 |
|
37 | 36 |
|
0 commit comments