Switch to unified view

a b/preprocessOfApneaECG/BioSigKit/Algorithms/RTpca.m
1
function [R,out,deni] = RTpca(X,nPCA,nit,T)
2
%% Inputs :
3
% X : Input Matrix, each variable is fed into one row and each single
4
% column denotes a single datapoint
5
% nPCA : Number of required PCs to estimate 
6
% nit : number of iterations
7
%% Outputs :
8
% R : EigenVectors of each PC, each row is a complete eigenvector.
9
% out : Estimated PCs in realtime; The las row is an outcome made by the
10
% wiegth of the first three PCs.
11
% deni : eigenvalues
12
%% Method
13
% The real time PCA (RTpca) is a brilliant approach to estimate the PCs in
14
% real time with the help of Hebian learning rule and iterative learning
15
% with a single neuron. This method is first inspired and motivated by Oja
16
% et al but the current algorithm is an out of the work by Principe et al.
17
% The convergence speed is determined by T. This function works perfect for 
18
% estimating the first 4 PCs.
19
20
%% Reference
21
% [1] H. Dongho; Y.N. Rao, J.C. Principe, K. Gugel.
22
% ‘’ Real-Time PCA(Principa1 Component Analysis)’’. IEEE International Joint Conference
23
% on Neural Networks (IEEE Cat. No.04CH37541) , 2004, p2159-2159, 1p. Publisher: IEEE
24
25
%% Author : Hooman Sedghamiz     hooman.sedghamiz@gmail.com
26
% Copyright August 2014
27
28
%%
29
if nargin < 4
30
    T = 0.90;                                                                  % default is 0.90
31
    if nargin < 3
32
       nit = 1;
33
       if nargin < 2
34
          nPCA = size(X,1); 
35
       end
36
    end
37
end
38
39
% get the dimensionality
40
[m,n] = size(X);
41
42
43
% ----------------- random initial weights if no input --------------- %
44
w = rand(m,nPCA);
45
46
47
% -------- T determines convergance (higher faster convergence)------- %
48
out = zeros(nPCA,n);
49
numi = zeros(m,nPCA);                                                      %first iteration always zero
50
deni = zeros(nPCA,1);                                                      %first iteration always zero
51
y = zeros(nPCA,1);
52
R = zeros(m,nPCA);
53
54
%dummi =[];
55
56
 for iter = 1:nit
57
  XX = X;       
58
  for ii = 1:n
59
        for r = 1 : nPCA 
60
          %% =================== Network ========================= %%
61
          y(r) = w(:,r)'*XX(:,ii); 
62
          % update network nominator
63
          numi(:,r) = (1-(1/ii))*numi(:,r) + (1/ii)*(XX(:,ii)*y(r)); 
64
          deni(r) = (1-(1/ii))*deni(r) + (1/ii)*y(r)^2;
65
          w(:,r) = (1 - T)*w(:,r) + T*(numi(:,r)/deni(r));  
66
          out(r,ii) = w(:,r)'*XX(:,ii);
67
          %% ========== Deflation to compute the remining PCs =========%%
68
          deflation_s = (w(:,r)*y(r));
69
          XX(:,ii) = XX(:,ii) - deflation_s;
70
          %% =============== Check for convergance =================== %%
71
          if R(:,r) == w(:,r)
72
             fprintf('Converged! in %d iteration and %d Sample',iter,ii);    
73
          end
74
          %% =============== Save the EigenVector ==================== %%
75
          R(:,r) = w(:,r);      
76
        end
77
        %dummi =[dummi deni(:)]; %length of eigenvectors
78
        %M = deni(1:end)./sum(deni(:));
79
        %out(r+1,ii) = M(3)*X(1,ii) + M(2)*X(2,ii) + M(1)*X(3,ii);
80
  end
81
  
82
 end
83
end
84