
%  COMPUTE_WEIGHTS expects a three dimensional input matrix M(codeword, vector_elemetns, codebook)
%  
%  It returns a 2 dimsnsional vector V(codebook, codeword) which contains the weight
%  for each codeword.

function weights = compute_weights(set_of_codebooks)
     
for i=1:size(set_of_codebooks, 3)
    for j=1:size(set_of_codebooks, 1)
        weights(i, j) = find_vector_weight(set_of_codebooks, i, j);
    end
end



function a_weight = find_vector_weight(set_of_codebooks, codebook_index, vector_index)
     
        sum=0;
        for k=1:size(set_of_codebooks, 3)
            if k~=codebook_index              % looking at all codebooks but the one the vector is from...
	       sum = sum + 1/distance_to_nearest(set_of_codebooks(vector_index, :, codebook_index), set_of_codebooks(:,:, k));
            end
        end
	a_weight = 1/sum;



function min_distance =  distance_to_nearest(codevector_in_question, codebook_to_compare)

     min_distance = distance(codevector_in_question, codebook_to_compare(1, :));
     for i=2:size(codebook_to_compare, 1)
	    if min_distance > distance(codevector_in_question,  codebook_to_compare(i, :))
                     min_distance = distance(codevector_in_question, codebook_to_compare(i, :));
	    end
     end


     
function dist =  distance(vector1, vector2)

     dist = disteusq(vector1, vector2);
