Hi. Can anyone provide the matlab code for spectrum sensing in cognitive radio using K-means?

18 Ansichten (letzte 30 Tage)
My code performs cooperative sensing with an energy detector, so I want to apply k-means to cluster the network. Can someone help me to provide the code in Matlab of how to implement.

Antworten (2)

Sam Chak
Sam Chak am 4 Dez. 2023
Sure @Alcides, the code for computing the k-means clustering is available in this link:
  2 Kommentare
Alcides
Alcides am 4 Dez. 2023
Thank you dear @Sam, I understand the concept of the algorithm, I have difficulty applying it to cooperative spectral sensing. I don't know how to do it. Can i share my code with you?
Sam Chak
Sam Chak am 4 Dez. 2023
Don't mention it, @Alcides. Just to let you know, I'm not an expert in cognitive radio networks. Other users or experts might be able to help out. Therefore, you are encouraged to provide the MATLAB clustering code (click this icon ) and attach the necessary data (click this icon ) for the cluster computation. This way, we can test what went wrong.

Melden Sie sich an, um zu kommentieren.


Alcides
Alcides am 4 Dez. 2023
clear variables
close all;
clc
%% cooperative spectrum sensing with energy detector
nPUs = 1; % Number of primary users
nSUs = 15; % Number of secondary users
nSamps = 1e3; % Number of sensing samples per sensing period
snrdB = linspace(-20,-5,nSUs); snr = db2pow(snrdB); % Signal-to-noise ratio (SNR) of each SU
sPow = ones(1,nSUs); % Received PU signal power at each SU
nPow = sPow./snr; % Noise power at each SU
nMCEvents = 5e3; % Number of Monte Carlo events
nROCPts = 50; % Number of receiver operating characteristic (ROC) curve points
%%% K-means clustering
raio = 100;
% Coordenadas do FC
FC_x = 0;
FC_y = 0;
% Coordenadas aleatórias para os sensores dentro do raio especificado
raios_sensores = raio * rand(1, nSUs);
angulos_sensores = 2 * pi * rand(1, nSUs);
sensores_x = FC_x + raios_sensores .* cos(angulos_sensores);
sensores_y = FC_y + raios_sensores .* sin(angulos_sensores);
% Calcular a distância entre o FC e o sensor
distancia = sqrt((FC_x - sensores_x).^2 + (FC_y - sensores_y).^2);
% Aplicando o algoritmo k-means
num_grupos = 3; % Número de grupos desejados
positions = [sensores_x; sensores_y]';
[idx, centros] = kmeans(positions, num_grupos);
%%% Cálculo da probabilidade relativa (Monte Carlo)
Ted = zeros(nMCEvents,1); % Preallocating for speed
TedSU = zeros(nMCEvents,[idx, centros]); % Preallocating for speed
TX = randi([0,1],nMCEvents,1); % Simulates hypotheses H0 and H1 for the received signal, 50% under H0 and 50% under H1
for i = 1:nMCEvents
H = sqrt(1/2)*complex(randn([idx, centros],nPUs),randn([idx, centros],nPUs)); % Rayleigh channel samples for each SU
X = sqrt(1/nPUs)*complex(randn(nPUs,nSamps),randn(nPUs,nSamps)); % Normally distributed primary user signal (weighted by sqrt(1/nPUs) for keeping desired SNR)
V = sqrt(diag(nPow)/2)*complex(randn([idx, centros],nSamps),randn([idx, centros],nSamps)); % AWGN
Y = TX(i)*sqrt(diag(sPow)/2)*(H*X) + V; % Received signal at each SU
Ted(i) = (1/[idx, centros])*sum((1/nSamps)*sum(abs(Y).^2,2)); % Energy detection test statistic
TedSU(i,:) = (1/nSamps)*sum(abs(Y).^2,2); % Energy detection test statistic
end
%%% FC
Thres = linspace(min(Ted),0.85*max(Ted),nROCPts); % Threshold decision for each ROC point
nH0 = sum(TX==0); % The number of times the PU transmitter(s) has been in the non-active state
nH1 = sum(TX==1); % The number of times the PU transmitter(s) has been in the active state
Pfa = sum(Ted>Thres&TX==0)/nH0; % Probability of false alarm for each decision threshold
Pd = sum(Ted>Thres&TX==1)/nH1; % Probability of detection for each decision threshold
%%% Figure
figure
plot(Pfa,Pd,'-*'), hold on
title('ROC curve')
xlabel('Probability of false alarm')
ylabel('Probability of detection')
legend('ED FC freq. relativa', 'ED FC Majority fusion rule','Location','SouthEast')
grid
axis([0,1,0,1])

Produkte


Version

R2017b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by