Friday, 12 September 2014

Pulse Code Modulation in java

-------------------Server-----------------------------
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.FlowLayout;
import java.sql.*;

public class Server
{
private ServerSocket serverSocket;
Server(int port)
{
try{
serverSocket=new ServerSocket(port);
System.out.println("Server waiting"+serverSocket.getLocalPort());
while(true)
{
Socket socket=serverSocket.accept();
System.out.println("client requested for conn");
TcpThread t=new TcpThread(socket);
System.out.println("starting thred");
t.start();
}
}catch(IOException e)
{
System.out.println("Exception on new ServerSocket:"+e);
}
}
public static void main(String a[])
{
new Server(8080);
}

class TcpThread extends Thread
{
Socket socket;
ObjectInputStream Sinput;
ObjectOutputStream Soutput;
String str;
TcpThread(Socket socket)
{this.socket=socket;}
public void run()
{
System.out.println("Creating object i/o Streams");
try{
Soutput=new ObjectOutputStream(socket.getOutputStream());
Soutput.flush();
Sinput= new ObjectInputStream(socket.getInputStream());
}catch(IOException e)
{System.out.println("Exception creating new i/o stream"+e);
return;
}
System.out.println("Thread waiting for a String from client");
try{
String str=(String)Sinput.readObject();

System.out.println("Client Sent: "+str);

str="OK";

Soutput.writeObject(str);

Soutput.flush();

}
catch(Exception e2)
{System.out.println("Exception reading/wr Streams:"+e2);
return;
}
finally{
try{

Soutput.close();
Sinput.close();
}catch(Exception e)
{}
}
}
}
}

---------------------Client---------------------------------


import java.net.*;
import java.io.*;
import javax.swing.*;
import static java.lang.Math.pow;
import java.util.*;

public class Client
{
ObjectInputStream Sinput;
ObjectOutputStream Soutput;
Socket socket;
int st=0,port;
String response;

Client(int port1)
{
port=port1;
}

void send(String s)
{
try{
socket=new Socket("localhost",port);
//socket=new Socket("192.168.1.140",port);
//socket=new Socket("localhost",port);
}catch(Exception e)
{JOptionPane.showMessageDialog(null,"Error in connecting to the server"+e);
return;
}
System.out.println("Conn Accepted"+socket.getInetAddress()+":"+socket.getPort());
try
{
Sinput=new ObjectInputStream(socket.getInputStream());
Soutput=new ObjectOutputStream(socket.getOutputStream());
}catch(IOException e)
{System.out.println("Exception creating new i/o stream"+e);
return;
}

System.out.println("Client sending '"+s+"' to server");
try
{
Soutput.writeObject(s);
Soutput.flush();
}
catch(IOException e)
{System.out.println("Error writing to socket:"+e);
return;
}

try{
Sinput.close();
Soutput.close();
}catch(Exception e)
{}
}

String myPCM()
{
 double axis_x[]=new double[512];
    long tmp[]=new long[512];
    int ampl;
    Scanner s= new Scanner(System.in);
    System.out.println("Enter amplitude:");
    ampl= s.nextInt();
 
    double f;

  
    int t=0,T;
    System.out.println("Enter time:");
    T= s.nextInt();
    f=1.0/T;
    for(t=0;t<=T;t++)
    {
        axis_x[t] = ampl * Math.sin(2 * (22.0/7) * f * t);
        System.out.println(axis_x[t]+"  ");
    }
    

    int b,size,m;
   System.out.println("Enter How many bits for Quantization :");
    b= s.nextInt();
    m=(int)Math.pow(2,b-1);
    size=ampl/m;
 
    int p,n,k;
    for(t=0;t<=T;t++)
    {
                    if(axis_x[t]>=0){
                    p=0;n=size;
                    for(k=0;k<m;k++)
                    {
                     if(axis_x[t]>=p && axis_x[t]<=n)
                     {
                             tmp[t]=k+m;
                break;               
                     } 
            p=n;
                    n=n+size;        
                    }
                 
                    }
                    else
                    {
                        p=-1;n=-size;
                    for(k=0;k<m;k++)
                    {
                     if(axis_x[t]<=p && axis_x[t]>=n)
                     {
                             tmp[t]=m-k-1;
                break;               
                     }
                p=n;
                    n=n-size;         
                    }
                 
                    }
    System.out.print(" "+tmp[t]);

    }
 
    System.out.print("\n\n");
    String data="";
    long i,rem,sum=0;
    int j=0;
    for(j=0;j<=T;j++)
    {
        i=1;    sum=0;
        do
        {
            rem=(long) tmp[j]%2;
            sum=sum + (i*rem);
            tmp[j]=tmp[j]/2;
            i=i*10;
        }while(tmp[j]>0);
        System.out.print(" "+sum);
    data=data+sum;

    }
  
        System.out.println("\n\nData to be sent is:"+data);
    System.out.print("\n");
    return data;
}




public static void main(String a[])
{
   
    Client a1=new Client(8080);
    String s1=a1.myPCM();
    a1.send(s1);

}
}

------------Client Output-----------------


[root@localhost apcoer]# javac Client.java
[root@localhost apcoer]# java Client
Enter amplitude:
10
Enter time:
4
0.0 
9.999998001333681 
-0.0126448893037729 
-9.999982012007937 
0.02528975838921635 
Enter How many bits for Quantization :
2
 2 3 0 0 2

 10 11 0 0 10

Data to be sent is:10110010

Conn Acceptedlocalhost/127.0.0.1:8080
Client sending '10110010' to server
[root@localhost apcoer]#

----------------Server Output-----------------

[root@localhost apcoer]# java Server
Server waiting8080
client requested for conn
starting thred
Creating object i/o Streams
Thread waiting for a String from client
Client Sent: 10110010

No comments:

Post a Comment