ardecode.m

来自「常用图像压缩编码码matlab实现。包括:DM编码、变换编码(FFT和DCT)、」· M 代码 · 共 34 行

M
34
字号
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

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?