Problem with nature of curve in MATLAB

6 views (last 30 days)
charu shree
charu shree on 19 Jul 2023
Hello all, I had written the following MATLAB code for which I am expecting the downward curve as output, However I am getting exactly opposite. Any help in this regard will be highly appreciated.
clc;
clear all;
close all;
%% Initialization
Mod = 4; % As RF signal is QPSK modulated
N = 256; % spreading gain i.e., one bit from tag remains constant for N s(n), Given in paper
zeta = 0.8; % tag reflection coefficient, Given in paper
P_s = 1; % power of s(n), given in paper
v_h = 1; % variance of channel between s-r, given in paper
v_f =1; % variance of channel between s-t, given in paper
v_g = 1;% variance of channel between t-r, given in paper
M_train = 10^3; % no. of tag symbols for training
M_test = 10^3;
N_w = 1; % var of noise , not given in paper
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
constellation = 1/sqrt(2)*[1+1i, -1+1i, -1-1i, 1-1i];
As = constellation;
M_train_detail = int32(randi([0, 1], [1, M_train])); % generating random tag symbols
%% SNR loop
ec_SVM = [];
for snr = -15:5:15
snr
feat_training = []; label_train = [];
for m = 1:M_train % This is for +1
m
g = sqrt(v_g/2)*(randn(1,1)+1i*randn(1,1)); % ch bet t-r
f = sqrt(v_f/2)*(randn(1,1)+1i*randn(1,1)); % ch bet s-t
h = sqrt(v_h/2)*(randn(1,1)+1i*randn(1,1)); % ch bet s-r
xn = randi([1 4], 1, N);
sn = constellation(xn);
wn = sqrt(N_w/2)*(randn(1,N)+1i*randn(1,N)); % AWGN
yn_1 = ((h+(zeta*f*g)).*sn)+(10^(-snr/20).*wn); % Received Signal with + 1
psi_m_1 = h+(zeta*f*g); psi_m_1_herm = conj(psi_m_1); psi_m_1_herm_nrm_sq = (norm(psi_m_1_herm))^2;
A1 = psi_m_1_herm/psi_m_1_herm_nrm_sq;
for n = 1:N
for k1 = 1:length(As)
P1(k1) = (abs(A1*yn_1(n)-As(k1)))^2;
%abs(y1'/(norm(y1)^2)*y(n) - s_API(k1))^2; %MRC
end
[min_P1_P,ind_P1_P] = min(P1);
s_m_hat_pls(1,n) = As(ind_P1_P);
end
en_pls_1 = ((norm(yn_1-(psi_m_1*s_m_hat_pls)))^2);
en_pls = (1/N)*(en_pls_1);
feat_train_pls = en_pls;
yn_0 = ((h-(zeta*f*g)).*sn)+(10^(-snr/20).*wn); % Received Signal with - 1
psi_m_0 = h-(zeta*f*g);
psi_m_0_herm = conj(psi_m_0);
psi_m_0_herm_nrm_sq = (norm(psi_m_0_herm))^2;
A0 = psi_m_0_herm/psi_m_0_herm_nrm_sq;
for n = 1:N
for k1 = 1:length(As)
P0(k1) = (abs(A0*yn_0(n)-As(k1)))^2;
end
[min_P0_P,ind_P0_P] = min(P0);
s_m_hat_min(1,n) = As(ind_P0_P);
end
en_min_1 = ((norm(yn_0-(psi_m_0*s_m_hat_min)))^2);
en_min = (1/N)*(en_min_1);
feat_train_min = en_min;
feat_train = [feat_train_pls,feat_train_min];
feat_training = [feat_training;feat_train];
if en_pls<en_min
labl_train = -1;
label_train = [label_train;labl_train];
else
labl_train = 1;
label_train = [label_train;labl_train];
end
end
% Model train
Mdl_SVM = fitcsvm(feat_training,label_train); % Model is trained
% Test data
[label_test,feat_test] = data_test7(v_h,v_f,v_g,N_w,N,M_test,zeta,As,snr,constellation);
Predic_label_SVM = predict(Mdl_SVM,feat_test);
% computing error count
ec_SVM = err_count(Predic_label_SVM, label_test, M_test,ec_SVM);
end
ber_sim_SVM = ec_SVM./(M_test); % simulated BER
%% Plot
SNRdB = -15:5:15;
figure(1)
semilogy(SNRdB,ber_sim_SVM,'g -','LineWidth',1);
%semilogy(SNRdB,err_kernel_SVM,'b*',SNRdB,ber_sim_SVM,'g-',SNRdB,err_kernel_KNN,'r*',SNRdB,ber_sim_KNN,'k--',SNRdB,err_kernel_RF,'r+',SNRdB,ber_sim_RF,'b-','LineWidth',1);
grid on
xlabel('SNR in dB')
ylabel('BER , error kernel')
%legend('error Kernel: SVM','BER: SVM','error Kernel: KNN','BER: KNN', 'error Kernel: RF','BER: RF')
axis([min(SNRdB) max(SNRdB) 1e-2 1])
%% Code related to function data_test7
function [label_test,feat_testing] = data_test7(v_h,v_f,v_g,N_w,N,M,zeta,As,snr,constellation)
% Test symbols for each SNR
feat_testing = []; label_test = [];
M_test_detail = int32(randi([0, 1], [1, M])); % generating random tag symbols
As;
constellation;
for m = 1:M % This is for +1
m
g = sqrt(v_g/2)*(randn(1,1)+1i*randn(1,1)); % ch bet t-r
f = sqrt(v_f/2)*(randn(1,1)+1i*randn(1,1)); % ch bet s-t
h = sqrt(v_h/2)*(randn(1,1)+1i*randn(1,1)); % ch bet s-r
xn = randi([1 4], 1, N);
sn = constellation(xn);
wn = sqrt(N_w/2)*(randn(1,N)+1i*randn(1,N)); % AWGN
yn_1 = ((h+(zeta*f*g)).*sn)+(10^(-snr/20).*wn); % Received Signal with + 1
psi_m_1 = h+(zeta*f*g); psi_m_1_herm = conj(psi_m_1); psi_m_1_herm_nrm_sq = (norm(psi_m_1_herm))^2;
A1 = psi_m_1_herm/psi_m_1_herm_nrm_sq;
for n = 1:N
for k1 = 1:length(As)
P1(k1) = (abs(A1*yn_1(n)-As(k1)))^2;
%abs(y1'/(norm(y1)^2)*y(n) - s_API(k1))^2; %MRC
end
[min_P1_P,ind_P1_P] = min(P1);
s_m_hat_pls(1,n) = As(ind_P1_P);
end
en_pls_1 = ((norm(yn_1-(psi_m_1*s_m_hat_pls)))^2);
en_pls = (1/N)*(en_pls_1);
feat_test_pls = en_pls;
yn_0 = ((h-(zeta*f*g)).*sn)+(10^(-snr/20).*wn); % Received Signal with - 1
psi_m_0 = h-(zeta*f*g);
psi_m_0_herm = conj(psi_m_0);
psi_m_0_herm_nrm_sq = (norm(psi_m_0_herm))^2;
A0 = psi_m_0_herm/psi_m_0_herm_nrm_sq;
for n = 1:N
for k1 = 1:length(As)
P0(k1) = (abs(A0*yn_0(n)-As(k1)))^2;
end
[min_P0_P,ind_P0_P] = min(P0);
s_m_hat_min(1,n) = As(ind_P0_P);
end
en_min_1 = ((norm(yn_0-(psi_m_0*s_m_hat_min)))^2);
en_min = (1/N)*(en_min_1);
feat_test_min = en_min;
feat_test = [feat_test_pls,feat_test_min];
feat_testing = [feat_testing;feat_test];
if en_pls<en_min
labl_test = -1;
label_test = [label_test;labl_test];
else
labl_test = 1;
label_test = [label_test;labl_test];
end
end
end
%% Code related to function err_count
function [ec_SVM] = err_count(Predic_label_SVM, labl_test, M,ec_SVM)
error_count = 0;
for i = 1: M % for loop to compare the labels
if Predic_label_SVM(i) ~= labl_test(i)
error_count = error_count + 1;
end
end
%end
ec_SVM =[ec_SVM, error_count]; % store error count for plot
end

Answers (0)

Community Treasure Hunt

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

Start Hunting!