Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Cooperative spectrum sensing(energy detection) in cognitive radio.Matlab code

Status
Not open for further replies.

manos87

Junior Member level 1
Joined
Oct 27, 2010
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,482
Hello to all!
It seems that i might need a little help with the matlab code in a
cooperative spectrum sensing project.
As you will see if you run the code the ROC figures for the theoretic part is correct but for the simulation part is not.Can somebody help me?

clear all;
close all;
clc


u=1000;%time bandwidth factor
N=2*u;%samples
a=2;%path loss exponent
C=2;%constant losses
Crs=10; %Number of cognitive radio users
PdAnd=0;
%----------Pfa------------%
Pf=0.01:0.01:1;
Pfa=Pf.^2;
%---------signal-----%
t=1:N;
s1 = cos(pi*t);
% s1power=var(s1);


%-------- SNR ----------%
% Snrdb=-15:1:15;
Snrdb=15;
Snreal=power(10,Snrdb/10);%Linear Snr

% while Snrdb<15
for i=1:length(Pfa)
lamda(i)=gammaincinv(1-Pfa(i),u)*2; %theshold
% lamdadB=10*log10(lamda);


%---------Local spectrum sensing---------%

for j=1:Crs %for each node
detect=0;
% d(j)=7+1.1*rand(); %random distanse
d=7:0.1:8;
PL=C*(d(j)^-a); %path loss

for sim=1:10%Monte Carlo Simulation for 100 noise realisation

%-------------AWGN channel--------------------%
noise = randn(1,N); %Noise production with zero mean and s^2 var
noise_power = mean(noise.^2); %noise average power
amp = sqrt(noise.^2*Snreal);
s1=amp.*s1./abs(s1);
% SNRdB_Sample=10*log10(s1.^2./(noise.^2));
Rec_signal=s1+noise;%received signal
localSNR(j)=mean(abs(s1).^2)*PL/noise_power;%local snr
Pdth(j,i)=marcumq(sqrt(2*localSNR(j)),sqrt(lamda(i)),u);%Pd for j node

%Computation of Test statistic for energy detection
Sum=abs(Rec_signal).^2*PL;
Test(j,sim)=sum(Sum);
if (Test(j,sim)>lamda(i))
detect=detect+1;
end

end %END Monte Carlo

Pdsim(j)=detect/sim; %Pd of simulation for the j-th CRuser


end
PdAND(i)=prod(Pdsim);
PdOR(i)=1-prod(1-Pdsim);

end


PdAND5=(Pdth(5,:)).^5;
Pmd5=1-PdAND5;
PdANDth=(Pdth(Crs,:)).^Crs;
PmdANDth=1-PdANDth; %Probability of miss detection
Pmdsim=1-PdAND;
figure(1);
plot(Pfa,Pmdsim,'r-*',Pfa,PmdANDth,'k-o',Pfa,Pmd5,'g-*');
title('Complementary ROC of Cooperative sensing with AND rule under AWGN');
grid on
axis([0.0001,1,0.0001,1]);
xlabel('Probability of False alarm (Pfa)');
ylabel('Probability of Missed Detection (Pmd)');
legend('Simulation','Theory n=10','Theory n=5');
 

hello;
have u then solve this problem?
i am also working on it,but im trying to plot Pd vs Pfa for different number of cognitive relays.can u help me in this regard.
thankx
 

i need two matlab coding regarding Pd vs sensing time and Pf vs sensing time of cognitive radio in cooperative spectrum sensing.if somebody can provide me any of these then it will be a great help for me. my email address is
thank you.

[QUOTE][COLOR="#FF0000"]Don't ask for help in private. This is an open forum and everything should be shared publicly so that all the members can benefit [alexan_e][/COLOR][/QUOTE]
 

can you tell me which paper do you simulate? i think there are something wrong with your function to compute threshold via false alarm probability function.
 
