找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 3019|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

(信息論編碼)Huffman編碼

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:105323 發(fā)表于 2016-2-23 01:13 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
  1. #include <stdio.h>
  2. #include <math.h>

  3. /*huffman tree 結(jié)構(gòu)定義*/
  4. typedef struct
  5. {
  6. float weight;
  7.     int   flag;
  8.   int   parent;
  9.   int   lchild;
  10.   int   rchild;
  11. }huffnode;



  12. void main(void)
  13. {
  14.   huffnode huff_node[50];         /*為huffman tree 定義了50個(gè)結(jié)點(diǎn)*/
  15.   int huff_code[50][10],cd,d[10],a[50];  
  16.   int i,j,x1,x2,n,c,p;
  17.   float m1,m2,temp,hx=0,L=0,sum=0;/*hx為信源熵,L為平均碼長(zhǎng)*/
  18.   printf("the number of input information source:\nN=  ");      /*輸入信源符號(hào)的個(gè)數(shù)*/
  19.   scanf("%d",&n);
  20.   for(i=0;i<=2*n-1;i++)   /*初始化huffman樹各個(gè)結(jié)點(diǎn)的值*/
  21.   {
  22.    huff_node[i].weight=0;
  23.     huff_node[i].parent=0;
  24.     huff_node[i].flag=0;
  25.     huff_node[i].lchild=-1;
  26.     huff_node[i].rchild=-1;
  27.    }
  28.    printf("Please input probability distribution :\n");
  29.    for(i=0;i<n;i++)    /*輸入信源輸入分布*/
  30.    {
  31.     printf("p[%d]=  ",i+1);
  32.     scanf("%f",&temp);
  33.     sum+=temp;
  34.     huff_node[i].weight=temp;
  35.     hx=hx-temp*3.332*log10(temp);  /*求信源的熵H(X)*/
  36.    }

  37.    if(fabs((sum-1))>0.00001) /*判斷信源輸入分布是否正確*/
  38.    {
  39.     printf("Error!");
  40.     exit(-1);
  41.    }

  42. /*構(gòu)建霍夫曼樹(參考數(shù)據(jù)結(jié)構(gòu)書)*/
  43.   for(i=0;i<n-1;i++)
  44.   {
  45.     m1=m2=1.1;
  46.     x1=x2=0;
  47.     for(j=0;j<n+i;j++)
  48.      {
  49.       if(huff_node[j].weight<m1&&huff_node[j].flag==0)
  50.       {
  51.        m2=m1;
  52.        x2=x1;
  53.        m1=huff_node[j].weight;
  54.        x1=j;
  55.       }
  56.       else
  57.       if(huff_node[j].weight<m2&&huff_node[j].flag==0)
  58.       {
  59.        m2=huff_node[j].weight;
  60.        x2=j;
  61.       }
  62.    
  63.     }
  64.          huff_node[x1].parent=n+i;
  65.   huff_node[x2].parent=n+i;         /*將找出的兩棵子樹合并為一棵子樹*/
  66.   huff_node[x1].flag=1;
  67.   huff_node[x2].flag=1;
  68.   huff_node[n+i].weight=huff_node[x1].weight+huff_node[x2].weight;
  69.   
  70.   if(huff_node[x1].weight==huff_node[x2].weight)       /*如果值相等,則將其置于最上面*/
  71.    {   
  72.     huff_node[n+i].lchild=x1;
  73.     huff_node[n+i].rchild=x2;
  74.    }
  75.    else
  76.    {
  77.     huff_node[n+i].lchild=x2;
  78.     huff_node[n+i].rchild=x1;
  79.    }
  80. }


  81. /*求信源符號(hào)的huffman編碼*/
  82.   for(i=0;i<n;i++)
  83.   {
  84.    cd=n;      
  85.    c=i;
  86.    p=huff_node[c].parent;
  87.    while(p!=0)      /*為信源si編碼*/
  88.      {
  89.          if(huff_node[p].lchild==c)    /*左孩子編碼為0*/
  90.      d[cd]=0;
  91.         else  
  92.      d[cd]=1;    /*左孩子編碼為1*/
  93.      cd=cd-1;
  94.      c=p;
  95.      p=huff_node[p].parent;
  96.      }
  97.     cd++;     
  98.   for(j=cd;j<=n;j++)   /*存儲(chǔ)信源si的huffman編碼*/
  99.   huff_code[i][j]=d[j];  
  100.   a[i]=cd;   
  101.   }
  102.   
  103. /*輸出信源符號(hào)的huffman編碼、信源熵和平均碼長(zhǎng)*/
  104.   printf("\ninformaition source\thuffmancode\n");
  105.   for(i=0;i<n;i++)
  106.   {
  107.     printf("p[%d]=%2.3f\t\t",i+1,huff_node[i].weight);/*輸出信源分布*/
  108.     for(j=a[i];j<=n;j++)
  109.     printf("%d",huff_code[i][j]);              /*輸出huffman編碼*/
  110.     /*求平均碼長(zhǎng)*/
  111.     L=L+(n+1-a[i])*huff_node[i].weight;                /*(n-cd+1)表示的是信源符號(hào)si的碼長(zhǎng)*/            
  112.     printf("\n");
  113.   }
  114.   printf("\nH(X)=%.2f bit/Symbol\n",hx);/*輸出信源熵*/
  115.   printf("The average length of code is:\tL=%.2f\n",L);/*輸出平均碼長(zhǎng)*/
  116.   printf("The rate is:\tR=%.3f",hx/L);           /*輸出編碼效率*/
  117. }
  118.   
復(fù)制代碼


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表