Last active
January 2, 2020 17:23
-
-
Save AnujJha-stack/d7e0d57d6bc33215e3e3cafc045b8d96 to your computer and use it in GitHub Desktop.
Wireless Networks And Mobile Computing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//coded by anuj_jha(170303105120) | |
//P2: CDMA for two user and one bit data. | |
import java.util.Scanner; | |
public class cdma { | |
static Scanner input = new Scanner(System.in); | |
public static void main(String[] ar){ | |
System.out.println("Enter Data 1 (1 bit) : "); | |
int data1 = inputData(); | |
System.out.println("Enter code 1 (4 bit) : "); | |
int[] codeArray1 = inputArray(); | |
System.out.println("Enter Data 2 (1 bit) : "); | |
int data2 = inputData(); | |
System.out.println("Enter code 2 (4 bit) : "); | |
int[] codeArray2 = inputArray(); | |
int[] sendingCode1=sendingCodeGenerator(codeArray1,data1);//multiply | |
int[] sendingCode2=sendingCodeGenerator(codeArray2,data2);//multiply | |
System.out.print("\nSending Code 1 :"); | |
printArray(sendingCode1); | |
System.out.print("\nSending Code 2 :"); | |
printArray(sendingCode2); | |
int[] channelCode=channelCodeGenerator(sendingCode1,sendingCode2);//add | |
System.out.print("\nChannel Code :"); | |
printArray(channelCode); | |
//multiply | |
int[] receivingCode1 = calculatedReceivingCode(codeArray1,channelCode); | |
//multiply | |
int[] receivingCode2 = calculatedReceivingCode(codeArray2,channelCode); | |
System.out.print("\nReceiving Code 1 : "); | |
printArray(receivingCode1); | |
System.out.print("\nReceiving Code 2 : "); | |
printArray(receivingCode2); | |
int outputCode1 = sum(receivingCode1);//sum/4 | |
int outputCode2 = sum(receivingCode2);//sum/4 | |
System.out.println("\nData 1 :"+outputCode1); | |
System.out.println("Data 2 :"+outputCode2); | |
} | |
static int inputData(){ | |
int data = input.nextInt(); | |
if(data == 0){ | |
data = -1; | |
} | |
return data; | |
} | |
static int[] inputArray(){ | |
int[] temp = new int[4]; | |
for(int i = 0; i < 4; i++){ | |
temp[i] = input.nextInt(); | |
if(temp[i]==0){ | |
temp[i] = -1; | |
} | |
} | |
return temp; | |
} | |
static int[] sendingCodeGenerator(int[] code, int data){ | |
int[] temp = new int[4]; | |
for(int i = 0; i < 4; i++){ | |
temp[i] = data * code[i]; | |
} | |
return temp; | |
} | |
static int[] channelCodeGenerator(int[] code1, int[] code2){ | |
int[] chCode = new int[4]; | |
for (int i = 0; i < 4; i++){ | |
chCode[i] = code1[i] + code2[i]; | |
} | |
return chCode; | |
} | |
static int[] calculatedReceivingCode(int[] code, int[] channel){ | |
int[] tmp = new int[4]; | |
for(int i = 0; i < 4; i++){ | |
tmp[i] = code[i] * channel[i]; | |
} | |
return tmp; | |
} | |
static int sum(int[] code){ | |
int sum = 0; | |
for(int i = 0; i < 4; i++){ | |
sum = sum + code[i]; | |
} | |
int res = sum / 4; | |
if(res == 0){ | |
res = -1; | |
} | |
return res; | |
} | |
static void printArray(int[] code) { | |
for (int i = 0; i < 4; i++) { | |
System.out.print(code[i]+"\t"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//coded by anuj_jha(170303105120) | |
//P2: CDMA for n user m bit data o bit code. | |
//please enter -1 in place of '_' i.e for no data. | |
import java.nio.channels.Channel; | |
import java.util.Scanner; | |
class DataCodeModulation{ | |
static Scanner input = new Scanner(System.in); | |
int[] data,code,sendingCode,channelCode; | |
DataCodeModulation(int u){ | |
data = dataArrGen(u); | |
code = codeArrGen(u); | |
sendingCode = sendingArrGen(data,code); | |
} | |
public int[] codeArrGen(int u){ | |
System.out.print("Enter code for user "+u+" : "); | |
return inputValue(); | |
} | |
public int[] dataArrGen(int u){ | |
System.out.print("Enter data for user "+u+" : "); | |
return inputValue(); | |
} | |
public int[] sendingArrGen(int[] data, int[] code){ | |
int sendingArrSize = (data.length)*(code.length); | |
int[] arr = new int[sendingArrSize]; | |
int k = 0; | |
for (int i = 0; i < data.length; i++) { | |
for (int j = 0; j < code.length && k < sendingArrSize; j++) { | |
arr[k++] = data[i] * code[j]; | |
} | |
} | |
return arr; | |
} | |
// methods to take input and return the inputted value in the form of array. | |
// This Also Convert 1 -> 1, 0 -> -1, other -> 0. | |
public int[] inputValue(){ | |
String[] val = input.nextLine().split(" "); | |
int[] arr = new int[val.length]; | |
for(int i = 0; i < arr.length; i++){ | |
arr[i] = Integer.parseInt(val[i]); | |
if(arr[i] == 0){ | |
arr[i] = -1; | |
} | |
else if(arr[i] == 1){ | |
arr[i] = 1; | |
} | |
else{ | |
arr[i] = 0; | |
} | |
} | |
return arr; | |
} | |
public void showData(){ | |
for(int i:data) { System.out.print(" "+i+" ");} | |
} | |
public void showCode(){ | |
for(int i:code) { System.out.print(" "+i+" ");} | |
} | |
public void showSendingCode(){ | |
for(int i:sendingCode) { System.out.print(" "+i+" ");} | |
} | |
public void setChannelCode(int[] channelCodeReceived){ | |
channelCode = channelCodeReceived; | |
} | |
} | |
public class cdmaMultipleUser{ | |
// Takes code during demodulation. | |
public static int[] inputCode(){ | |
Scanner input = new Scanner(System.in); | |
String[] val = input.nextLine().split(" "); | |
int[] arr = new int[val.length]; | |
for(int i = 0; i < arr.length; i++){ | |
arr[i] = Integer.parseInt(val[i]); | |
if(arr[i] == 0){ | |
arr[i] = -1; | |
} | |
} | |
return arr; | |
} | |
// it generate the intermediateCode by multiplying code in each block of channelCode | |
public static int[] intermediateCodeGen(int[] code, int[] channelCode){ | |
int[] arr = new int[channelCode.length]; | |
for(int i = 0,j = 0; i < channelCode.length ; i++){ | |
if( j == code.length){ | |
j = 0; | |
} | |
arr[i] = code[j++] * channelCode[i]; | |
} | |
return arr; | |
} | |
// calculate sum(intermediateCode[start to end]) / divisor. | |
// range : including start excluding end | |
public static int calculateData(int start, int end,int blockSize, int[] intermediateCode){ | |
int val = 0; | |
for(int i = start; i < end; i++){ | |
val += intermediateCode[i]; | |
} | |
return (val/blockSize); | |
} | |
public static void main(String[] args){ | |
Scanner input = new Scanner(System.in); | |
int numberOfUser; | |
System.out.print("How many Users : "); | |
numberOfUser = input.nextInt(); | |
// This create number of object for $(numberOfUser) users of DataCode type. | |
// ---------------------------------------------- | |
DataCodeModulation[] user = new DataCodeModulation[numberOfUser]; | |
for(int i = 0; i < numberOfUser; i++){ | |
DataCodeModulation obj = new DataCodeModulation(i); | |
user[i] = obj;//name of obj like user[0], user[1].... | |
} | |
// ------------------------------------------------ | |
for(int i = 0; i < numberOfUser; i++) { | |
System.out.print("\nData for User " + i + ": "); | |
user[i].showData(); | |
System.out.print("\nCode for User " + i + ": " ); | |
user[i].showCode(); | |
System.out.print("\nSending_Code for User " + i + ": "); | |
user[i].showSendingCode(); | |
} | |
int size = (user[0].sendingCode).length;//get the size of the array for receiving code as all have same length so we | |
int[] channelCode = new int[size]; | |
for(int i = 0; i < size; i++ ){ | |
for(int j = 0; j < numberOfUser; j++ ) { | |
channelCode[i] += user[j].sendingCode[i]; | |
} | |
} | |
// this set the channelCode[] for each of user in DataCode class. | |
for (int i = 0; i < numberOfUser; i++){ | |
user[i].setChannelCode(channelCode); | |
} | |
System.out.print("\nChannel_Code : "); | |
for(int i = 0; i < size; i++){ | |
System.out.print(" "+channelCode[i]+" "); | |
} | |
//demodulation menu | |
while(true){ | |
System.out.print("\n\n###########################################\n"); | |
System.out.print("\n########### Demodulation Wizard ###########\n"); | |
System.out.print("\n###########################################\n"); | |
System.out.print("\nEnter your Code : "); | |
int[] code = inputCode(); | |
int[] intermediateCode = intermediateCodeGen(code,channelCode); | |
int blockSize = code.length; | |
int numOfBlock = channelCode.length/blockSize; | |
int[] data = new int[numOfBlock]; | |
for (int i = 0; i < numOfBlock; i++){ | |
int start = i * blockSize; | |
int end = (i + 1) * blockSize; | |
data[i]= calculateData(start,end,blockSize,intermediateCode);//range : including start excluding end | |
} | |
System.out.print("\nYour Data : "); | |
for(int i = 0; i < numOfBlock; i++){ | |
if(data[i] == 0){ | |
System.out.print(" _ "); | |
} | |
else if(data[i] == -1){ | |
System.out.print(" 0 "); | |
} | |
else if(data[i] == 1){ | |
System.out.print(" 1 "); | |
} | |
} | |
// System.out.print(data[0]);//***************remove | |
// at end ask user to continue or exit | |
System.out.print("\n\n1.Continue"); | |
System.out.print("\n2.Exit"); | |
System.out.print("\n\nEnter Choice : "); | |
int ch = input.nextInt(); | |
if(ch == 2){ | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment