├── sum_rate_plot_100and80.mat ├── sum_rate_plot_80_only.mat ├── README.md └── MCMC_k_user.m /sum_rate_plot_100and80.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaushik333/D2D-communication/HEAD/sum_rate_plot_100and80.mat -------------------------------------------------------------------------------- /sum_rate_plot_80_only.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaushik333/D2D-communication/HEAD/sum_rate_plot_80_only.mat -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # D2D-communication 2 | optimal mode selection between normal cellular and D2D mode. 3 | 4 | MCMC_k_user.m is the main file to be run. This takes the input for No. of users, Power of each device, power emitted by the base station 5 | and path loss exponent and generates a plot of the optimal mode selection for different users. 6 | 7 | The remaining two files are just saved workspaces for different configurations. 8 | -------------------------------------------------------------------------------- /MCMC_k_user.m: -------------------------------------------------------------------------------- 1 | clc; 2 | clear; 3 | close; 4 | Pd_intended = input('What is the power intended at the base station by each cellular user?'); 5 | P_d2d_intended = input('What is the total intended interference at the BS due to all D2D users?'); 6 | Pb_intended = input('What is the intended power at the cell edge? (in watts please)'); 7 | 8 | %num = [5 6 7 8 9 10 15 20 25 30 35 40 45 50 60 70 80 100 150 200]; 9 | %num = [1:9 10:5:25 30:10:70 100]; 10 | num = [80, 90]; 11 | side_len = 400; % units: meters 12 | 13 | N = 1; % noise power 14 | 15 | eta = 4; 16 | 17 | d0 = 10; % meters 18 | %sum_rates = zeros(size(num,2), 100); 19 | for s = 1:size(num,2) 20 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 21 | %%% Plotting the data points 22 | %%% Red is transmitter and blue is Receiver 23 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 24 | %subplot(121); 25 | num_users = num(s); 26 | points_required = 2*(num_users) + 1; 27 | %side_len = 400; % units: meters 28 | for iter = 1:100 29 | t = side_len*rand(num_users,2); 30 | r = side_len*rand(num_users,2); 31 | t = [t; side_len/2 side_len/2]; %% Base station 32 | r = [r; side_len/2 side_len/2]; %% Base station 33 | 34 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 35 | %%% Calculate distance from every Tx or every other Rx and the BS. Note that 36 | %%% the last element of the array will be 0, since its the distance between 37 | %%% the base station and itself. Then calculate the channel gains too 38 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 39 | 40 | for i=1:num_users+1 41 | for j=1:num_users+1 42 | dis(i,j) = norm(t(i,:)-r(j,:)); 43 | h(i,j) = min(1,((dis(i,j)/d0)^(-eta))); 44 | end 45 | end 46 | 47 | %rand_vec = round(rand(1,num_users)); 48 | 49 | 50 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 51 | %%% MCMC Optimisation Algorithm 52 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 53 | % while(size(find(rand_vec==1),2)<=2) 54 | % rand_vec = round(rand(1,num_users)); 55 | % end 56 | % disp(rand_vec); 57 | 58 | tic; 59 | 60 | %rand_vec = ones(1,num_users); 61 | rand_vec = round(rand(1,num_users)); 62 | %rand_vec = zeros(1,num_users); 63 | disp(rand_vec); 64 | d2d_pair_first = find(rand_vec==1) % All d2d users 65 | cell_pair_first = find(rand_vec==0) % All cellular users 66 | [R_cell_up_first, R_cell_down_first, sum_rate] = sumrate(d2d_pair_first, cell_pair_first, dis, h, num_users, Pd_intended, Pb_intended, P_d2d_intended, d0, eta, side_len) 67 | a_old = 0; 68 | %a_new = randi(num_users); 69 | a_new = 0; 70 | 71 | if (2^num_users) < 10000 72 | iterations = 2^(num_users-1); 73 | else 74 | iterations = 200*num_users; 75 | end 76 | 77 | for i = 1:iterations 78 | 79 | rand_vec_new = rand_vec; 80 | 81 | %To pick a random element and toggle its state 82 | 83 | %1 means d2d user and 0 means cellular user 84 | 85 | 86 | while(a_old==a_new) 87 | a_new = randi(num_users); 88 | end 89 | %a_new 90 | if(rand_vec_new(a_new)==0) 91 | rand_vec_new(a_new)=1; 92 | elseif(rand_vec_new(a_new)==1) 93 | rand_vec_new(a_new)=0; 94 | end 95 | 96 | 97 | a_old = a_new; 98 | d2d_pair = find(rand_vec_new==1); % All d2d users 99 | cell_pair = find(rand_vec_new==0); % All cellular users 100 | %disp('iteration is');disp(i); 101 | % cell_pair_first 102 | % R_cell_up_first 103 | [delta_R, R_cell_up, R_cell_down] = sumrate1(d2d_pair, d2d_pair_first, cell_pair, cell_pair_first, R_cell_up_first, R_cell_down_first, h, dis, num_users, Pd_intended, Pb_intended, P_d2d_intended, d0, eta, side_len); 104 | disp(['delta R is ', num2str(delta_R)]); 105 | disp(num(s)); 106 | disp(iter); 107 | if(delta_R > 0) 108 | rand_vec = rand_vec_new; 109 | d2d_pair_first = d2d_pair; 110 | cell_pair_first = cell_pair; 111 | R_cell_up_first = R_cell_up; 112 | R_cell_down_first = R_cell_down; 113 | %sum_rate = sum_rate_new; 114 | end 115 | 116 | 117 | 118 | end 119 | 120 | time = toc; 121 | disp('The time taken is : '); disp(time); 122 | 123 | disp(rand_vec); 124 | 125 | d2d_pair = find(rand_vec==1); % All d2d users 126 | cell_pair = find(rand_vec==0); % All cellular users 127 | 128 | [R_up, R_down, sum_rates(s,iter)] = sumrate(d2d_pair, cell_pair, dis, h, num_users, Pd_intended, Pb_intended, P_d2d_intended, d0, eta, side_len); 129 | 130 | end 131 | end 132 | sum_rates_new = sum_rates'; 133 | sum = mean(sum_rates_new); 134 | plot(num, sum); --------------------------------------------------------------------------------