Sei sulla pagina 1di 8

*/

import java.util.Scanner;
import java.util.Arrays;
public class Exceptionintransrecev {

/**
* @param args the command line arguments
*/

public static void main(String[] args) {


try{
Scanner input = new Scanner(System.in);
System.out.print("Enter your word: ");
String Y= input.next();
int z;
z=Y.length();
int i=0;
int n[]=new int[z];

while (i<z){
n[i]=(int)(Y.charAt(i));
i++;

int N[]= Chartobits(n);


System.out.print("The array N : [ ");
for (i=0; i<N.length; i++){
System.out.print(N[i]+" ");
}
System.out.print("]");
System.out.println();

int pack[][]= Packetize(N);


System.out.println("Packets: ");
for (int row=0; row<(pack.length);row++){
System.out.print("Packet : [ ");
for (int col=0; col<(pack[0].length);col++){
System.out.print(pack[row][col]+" ");
}
System.out.print("]");
System.out.println();
}
System.out.println("Parity packets:");
int par[][]=AddParity(pack);
for (int row=0; row<(par.length);row++){
System.out.print("Parity Packet : [ ");
for (int col=0; col<(par[0].length);col++){
System.out.print(par[row][col]+" ");
}
System.out.print("]");
System.out.println();
}
int BiPolar[][]=Bipolar(par);
for (int row =0; row<(BiPolar.length); row++){
System.out.print("BiPolar Packet : [ ");
for (int col=0; col<(BiPolar[0].length);col++){
System.out.print(BiPolar[row][col]+" ");
}
System.out.print("]");
System.out.println();
}

int Noise[][]=noisy(BiPolar);
for(int row=0;row<BiPolar.length;row++){
System.out.print("Noisy added packet: [");
for(int col=0;col<BiPolar[0].length;col++){
System.out.print(Noise[row][col]+" ");
}
System.out.print("]");
System.out.println();

System.out.println("BiPolar packets Demodulated");


int BiPolarDem[][]=BipolarDemodulator(Noise);
for (int row =0; row<(BiPolarDem.length); row++){
System.out.print("BiPolar Demodulated Packet : [ ");
for (int col=0; col<(BiPolarDem[0].length);col++){
System.out.print(BiPolarDem[row][col]+" ");
}
System.out.print("]");
System.out.println();
}

System.out.println("Parity removal packets: ");


int RemParry[][]=RemoveParity(BiPolarDem);
for (int row =0; row<(RemParry.length); row++){
System.out.print("Demodulated Packet without parity : [ ");
for (int col=0; col<(RemParry[0].length);col++){
System.out.print(RemParry[row][col]+" ");
}
System.out.print("]");
System.out.println();
}

int error[][]=Error(BiPolarDem);
for (int Z=0;Z<BiPolarDem.length;Z++){
if(error[Z][0]==0){
System.out.print("Packet "+Z+": No error");
}
else{
System.out.print("Packet "+Z+": error");
}

System.out.println();
}

int P[]= bitstreams (RemParry);


System.out.print("[");
for (int y=0;y<50;y++){

System.out.print(P[y]+" ");
}
System.out.print("]");
System.out.println();
int Numbiterror=0;
double Bit;
for(int y=0;y<N.length;y++){
if(N[y]!=P[y]){
Numbiterror=Numbiterror+1;

}
}
char []Z;
Z=new char[7];
Bit=N.length/Numbiterror;
double BER;
BER=1/Bit;

System.out.print("BER: ");
System.out.println(BER);

int count=0;
for(int m=0;m<7;m++){
if(Z[m]!=Y.charAt(m)){
count=count+1;

}
}
double V1,CER;
V1=7/count;
CER=1/V1;
System.out.print("CER: ");
System.out.println(CER);
System.out.println();
}
catch (ArrayIndexOutOfBoundsException e){
System.out.println("ArrayIndexOutOfBounds occurs");
System.out.println("Length of characters should be equal to 7");
System.exit(0);
}

}
public static int[] Chartobits(int[]n){
int j; int k; int con; int m;int fillN; int endOfBit = -7;
int []Bin= new int [7];
int X;
int N[]= new int [49];

for(j=0; j<(7); j++){


// System.out.print("The binary equivalent of "+(char)n[j]+" is :
");
k = n[j];
while(k>0){
for (m=0; m<7 ; m++){

con=((k%2)==0? 0 : 1);
k=k/2;
Bin[m]=con;
}
}

endOfBit+=7;
for (X=6,fillN=(endOfBit); X>=0 ;X--,fillN++){
//System.out.print(Bin[X]);
N[fillN]=Bin[X];
}

//System.out.println();
}
return(N);
}
public static int[][] Packetize(int N[]){
int F; int R; int i;

Scanner inputPacketize = new Scanner(System.in);


System.out.print("Enter the size of packet P: ");
F = inputPacketize.nextInt();
try{
if (F<0){
throw new IllegalArgumentException("Packet size cannot be negative ");
}
}
catch (IllegalArgumentException e) {
System.out.println(e);
System.exit(0);
}

if (F<=0){
while (F<=0){
System.out.println("Packet size cannot be negative, re-enter packet
size");
F= inputPacketize.nextInt();
}
}
if((N.length)%F==0){
R= ((N.length)/F);
}
else{
R= (((N.length)/F)+1);
}
int pack[][]= new int [R][F];
for (int count=0; count<(N.length);count++){

for (i=0; i<R; i++){


for (int j=0; j<F; j++){
if (count<(N.length)){
pack[i][j]= N[count];
count+=1;
}
}

}
}

return (pack);

public static int[][] AddParity(int pack[][]){


int newP = (pack[0].length)+1; int K;
int P = (pack[0].length);
int R= (pack.length);
int Parry[][]= new int[R][newP];
for(int i=0; i<R; i++){
int sum=0;
for (int j=0;j<P;j++){
K=pack[i][j];
sum+=K;
}
Parry[i][0]= (sum%2);
}
for(int inew=0; inew<R; inew++){
for(int jnew=1, jPack=0; jnew < newP; jnew++, jPack++){
Parry[inew][jnew]=pack[inew][jPack];
}
}
return(Parry);
}
public static int[][] Bipolar(int Parry[][]){
int P = (Parry[0].length);
int R= (Parry.length);
int BiPolar[][]= new int[R][P];
for(int i=0; i<R; i++){
for(int j=0; j<P; j++){

if(Parry[i][j]==0){

BiPolar[i][j]= -1;
}
else{
BiPolar[i][j]= 1;}
try{
if (BiPolar[i][j]!=1 && BiPolar[i][j]!=-1)
throw new IllegalArgumentException ("Bipolar modulated
data can take only -1 or +1");

catch(IllegalArgumentException e){
System.out.println("IllegalArgumentException occurs");
System.exit(0);
}
}
}
return(BiPolar);
}
public static int[][]noisy(int BiPolar[][]){

int W=BiPolar[0].length;
int[]m= new int[BiPolar.length];
for(int w=0;w<BiPolar.length;w++){
Scanner stdin=new Scanner(System.in);
try{
System.out.print("Enter the level of noise: ");
m[w]=stdin.nextInt();
if (m[w]>4)
throw new ArrayIndexOutOfBoundsException ("Maximum amplitude of noisy
corrupted data should be less than 5");

}
catch(ArrayIndexOutOfBoundsException e){
System.out.println(e);
System.exit(0);

}
double[]r;
r=new double[55];

if(m[2]<=1){
for(int u=0;u<W;u++){
r[u]=(Math.random() * 1.2* (Math.random() > 0.5 ? 1 : -1));
}}
if(m[3]>=2){for(int u=0;u<W;u++){
r[u] = (Math.random() * 1.6* (Math.random() > 0.5 ? 1 : -1));
}}
else{for(int u=0;u<W;u++){
r[u] =(Math.random() * 2.0* (Math.random() > 0.5 ? 1 : -1));

}
}

int E=(BiPolar.length);
int T=(BiPolar[0].length);

double Noise[][]=new double[E][T];


for(int B=0;B<E;B++){
for(int g=0,c=0;g<T;g++,c++){
Noise[B][g]=r[c];
}
}
int Noisy[][]=new int[E][T];
for(int O=0;O<E;O++){
for(int s=0;s<T;s++){
Noisy[O][s]=(int)(Noise[O][s]+BiPolar[O][s]);

}
}
return(Noisy);

public static int[][]BipolarDemodulator(int Noisy1[][]){


int T=(Noisy1[0].length);
int S=(Noisy1.length);
int Bipolardem[][]=new int[S][T];
for(int i=0;i<S;i++){
for(int j=0;j<T;j++){
if(Noisy1[i][j]>=0){
Bipolardem[i][j]=1;
}
else{
Bipolardem[i][j]=0;
}

}
}
return(Bipolardem);
}
public static int[][]Error(int Bipolardem[][]){
int X=Bipolardem.length;int B;
int Z=Bipolardem[0].length;
int error[][]=new int[X][1];
for(int k=0;k<X;k++){
int sum1=0;
for(int n=0;n<Z;n++){
B=Bipolardem[k][n];
sum1+=B;
}
error[k][0]=sum1%2;

}
return(error);
}

public static int[][] RemoveParity(int Bipolardem[][]){


int T=(Bipolardem[0].length);
int newT=(Bipolardem[0].length-1);
int R=(Bipolardem.length);
int Remparry[][]=new int[R][newT];
for(int i=0;i<R;i++){
for(int j=1,k=0;j<T;j++,k++)
Remparry[i][k]=Bipolardem[i][j];

}
return(Remparry);
}
public static int[] bitstreams (int Remparry[][]){
int T=(Remparry[0].length);
int R=(Remparry.length);
int P[]=new int [50];
int y;
for( y=0;y<50;y++){
for(int i=0;i<R;i++){
for(int j=0;j<T;j++){
P[y]=Remparry[i][j];
y++;
}

}
}
return(P);
}
}

Potrebbero piacerti anche