As for the calculation of threshold in energy detection, let say we refer to Digham et al. 2003 or digham et al. 2007 papers, then how can we compute the threshold for the corresponding P_fa ???
 

i dont know, i think gammaincinv can only compute its first parameter. have you find the solution to the calculation of threshold? if u do, please tell me.thx
 

is this code an implemetation of energy detection algortihm for cognitive radios..i need help to understand it..please reply?
 

Hello all,

I am referring a paper by S. Atapattu, C. Tellambura & H. Jiang which is based on "Relay based Cooperative Spectrum Sensing in Cognitive radio Networks". I don't know how to proceed toward implementing this paper in matlab i.e generating Probability of Detection VS Probability of False Alarm curves for different numbers of Cognitive Relays. Can anyone you help me with this?
I have also attached the paper with this thread.
 

Attachments

  • Relay Based Cooperative spectrum sensing.pdf
    246.6 KB · Views: 166

you need inverse gamma incomplete function
 

Pls i need to plot pd vs snr in for Energy Detection technique in spectrum sensing, pls how do i go about it. i was only successful with the ROC plot of Pd vs Pf. but Pd vs snr(db) has been an issue for me. Pls can i get some help. The matlab codes I'v been working on are given below:



close all;
clear all;
clc;
m=1000;
N =2*m; %sample points N=T/Ts=T/1/2W=2TW
Base= 0.01:0.02:1;
Pf =Base.^2; %False alarm
totalaccum= 0;
snr_avgdB =-20:20; %SNR=Ps/(N0*W)
snr_avg = power(10,snr_avgdB/10); %db to linear snr

for i=1:length(Pf)
Over_Num = 0;
averageovernum = 0;
initover_Num = 0;
Th(i) = chi2inv(1-Pf(i),2*m);
%Th(i) = gaminv(1-Pf(i),m,1)*2; %Threshold for given Pf(i)according to Digham 2003
for kk = 1:1000 %Monter-Carlo times
%If Transmmiting Single tone
t = 1:N; %samples
x = sin(pi*t); %single tone, Fs=2F0
noise = randn(1,N); %Noise production
pn = mean(noise.^2); %Noise power average
% pn = (std(noise))^2; %Noise power average
amp = sqrt(noise.^2*snr_avg);
x=amp.*x./abs(x);
SNRdB_Sample=10*log10(x.^2./(noise.^2));
signal = x(1:N);
ps = mean(abs(signal).^2); %signal average power
%awgn channels

Rev_sig = signal + noise; %received signal for detection
accum_power(i) = sum(abs(Rev_sig.^2))/pn; %accumulated received power(normalized to noise power)


if accum_power(i)>Th(i)
Over_Num = Over_Num +1; %decide 1 or 0, present or absent
end
end
Pd_sim(i) = Over_Num / kk; %Detection probability computation
end
figure
Plot (snr_avgdB,Pd_sim,'*r');%Complementary ROC Curve
title('Plot of Pd vs Snr')
grid on
axis([20,20,0.0001,1]);
xlabel('Snr(db)');
ylabel('Pd');

- - - Updated - - -

Pls i need to plot pd vs snr in for Energy Detection technique in spectrum sensing, pls how do i go about it. i was only successful with the ROC plot of Pd vs Pf. but Pd vs snr(db) has been an issue for me. Pls can i get some help. The matlab codes I'v been working on are given below:



close all;
clear all;
clc;
m=1000;
N =2*m; %sample points N=T/Ts=T/1/2W=2TW
Base= 0.01:0.02:1;
Pf =Base.^2; %False alarm
totalaccum= 0;
snr_avgdB =-20:20; %SNR=Ps/(N0*W)
snr_avg = power(10,snr_avgdB/10); %db to linear snr

