Advertisement
makispaiktis

Transmit - Receive (with AWGN noise)

Mar 31st, 2020 (edited)
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.27 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Random;
  3.  
  4. public class App {
  5.    
  6.     // Variables
  7.     static double stdDevOfNoise = 2 * Math.pow(10, -1);
  8.    
  9.     // Methods
  10.     // 1. Random Integer
  11.     public static int generateNumber(int n) {
  12.         Random random = new Random();
  13.         int Tx = random.nextInt(n);
  14.         return Tx;
  15.     }
  16.    
  17.     // 2. QPSK Modulation ---> Matches a number(0, 1, 2, 3) with a spot/vector in a 2-D field (s0, s1, s2, s3)
  18.     // s0 = {sqrt(2)/2, sqrt(2)/2},   s1 = {sqrt(2)/2, -sqrt(2)/2},   s2 = {-sqrt(2)/2, -sqrt(2)/2},   s3 = {-sqrt(2)/2, sqrt(2)/2}
  19.     public static ArrayList <Double> QPSKModulation(int Tx) {
  20.         ArrayList <Double> s_t = new ArrayList <Double> ();
  21.         // This vector has 2 elements ---> 1st = x, 2nd = y
  22.         switch(Tx) {
  23.         case 0:
  24.             s_t.add(Math.sqrt(2) / 2);
  25.             s_t.add(Math.sqrt(2) / 2);
  26.             break;
  27.         case 1:
  28.             s_t.add(Math.sqrt(2) / 2);
  29.             s_t.add(-Math.sqrt(2) / 2);
  30.             break;
  31.         case 2:
  32.             s_t.add(-Math.sqrt(2) / 2);
  33.             s_t.add(-Math.sqrt(2) / 2);
  34.             break;
  35.         case 3:
  36.             s_t.add(-Math.sqrt(2) / 2);
  37.             s_t.add(Math.sqrt(2) / 2);
  38.             break;
  39.         default:
  40.             System.out.print("Not a valid input for function 'QPSKModulation'");
  41.         }
  42.         return s_t;
  43.     }
  44.    
  45.     // 3. AWGN Box ---> This function inserts white Gaussian Noise and changes our signal coordinates
  46.     public static ArrayList <Double> signalAfterAWGNoise(ArrayList <Double> s_t){
  47.         ArrayList <Double> r_t = new ArrayList <Double> ();
  48.         // Somehow, I have to insert noise. I will create new random coordinates, that will be added to our s_t
  49.         // White Gaussian Noise has a mean value of 0 and standard deviation equal to No/2
  50.         Random random = new Random();
  51.         double noiseX = random.nextGaussian() * stdDevOfNoise;
  52.         double noiseY = random.nextGaussian() * stdDevOfNoise;
  53.         // System.out.print(noiseX + " " + noiseY);
  54.         // Now, I add the noise coordinates in the vector I have sent
  55.         r_t.add(s_t.get(0) + noiseX);
  56.         r_t.add(s_t.get(1) + noiseY);
  57.         return r_t;
  58.     }
  59.    
  60.     // 4. QPSK Demodulation ---> It is based on the decision areas
  61.     public static int QPSKDemodulation(ArrayList <Double> r_t) {
  62.         // The demodulation function makes the reverse process
  63.         if(r_t.get(0) >= 0 && r_t.get(1) >= 0) {
  64.             return 0;
  65.         }
  66.         else if(r_t.get(0) > 0 && r_t.get(1) < 0) {
  67.             return 1;
  68.         }
  69.         else if(r_t.get(0) < 0 && r_t.get(1) < 0) {
  70.             return 2;
  71.         }
  72.         else if(r_t.get(0) < 0 && r_t.get(1) > 0) {
  73.             return 3;
  74.         }
  75.         else {
  76.             System.out.println("Error in function 'QPSKDemodulation'");
  77.             return -1000;
  78.         }
  79.     }
  80.    
  81.    
  82.     // MAIN FUNCTION
  83.     public static void main(String[] args) {
  84.         int successes = 0;
  85.         int failures = 0;
  86.         Random random = new Random();
  87.         int iterations = random.nextInt(5001) + 5000;
  88.         for(int i=0; i<iterations; i++) {
  89.             int Tx = generateNumber(4);
  90.             ArrayList <Double> s_t = QPSKModulation(Tx);
  91.             ArrayList <Double> r_t = signalAfterAWGNoise(s_t);
  92.             int Rx = QPSKDemodulation(r_t);
  93.             if(Tx == Rx) {
  94.                 // System.out.println("Success");
  95.                 successes++;
  96.             }
  97.             else {
  98.                 // System.out.println("Failure");
  99.                 failures++;
  100.             }
  101.         }
  102.         // Print the statistics
  103.         System.out.println("Successes: " + successes);
  104.         System.out.println("Failures: " + failures);
  105.         System.out.println("Error rate: " + (double)failures/(double)(failures + successes));
  106.     }   // END OF MAIN FUNCTION
  107.  
  108. }    // END OF CLASS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement