数据压缩 第四次作业

5.给定如表4-9所示的概率模型,求出序列a1a1a3a2a3a1的实值标签。

算术编码代码

clc,clear all;  
symbol=['abc'];          %输入一串序列 
pr=[0.2 0.3 0.5];        %各字符出现的概率  
temp=[0.0 0.2 0.5 1.0];  %各字符的累积概率
orignal=temp;  
in=input('input a string of abc:');  
n=length(in);  
%编码  
for i=1:n  
    width=temp(4)-temp(1);  
    w=temp(1);  
    switch in(i)  
        case 'a'  
            m=1;  
        case 'b'  
            m=2;  
        case 'c'  
            m=3;  
        otherwise  
            error('do not input other character');  
    end  
    temp(1)=w+orignal(m)*width;  
    temp(4)=w+orignal(m+1)*width;  
    left=temp(1);  
    right=temp(4);  
    fprintf('left=%.6f',left);  
    fprintf('    ');  
    fprintf('right=%.6f\n',right);  
end  
encode=(temp(1)+temp(4))/2 

结果:

input a string of abc:'aacbca'
left=0.000000    right=0.200000
left=0.000000    right=0.040000
left=0.020000    right=0.040000
left=0.024000    right=0.030000
left=0.027000    right=0.030000
left=0.027000    right=0.027600

encode =

    0.0273

结论:  a1a1a3a2a3a1的实值标签为 0.0273

 

6.对于表4-9给出的概率模型,对于一个标签为0.63215699的长度为10的序列进行解码。

算术解码代码:

encode=0.63215699;     %序列标签
n=10;    %序列长度
pr=[0.2 0.3 0.5];        %各字符出现的概率  
temp=[0.0 0.2 0.5 1.0];  %各字符的累积概率
orignal=temp; 
decode=['0'];  
for i=1:n  
    fprintf('tmp=%.6f\n',encode);  
    if(encode>=orignal(1)& encode<orignal(2))  
        decode(i)='a';  
        t=1;  
    elseif(encode>=orignal(2)& encode<orignal(3))  
        decode(i)='b';  
        t=2;  
    else  
        decode(i)='c';  
        t=3;  
    end  
    encode=(encode-orignal(t));  
    encode=encode/pr(t);  
end  
decode  

结果:

tmp=0.632157
tmp=0.264314
tmp=0.214380
tmp=0.047933
tmp=0.239666
tmp=0.132219
tmp=0.661093
tmp=0.322185
tmp=0.407284
tmp=0.690947

decode =

cbbabacbbc

结论:  标签为0.63215699的长度为10的序列为  a3a2a2a1a2a1a3a2a2a3

posted @ 2016-11-05 17:33  黑眼眶  阅读(370)  评论(0编辑  收藏  举报