zoukankan      html  css  js  c++  java
  • (matlab)对某一序列进行Huffman编码

    使用说明:输入序列,然后对此序列进行多元(默认是二元)Huffman编码

    @author Boychenney

    @version 2011年12年30日

    Contents

    参数说明

    @param Pj_i 转换概率矩阵

    @param Pi 初始输入概率分布向量,一定要求是列向量

    *@param level 默认为2,即进行二元Huffman编码

    @return comp 编码后的码字

    *如果要多个输出,则第二个是字典(dict),第三个输出是信源熵(entropy),第四个是相应的编码信息(str)

    function [comp,varargout]=huffEncode(seq,p,level) 
    %默认进行二元Huffman编码 
    if nargin < 3 
     level=2; 
    end 

     

    %计算比例log2(r) 
    a=log2(level); 

     

    %获得长度 
    len=length(p); 

     

    %给符号编号,从1开始到len 
    symbols=1:len; 

     

    %调用huffmandict函数生成字典——符号映射成码字 
    [dict,avg_len]=huffmandict(symbols,p,level); 

     

    %计算熵,在这之前需要处理概率为零的值 
    p(p<1e-6)=1; 
    entropy=-p*log2(p.'); 

     

    %对序列进行编码:huffmanenco 
    comp=huffmanenco(seq,dict); 

     

    %计算其它参数 
    source_len=length(seq); %输入序列长度 
    eta = entropy/(avg_len*a); % 理论编码效率:eta=H(x)/(平均码长*log2(r)) 
    codeseq_len=length(comp); %编码后序列的长度 
    actual_avg_len=codeseq_len/source_len; %实际平均码长=编码后序列长/编码前序列长 
    actual_eta= entropy/(actual_avg_len*a); %实际编码效率=H(x)/(实际平均码长*log2(r)); 

     

    %生成信息 
    str = sprintf('编码信息:\n  信息源熵:%8.4f bit/符号;输入的序列长度:%d\n  理论平均长度:%8.4f;实际平均长度:%8.4f\n 理论编码效率:%8.4f%%;实际编码效率:%8.4f%% ',... 
     entropy,source_len,avg_len,actual_avg_len,eta*100,actual_eta*100); 
     %决定输出 
     if nargout>1 
     varargout{1}=dict; 
     end 
     if nargout>2 
     varargout{2}=entropy; 
     end 
     if nargout>3 
     varargout{3}=str; 
     end 
    【代码示例】 

    p=[0.2,0.19,0.18,0.17,0.15,0.1,0.01];%信源有7个符号,且概率分布为p

    slen=1000;

    seq=randsrc(1,slen,[1:7;p]);%生成1000个由1~7组成的随机序列

    [comp,dict,entropy,str]=huffEncode(seq,p);%对序列进行huffman编码

    disp('---待编码信源符号及概率分布---');

    table=[1:7;p];

    disp(table);

    disp('-----二元编码---=');

    disp(str);%显示二元Huffman编码效果

    [comp2,dict2,entropy2,str2]=huffEncode(seq,p,3);

    disp('-----三元编码---=');

    disp(str2);%显示三元Huffman编码效果

    【结果】
    
    ---待编码信源符号及概率分布---
    
      Columns 1 through 6
    
        1.0000    2.0000    3.0000    4.0000    5.0000    6.0000
    
        0.2000    0.1900    0.1800    0.1700    0.1500    0.1000
    
      Column 7
    
        7.0000
    
        0.0100
    
    -----二元编码---=
    
    编码信息:
    
      信息源熵:  2.6087 bit/符号;输入的序列长度:1000
    
      理论平均长度:  2.7200;实际平均长度:  2.7440
    
     理论编码效率: 95.9075%;实际编码效率: 95.0686% 
    
    -----三元编码---=
    
    编码信息:
    
      信息源熵:  2.6087 bit/符号;输入的序列长度:1000
    
      理论平均长度:  1.8000;实际平均长度:  1.7980
    
     理论编码效率: 91.4386%;实际编码效率: 91.5404% 
    


    Published with MATLAB® 7.11

  • 相关阅读:
    Deep Reinforcement Learning: Pong from Pixels
    [TensorFlow] Creating Custom Estimators in TensorFlow
    [TensorFlow] Introducing TensorFlow Feature Columns
    [TensorFlow] Introduction to TensorFlow Datasets and Estimators
    [Golang] GoConvey测试框架使用指南
    [机器学习]一个例子完美解释朴素贝叶斯分类器
    [深度学习]理解RNN, GRU, LSTM 网络
    [深度学习]CNN--卷积神经网络中用1*1 卷积有什么作用
    Tensorflow学习笔记(2):tf.nn.dropout 与 tf.layers.dropout
    TensorFlow学习笔记(1):variable与get_variable, name_scope()和variable_scope()
  • 原文地址:https://www.cnblogs.com/boychenney/p/2307892.html
Copyright © 2011-2022 走看看