# Whitening and unwhitening for sparse coding

Is this procedure for whitening and unwhitening correct?

Given an image $i$:

decompose the image in patches:

patch=im2col(i,[8 8],'sliding');


Whitening step:

1) subtract the mean from each patch (i.e., from each column):

meanP=mean(patch);
patch_m=patch-repmat(mean(patch),[size(patch,1) 1]);


2) divide by the standard deviation:

devP=sqrt(sum(patch.^2));
devpatch_m=sqrt(sum(patch_m.^2));
patch_m_s=patch_m./repmat(sqrt(sum(patch_m.^2)),[size(patch_m,1) 1]);


Unwhitening step:

1) multiply by the standard deviation:

rec_patch= rec_patch .* repmat(devpatch_m, [size(rec_patch,1) 1]);


rec_patch=rec_patch+repmat(meanP,[size(rec_patch,1) 1]);


I have also read that before starting the unwhitening step, it is possible to subtract the mean from the "new patches" obtained from the sparse coding step. Which is the correct procedure?

• depends on the dimensions of patch.... Jan 21, 2013 at 20:11
• @user603 Could you please elaborate a bit, as this alone is unlikely to help the asker? Jan 22, 2013 at 10:35
• if patch is a vector then yes, this is how whitening is done. If patch is matrix then, no. Jan 22, 2013 at 11:58
• @user603 patch is a matrix, whose columns are patches in a vector form. As you can see in the code I posted, mean and std dev are computed for each column. Your answer is definitely obscure. Jan 22, 2013 at 12:16
• then, no, you are not whitening patch correctly. I will answer below. Jan 22, 2013 at 12:35

To whiten a $n$ by $d$ matrix $M$ it is not enough to center and scale: you also have to remove the correlation structure. So, denoting $M'$ your whitened matrix, we have:
$$M'=(M-t_M)'S_M^{-1/2}$$
where $t_M$ is the $d$ vector of columnwize means of $M$ and $S_M$ is $M$'s covariance matrix:
$$S_M=\frac{1}{n-1}(M-t_M)'(M-t_M)$$