forked from rsameni/BSSLecture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEx03_testICAmethods.m
80 lines (69 loc) · 2.43 KB
/
Ex03_testICAmethods.m
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
% Independent component analysis using classical methods
%
% BMI500 Course
% Lecture: An Introduction to Blind Source Separation and Independent Component Analysis
% By: R. Sameni
% Department of Biomedical Informatics, Emory University, Atlanta, GA, USA
% Fall 2020
%
% Dependency: The open-source electrophysiological toolbox (OSET):
% https://github.com/alphanumericslab/OSET.git
% OR
% https://gitlab.com/rsameni/OSET.git
%
clc
clear
close all
example = 3;
switch example
case 1 % A sample EEG from the OSET package
load EEGdata textdata data % Load a sample EEG signal
fs = 250;
x = data'; % make the data in (channels x samples) format
% Check the channel names
disp(textdata)
case 2 % A sample ECG from the OSET package
load SampleECG2 data % Load a sample ECG signal
fs = 1000;
x = data(:, 2:end)'; % make the data in (channels x samples) format
x = x - LPFilter(x, 1.0/fs); % remove the lowpass baseline
case 3 % A synthetic signal
fs = 500;
len = round(3.0*fs);
s1 = sin(2*pi*7.0/fs * (1 : len));
s2 = 2*sin(2*pi*1.3/fs * (1 : len) + pi/7);
period = 76.0;
s3 = (mod(1:len, 76.0) - period/2)/(period/2);
A = rand(3);
noise = 0.01*randn(3, len);
x = A * [s1 ; s2 ; s3] + noise;
otherwise
error('unknown example');
end
N = size(x, 1); % The number of channels
T = size(x, 2); % The number of samples per channel
% Plot the channels
PlotECG(x, 4, 'b', fs, 'Raw data channels');
% Run fastica
approach = 'defl'; % 'symm' or 'defl'
g = 'gauss'; % 'pow3', 'tanh', 'gauss', 'skew'
lastEigfastica = N; % PCA stage
numOfIC = N; % ICA stage
interactivePCA = 'off';
[s_fastica, A_fatsica, W_fatsica] = fastica (x, 'approach', approach, 'g', g, 'lastEig', lastEigfastica, 'numOfIC', numOfIC, 'interactivePCA', interactivePCA, 'verbose', 'off', 'displayMode', 'off');
% Check the covariance matrix
Cs = cov(s_fastica');
% Run JADE
lastEigJADE = N; % PCA stage
W_JADE = jadeR(x, lastEigJADE);
s_jade = W_JADE * x;
% Run SOBI
lastEigSOBI = N; % PCA stage
num_cov_matrices = 100;
[W_SOBI, s_sobi] = sobi(x, lastEigSOBI, num_cov_matrices);
% Plot the sources
PlotECG(s_fastica, 4, 'r', fs, 'Sources extracted by fatsica');
% Plot the sources
PlotECG(s_jade, 4, 'k', fs, 'Sources extracted by JADE');
% Plot the sources
PlotECG(s_sobi, 4, 'm', fs, 'Sources extracted by SOBI');