# Whiten a matrix: Matlab code

Whitening a matrix is a useful preprocessing step in data analysis. The goal is to transform matrix X into matrix Y such that Y has identity covariance matrix. This is straightforward enough but in case you are too lazy to write such a function here’s how you might do it:

```function [X] = whiten(X,fudgefactor)
X = bsxfun(@minus, X, mean(X));
A = X'*X;
[V,D] = eig(A);
X = X*V*diag(1./(diag(D)+fudgefactor).^(1/2))*V';
end```

You can read about bsxfun here if you are unfamiliar with this function. Because the results of whitening can be noisy, a fudge factor is used so that eigenvectors associated with small eigenvalues do not get overamplified. Thus the whitening is only approximate. Here’s an image patch whitened this way:

Unsurprisingly this accentuates high frequencies in the image. At the bottom you can see the covariance matrix of the set of natural image patches before and after whitening.

Update: Here’s the explanation for why this works as well as a reference you may cite.

## 5 thoughts on “Whiten a matrix: Matlab code”

1. Champenois says:

Hi

How did you calculate the covariance matrixon the left bottom corner?

Thanks

2. [...] Previously, I showed how to whiten a matrix in Matlab. This involves finding the inverse square root of the covariance matrix of a set of observations, which is prohibitively expensive when the observations are high-dimensional – for instance, high-resolution natural images. [...]

3. Thanks for the snippet. But what would be a good value for fudgefactor? 0-1 or higher?

• xcorr says:

Something like 1e-6 times the largest eigenvalue of the A matrix.

4. [...] directly on a design matrix X where you would use the eigenvalue decomposition on X’X; whitening images is one application of this. It’s an important tool to add to your belt. Eco World Content From Across The Internet. [...]