-
Notifications
You must be signed in to change notification settings - Fork 31
/
whiten.m
59 lines (54 loc) · 1.54 KB
/
whiten.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
function [Xwh, mu, invMat, whMat] = whiten(X,epsilon)
%function [X,mu,invMat] = whiten(X,epsilon)
%
% ZCA whitening of a data matrix (make the covariance matrix an identity matrix)
%
% WARNING
% This form of whitening performs poorly if the number of dimensions are
% much greater than the number of instances
%
%
% INPUT
% X: rows are the instances, columns are the features
% epsilon: small number to compensate for nearly 0 eigenvalue [DEFAULT =
% 0.0001]
%
% OUTPUT
% Xwh: whitened data, rows are instances, columns are features
% mu: mean of each feature of the orginal data
% invMat: the inverse data whitening matrix
% whMat: the whitening matrix
%
% EXAMPLE
%
% X = rand(100,20); % 100 instance with 20 features
%
% figure;
% imagesc(cov(X)); colorbar; title('original covariance matrix');
%
% [Xwh, mu, invMat, whMat] = whiten(X,0.0001);
%
% figure;
% imagesc(cov(Xwh)); colorbar; title('whitened covariance matrix');
%
% Xwh2 = (X-repmat(mean(X), size(X,1),1))*whMat;
% figure;
% plot(sum((Xwh-Xwh2).^2),'-rx'); title('reconstructed whitening error (should be 0)');
%
% Xrec = Xwh*invMat + repmat(mu, size(X,1),1);
% figure;
% plot(sum((X-Xrec).^2),'-rx'); ylim([-1,1]); title('reconstructed data error (should be zero)');
%
% Author: Colorado Reed [email protected]
% Last Updated: Peng Qi, Jul. 23, 2013
if ~exist('epsilon','var')
epsilon = 0.0001;
end
mu = mean(X);
X = bsxfun(@minus, X, mu);
A = X'*X / sqrt(size(X,1)-1);
[V,D,notused] = svd(A);
whMat = V*sqrtm(inv(D + eye(size(D))*epsilon))*V';
Xwh = X*whMat;
invMat = pinv(whMat);
end