|
a |
|
b/code/preprocessing/EEG/fastica/whitenv.m |
|
|
1 |
function [newVectors, whiteningMatrix, dewhiteningMatrix] = whitenv ... |
|
|
2 |
(vectors, E, D, s_verbose); |
|
|
3 |
%WHITENV - Whitenv vectors. |
|
|
4 |
% |
|
|
5 |
% [newVectors, whiteningMatrix, dewhiteningMatrix] = ... |
|
|
6 |
% whitenv(vectors, E, D, verbose); |
|
|
7 |
% |
|
|
8 |
% Whitens the data (row vectors) and reduces dimension. Returns |
|
|
9 |
% the whitened vectors (row vectors), whitening and dewhitening matrices. |
|
|
10 |
% |
|
|
11 |
% ARGUMENTS |
|
|
12 |
% |
|
|
13 |
% vectors Data in row vectors. |
|
|
14 |
% E Eigenvector matrix from function 'pcamat' |
|
|
15 |
% D Diagonal eigenvalue matrix from function 'pcamat' |
|
|
16 |
% verbose Optional. Default is 'on' |
|
|
17 |
% |
|
|
18 |
% EXAMPLE |
|
|
19 |
% [E, D] = pcamat(vectors); |
|
|
20 |
% [nv, wm, dwm] = whitenv(vectors, E, D); |
|
|
21 |
% |
|
|
22 |
% |
|
|
23 |
% This function is needed by FASTICA and FASTICAG |
|
|
24 |
% |
|
|
25 |
% See also PCAMAT |
|
|
26 |
|
|
|
27 |
% @(#)$Id$ |
|
|
28 |
|
|
|
29 |
% ======================================================== |
|
|
30 |
% Default value for 'verbose' |
|
|
31 |
if nargin < 4, s_verbose = 'on'; end |
|
|
32 |
|
|
|
33 |
% Check the optional parameter verbose; |
|
|
34 |
switch lower(s_verbose) |
|
|
35 |
case 'on' |
|
|
36 |
b_verbose = 1; |
|
|
37 |
case 'off' |
|
|
38 |
b_verbose = 0; |
|
|
39 |
otherwise |
|
|
40 |
error(sprintf('Illegal value [ %s ] for parameter: ''verbose''\n', s_verbose)); |
|
|
41 |
end |
|
|
42 |
|
|
|
43 |
% ======================================================== |
|
|
44 |
% In some cases, rounding errors in Matlab cause negative |
|
|
45 |
% eigenvalues (elements in the diagonal of D). Since it |
|
|
46 |
% is difficult to know when this happens, it is difficult |
|
|
47 |
% to correct it automatically. Therefore an error is |
|
|
48 |
% signalled and the correction is left to the user. |
|
|
49 |
if any (diag (D) < 0), |
|
|
50 |
error (sprintf (['[ %d ] negative eigenvalues computed from the' ... |
|
|
51 |
' covariance matrix.\nThese are due to rounding' ... |
|
|
52 |
' errors in Matlab (the correct eigenvalues are\n' ... |
|
|
53 |
'probably very small).\nTo correct the situation,' ... |
|
|
54 |
' please reduce the number of dimensions in the' ... |
|
|
55 |
' data\nby using the ''lastEig'' argument in' ... |
|
|
56 |
' function FASTICA, or ''Reduce dim.'' button\nin' ... |
|
|
57 |
' the graphical user interface.'], ... |
|
|
58 |
sum (diag (D) < 0))); |
|
|
59 |
end |
|
|
60 |
|
|
|
61 |
% ======================================================== |
|
|
62 |
% Calculate the whitening and dewhitening matrices (these handle |
|
|
63 |
% dimensionality simultaneously). |
|
|
64 |
whiteningMatrix = inv (sqrt (D)) * E'; |
|
|
65 |
dewhiteningMatrix = E * sqrt (D); |
|
|
66 |
|
|
|
67 |
% Project to the eigenvectors of the covariance matrix. |
|
|
68 |
% Whiten the samples and reduce dimension simultaneously. |
|
|
69 |
if b_verbose, fprintf ('Whitening...\n'); end |
|
|
70 |
newVectors = whiteningMatrix * vectors; |
|
|
71 |
|
|
|
72 |
% ======================================================== |
|
|
73 |
% Just some security... |
|
|
74 |
if ~isreal(newVectors) |
|
|
75 |
error ('Whitened vectors have imaginary values.'); |
|
|
76 |
end |
|
|
77 |
|
|
|
78 |
% Print some information to user |
|
|
79 |
if b_verbose |
|
|
80 |
fprintf ('Check: covariance differs from identity by [ %g ].\n', ... |
|
|
81 |
max (max (abs (cov (newVectors', 1) - eye (size (newVectors, 1)))))); |
|
|
82 |
end |