[例3.6]某对称离散信道的信道转移概率矩阵P为:1/31/31/61/61/61/61/31/3计算其最佳信源概率和信道容量C。
附:程序代码如下:#include#include#defineMAX50doubleCalculate_a(intk,doublepa[]);doubleCalculate_C1(doublepa[],doublea[]);doubleCalculate_C2(doublea[]);intr,s;doublepba[MAX][MAX];voidmain(){ inti,j;doubleC1,C2,E; doublea[MAX],pa[MAX]; E=0.000001; printf("请输出信源符号个数r:\n"); scanf("%d",&r); printf("请输出信宿符号个数s:\n"); scanf("%d",&s); printf("请输出精确度E:\n"); scanf("%lf",&E); printf("请输出信源P[ai]:\n"); for(i=0;i<r;i++) scanf("%lf",&pa[i]); printf("请输出信道转移概率矩阵P[bj][ai]:\n"); for(i=0;i<r;i++) for(j=0;j<s;j++) scanf("%lf",&pba[i][j]); do { for(i=0;i=E) { doublesum=0; for(i=0;i<r;i++) sum+=pa[i]*a[i]; for(i=0;i<r;i++) pa[i]=pa[i]*a[i]/sum; } else { printf("最佳信源概率:\n"); for(i=0;i=E);printf("信道容量为:%lf\n",C1/log(2));}doubleCalculate_a(intk,doublepa[]){ inti,j; doubletemp,sum2=0; for(j=0;j<s;j++) { doublesum1=0; for(i=0;i<r;i++) { sum1+=pa[i]*pba[i][j]; } temp=pba[k][j]/sum1; temp=log(temp); sum2+=pba[k][j]*temp; } returnexp(sum2);}doubleCalculate_C1(doublepa[],doublea[]){ inti; doublesum=0; for(i=0;i<r;i++) sum+=pa[i]*a[i]; returnlog(sum);}doubleCalculate_C2(doublea[]){ inti; doublemax=a[0]; for(i=0;i<r;i++) if(max<a[i])max=a[i]; returnlog(max);}
1