% [E,E_min,validcodeword] = FERD(c,s) implements the Free Energy Ribosome
% Decoding Algorithm. It compares the subsequence s of length N with L
% codewords defined by the codebook c and finds the most complimentary
% codeword for s, i.e. the codeword that results in a minimum free energy.
%
% E             :  is a vecotor of length L that represents the free energy 
%                  dissipated on binding with the nuceotide doublet c{k}(n) 
%                  and c{k}(n+1) (i.e. the (n)th and the (n+1)th  sybols of 
%                  the (k)th codeword. 
% E_min         : is the minimum free energy of the vector E. 
% validcodeword : is the most complimentary codeword to s.

function E = FreeEnergyDist(RS,codebook)

L = length(codebook);
n = length(codebook{1});
E(1:L) = 0;  % Initialization
for i = 1:L
    for j = 1:n-1
        if isequal({codebook{i}(j:j+1)},{RS{1}(j:j+1)})
            FED = codebook{i}(j:j+1);
            E(i) = E(i) + energy(FED);
        else
            E(i) = E(i);
        end
    end
end

