Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Random;
- public class App {
- // Variables
- static double stdDevOfNoise = 2 * Math.pow(10, -1);
- // Methods
- // 1. Random Integer
- public static int generateNumber(int n) {
- Random random = new Random();
- int Tx = random.nextInt(n);
- return Tx;
- }
- // 2. QPSK Modulation ---> Matches a number(0, 1, 2, 3) with a spot/vector in a 2-D field (s0, s1, s2, s3)
- // 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}
- public static ArrayList <Double> QPSKModulation(int Tx) {
- ArrayList <Double> s_t = new ArrayList <Double> ();
- // This vector has 2 elements ---> 1st = x, 2nd = y
- switch(Tx) {
- case 0:
- s_t.add(Math.sqrt(2) / 2);
- s_t.add(Math.sqrt(2) / 2);
- break;
- case 1:
- s_t.add(Math.sqrt(2) / 2);
- s_t.add(-Math.sqrt(2) / 2);
- break;
- case 2:
- s_t.add(-Math.sqrt(2) / 2);
- s_t.add(-Math.sqrt(2) / 2);
- break;
- case 3:
- s_t.add(-Math.sqrt(2) / 2);
- s_t.add(Math.sqrt(2) / 2);
- break;
- default:
- System.out.print("Not a valid input for function 'QPSKModulation'");
- }
- return s_t;
- }
- // 3. AWGN Box ---> This function inserts white Gaussian Noise and changes our signal coordinates
- public static ArrayList <Double> signalAfterAWGNoise(ArrayList <Double> s_t){
- ArrayList <Double> r_t = new ArrayList <Double> ();
- // Somehow, I have to insert noise. I will create new random coordinates, that will be added to our s_t
- // White Gaussian Noise has a mean value of 0 and standard deviation equal to No/2
- Random random = new Random();
- double noiseX = random.nextGaussian() * stdDevOfNoise;
- double noiseY = random.nextGaussian() * stdDevOfNoise;
- // System.out.print(noiseX + " " + noiseY);
- // Now, I add the noise coordinates in the vector I have sent
- r_t.add(s_t.get(0) + noiseX);
- r_t.add(s_t.get(1) + noiseY);
- return r_t;
- }
- // 4. QPSK Demodulation ---> It is based on the decision areas
- public static int QPSKDemodulation(ArrayList <Double> r_t) {
- // The demodulation function makes the reverse process
- if(r_t.get(0) >= 0 && r_t.get(1) >= 0) {
- return 0;
- }
- else if(r_t.get(0) > 0 && r_t.get(1) < 0) {
- return 1;
- }
- else if(r_t.get(0) < 0 && r_t.get(1) < 0) {
- return 2;
- }
- else if(r_t.get(0) < 0 && r_t.get(1) > 0) {
- return 3;
- }
- else {
- System.out.println("Error in function 'QPSKDemodulation'");
- return -1000;
- }
- }
- // MAIN FUNCTION
- public static void main(String[] args) {
- int successes = 0;
- int failures = 0;
- Random random = new Random();
- int iterations = random.nextInt(5001) + 5000;
- for(int i=0; i<iterations; i++) {
- int Tx = generateNumber(4);
- ArrayList <Double> s_t = QPSKModulation(Tx);
- ArrayList <Double> r_t = signalAfterAWGNoise(s_t);
- int Rx = QPSKDemodulation(r_t);
- if(Tx == Rx) {
- // System.out.println("Success");
- successes++;
- }
- else {
- // System.out.println("Failure");
- failures++;
- }
- }
- // Print the statistics
- System.out.println("Successes: " + successes);
- System.out.println("Failures: " + failures);
- System.out.println("Error rate: " + (double)failures/(double)(failures + successes));
- } // END OF MAIN FUNCTION
- } // END OF CLASS
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement