-
Notifications
You must be signed in to change notification settings - Fork 3
/
MPP_Decomp.m
165 lines (146 loc) · 5.18 KB
/
MPP_Decomp.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
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
% Function that decomposes single-channel traces (for a particular EEG
% rhythm) according to a Marked Point Process framework
% Testing stage
% Author: Carlos Loza
%%
function MPP_c = MPP_Decomp(X, D, th)
% INPUTS
% X - EEG data. It can be 1. single-trial (row vector)
% 2. multi-trial/same duration (matrix form), or
% 3. multi-trial/different durations (cell) input of single-channel
% bandpassed EEG data
% KEY: Single traces MUST be row vectors
% D - Dictionary, M x K matrix
% th - Threshold to discriminate between noise and phasic event component
% according to the Embedding Transform and HOM (Higher-order moments).
% OUTPUTS
% MPP_c - cell with matrices that have the Marked Point Process features,
% i.e. amplitude, timing and index (each cell element is a particular trial)
% Check if input is cell
X = squeeze(X);
n_tr = size(X,1);
if iscell(X) == 0
X = mat2cell(X,ones(1,n_tr));
end
MPP_c = cell(n_tr,1);
for i = 1:n_tr
[alph, tau, D_idx] = PhEv_nonovp(X{i,1}, D, th);
MPP_c{i,1} = [tau alph D_idx];
end
end
%%
function [alph, tau, D_idx] = PhEv_nonovp(x, D, th)
% FFT-based algorithm for Decomposing single-channel, single trial EEG
% traces.
% Non-overlapping phasic events only
% Critically sparse case -> vector quantization approach
% INPUTS:
% x - input time series (single-trial, single channel), N time samples
% D - dictionary, MxK
% th - Threshold to discriminate between noise and phasic event component
% according to the Embedding Transform and HOM (Higher-order moments).
% OUTPUTS:
% alph - decomposition amplitudes, column vector Lx1
% tau - decomposition timings (centered), column vector Lx1
% D_idx - index of atoms used in the decomposition, column vector Lx1
N = length(x);
[M,~] = size(D);
stp_fl = 0; % Stopping flag for main loop
% L is the number of extracted phasic events. It will depend on the
% temporal structure of the EEG trace as well as on the Dictionary elements
L = ceil((N+M-1)/M); % Maximum possible number of non-overlapping atoms
alph = zeros(L,1);
tau = zeros(L,1);
D_idx = zeros(L,1);
% Check if input is row
if iscolumn(x) == 0
x = x';
end
% Only one run over the dictionary is necessary
corrs = conv2(x,flipud(D));
abs_corrs = abs(corrs);
[max_tau, max_D_idx] = max(abs_corrs,[],2);
% First iteration
[~, idx_max] = max(max_tau);
alph(1,1) = corrs(idx_max, max_D_idx(idx_max));
if abs(alph(1,1)) > th
if idx_max - M <= 0 % Condition for left edge
z_padd = idx_max - 1;
max_tau(1:z_padd+M) = zeros(z_padd+M,1);
elseif idx_max > N % Condition for right edge
z_padd = length(max_tau) - idx_max;
max_tau(idx_max-M+1:end) = zeros(z_padd+M,1);
else
max_tau(idx_max-M+1:idx_max+M-1) = zeros(2*M-1,1);
tau(1,1) = idx_max - M + 1;
D_idx(1,1) = max_D_idx(idx_max);
end
% Remaining iterations
i = 2;
while stp_fl == 0
[tau_p, fl] = check_potential_PhEv(max_tau, M);
if fl == 0
for j = 1:length(tau_p)
[~, idx_max] = max(max_tau);
alph(i,1) = corrs(idx_max, max_D_idx(idx_max));
if abs(alph(i,1)) <= th
stp_fl = 1;
if idx_max - M > 0 && idx_max < N
tau(i,1) = idx_max - M + 1;
D_idx(i,1) = max_D_idx(idx_max);
else
alph(i,1) = 0;
end
i = i + 1;
break;
end
if idx_max - M <= 0 % Condition for left edge
z_padd = idx_max - 1;
max_tau(1:z_padd+M) = zeros(z_padd+M,1);
elseif idx_max > N % Condition for right edge
z_padd = length(max_tau) - idx_max;
max_tau(idx_max-M+1:end) = zeros(z_padd+M,1);
else
max_tau(idx_max-M+1:idx_max+M-1) = zeros(2*M-1,1);
tau(i,1) = idx_max - M + 1;
D_idx(i,1) = max_D_idx(idx_max);
i = i + 1;
end
end
else
stp_fl = 1;
end
end
% Update outputs
alph = alph(1:i-1,1);
tau = tau(1:i-1,1);
D_idx = D_idx(1:i-1,1);
% Center timings
tau = tau + round(M/2);
else
% No M-snippet will/can be slected according to the threshold input
display('No M-snippets satisfy the threshold criterion')
alph = zeros(0,0);
tau = zeros(0,0);
D_idx = zeros(0,0);
end
end
%%
function [tau_p, fl] = check_potential_PhEv(max_tau, M)
% Function to check if there are any potential atoms left to be discovered
% in the non-overlapping case of the decomposition
% if fl = 0 -> no potential phasic events to be found
% if fl = 1 -> potential phasic events still available
max_tau_ones = double((max_tau ~= 0));
aux_fl = conv(max_tau_ones,ones(1,M),'valid');
idx = find(aux_fl == M);
if isempty(idx) == 1
fl = 1;
tau_p = 0;
else
fl = 0;
aux_idx = find(diff(idx) >= M) + 1;
tau_p = [idx(1); idx(aux_idx)];
end
end