-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSP.m
59 lines (41 loc) · 1.56 KB
/
CSP.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
% CSP Function
% Coded by James Ethridge and William Weaver
function [result] = CSP(varargin)
if (nargin ~= 2)
disp('Must have 2 classes for CSP!')
end
Rsum=0;
%finding the covariance of each class and composite covariance
for i = 1:nargin
%mean here?
R{i} = ((varargin{i}*varargin{i}')/trace(varargin{i}*varargin{i}'));%instantiate me before the loop!
%Ramoser equation (2)
Rsum=Rsum+R{i};
end
% Find Eigenvalues and Eigenvectors of RC
% Sort eigenvalues in descending order
[EVecsum,EValsum] = eig(Rsum);
[EValsum,ind] = sort(diag(EValsum),'descend');
EVecsum = EVecsum(:,ind);
% Find Whitening Transformation Matrix - Ramoser Equation (3)
W = sqrt(inv(diag(EValsum))) * EVecsum';
for k = 1:nargin
S{k} = W * R{k} * W'; % Whiten Data Using Whiting Transform - Ramoser Equation (4)
end
% Ramoser equation (5)
% [U{1},Psi{1}] = eig(S{1});
% [U{2},Psi{2}] = eig(S{2});
%generalized eigenvectors/values
[B,D] = eig(S{1},S{2});
% Simultanous diagonalization
% Should be equivalent to [B,D]=eig(S{1});
%verify algorithim
%disp('test1:Psi{1}+Psi{2}=I')
%Psi{1}+Psi{2}
%sort ascending by default
%[Psi{1},ind] = sort(diag(Psi{1})); U{1} = U{1}(:,ind);
%[Psi{2},ind] = sort(diag(Psi{2})); U{2} = U{2}(:,ind);
[D,ind]=sort(diag(D));B=B(:,ind);
%Resulting Projection Matrix-these are the spatial filter coefficients
result = B'*W;
end