【问题标题】:Extended Huffman coding in MatlabMatlab 中的扩展霍夫曼编码
【发布时间】:2012-04-26 19:06:45
【问题描述】:

您好,我无法使用扩展霍夫曼编码对 1000 个符号的消息进行编码。我已经有字典了。我只需要对消息进行编码。但是,我不知道该怎么做。有什么想法吗?

我正在使用 Matlab bdw。

【问题讨论】:

标签: matlab huffman-code


【解决方案1】:

这是我使用的代码:

%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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多