%Generation of gussian noise with the variance var.
noise=randn(1,len)*sigma;
rx=tx+noise;
switch y
case 1
%Bayes technique.
threshold=((2*sigma^2)*log(p0*x(2)/(1-p0)*x(1))-level0^2+level1^2)/...
(2*(level1-level0));
detected=[];
for i=1:1:len
if(rx(i)>=threshold)
detected=[detected 1];
else
detected=[detected 0];
end
end
pd_obtained=(length(find((data-detected)==0))/len);
pf_obtained=(length(find((data-detected)==-1))/len);
subplot(5,1,1)
plot(data)
subplot(5,1,2)
plot(tx)
subplot(5,1,3)
plot(noise)
subplot(5,1,4)
plot(rx)
hold on
plot(ones(1,len)*threshold,'r')
subplot(5,1,5)
plot(detected,'r')
u1=(threshold-level0)/(sqrt(2)*sigma);
u2=(threshold-level1)/(sqrt(2)*sigma);
pf_expected=1-(1/2)-(1/2)*erf(u1);
pd_expected=1-(1/2)-(1/2)*erf(u2);
case 2
%Mini-max technique
%Threshold is obtained by equating probability of false alarm (part1) and the
%probability of miss (part2). In this case uniform costs are assumed
%i.e, x=[1 1];erf(alpha) is the error fuction that computes the integration
%2/sqrt(pi)exp(-tˆ2) over the limit 0 to alpha.
mini=100;
for thresholdrange=-10:0.001:10;
u1=(thresholdrange-level0)/(sqrt(2)*sigma);
u2=(thresholdrange-level1)/(sqrt(2)*sigma);
part1=1-(1/2)-(1/2)*erf(u1);
part2=(1/2)+(1/2)*erf(u2);
if(abs(part1-part2)<=mini)
threshold=thresholdrange;
mini=abs(part1-part2);
end
end
detected=[];
for i=1:1:len
if(rx(i)>=threshold)
detected=[detected 1];
else
detected=[detected 0];
end
end
pd_obtained=(length(find((data-detected)==0))/len);
pf_obtained=(length(find((data-detected)==-1))/len);
subplot(5,1,1)
plot(data)
subplot(5,1,2)
plot(tx)
subplot(5,1,3)
plot(noise)
subplot(5,1,4)
plot(rx)
hold on
plot(ones(1,len)*threshold,'r')
subplot(5,1,5)
plot(detected,'r')
u1=(threshold-level0)/(sqrt(2)*sigma);
u2=(threshold-level1)/(sqrt(2)*sigma);
pf_expected=1-(1/2)-(1/2)*erf(u1);
pd_expected=1-(1/2)-(1/2)*erf(u2);
case 3
%Neyman-pearson technique
%The threshold is obtained by maximizing the probability of detection such
%that the probability of false-alarm is less than pfa. The solution is to
%obtain the threshod such that the probability of false-alarm is equal pfa.
mini=100;
threshold=0;
for thresholdrange=-10:0.001:10;
u1=(thresholdrange-level0)/(sqrt(2)*sigma);
part1=1-(1/2)-(1/2)*erf(u1);
if(abs(part1-pfa)<=mini)
threshold=thresholdrange;
mini=abs(part1-pfa);
end
end
detected=[];
for i=1:1:len
if(rx(i)>=threshold)
detected=[detected 1];
else
detected=[detected 0];
end
end
pd_obtained=(length(find((data-detected)==0))/len);
pf_obtained=(length(find((data-detected)==-1))/len);
subplot(5,1,1)
plot(data)
subplot(5,1,2)
plot(tx)
subplot(5,1,3)
plot(noise)
subplot(5,1,4)
plot(rx)
hold on
plot(ones(1,len)*threshold,'r')
subplot(5,1,5)
plot(detected,'r')
u1=(threshold-level0)/(sqrt(2)*sigma);
u2=(threshold-level1)/(sqrt(2)*sigma);
pf_expected=1-(1/2)-(1/2)*erf(u1);
pd_expected=1-(1/2)-(1/2)*erf(u2);
end
