-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathICA_custom.m
More file actions
209 lines (167 loc) · 5.39 KB
/
ICA_custom.m
File metadata and controls
209 lines (167 loc) · 5.39 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
% Script with our ICA function and testing it
% code adapted from https://towardsdatascience.com/independent-component-analysis-ica-in-python-a0ef0db0955e
%% ICA testing: Test Signals
tolerance = 1e-5; % If the new weights vector differs from the old one by less than 'tolerance', we terminate the iteration
% Generating test signals
n_samples = 2000; % Length of independent sources
time = linspace(0, 8, n_samples);
s1 = sin(2*time);
s2 = sign(sin(3*time));
s3 = sawtooth(2*pi*time);
% Building mixed signals matrix
X = [s1', s2', s3']; % each column contains one recording
% (3 columns = want to identify 3 sources)
% hence for our example we want 98 columns?
%X = [s1; s2; s3];
A = [1,1,1; 0.5,2,1; 1.5,1,2];
X = X*A';
%% ICA testing: Algorithm
iterations = 1000;
S = ica(X, iterations, tolerance);
plot_mixture_sources_predictions(X, [s1; s2; s3], S)
%%
load('monkeydata_training.mat');
%% ICA: Create input data
% Perform separate ICA on each neuron
train = zeros(98,600,975); % neurons, 3/4 of trials (for 8 angles), time bins
test = zeros(98,200,975); % Leave some data for test
for n = 0:97
count_train = 1;
count_test = 1;
for j = 0:7
% Add 75 trials from each direction to the training data
for i = 1:75
len = length(trial(i,j+1).spikes);
train(n+1,count_train,1:len) = trial(i,j+1).spikes(n+1,:);
count_train = count_train + 1;
end
% Add 25 trials from each direction to the testing data
for i = 76:100
len = length(trial(i,j+1).spikes);
test(n+1,count_test,1:len) = trial(i,j+1).spikes(n+1,:);
count_test = count_test + 1;
end
end
end
%% ICA
% Number of ICs is the rows of the input
N = 98;
transform = zeros(98, 975, N);
iterations = 20;
tolerance = 1e-4;
for i = 1:98
[~, transform(i, :, :)] = ica(squeeze(train(i, :, :)), iterations, tolerance);
end
% Doesn't work because output of ica is 975*600 (was sources_est, now
% changed to W)
%cluster(N, transform, train, test);
%% Functions
% Clustering algorithm
function cluster(N, transform, train, test)
% Transform data using ica/pca principal components
transformed_train = zeros(98,600,N);
transformed_test = zeros(98,200,N);
for i = 1:98
transformed_train(i,:,:) = squeeze(train(i,:,:)) * squeeze(transform(i,:,:));
transformed_test(i,:,:) = squeeze(test(i,:,:)) * squeeze(transform(i,:,:));
end
end
% Hyperbolic tangent
function hyper_tan = g(x)
hyper_tan = tanh(x);
end
% Derivative of the hyperbolic tangent
function hyper_tan_der = g_der(x)
hyper_tan_der = 1 - (g(x) .* g(x));
end
% Center the signal (might not be needed in our case?)
function centered = center(X)
avg = mean(X); % check if you need to specify dimension
centered = X - avg;
end
% Whitening the signal (remove correlation between components)
function X_whitened = whiten(X)
% Covariance matrix
covar = cov(X);
% Eigenvalue decomposition
[V, D] = eig(covar);
% D: diagonal matrix of eigenvalues
% V: orthogonal matrix whose columns correspond to eigenvectors in*V = V*D
D_inv = sqrt(inv(D));
% Whitened signal
X_whitened = V *(D_inv * (V' * X'));
%X_whitened = V *(D \ (V' * X'));
%check_mat = abs(X_whitened_slow - X_whitened);
%check = max(check_mat);
%print(check)
end
% Update the demixing matrix
function w_new = update_demixing_mat(w, X)
% Function solving the following equation (written in pseudocode)
%w_new = mean((X * g(w' * X))) - mean(g_der(w'*X) * w);
% First term
a = g(w * X);
%b = [(X(1,:).* a); (X(2,:).*a); (X(3,:).* a)];
[components_num, len] = size(X); % rows of X = number of independent components
b = zeros(components_num, len);
for i = 1:components_num
b(i, :) = X(i,:).* a;
end
c = mean(b, 2);
% Second term
a2 = g_der(w * X);
b2 = mean(a2);
c2 = b2 .* w;
w_new = c' - c2;
w_new = w_new/sqrt(sum(w_new .^2));
end
% ICA
% Note: would be better trying to implement with mutual information
function [sources_est, W] = ica(X, iterations, tolerance)
% Center
X = center(X);
% Whiten
X = whiten(X);
[components_num, ~] = size(X); % rows of X = number of independent components
W = zeros(components_num);
for i = 1:components_num
w = rand(1, components_num);
%w = [0.2, 0.5, 0.3];
for j = 1:iterations
w_new = update_demixing_mat(w, X);
if i>1
w_new = w_new - (w_new * (W(1:i, :)') * W(1:i, :));
end
distance = abs(abs(sum(w .* w_new))- 1);
% Update
w = w_new;
if distance < tolerance
break
end
end
W(i, :) = w;
end
sources_est = W * X;
end
% Function to plot and compare the original, mixed and predicted signals
function plot_mixture_sources_predictions(X, original_sources, sources_est)
figure();
subplot(3, 1, 1); hold on
for i = 1:3
x = X(:, i)';
plot(x)
end
title('mixtures');
subplot(3, 1, 2); hold on
for i = 1:3
s = original_sources(i, :);
plot(s)
end
title('real sources')
subplot(3, 1, 3); hold on
for i = 1:3
s = sources_est(i, :);
plot(s)
end
title('predicted sources')
end