Advertisement
Sirallens

Untitled

Sep 22nd, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.34 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package tarea1;
  7.  
  8. import java.lang.Integer;
  9. import java.io.*;
  10. import java.util.*;
  11. import java.lang.*;
  12.  
  13. /**
  14.  *
  15.  * @authors: Brian K. Salinas, Jorge Lecea
  16.  *
  17.  */
  18.  
  19.  
  20. public class Tarea1 {
  21.  
  22.     /**
  23.      * @param args the command line arguments
  24.      */
  25.     public static void main(String[] args)throws Exception {
  26.          
  27.         int ask = 0;
  28.         int bits = 3;  
  29.        
  30.         //User Input if he wants to Encode or Decode  
  31.        
  32.         // 1. Encode and generate data.
  33.         // 2. Decode encoded data.
  34.         // 3. Change N-Bits to new integer.
  35.        
  36.        
  37.             Scanner keyboard = new Scanner(System.in);
  38.        
  39.         while(true)
  40.         {
  41.              System.out.println("1. Encode and generate data.");
  42.             System.out.println("2. Decode encoded data.");
  43.             System.out.println("3. Change N-Bits to new integer.");
  44.             System.out.println("4. Exit");
  45.            
  46.             ask = keyboard.nextInt();
  47.  
  48.              if(ask == 4)
  49.              {
  50.                  break;
  51.              }
  52.  
  53.              if(ask == 3)
  54.              {
  55.                 System.out.println("How many bits do you want for the encode?");
  56.                 bits = keyboard.nextInt();
  57.              }
  58.  
  59.             if(ask == 1)
  60.             {
  61.  
  62.                 // Encode
  63.                 System.out.println("Encoding");
  64.                 //Take sample every 50 milliseconds
  65.                 double Ts=.05;
  66.                 //Time constants
  67.                 double start= -Ts;
  68.                 double current=start;
  69.                 double finish=1.5;
  70.                 // nTs should be 30 for this project
  71.                 double nTs = (finish - start)/Ts;
  72.                 //Analog outputs
  73.                 double f=0;
  74.                 int c=1;      
  75.                 //Arbitrary # of bits can be changed without isssue
  76.  
  77.                 // V min is 0 and V max is +2 for this hwk
  78.                 // # of levels is 2^bits
  79.                 double Vmin=0;
  80.                 double Vmax=2;
  81.                 double levels = Math.pow(2, bits);      
  82.                 // Used for Qi
  83.                 int i=0;
  84.                 boolean t=true;    
  85.                 // Quantized F(t)
  86.                 double F_Q=0;  
  87.                 // Encoded value
  88.                 String F_E="";
  89.                 //Stream variables for Encode
  90.                 FileWriter fw = new FileWriter("encoded_output.txt");
  91.                 PrintWriter writer = new PrintWriter(fw);
  92.                 //Quantization Variables
  93.                 double D_Q = ((Vmax- Vmin) / levels);
  94.                 double Q_i=0;
  95.  
  96.  
  97.                 // Sampling
  98.                 while(current <= finish)
  99.                 {
  100.                     //f(t)= 2*E^(-t^2)
  101.  
  102.                     f = 2*Math.exp( - (Math.pow(current, 2)) );
  103.  
  104.                     // Values Sampled
  105.  
  106.  
  107.                     while(t==true)
  108.                     {            
  109.                         Q_i = Vmin + D_Q*i;
  110.  
  111.                         // Not the Qi we need if F is bigger use i+1
  112.                         if(f > Q_i)
  113.                             ;
  114.                         else
  115.                         {
  116.                             //Break
  117.                             t=false;
  118.                             // We found our Quant Value
  119.                             F_Q = Q_i;
  120.                             // Our Encoded value is the i in bynary
  121.                             F_E = Integer.toBinaryString(i-1);      
  122.                             writer.println(F_E);
  123.  
  124.                         }
  125.                         i++;
  126.                     }
  127.  
  128.  
  129.  
  130.                    // Update Variables used for loop
  131.                     f = 0;
  132.                     i=0;
  133.                     t=true;
  134.                     current += Ts;
  135.  
  136.                 }
  137.                     writer.flush();
  138.                     fw.flush();  
  139.                     writer.close();
  140.                     fw.close();
  141.                     System.out.println("Encoding done");
  142.             }
  143.  
  144.  
  145.  
  146.  
  147.  
  148.         if(ask == 2)
  149.           {
  150.  
  151.              // Decode
  152.                FileReader in = null;
  153.                FileWriter out = null;
  154.                String thisLine = null;
  155.                List<String> encodedList = new ArrayList<String>();
  156.                int i2 = 0;  ////
  157.                double code_length;
  158.                double deltaQ;
  159.                double Maxlvl;
  160.                int V_level;
  161.                double V;
  162.  
  163.             try
  164.             {
  165.  
  166.                 in = new FileReader("encoded_output.txt");
  167.                 BufferedReader br = new BufferedReader(in);
  168.  
  169.                 while ((thisLine = br.readLine()) != null)
  170.                 {
  171.                    encodedList.add(thisLine);
  172.  
  173.                    i2++;
  174.                 }    
  175.  
  176.              }
  177.              catch(Exception e)
  178.              {
  179.                 e.printStackTrace();
  180.              }
  181.  
  182.            try
  183.            {
  184.                code_length = encodedList.get(0).length();  // get n, which is the amount of bits used to encode
  185.                Maxlvl = Math.pow(2, code_length);          // get the highest level possible
  186.                deltaQ = 2/Maxlvl;                          // the increase intervals of V
  187.                out = new FileWriter("output.txt");         // out file
  188.  
  189.                for(i2=0; i2<encodedList.size(); i2++)  // parse each code to its V value and store it in a file
  190.                {
  191.                      thisLine = "";   //clean and reuse of the string variable created at the beginning
  192.                      thisLine = encodedList.get(i2);
  193.                      V_level = Integer.parseInt(thisLine, 2);
  194.                      V = deltaQ * V_level;
  195.                      thisLine = Double.toString(V);
  196.                      out.write(thisLine);
  197.                      out.write("\n");
  198.                }
  199.  
  200.  
  201.            }
  202.  
  203.           catch(Exception e)
  204.              {
  205.                 e.printStackTrace();
  206.              }
  207.  
  208.            finally
  209.            {
  210.                in.close();
  211.                out.close();
  212.                System.out.println("Decode encoded data.");
  213.            }
  214.  
  215.            }
  216.         Thread.sleep(500);
  217.         System.out.flush();
  218.     }
  219.      
  220.    
  221. }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement