-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path06_ICA.py
More file actions
40 lines (35 loc) · 1.02 KB
/
06_ICA.py
File metadata and controls
40 lines (35 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import numpy as np
from random import uniform, seed
from matplotlib import pyplot as plt
from ml_algo import ICA
#seed(0)
def main():
#data preparing
n_samples = 8000
time = np.linspace(0, 8, n_samples)
s1 = np.sin(time - 1.5)
s2 = np.sign(np.sin(7 * time + 2.7))
s3 = np.sin(3 * time + 10)
s4 = np.arcsin(np.sin(20 * time + 3.85))
S = np.c_[s1, s2, s3, s4]
S += 0.05 * np.random.normal(size=S.shape)
S /= S.std(axis=0)
A = np.array([[1, 1, 1, 0.7], [0.5, 2, 1.0, 1.3], [1.5, 1.0, 2.0, 0.4], [1.8, 1.7, 0.4, 0.6] ])
X = np.dot(S, A.T)
#unmixing with ICA
model = ICA(tolerance=1e-5)
Shat = model.recover_sources(X)
Shat /= Shat.std(axis=0)
models = [X, S, Shat]
names = ['Observations (mixed signal)',
'True Sources',
'ICA recovered signals']
colors = ['green', 'red', 'steelblue', 'orange']
for ii, (model, name) in enumerate(zip(models, names), 1):
plt.subplot(3, 1, ii)
plt.title(name)
for sig, color in zip(model.T, colors):
plt.plot(sig, color=color)
plt.show()
if __name__ == '__main__':
main()