-
Notifications
You must be signed in to change notification settings - Fork 6
/
unsupervised_methods.py
171 lines (156 loc) · 5.77 KB
/
unsupervised_methods.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import numpy as np
import scipy, math
from utils import detrend
def CHROM(RGB):
Xcomp, Ycomp = (np.array([[3 , -2 , 0],
[1.5, 1, -1.5]]) @ RGB.T)
sX = np.std(Xcomp)
sY = np.std(Ycomp)
alpha = sX/sY
alpha = np.repeat(alpha, Xcomp.shape[0], 0)
bvp = Xcomp - np.multiply(alpha, Ycomp)
return bvp
def POS(RGB, fs=30):
WinSec = 1.6
N = RGB.shape[0]
H = np.zeros((1, N))
l = math.ceil(WinSec * fs)
for n in range(N):
m = n - l
if m >= 0:
Cn = np.true_divide(RGB[m:n, :], np.mean(RGB[m:n, :], axis=0))
Cn = np.mat(Cn).H
S = np.matmul(np.array([[0, 1, -1], [-2, 1, 1]]), Cn)
h = S[0, :] + (np.std(S[0, :]) / np.std(S[1, :])) * S[1, :]
mean_h = np.mean(h)
for temp in range(h.shape[1]):
h[0, temp] = h[0, temp] - mean_h
H[0, m:n] = H[0, m:n] + (h[0])
BVP = H
return BVP
def jade(X, m, Wprev):
n = X.shape[0]
T = X.shape[1]
nem = m
seuil = 1 / math.sqrt(T) / 100
if m < n:
D, U = np.linalg.eig(np.matmul(X, np.mat(X).H) / T)
Diag = D
k = np.argsort(Diag)
pu = Diag[k]
ibl = np.sqrt(pu[n - m:n] - np.mean(pu[0:n - m]))
bl = np.true_divide(np.ones(m, 1), ibl)
W = np.matmul(np.diag(bl), np.transpose(U[0:n, k[n - m:n]]))
IW = np.matmul(U[0:n, k[n - m:n]], np.diag(ibl))
else:
IW = scipy.linalg.sqrtm(np.matmul(X, X.H) / T)
W = np.linalg.inv(IW)
Y = np.mat(np.matmul(W, X))
R = np.matmul(Y, Y.H) / T
C = np.matmul(Y, Y.T) / T
Q = np.zeros((m * m * m * m, 1))
index = 0
for lx in range(m):
Y1 = Y[lx, :]
for kx in range(m):
Yk1 = np.multiply(Y1, np.conj(Y[kx, :]))
for jx in range(m):
Yjk1 = np.multiply(Yk1, np.conj(Y[jx, :]))
for ix in range(m):
Q[index] = np.matmul(Yjk1 / math.sqrt(T), Y[ix, :].T / math.sqrt(
T)) - R[ix, jx] * R[lx, kx] - R[ix, kx] * R[lx, jx] - C[ix, lx] * np.conj(C[jx, kx])
index += 1
# Compute and Reshape the significant Eigen
D, U = np.linalg.eig(Q.reshape(m * m, m * m))
Diag = abs(D)
K = np.argsort(Diag)
la = Diag[K]
M = np.zeros((m, nem * m), dtype=complex)
Z = np.zeros(m)
h = m * m - 1
for u in range(0, nem * m, m):
Z = U[:, K[h]].reshape((m, m))
M[:, u:u + m] = la[h] * Z
h = h - 1
# Approximate the Diagonalization of the Eigen Matrices:
B = np.array([[1, 0, 0], [0, 1, 1], [0, 0 - 1j, 0 + 1j]])
Bt = np.mat(B).H
encore = 1
if Wprev == 0:
V = np.eye(m).astype(complex)
else:
V = np.linalg.inv(Wprev)
# Main Loop:
while encore:
encore = 0
for p in range(m - 1):
for q in range(p + 1, m):
Ip = np.arange(p, nem * m, m)
Iq = np.arange(q, nem * m, m)
g = np.mat([M[p, Ip] - M[q, Iq], M[p, Iq], M[q, Ip]])
temp1 = np.matmul(g, g.H)
temp2 = np.matmul(B, temp1)
temp = np.matmul(temp2, Bt)
D, vcp = np.linalg.eig(np.real(temp))
K = np.argsort(D)
la = D[K]
angles = vcp[:, K[2]]
if angles[0, 0] < 0:
angles = -angles
c = np.sqrt(0.5 + angles[0, 0] / 2)
s = 0.5 * (angles[1, 0] - 1j * angles[2, 0]) / c
if abs(s) > seuil:
encore = 1
pair = [p, q]
G = np.mat([[c, -np.conj(s)], [s, c]]) # Givens Rotation
V[:, pair] = np.matmul(V[:, pair], G)
M[pair, :] = np.matmul(G.H, M[pair, :])
temp1 = c * M[:, Ip] + s * M[:, Iq]
temp2 = -np.conj(s) * M[:, Ip] + c * M[:, Iq]
temp = np.concatenate((temp1, temp2), axis=1)
M[:, Ip] = temp1
M[:, Iq] = temp2
# Whiten the Matrix
# Estimation of the Mixing Matrix and Signal Separation
A = np.matmul(IW, V)
S = np.matmul(np.mat(V).H, Y)
return A, S
def ica(X, Nsources, Wprev=0):
nRows = X.shape[0]
nCols = X.shape[1]
if nRows > nCols:
print(
"Warning - The number of rows is cannot be greater than the number of columns.")
print("Please transpose input.")
if Nsources > min(nRows, nCols):
Nsources = min(nRows, nCols)
print(
'Warning - The number of soures cannot exceed number of observation channels.')
print('The number of sources will be reduced to the number of observation channels ', Nsources)
Winv, Zhat = jade(X, Nsources, Wprev)
W = np.linalg.pinv(Winv)
return W, Zhat
def ICA(RGB, FS=30):
RGB = (RGB-RGB.mean())/RGB.std(axis=0)
NyquistF = 1 / 2 * FS
BGRNorm = np.zeros(RGB.shape)
for c in range(3):
BGRDetrend = detrend(RGB[:, c])
BGRNorm[:, c] = (BGRDetrend - np.mean(BGRDetrend)) / np.std(BGRDetrend)
_, S = ica(np.mat(BGRNorm).H, 3)
# select BVP Source
MaxPx = np.zeros((1, 3))
for c in range(3):
FF = np.fft.fft(S[c, :])
F = np.arange(0, FF.shape[1]) / FF.shape[1] * FS * 60
FF = FF[:, 1:]
FF = FF[0]
N = FF.shape[0]
Px = np.abs(FF[:math.floor(N / 2)])
Px = np.multiply(Px, Px)
Fx = np.arange(0, N / 2) / (N / 2) * NyquistF
Px = Px / np.sum(Px, axis=0)
MaxPx[0, c] = np.max(Px)
MaxComp = np.argmax(MaxPx)
BVP_I = S[MaxComp, :]
return np.array(BVP_I).real[0]