2012-04-26 87 views
1

您好我无法用扩展霍夫曼编码编码1000符号消息。我已经有字典了。我只需要编码消息。但是,我不知道如何做到这一点。有任何想法吗?在Matlab中扩展霍夫曼编码

我正在使用Matlab bdw。

+0

在哪种格式中,你有你的字典和消息?这些信息将有助于理解你的霍夫曼编码实现。 – nrz 2012-04-26 19:32:33

+0

你可以在这里的代码中检查'norm2huff' http://www.mathworks.com/matlabcentral/fileexchange/4900-huffman-code – petrichor 2012-04-26 22:43:23

回答

2

这里是我使用的代码:我已经试过这种方法延长霍夫曼

%Extended Huffman 
prob=0.1; 
m=4; 

%Generating the probabilities 

for i = 1:2^m 
    q(i) = 1; 
    for j=0: m-1 
     b=2^j; 
     if bitand(i-1,b) 
      q(i)= q(i)*prob; 
     else 
      q(i)= q(i)*(1-prob); 
    end 
    end 
end 


disp ('Sum of probabilities'); 
disp (sum(q)); 

disp('Entropy per symbol');%should be equal to 1 
E=sum(q.*log2(1./q)); 

disp(E/m); 


%huffman 

s=0:2^m-1; %There are 16 symbols from 0000 -> 1111 
[dict,avglen] = huffmandict(s,q); %probabilities 

和邮件大小没有下降,但不是很多,我不知道这是否是一个正确的做法。消息首先被分成4位,并将得到的十进制值与字典进行比较。然后获得新的编码消息:

for j=(0:4:1000-1) 
    newcode=message(j+1:j+4); %Dividing the message into 4 bits and saving the  
           %corresponding decimal values 
    array(:,a)=bi2de(newcode); 
    a=a+1; 
end 

for(f=1:250) 
    for(i=1:15) 
    if(array(f)==cell2mat((dict(i,1)))) %cell2mat will obtain the value of the cell 
    encodedmsg= horzcat(encodedmsg, dict(i,2)); %horzcat will concatenate the array     with its corresponding codeword 
    end 
end 
end