This repository has been archived by the owner on Aug 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscrls_regression.m
182 lines (167 loc) · 5.27 KB
/
scrls_regression.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
function [Y,theta,Hh] = scrls_regression(X, opt)
% scrls_regression() - Performs automatic EOG artifact correction using
% multiple adaptive regression. The adaptation is made using the Conventional
% Recursive Least Squares Algorithm (CRLS) [1,2]. A forgetting factor can be
% used for dealing with time-varying scenarios. The precision of the
% computations is set so that stability is guaranteed [3]. If stability is
% not an issue crls_regression() does the same job much faster.
%
% Usage:
% >> [Y,H,Hh] = scrls_regression( X, opt)
%
% Inputs:
% X - Input data matrix (dxN)
% opt - Analysis options (see below)
% opt.refdata - Reference signal(s) (dref x N) (default: [])
% opt.M - Order of the adaptive filter (def: 3)
% opt.lambda - Forgetting factor (def: 0.9999)
% opt.sigma - Initialization constant sigma<<1 (def: 0.01)
% opt.prec - precision (in bits) to use for the computations (def: 50)
%
%
% Outputs:
% Y - Output data matrix (artifact corrected)
% H - Final filter weights (M*dref x d)
% Hh - filter weights evolution (M*dref x d x N)
%
% References:
% [1] P. He et al., Med. Biol. Comput. 42 (2004), 407-412
% [2] S. Haykin. Adaptive Filter Theory, (1996), Prentice Hall
% [3] A. P. Liavas and P. A. Regalia, IEEE Trans. Sig. Proc. 47 (1999),
% 88-96
%
%
% See also:
% POP_SCRLS_REGRESSION, POP_CRLS_REGRESSION, CRLS_REGRESSION, EEGLAB
%
% Copyright (C) <2007> German Gomez-Herrero, http://germangh.com
% overflow value for the error
% -----------------------------------
OVERFLOW = 1e12;
if nargin < 1,
help scrls_regression;
return;
end
if ~exist('opt','var'),
opt = def_scrls_regression;
else
opt = def_scrls_regression(opt);
end
if isempty(opt.refdata),
error('(scrls_regression) I need a reference signal!');
end
sigma = opt.sigma;
lambda = opt.lambda;
M = opt.M;
Xref = opt.refdata;
[deeg,Leeg] = size(X);
[dref,Lref] = size(Xref);
if Leeg~=Lref,
error('(scrls_regression) Input and reference data must have the same length');
end
% compute the maximum allowed precision (in bits)
% ----------------------------------------------
if isempty(opt.prec),
if opt.verbose, fprintf('\n(scrls_regression) computing precision bound'); end
Phi = 0;
Rk = eye(dref*M)*sigma; R = 0;
Pk = eye(dref*M)./sigma; P = 0;
K = 0;
for i = M:L,
phi = Xref(:,i:-1:(i-M+1));
phi = reshape(phi', M*dref,1);
Phi = max(Phi,norm(phi,1));
Rk = lambda*Rk+phi*phi';
R = max(norm(Rk,1),R);
rk = lambda+phi'*Pk*phi;
Pk = (1/lambda)*(Pk-(Pk*phi*phi'*Pk)/rk);
P = max(norm(Pk,1),P);
K = max(norm(Pk,1)*norm(Rk,1),K);
if opt.verbose && ~mod(i,floor(Leeg/10)), fprintf('.'); end
end
A1=Phi^2*(K+P*Phi^2)^2;
A2=lambda*(1-lambda)*Phi^2;
epsilon = (1-lambda)/(K^2*P);
if isinf(A1^2+A1*A2),
k = kmax;
else
p1=(lambda/Phi^2)*(1-sqrt((A1^2+A1*A2)/(A1+A2)^2));
epsilon = epsilon*(p1-(A1*p1^2)/(lambda*(1-lambda)*(lambda-Phi^2*p1)));
k = log2(1/epsilon);
end
if opt.verbose,fprintf('[OK]\n');end
else
k = opt.prec;
end
% initialization of the adaptation loop
% -------------------------------------------
theta = zeros(dref*M,deeg);
P = eye(dref*M)./sigma;
Y = zeros(deeg,Leeg);
% filter recursion
% -------------------------------------------
if opt.verbose, fprintf('\n(scrls_regression) '); end
if nargout < 2,
Hh = zeros(dref*M,deeg,Leeg);
Hh(:,:,1:M-1) = repmat(theta,[1,1,M-1]);
end
for i = M:Leeg
phi = Xref(:,i:-1:(i-M+1));
phi = reshape(phi', M*dref,1);
u = X(:,i);
e = u'-phi'*theta;
e = fl(e,k);
r = lambda + phi'*P*phi;
r = fl(r,k);
v = ((P*phi));
v = fl(v,k);
v = v/r;
v = fl(v,k);
% update filter weights
theta = theta + repmat(v,1,deeg).*repmat(e,M*dref,1);
theta = fl(theta,k);
if nargout > 2, Hh(:,:,i) = theta; end
% update inverse of correlation matrix
Pnew = P*phi;
Pnew = fl(Pnew,k);
Pnew = Pnew*phi';
Pnew = fl(Pnew,k);
Pnew =Pnew*P;
Pnew = fl(Pnew,k);
Pnew = Pnew./r;
Pnew = fl(Pnew,k);
Pnew = P - Pnew;
m = (1/lambda);
m = fl(m,k);
Pnew = m*Pnew;
Pnew = fl(Pnew,k);
P = Pnew;
fout = phi'*theta;
if ~isempty(find(abs(fout(:))>OVERFLOW, 1)),
error('(scrls_regression) Algorithm became unstable');
end
Y(:,i) = (u'-fout)';
if opt.verbose && ~mod(i,floor(Leeg/10)), fprintf('.'); end
end
if opt.verbose,fprintf('[OK]\n');end
% sub-function to initialize the default parameter values
% ---------------------------------------------------------
function [opt] = def_scrls_regression(opt)
if ~exist('opt','var') || isempty(opt) || ~isfield(opt,'refdata'),
opt.refdata = [];
end
if ~isfield(opt, 'verbose') || isempty(opt.verbose),
opt.verbose = 1;
end
if ~isfield(opt,'sigma') || isempty(opt.sigma),
opt.sigma = 0.01;
end
if ~isfield(opt, 'lambda') || isempty(opt.lambda),
opt.lambda = 0.999;
end
if ~isfield(opt, 'M') || isempty(opt.M),
opt.M = 3;
end
if ~isfield(opt, 'prec') || isempty(opt.prec),
opt.prec = 50;
end