欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3079|回復: 0
收起左側

(信息論編碼)Huffman編碼

[復制鏈接]
ID:105323 發表于 2016-2-23 01:13 | 顯示全部樓層 |閱讀模式
  1. #include <stdio.h>
  2. #include <math.h>

  3. /*huffman tree 結構定義*/
  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個結點*/
  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為平均碼長*/
  18.   printf("the number of input information source:\nN=  ");      /*輸入信源符號的個數*/
  19.   scanf("%d",&n);
  20.   for(i=0;i<=2*n-1;i++)   /*初始化huffman樹各個結點的值*/
  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. /*構建霍夫曼樹(參考數據結構書)*/
  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. /*求信源符號的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++)   /*存儲信源si的huffman編碼*/
  99.   huff_code[i][j]=d[j];  
  100.   a[i]=cd;   
  101.   }
  102.   
  103. /*輸出信源符號的huffman編碼、信源熵和平均碼長*/
  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.     /*求平均碼長*/
  111.     L=L+(n+1-a[i])*huff_node[i].weight;                /*(n-cd+1)表示的是信源符號si的碼長*/            
  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);/*輸出平均碼長*/
  116.   printf("The rate is:\tR=%.3f",hx/L);           /*輸出編碼效率*/
  117. }
  118.   
復制代碼


回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表