Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132930 views
License: OTHER
1
import collections
2
import numpy as np
3
4
5
def mackey_glass(sample_len=1000, tau=17, seed=None, n_samples = 1):
6
'''
7
mackey_glass(sample_len=1000, tau=17, seed = None, n_samples = 1) -> input
8
Generate the Mackey Glass time-series. Parameters are:
9
- sample_len: length of the time-series in timesteps. Default is 1000.
10
- tau: delay of the MG - system. Commonly used values are tau=17 (mild
11
chaos) and tau=30 (moderate chaos). Default is 17.
12
- seed: to seed the random generator, can be used to generate the same
13
timeseries at each invocation.
14
- n_samples : number of samples to generate
15
'''
16
delta_t = 10
17
history_len = tau * delta_t
18
# Initial conditions for the history of the system
19
timeseries = 1.2
20
21
if seed is not None:
22
np.random.seed(seed)
23
24
samples = []
25
26
for _ in range(n_samples):
27
history = collections.deque(1.2 * np.ones(history_len) + 0.2 * \
28
(np.random.rand(history_len) - 0.5))
29
# Preallocate the array for the time-series
30
inp = np.zeros((sample_len,1))
31
32
for timestep in range(sample_len):
33
for _ in range(delta_t):
34
xtau = history.popleft()
35
history.append(timeseries)
36
timeseries = history[-1] + (0.2 * xtau / (1.0 + xtau ** 10) - \
37
0.1 * history[-1]) / delta_t
38
inp[timestep] = timeseries
39
40
# Squash timeseries through tanh
41
inp = np.tanh(inp - 1)
42
samples.append(inp)
43
return samples
44
45
46
def mso(sample_len=1000, n_samples = 1):
47
'''
48
mso(sample_len=1000, n_samples = 1) -> input
49
Generate the Multiple Sinewave Oscillator time-series, a sum of two sines
50
with incommensurable periods. Parameters are:
51
- sample_len: length of the time-series in timesteps
52
- n_samples: number of samples to generate
53
'''
54
signals = []
55
for _ in range(n_samples):
56
phase = np.random.rand()
57
x = np.atleast_2d(np.arange(sample_len)).T
58
signals.append(np.sin(0.2 * x + phase) + np.sin(0.311 * x + phase))
59
return signals
60
61
62
def lorentz(sample_len=1000, sigma=10, rho=28, beta=8 / 3, step=0.01):
63
"""This function generates a Lorentz time series of length sample_len,
64
with standard parameters sigma, rho and beta.
65
"""
66
67
x = np.zeros([sample_len])
68
y = np.zeros([sample_len])
69
z = np.zeros([sample_len])
70
71
# Initial conditions taken from 'Chaos and Time Series Analysis', J. Sprott
72
x[0] = 0;
73
y[0] = -0.01;
74
z[0] = 9;
75
76
for t in range(sample_len - 1):
77
x[t + 1] = x[t] + sigma * (y[t] - x[t]) * step
78
y[t + 1] = y[t] + (x[t] * (rho - z[t]) - y[t]) * step
79
z[t + 1] = z[t] + (x[t] * y[t] - beta * z[t]) * step
80
81
x.shape += (1,)
82
y.shape += (1,)
83
z.shape += (1,)
84
85
return np.concatenate((x, y, z), axis=1)
86
87