霍夫曼编码——matlab代码解释

这里对霍夫曼原理不做多加描述,主要讲解代码。

1.代码第一步主要是生成一个编码表B,它的第一列记录原始数据降序排列后的数,将两个最小的概率相加后,再进行排序记录到第二列,依次记录。以0.4 0.2 0.2 0.1 0.1为例

其中B的最后一行记录特征元素的行位置,特征元素是指两个最小概率之和相加的值。如,第一列0.1+0.1=0.2,因为在程序中设置了“若概率之和与原信源中的某概率相等,将概率之和向上排”,所以特征元素在第二行,记为2. 代码如下

clc;clear all;close all;
A=[0.4 0.2 0.2 0.1 0.1];
A=sort(A,'descend');%按降序排列
T=A;
[m,n]=size(A);%一行七列
B=zeros(n,n-1);%空的编码表(矩阵)
B(:,1)=T;%生成编码表的第一列
r=B(n,1)+B(n-1,1);%最后两个元素相加的值赋给r
T(n-1)=r;%令第n-1个数为r的值
T(n)=0;
T=sort(T,'descend');%将加完后的概率再次进行排序
t=n-1;
for j=2:n-1%生成编码表的其他各列,从第2列开始
    B(1:t,j)=T(1:t);%B每一列记录相加完排序后的数列
    K=find(T==r);%%T是向量,r是要查找的元素值,返回位置K
    %B(n,j)=K(end);%从第二列开始,每列的最后一个元素记录特征元素在该列的位置 概率之和往下排
    B(n,j)=K(1);%从第二列开始,每列的最后一个元素记录特征元素在该列的位置,概率之和往上排;
    %如果合并后的概率与其他概率相等, 将合并概率当大概率排列
    r=(B(t-1,j)+B(t,j));%最后两个元素相加
    T(t-1)=r;
    T(t)=0;
    T=sort(T,'descend');
    t=t-1;
end
B;%输出编码表 

2.代码第二部分是进行编码

 先对最后一列进行编码,大为1,小为0.

再从倒数第二列开始编码,这时候分为两部分编码,即其他概率和最小两个概率分别编码。

先对其他概率编码:

如,倒数第二列,对第一个0.4,先查找到它在后一列的位置,根据位置取出它的编码,把这个编码加到0.4 的前面。因为这个0.4不是两个最小概率,不用相加,那么进行下一次排序后的编码仍然是相同的。

注意,这里有一行代码 B(B(n,j+1),j+1)=-1; 令后面一列特征元素为-1,是为避免在查找位置查错了。这里以倒数第三列为例,对于0.4和0.2,查找0.4在后一列的位置时有两个0.4,其中有一个为特征元素,所以令特征元素为-1,可以准确查到0.4的位置。

对最小两个概率编码,它是先查找特征元素的位置,取出特征元素的编码,加到他们俩前面,再大的加1,小的加0.如,从倒数第二列开始,特征元素为0.4+0.2=0.6,0.6在后一列的行数为1,取出它的编码为1,0.4大为11,0.2为10.代码如下

ENDc1={'1','0'};%给最后一列的元素编码
ENDc=ENDc1;
t=3;
d=1;
for j=n-2:-1:1%从倒数第二列开始依次对各列元素编码 
    for i=1:t-2
        if i>1&&B(i,j)==B(i-1,j)
   d=d+1;
        else
            d=1;
         end
        B(B(n,j+1),j+1)=-1;
        temp=B(:,j+1);%令temp为B的第j+1列
        x=find(temp==B(i,j));%X记录这一列前t-2个元素在后一列的位置
        ENDc(i)=ENDc1(x(d));
    end
    %给每一列两个最小的概率编码,t-1,t
    y=B(n,j+1);%最小两个元素相加的概率在后一列的位置
    
    ENDc{t-1}=[ENDc1{y},'1'];%第t-1个元素在后一列的编码加到前面,并在后面加一
    ENDc{t}=[ENDc1{y},'0'];
    t=t+1;
    ENDc1=ENDc;
end
A%排序后的原概率序列
ENDc%编码结果
 

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
以下是使用Matlab实现霍夫曼编码代码: ```matlab function [dict, avglen] = huffmandict(symbols, prob) % HUFFMANDICT Create Huffman code dictionary. % [DICT,AVGLEN] = HUFFMANDICT(SYMBOLS,PROB) returns a Huffman code % dictionary, DICT, for the given input symbols and their probabilities, % PROB. DICT is a cell array of size 2-by-N, where N is the number of % input symbols. The first row of DICT contains the input symbols, and % the second row contains the corresponding Huffman codewords. AVGLEN % is the average length of the Huffman codewords. % % Class support for inputs SYMBOLS and PROB: % float: double, single % % Example: % symbols = {'A', 'B', 'C', 'D', 'E'}; % prob = [0.35, 0.15, 0.2, 0.05, 0.25]; % [dict, avglen] = huffmandict(symbols, prob); % % See also HUFFMANENCODE, HUFFMANDECODE. % Copyright 2011-2017 The MathWorks, Inc. % Validate input arguments narginchk(2, 2); validateattributes(symbols, {'cell'}, {'vector'}, mfilename, 'SYMBOLS'); validateattributes(prob, {'numeric'}, {'vector', 'real', 'nonnegative', '<=', 1}, mfilename, 'PROB'); % Sort symbols and probabilities in decreasing order of probability [prob, sortIndex] = sort(prob, 'descend'); symbols = symbols(sortIndex); % Create initial cell array of nodes n = numel(prob); nodes = cell(n, 1); for i = 1:n nodes{i} = i; end % Build Huffman tree while numel(nodes) > 1 % Combine two nodes with smallest probability newProb = prob(end-1) + prob(end); nodes{end+1} = [nodes{end-1}, nodes{end}]; prob(end-1:end) = []; prob(end+1) = newProb; % Sort nodes and probabilities in decreasing order of probability [prob, sortIndex] = sort(prob, 'descend'); nodes = nodes(sortIndex); end % Traverse Huffman tree to generate codewords dict = cell(2, n); traverseHuffmanTree(nodes{1}, '', dict); % Sort codewords by input symbols [~, sortIndex] = sort(sortIndex); dict = dict(:, sortIndex); % Calculate average codeword length avglen = sum(prob .* cellfun('length', dict(2,:))); end function traverseHuffmanTree(node, prefix, dict) % Traverse Huffman tree to generate codewords if isscalar(node) dict{1, node} = node; dict{2, node} = prefix; else traverseHuffmanTree(node(1), [prefix, '0'], dict); traverseHuffmanTree(node(2), [prefix, '1'], dict); end end ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

sftmnggsujgff.shhgft

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

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

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

打赏作者

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

抵扣说明:

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

余额充值