MATLAB PROGRAM
% Parameters
num_clusters = 4;
nodes_per_cluster = 10;
cluster_radius = 5;
cluster_distance = 20; % Minimum distance between clusters
% Preallocate arrays for cluster centers and node positions
cluster_centers = zeros(num_clusters, 2);
node_positions = cell(num_clusters, 1);
PU_nodes = cell(num_clusters, 1);
SU_nodes = cell(num_clusters, 1);
for i = 1:num_clusters
% Generate unique cluster center
valid_position = false;
while ~valid_position
x = randi([0, 100]);
y = randi([0, 100]);
valid_position = true;
for j = 1:i-1
if norm([x, y] - cluster_centers(j,:)) < cluster_distance
valid_position = false;
break;
end
end
end
cluster_centers(i,:) = [x, y];
% Generate nodes within cluster
nodes = zeros(nodes_per_cluster, 2);
nodes(1, :) = cluster_centers(i,:); % Cluster head at center
for j = 2:nodes_per_cluster
theta = 2*pi*rand();
r = cluster_radius*sqrt(rand());
nodes(j, :) = cluster_centers(i,:) + [r*cos(theta), r*sin(theta)];
end
% Assign nodes to clusters
node_positions{i} = nodes;
% Assign PU and SU nodes
num_PUs = randi([2, 4]);
PU_idx = randperm(nodes_per_cluster-1, num_PUs) + 1; % Avoiding the CH
PU_nodes{i} = nodes(PU_idx, :);
SU_nodes{i} = nodes(setdiff(2:nodes_per_cluster, PU_idx), :);
end
figure;
hold on;
for i = 1:num_clusters
plot(cluster_centers(i,1), cluster_centers(i,2), 'ro', 'MarkerSize', 10, 'LineWidth', 2);
plot(PU_nodes{i}(:,1), PU_nodes{i}(:,2), 'bs', 'MarkerSize', 8, 'LineWidth', 2);
plot(SU_nodes{i}(:,1), SU_nodes{i}(:,2), 'g^', 'MarkerSize', 8, 'LineWidth', 2);
viscircles(cluster_centers(i,:), cluster_radius, 'LineStyle', '--');
end
xlabel('X-coordinate');
ylabel('Y-coordinate');
title('CR-WSN Topology');
legend('Cluster Head', 'Primary User', 'Secondary User');
hold off;
% Monte Carlo Simulation for SNR values
for i = 1:num_clusters
num_SUs = size(SU_nodes{i}, 1);
num_PUs = size(PU_nodes{i}, 1);
for snr_idx = 1:length(SNR)
snr = SNR(snr_idx);
Pd_cluster_ED = zeros(1, num_iterations);
Pf_cluster_ED = zeros(1, num_iterations);
Pd_cluster_AND = zeros(1, num_iterations);
Pf_cluster_AND = zeros(1, num_iterations);
Pd_cluster_OR = zeros(1, num_iterations);
Pf_cluster_OR = zeros(1, num_iterations);
Pd_cluster_Voting = zeros(1, num_iterations);
Pf_cluster_Voting = zeros(1, num_iterations);
for iter = 1:num_iterations
% Generate Rayleigh fading channel
fading = raylrnd(1, num_SUs, num_PUs);
% Simulate SU sensing PUs for ED
ED_results = zeros(num_SUs, 1);
for su_idx = 1:num_SUs
energy_sum = 0;
for pu_idx = 1:num_PUs
noise = randn(1, 100);
received_signal = sqrt(10^(snr/10)) * fading(su_idx, pu_idx) + noise;
% Energy detection
energy = sum(abs(received_signal).^2) / length(received_signal);
energy_sum = energy_sum + energy;
end
% Threshold for ED
threshold_ED = chi2inv(0.9, 2); % Assuming a fixed threshold for simplicity
ED_results(su_idx) = energy_sum > threshold_ED;
end
Pd_cluster_ED(iter) = mean(ED_results); % Probability of detection
Pf_cluster_ED(iter) = mean(~ED_results); % Probability of false alarm
% Collaborative sensing using AND logic
Pd_cluster_AND(iter) = all(ED_results);
Pf_cluster_AND(iter) = ~any(ED_results);
% Collaborative sensing using OR logic
Pd_cluster_OR(iter) = any(ED_results);
Pf_cluster_OR(iter) = ~all(ED_results);
% Collaborative sensing using Voting logic
% Combine results from AND and OR logic
% Voting logic: weighted average of AND and OR results
voting_threshold = 0.5; % Adjust this threshold to balance detection and false alarm
Pd_cluster_Voting(iter) = (mean(ED_results) + Pd_cluster_AND(iter) + Pd_cluster_OR(iter)) / 3 > voting_threshold;
Pf_cluster_Voting(iter) = (1 - mean(ED_results) + (1 - Pd_cluster_AND(iter)) + (1 - Pd_cluster_OR(iter))) / 3 > voting_threshold;
end
Pd_ED(i, snr_idx) = mean(Pd_cluster_ED);
Pf_ED(i, snr_idx) = mean(Pf_cluster_ED);
Pd_AND(i, snr_idx) = mean(Pd_cluster_AND);
Pf_AND(i, snr_idx) = mean(Pf_cluster_AND);
Pd_OR(i, snr_idx) = mean(Pd_cluster_OR);
Pf_OR(i, snr_idx) = mean(Pf_cluster_OR);
Pd_Voting(i, snr_idx) = mean(Pd_cluster_Voting);
Pf_Voting(i, snr_idx) = mean(Pf_cluster_Voting);
end
end
% Plot Probability of Detection vs SNR for ED, AND, OR, and Voting logic using subplots
figure;
for i = 1:num_clusters
subplot(ceil(sqrt(num_clusters)), ceil(sqrt(num_clusters)), i);
hold on;
plot(SNR, Pd_ED(i, :), '-o', 'DisplayName', 'ED');
plot(SNR, Pd_AND(i, :), '--x', 'DisplayName', 'AND');
plot(SNR, Pd_OR(i, :), '-.*', 'DisplayName', 'OR');
plot(SNR, Pd_Voting(i, :), '-s', 'DisplayName', 'Voting');
xlabel('SNR (dB)');
ylabel('Probability of Detection');
title(['Cluster ' num2str(i)]);
legend('show');
hold off;
end
sgtitle('Probability of Detection vs SNR for ED, AND, OR, and Voting logic across Clusters');
% Plot Probability of False Alarm vs SNR for ED, AND, OR, and Voting logic using subplots
figure;
for i = 1:num_clusters
subplot(ceil(sqrt(num_clusters)), ceil(sqrt(num_clusters)), i);
hold on;
plot(SNR, Pf_ED(i, :), '-o', 'DisplayName', 'ED');
plot(SNR, Pf_AND(i, :), '--x', 'DisplayName', 'AND');
plot(SNR, Pf_OR(i, :), '-.*', 'DisplayName', 'OR');
plot(SNR, Pf_Voting(i, :), '-s', 'DisplayName', 'Voting');
xlabel('SNR (dB)');
ylabel('Probability of False Alarm');
title(['Cluster ' num2str(i)]);
legend('show');
hold off;
end
sgtitle('Probability of False Alarm vs SNR for ED, AND, OR, and Voting logic across Clusters');
% Plot Probability of Detection vs SNR for ED, AND, OR, and Voting logic
figure;
hold on;
for i = 1:num_clusters
plot(SNR, Pd_ED(i, :), '-o', 'DisplayName', ['Cluster ' num2str(i) ' ED']);
plot(SNR, Pd_AND(i, :), '--x', 'DisplayName', ['Cluster ' num2str(i) ' AND']);
plot(SNR, Pd_OR(i, :), '-.*', 'DisplayName', ['Cluster ' num2str(i) ' OR']);
plot(SNR, Pd_Voting(i, :), '-s', 'DisplayName', ['Cluster ' num2str(i) ' Voting']);
end
xlabel('SNR (dB)');
ylabel('Probability of Detection');
title('Probability of Detection vs SNR for ED and Collaborative ED (AND, OR, Voting)');
legend('show');
hold off;
% Plot Probability of False Alarm vs SNR for ED, AND, OR, and Voting logic
figure;
hold on;
for i = 1:num_clusters
plot(SNR, Pf_ED(i, :), '-o', 'DisplayName', ['Cluster ' num2str(i) ' ED']);
plot(SNR, Pf_AND(i, :), '--x', 'DisplayName', ['Cluster ' num2str(i) ' AND']);
plot(SNR, Pf_OR(i, :), '-.*', 'DisplayName', ['Cluster ' num2str(i) ' OR']);
plot(SNR, Pf_Voting(i, :), '-s', 'DisplayName', ['Cluster ' num2str(i) ' Voting']);
end
xlabel('SNR (dB)');
ylabel('Probability of False Alarm');
title('Probability of False Alarm vs SNR for ED and Collaborative ED (AND, OR, Voting)');
legend('show');
hold off;
SIMULATED OUTPUTS
DOWNLOAD LINK