for i=1:length(Pf)
Over_Num = 0;
averageovernum = 0;
initover_Num = 0;
Th(i) = chi2inv(1-Pf(i),2*m);
%Th(i) = gaminv(1-Pf(i),m,1)*2; %Threshold for given Pf(i)according to Digham 2003
for kk = 1:1000 %Monter-Carlo times
%If Transmmiting Single tone
t = 1:N; %samples
x = sin(pi*t); %single tone, Fs=2F0
noise = randn(1,N); %Noise production
pn = mean(noise.^2); %Noise power average
% pn = (std(noise))^2; %Noise power average
amp = sqrt(noise.^2*snr_avg);
x=amp.*x./abs(x);
SNRdB_Sample=10*log10(x.^2./(noise.^2));
signal = x(1:N);
ps = mean(abs(signal).^2); %signal average power
%awgn channels

Rev_sig = signal + noise; %received signal for detection
accum_power(i) = sum(abs(Rev_sig.^2))/pn; %accumulated received power(normalized to noise power)


if accum_power(i)>Th(i)
Over_Num = Over_Num +1; %decide 1 or 0, present or absent
end
end
Pd_sim(i) = Over_Num / kk; %Detection probability computation
end
figure
Plot (snr_avgdB,Pd_sim,'*r');%Complementary ROC Curve
title('Plot of Pd vs Snr')
grid on
axis([20,20,0.0001,1]);
xlabel('Snr(db)');
ylabel('Pd');

- - - Updated - - -

pls i need help in plotting Snr(db) vs Pd using Energy Detection technique of cognitive radio spectrum sensing. I'v been able to plot the ROC of PD vs PF. but the plot of Snr(db) vs Pd has been giving me a really tough time. Pls i need help. the codes I'm working on are given below:


close all;
clear all;
clc;
m=1000;
N =2*m; %sample points N=T/Ts=T/1/2W=2TW

Pf =0.01 %False alarm
totalaccum= 0;
snr_avgdB =-15: 20; %SNR=Ps/(N0*W)
snr_avg = power(10,snr_avgdB/10); %db to linear snr

for i=1:length(Pf)
Th(i) = chi2inv(1-Pf(i),2*m);
%Th(i) = gaminv(1-Pf(i),m,1)*2; %Threshold for given Pf(i)according to Digham 2003
for kk = 1:1000 %Monter-Carlo times
%If Transmmiting Single tone
t = 1:N; %samples
x = sin(pi*t); %single tone, Fs=2F0
noise = randn(1,N); %Noise production
pn = mean(noise.^2); %Noise power average
% pn = (std(noise))^2; %Noise power average
amp = sqrt(noise.^2*snr_avg);
x=amp.*x./abs(x);
SNRdB_Sample=10*log10(x.^2./(noise.^2));
signal = x(1:N);
ps = mean(abs(signal).^2); %signal average power
%awgn channels

Rev_sig = signal + noise; %received signal for detection
accum_power(i) = sum(abs(Rev_sig.^2))/pn; %accumulated received power(normalized to noise power)

if accum_power(i)>Th(i)
Over_Num = Over_Num +1; %decide 1 or 0, present or absent
end
end
Pd_sim(i) = Over_Num / kk; %Detection probability computation
end
figure
loglog(snr_avgdB,Pd_sim,'*r');%Complementary ROC Curve
title('Plot of Snr vs Pd')
grid on
axis([-20,20,0.1,1]);
xlabel('Snr(db)');
ylabel('Pd');
 

have you resolved the problem?
can you post the solution please.
Thx.
 

hey dude i got the answer , there were no errors in the code now. i got the answer.
 
Hello to all,

can any one help me with the code of Cooperative spectrum sensing(energy detection) in cognitive by using stochastic resonance.

thanks

- - - Updated - - -

dear,

I got a problem in your code of " Snr(db) vs Pd" in this line " amp = sqrt(noise.^2*snr_avg);" could any one help me with this as there is an error message from matlab "Error using *
Inner matrix dimensions must agree.

Error in sor (line 23)
amp = sqrt(snr_avg*noise.^2);

thanks,
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top