changeset 200:69ce11724b1f luisf_dev

Added dictionary decorrelation for multiple dictionaries
author bmailhe
date Tue, 20 Mar 2012 12:25:50 +0000
parents d50f5bdbe14c
children 5140b0e06c22
files DL/two-step DL/dico_color_separate.m DL/two-step DL/dico_decorr_symetric.m
diffstat 2 files changed, 201 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DL/two-step DL/dico_color_separate.m	Tue Mar 20 12:25:50 2012 +0000
@@ -0,0 +1,95 @@
+function [colors nbColors] = dico_color_separate(dico, mu)
+    % DICO_COLOR cluster several dictionaries in pairs of high correlation
+    % atoms. Called by dico_decorr.
+    %
+    % Parameters:
+    % -dico: the dictionaries
+    % -mu: the correlation threshold
+    %
+    % Result:
+    % -colors: a cell array of indices. Two atoms with the same color have
+    % a correlation greater than mu
+    
+    
+    numDico = length(dico);
+    colors = cell(numDico,1);
+    for i = 1:numDico
+        colors{i} = zeros(length(dico{i}),1);
+    end
+    
+    G = cell(numDico);
+    
+    % compute the correlations
+    for i = 1:numDico
+        for j = i+1:numDico
+            G{i,j} = abs(dico{i}'*dico{j});
+        end
+    end
+    
+    % iterate on the correlations higher than mu
+    c = 1;
+    [maxCorr, i, j, m, n] = findMaxCorr(G);
+    while maxCorr > mu
+        % find the highest correlated pair
+        
+        % color them
+        colors{i}(m) = c;
+        colors{j}(n) = c;
+        c = c+1;
+        
+        % make sure these atoms never get selected again
+        % Set to zero relevant lines in the Gram Matrix
+        for j2 = i+1:numDico
+            G{i,j2}(m,:) = 0;
+        end
+        
+        for i2 = 1:i-1
+            G{i2,i}(:,m) = 0;
+        end
+        
+        for j2 = j+1:numDico
+            G{j,j2}(n,:) = 0;
+        end
+        
+        for i2 = 1:j-1
+            G{i2,j}(:,n) = 0;
+        end
+        
+        % find the next correlation
+        [maxCorr, i, j, m, n] = findMaxCorr(G);
+    end
+    
+    % complete the coloring with singletons
+    % index = find(colors==0);
+    % colors(index) = c:c+length(index)-1;
+    nbColors = c-1;
+end
+
+function [val, i, j, m, n] = findMaxCorr(G)
+    %FINDMAXCORR find the maximal correlation in the cellular Gram matrix
+    %
+    %   Input:
+    %   -G: the Gram matrix
+    %
+    %   Output:
+    %   -val: value of the correlation
+    %   -i,j,m,n: indices of the argmax. The maximal correlation is reached
+    %   for the m^th atom of the i^th dictionary and the n^h atom of the
+    %   j^h dictionary
+    
+    val = -1;
+    for tmpI = 1:length(G)
+        for tmpJ = tmpI+1:length(G)
+            [tmpVal tmpM] = max(G{tmpI,tmpJ},[],1);
+            [tmpVal tmpN] = max(tmpVal);
+            if tmpVal > val
+                val = tmpVal; 
+                i = tmpI;
+                j = tmpJ;
+                n = tmpN;
+                m = tmpM(n);
+            end
+        end
+    end
+end
+        
\ No newline at end of file
--- a/DL/two-step DL/dico_decorr_symetric.m	Wed Mar 14 14:42:52 2012 +0000
+++ b/DL/two-step DL/dico_decorr_symetric.m	Tue Mar 20 12:25:50 2012 +0000
@@ -1,15 +1,17 @@
-function dico = dico_decorr_symetric(dico, mu, amp)
+function dico = dico_decorr_symetric(dico, mu)
     %DICO_DECORR decorrelate a dictionary
     %   Parameters:
-    %   dico: the dictionary
+    %   dico: the dictionary, either a matrix or a cell array of matrices.
     %   mu: the coherence threshold
-    %   amp: the amplitude coefficients, only used to decide which atom to
-    %   project
     %
     %   Result:
-    %   dico: a dictionary close to the input one with coherence mu.
+    %   dico: if the input dico was a matrix, then a matrix close to the 
+    %   input one with coherence mu.
+    %   If the input was a cell array, a cell array of the same size
+    %   containing matrices such that the coherence between different cells
+    %   is lower than mu.
     
-    eps = 1e-6; % define tolerance for normalisation term alpha
+    eps = 1e-3; % define tolerance for normalisation term alpha
     
     % convert mu to the to the mean direction
     theta = acos(mu)/2;
@@ -23,39 +25,115 @@
     %         rank = randperm(length(dico));
     %     end
     
-    % several decorrelation iterations might be needed to reach global
-    % coherence mu. niter can be adjusted to needs.
-    niter = 1;
-    while max(max(abs(dico'*dico -eye(length(dico))))) > mu + 0.01
-        % find pairs of high correlation atoms
-        colors = dico_color(dico, mu);
+    % if only one dictionary is provided, then decorrelate it
+    if ~iscell(dico)
+        % several decorrelation iterations might be needed to reach global
+        % coherence mu. niter can be adjusted to needs.
+        niter = 1;
+        while max(max(abs(dico'*dico -eye(length(dico))))) > mu + eps
+            % find pairs of high correlation atoms
+            colors = dico_color(dico, mu);
+            
+            % iterate on all pairs
+            nbColors = max(colors);
+            for c = 1:nbColors
+                index = find(colors==c);
+                if numel(index) == 2
+                    if dico(:,index(1))'*dico(:,index(2)) > 0
+                        %build the basis vectors
+                        v1 = dico(:,index(1))+dico(:,index(2));
+                        v1 = v1/norm(v1);
+                        v2 = dico(:,index(1))-dico(:,index(2));
+                        v2 = v2/norm(v2);
+                        
+                        dico(:,index(1)) = ctheta*v1+stheta*v2;
+                        dico(:,index(2)) = ctheta*v1-stheta*v2;
+                    else
+                        v1 = dico(:,index(1))-dico(:,index(2));
+                        v1 = v1/norm(v1);
+                        v2 = dico(:,index(1))+dico(:,index(2));
+                        v2 = v2/norm(v2);
+                        
+                        dico(:,index(1)) = ctheta*v1+stheta*v2;
+                        dico(:,index(2)) = -ctheta*v1+stheta*v2;
+                    end
+                end
+            end
+            niter = niter+1;
+        end
+        %if a cell array of dictionaries is provided, decorrelate among
+        %different dictionaries only
+    else
+        niter = 1;
+        numDicos = length(dico);
+        G = cell(numDicos);
+        maxCorr = 0;
+        for i = 1:numDicos
+            for j = i+1:numDicos
+                G{i,j} = dico{i}'*dico{j};
+                maxCorr = max(maxCorr,max(max(abs(G{i,j}))));
+            end
+        end
         
-        % iterate on all pairs
-        nbColors = max(colors);
-        for c = 1:nbColors
-            index = find(colors==c);
-            if numel(index) == 2
-                if dico(:,index(1))'*dico(:,index(2)) > 0               
+        while maxCorr > mu + eps
+            % find pairs of high correlation atoms
+            [colors nbColors] = dico_color_separate(dico, mu);
+
+            % iterate on all pairs
+            for c = 1:nbColors
+                for tmpI = 1:numDicos
+                    index = find(colors{tmpI}==c);
+                    if ~isempty(index)
+                        i = tmpI;
+                        m = index;
+                        break;
+                    end
+                end
+                for tmpJ = i+1:numDicos
+                    index = find(colors{tmpJ}==c);
+                    if ~isempty(index)
+                        j = tmpJ;
+                        n = index;
+                        break;
+                    end
+                end
+                
+                if dico{i}(:,m)'*dico{j}(:,n) > 0
                     %build the basis vectors
-                    v1 = dico(:,index(1))+dico(:,index(2));
+                    v1 = dico{i}(:,m)+dico{j}(:,n);
                     v1 = v1/norm(v1);
-                    v2 = dico(:,index(1))-dico(:,index(2));
+                    v2 = dico{i}(:,m)-dico{j}(:,n);
                     v2 = v2/norm(v2);
                     
-                    dico(:,index(1)) = ctheta*v1+stheta*v2;
-                    dico(:,index(2)) = ctheta*v1-stheta*v2;
+                    dico{i}(:,m) = ctheta*v1+stheta*v2;
+                    dico{j}(:,n) = ctheta*v1-stheta*v2;
                 else
-                    v1 = dico(:,index(1))-dico(:,index(2));
+                    v1 = dico{i}(:,m)-dico{j}(:,n);
                     v1 = v1/norm(v1);
-                    v2 = dico(:,index(1))+dico(:,index(2));
+                    v2 = dico{i}(:,m)+dico{j}(:,n);
                     v2 = v2/norm(v2);
                     
-                    dico(:,index(1)) = ctheta*v1+stheta*v2;
-                    dico(:,index(2)) = -ctheta*v1+stheta*v2;
+                    dico{i}(:,m) = ctheta*v1+stheta*v2;
+                    dico{j}(:,n) = -ctheta*v1+stheta*v2;
+                end
+            end
+            niter = niter+1;
+            
+            % Remove noegative components and renormalize
+            for i = 1:length(dico)
+                dico{i} = max(dico{i},0);
+                for m = 1:size(dico{i},2)
+                    dico{i}(:,m) = dico{i}(:,m)/norm(dico{i}(:,m));
+                end
+            end
+            
+            maxCorr = 0;
+            for i = 1:numDicos
+                for j = i+1:numDicos
+                    G{i,j} = dico{i}'*dico{j};
+                    maxCorr = max(maxCorr,max(max(abs(G{i,j}))));
                 end
             end
         end
-        niter = niter+1;
     end
 end
-