图像压缩编码码matlab实现——算术编码

clear all
clc
format long;
symbol = ['abcd'];
pr = [0.1 0.4 0.2 0.3];
seqin = ['cadacdb'];
codeword = arencode(symbol, pr, seqin)
seqout = ardecode(symbol, pr, codeword, 7)

 

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
 

 

function arcode = arencode(symbol, pr, seqin)
%算术编码
%输出:码串
%输入:symbol:字符行向量
%      pr:字符出现概率
%      seqin:待编码字符串

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)];
sbidx = zeros(size(seqin));
for i = 1: length(seqin),
    sbidx(i) = find(symbol == seqin(i));
end

low = 0; high = 1;
for i = 1: length(seqin),
    range = high - low;
    high = low + range * high_range(sbidx(i));
    low = low + range * low_range(sbidx(i));
end
arcode = low;
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fpga和matlab

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值