I have developed the Matlab code for Max likelihood detector from the research paper. My problem is that the code is running but my BER curve is not correct i.e, my BER values sometimes decrese or increase. The title of the paper is "Semi-coherent Detection and Performance Analysis for Ambient Backscatter System".Thanks in advance. Also attaching the image of algorithm.Optimal detector
`clc;
clear all;
close all;
% Initialization
Nw = 1; % noise variance
M = 50 ; % symbol period of the tag
Mt = 4; % training symbols
al = 0.5 ; % alpha is tag coefficient
L = 1000;
N_mc = 10^6;
N = 50; % comsecutive samples of s(n)
RCD = 0.5;% Relative channel difference.
SNRdB = 0:5:20;
% source
sn = sqrt(1/2)*(randn(1,L)+1i*randn(1,L));
Ps = 1; % RF source signal power
% channels
hst = sqrt(1/2)*(randn(1,L)+1i*randn(1,L));
hsr = sqrt(1/2)*(randn(1,L)+1i*randn(1,L));
vn = 10; % var of htr
htr = sqrt(vn/2)*(randn(1,L)+1i*randn(1,L));
wn = sqrt(1/2)*(randn(1,L)+1i*randn(1,L));
ho = hsr;
ho_abs = sqrt(sum(abs(ho).^2));
h1 = (hsr)+ (al*(hst.*htr));
h1_abs = sqrt(sum(abs(h1).^2));
% Thresholds
so2 = ((ho_abs)^2*Ps)+Nw;% sigmao square
s12 = ((h1_abs)^2*Ps)+Nw; % sigma1 square
ThCG = ((N*so2*s12)/(s12-so2))*log(s12/so2); % threshold for complex gaussian optimal detector.
% Reader
I = eye(N);
vn_yo = so2*I;
y_Ho = sqrt(vn_yo/2).*(randn(N,N)+1i*randn(N,N)); % matrix
vn_y1 = s12*I;
y_H1 = sqrt(vn_y1/2).*(randn(N,N)+1i*randn(N,N)); % matrix
y_bar_H1 = diag(y_H1);
y_bar_Ho = diag(y_Ho);
z = norm(y_bar_H1);
Z = z^2;
Nid = 1000; % number of incorrect bit decisions
for i = 1:length(SNRdB)
errcnt = 0;
bitcnt = 0;
while errcnt < Nid
d = (rand > 1/2);
if d == 1
y_bar_H1 = diag(y_H1);
end
if d == 0
y_bar_Ho = diag(y_Ho);
end
if so2 > s12
if Z > ThCG
dhat = 0;
else
dhat = 1;
end % for Z>ThCG
else
if Z < ThCG
dhat = 0;
else
dhat = 1;
end
end
if dhat ~= d
errcnt = errcnt+1;
end
bitcnt = bitcnt+1;
end
BER(i) = errcnt/bitcnt;
end
figure
grid on;
semilogy(SNRdB, BER);
`