2012-04-26 85 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