forked from EsiSeraj/EEG-PhaseFreq-Analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Phase_Features.m
167 lines (160 loc) · 6.17 KB
/
Phase_Features.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
166
167
function varargout = Phase_Features(phase_sig1, fs, Th, varargin)
%
% varargout = Phase_Features(phase_sig1, fs, Th)
% varargout = Phase_Features(phase_sig1, fs, Th, phase_sig2)
%
% *************************************************************************
% * Calculating popular phase related quantities, i.e. Phase Shift (PS), *
% * Phase Lock (PL), Phase Reset (PR), Phase Difference (PD) and *
% * Instantaneous Frequency (IF) in SINGLE or MULTI-Channel modes using *
% * phase sequences. Refer to the User Guide for further details. *
% *************************************************************************
%
% Usage:
% Multi-channel :
% [PR, PS, PL, PDV, PD] = Phase_Features(phase_sig1, fs, Th, phase_sig2)
% [PR, PS, PL, PDV, PD] = Phase_Features(phase_sig1, fs, Th)
% *NOTE: While using first case the phase sequenses (phase_sig1 and
% phase_sig2) have to be row vectors. In secons case, the
% phase_sig1 have to be a (2*n) matrix where the rows are phase
% signals.
% Single-channel :
% [IF, PR, PS, PL] = Phase_Features(phase_sig1, fs, Th)
% *NOTE: In this case the phase_sig1 is a vector containing instantaneous
% phase signal.
%
% Inputs:
% phase_sig1 : instantaneous phase vector or matrix
% fs : sampling frequency (Hz)
% Th : Threshold value used to detect phase shift
% events (Radians)
% phase_sig2 : instantaneous phase vector
%
% Outputs:
% PS : Phase Shift Events
% PL : Phase Lock Events
% PR : Phase Resetting Events
% PD : Phase Difference
% PDV : Phase Difference Derivatives
% IF : Instantaneous Frequency
%
% This program is provided by ESMAEIL SERAJ ([email protected]).
% Please make sure to reference BOTH the original studies [1-2] and the
% OSET [3] to help others find these items.
%
% [1] Esmaeil Seraj, Reza Sameni. ”Robust Electroencephalogram Phase
% Estimation with Applications in Brain-computer Interface Systems”
% Physiological Measurements (2017)
% [2] Reza Sameni and Esmaeil Seraj, “A Robust Statistical Framework
% for Instantaneous Electroencephalogram Phase and Frequency
% Analysis” Physiological Measurements (2017)
% [3] R. Sameni, The Open-Source Electrophysiological Toolbox (OSET),
% version 3.1 (2014). URL http://www.oset.ir
% Released under the GNU General Public License
% Copyright (C) 2012 Reza Sameni
% Shiraz University, Shiraz, Iran
%
% This program is free software; you can redistribute it and/or modify it
% under the terms of the GNU General Public License as published by the
% Free Software Foundation; either version 2 of the License, or (at your
% option) any later version.
% This program is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
% Public License for more details. You should have received a copy of the
% GNU General Public License along with this program; if not, write to the
% Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
% MA 02110-1301, USA.
%
%%-Checking inputs and assigning default values-%%
[m1, n1] = size(phase_sig1);
if nargin==4
phase_sig2 = varargin{1};
[m2, n2] = size(phase_sig2);
if m1~=1 || m2~=1
error('***Phase Sequences Must Contain Only One Channel of Information***')
end
if m1~=m2 || n1~=n2
error('***In Multi-signal Case, Phase Sequences Have to Be in Same Length***')
end
elseif nargin>4
error('Too Many Input Arguments: Please Check the Instructions')
end
%%-Calculating Phase features-%%
switch nargin
case 4
flg = 2;
PD = phase_sig1 - phase_sig2;
PDV = [diff(PD), 0];
PS = zeros(m1 ,n1);
for j=1:n1-1
if PDV(j)>=Th || PDV(j)<=-Th
PS(j) = 1+Th/100;
end
end
PL = ~PS;
PR = ones(m1, n1)+Th/100; % shifting a bit to have a better view
ind = find(PL~=0);
for c=1:length(ind)-1
if ind(c+1)~=ind(c)+1
PR(1, ind(c)+1:end) = -PR(1, ind(c)+1:end);
end
end
PL = PL+Th/100;
case 3
if m1==1
flg = 1;
PD = [diff(phase_sig1), 0];
IF = fs*PD/2/pi;
PDV = [diff(PD), 0];
PS = zeros(m1 ,n1);
for j=1:n1-1
if PDV(j)>=Th || PDV(j)<=-Th
PS(j) = 1+Th/100;
end
end
PL = ~PS;
PR = ones(m1, n1)+Th/100;
ind = find(PL~=0);
for c=1:length(ind)-1
if ind(c+1)~=ind(c)+1
PR(1, ind(c)+1:end) = -PR(1, ind(c)+1:end);
end
end
PL = PL+Th/100;
end
if m1==2
phase_sig2 = phase_sig1(2, :);
flg = 2;
PD = phase_sig1 - phase_sig2;
PDV = [diff(PD), 0];
PS = zeros(m1 ,n1);
for j=1:n1-1
if PDV(j)>=Th || PDV(j)<=-Th
PS(j) = 1+Th/100;
end
end
PL = ~PS;
PR = ones(m1, n1)+Th/100;
ind = find(PL~=0);
for c=1:length(ind)-1
if ind(c+1)~=ind(c)+1
PR(1, ind(c)+1:end) = -PR(1, ind(c)+1:end);
end
end
PL = PL+Th/100;
elseif m1>2
error('***Input Phase Matrix Must Contain at Most TWO Channels***')
end
otherwise
error('***Either the Fs or Threshold Value for Detecting Phase Shift Events (Th_PS) are not Specified***')
end
if flg == 2
varargout = {PR, PS, PL, PDV, PD};
elseif flg == 1
varargout = {IF, PR, PS, PL};
else
error('***Something is NOT Right. Please Double-Check the Required Arguments!***')
end
end