www.gusucode.com > 图像压缩编码码matlab实现 > 图像压缩编码码matlab实现/算术编码/ardecode.m

    function symseq = ardecode(symbol, pr, codeword, symlen)
%给定字符概率的算术编码
%输出:symse:字符串
%输入:symbol:由字符组成的行向量
%      pr:字符出现的概率
%      codeword:码字
%      symlen:待解码字符串长度
format long
high_range = [];
for k = 1 : length(pr),
    high_range = [high_range sum(pr(1 : k))];
end
low_range = [0 high_range(1 : length(pr) - 1)];
prmin = min(pr);
symseq = [];
symseq = [];
for i = 1 : symlen,
    index = max(find(low_range <= codeword));
    codeword = codeword - low_range(index);
    
    %duo to numerical error, sometimes the encoded number
    %will be slightly smaller than the current lower bound.
    %If this happens, a correction is required.
    if abs(codeword - pr(index)) < 0.01 * prmin,
        index = index + 1;
        codeword = 0;
    end
    symseq = [symseq symbol(index)];
    codeword = codeword/pr(index);
    if abs(codeword) < 0.01 * prmin,
        i = symlen + 1;        %break the for loop immediately
    end
end