Saturday, October 27, 2012

Channel Capacity code in c


#include<stdio.h>
#include<conio.h>
include<math.h>
void main()
{
int i,j,k,1,m,n,x,y,cim,row;
float sumr,sume,cs,p;

float a[10][10];
clrscr();
printf("\n ENTER THE NO OF INPUTS(m)\t:");
scanf("%d",&m);
printf("\n ENTER THE NO OF OUTPUTS(n)\t:");
scanf("%d",&n);
check:
for(i=0;i<m;i++)
{
sumr=0;
for(j=0;j<n;j++)
{
printf("\n ENTER ELEMENT a(%d)(%d): ",i+1,j+1);
  scanf("%f",&a[i][j]);
 sumr=sumr+a[i][j];
}
if (sumr!=l)
{
    printf("\n ADDITION OF ALL ELEMENTS IN ROW %d IS NOT 1",i+1);
    printf("\n PLEASE ENTER VALUE AGAIN");
   goto check;
}
}
 for (j=0;j<n;j++)
{
 clm=row=sumr=sumc=0;
for(i=0;i<m;i++)
{
if (a[i][j]!=0)
{
k=i;
l=j;
sumc=sumc+a[i][j];}
if (a[j][i]!=0)
{
x=j;
y=i;
sumr=sumr+a[j][i];
}
}
   if (sume!=a[k][l])
{
row=1;
break;
}
if(sumr!=a[y][x])
{
clm=1;
break;
}
}
if(m==n)
{
if(m==2) .
{
printf("\n ENTERED MATRIX IS BINAEY SYMMETRIC CHANNEL MATRIX");
p=a[0][1];
cs=l+p*(log(p)/log(2))+(1-p)*((log(1-p)/log(2)));
}
 else
{
printf("\n ENTERED MATRIX IS NOISE FREE CHANNEL MATRIX");
cs=(log(m)/log(2));
}
}
else
{
if(clm==1)
{
  printf("\n ENTERED MATRIX IS LOOS LESS CHANNEL MATRIX");
cs=(log(m)/log(2));
}

else
{
if(row==1)
{
printf("\n ENTERED MATRIX IS DETERMINISTIC MATRIX");
cs=(log(n)/log(2));
}}}
printf("\n\n CHANNEL CAPACITY =%f ,cs);
getch();
return;
}

Output:
ENTER THE NO OF INPUTS(m)      :3
ENTER THE NO OF OUTPUTS(n)   :5

ENTER ELEMENT a(1)(1): 0.75
ENTER ELEMENT a(1)(2): 0.25
ENTER ELEMENT a(1)(3): 0
ENTER ELEMENT a(1)(4): 0
ENTER ELEMENT a(1)(5): 0
ENTER ELEMENT a(2)(1): 0
ENTER ELEMENT a(2)(2): 0.33
ENTER ELEMENT a(2)(3): 0.67
ENTER ELEMENT a(2)(4): 0
ENTER ELEMENT a(2)(5): 0
ENTER ELEMENT a(3)(1): 0
ENTER ELEMENT a(3)(2): 0
ENTER ELEMENT a(3)(3): 0
ENTER ELEMENT a(3)(4): 1
ENTER ELEMENT a(3)(5): 0

ENTERED MATRIX IS LOOS LESS CHANNEL MATRIX

CHANNEL CAPACITY =1.584962

No comments:

Post a Comment