Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package tarea1;
- import java.lang.Integer;
- import java.io.*;
- import java.util.*;
- import java.lang.*;
- /**
- *
- * @authors: Brian K. Salinas, Jorge Lecea
- *
- */
- public class Tarea1 {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args)throws Exception {
- int ask = 0;
- int bits = 3;
- //User Input if he wants to Encode or Decode
- // 1. Encode and generate data.
- // 2. Decode encoded data.
- // 3. Change N-Bits to new integer.
- Scanner keyboard = new Scanner(System.in);
- while(true)
- {
- System.out.println("1. Encode and generate data.");
- System.out.println("2. Decode encoded data.");
- System.out.println("3. Change N-Bits to new integer.");
- System.out.println("4. Exit");
- ask = keyboard.nextInt();
- if(ask == 4)
- {
- break;
- }
- if(ask == 3)
- {
- System.out.println("How many bits do you want for the encode?");
- bits = keyboard.nextInt();
- }
- if(ask == 1)
- {
- // Encode
- System.out.println("Encoding");
- //Take sample every 50 milliseconds
- double Ts=.05;
- //Time constants
- double start= -Ts;
- double current=start;
- double finish=1.5;
- // nTs should be 30 for this project
- double nTs = (finish - start)/Ts;
- //Analog outputs
- double f=0;
- int c=1;
- //Arbitrary # of bits can be changed without isssue
- // V min is 0 and V max is +2 for this hwk
- // # of levels is 2^bits
- double Vmin=0;
- double Vmax=2;
- double levels = Math.pow(2, bits);
- // Used for Qi
- int i=0;
- boolean t=true;
- // Quantized F(t)
- double F_Q=0;
- // Encoded value
- String F_E="";
- //Stream variables for Encode
- FileWriter fw = new FileWriter("encoded_output.txt");
- PrintWriter writer = new PrintWriter(fw);
- //Quantization Variables
- double D_Q = ((Vmax- Vmin) / levels);
- double Q_i=0;
- // Sampling
- while(current <= finish)
- {
- //f(t)= 2*E^(-t^2)
- f = 2*Math.exp( - (Math.pow(current, 2)) );
- // Values Sampled
- while(t==true)
- {
- Q_i = Vmin + D_Q*i;
- // Not the Qi we need if F is bigger use i+1
- if(f > Q_i)
- ;
- else
- {
- //Break
- t=false;
- // We found our Quant Value
- F_Q = Q_i;
- // Our Encoded value is the i in bynary
- F_E = Integer.toBinaryString(i-1);
- writer.println(F_E);
- }
- i++;
- }
- // Update Variables used for loop
- f = 0;
- i=0;
- t=true;
- current += Ts;
- }
- writer.flush();
- fw.flush();
- writer.close();
- fw.close();
- System.out.println("Encoding done");
- }
- if(ask == 2)
- {
- // Decode
- FileReader in = null;
- FileWriter out = null;
- String thisLine = null;
- List<String> encodedList = new ArrayList<String>();
- int i2 = 0; ////
- double code_length;
- double deltaQ;
- double Maxlvl;
- int V_level;
- double V;
- try
- {
- in = new FileReader("encoded_output.txt");
- BufferedReader br = new BufferedReader(in);
- while ((thisLine = br.readLine()) != null)
- {
- encodedList.add(thisLine);
- i2++;
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- try
- {
- code_length = encodedList.get(0).length(); // get n, which is the amount of bits used to encode
- Maxlvl = Math.pow(2, code_length); // get the highest level possible
- deltaQ = 2/Maxlvl; // the increase intervals of V
- out = new FileWriter("output.txt"); // out file
- for(i2=0; i2<encodedList.size(); i2++) // parse each code to its V value and store it in a file
- {
- thisLine = ""; //clean and reuse of the string variable created at the beginning
- thisLine = encodedList.get(i2);
- V_level = Integer.parseInt(thisLine, 2);
- V = deltaQ * V_level;
- thisLine = Double.toString(V);
- out.write(thisLine);
- out.write("\n");
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
- in.close();
- out.close();
- System.out.println("Decode encoded data.");
- }
- }
- Thread.sleep(500);
- System.out.flush();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement