my code keeps looping indefinitely and I could not figure out why, please help

1 visualizzazione (ultimi 30 giorni)
%% parameter
SNR_dB = 10;%[0:5:20];
SNR = 10^(SNR_dB/10.);
N_iter = 100;
n = 256; % number of beams (transmit antennas)
%K = 32; % number of users
L = 3; % number of paths per user
lamada = 1; % wavelength
d = lamada/2;
%N = K; % number of retained RF chains for conventional beamspace MIMO IA-BS
P = 32; % total transmitted power
Imax = 30; % iteration times of power allocation
temp_it = [];
K_list = 10:10:50;
%%
for i_snr = 1:length(K_list)
K = K_list(i_snr);
N = K;
%SNR = SNR_linear(i_snr);
temp = 0; temp1 = 0; temp2 = 0; temp3 = zeros(1,Imax); temp4 = zeros(1,Imax); temp5 = 0; temp6 = 0;
temp22 = 0; temp33 = zeros(1,Imax); temp44 = zeros(1,Imax); temp55 = 0; temp66 = 0;
for iter = 1:N_iter
H = beamspace_channel(n,K,L); % generate the signalspace channel
U = zeros(n,n);
deta = 1/n;
for i = -(n-1)/2:1:(n-1)/2
U(:,i+(n+1)/2) = sqrt(1/n)*exp(1i*[0:n-1]*2*pi*deta*i).';
end
H_beam = U.'*H; % beamspace channel
[Hr1,Hr1_e] = IA_BS(H_beam,H_beam); % IA-BS
[Hr3,Hr3_e,F3,rf_num3,setf3] = reduce_RF(H_beam,K,H_beam); % NOMA mmWave-P1
%% Full digital
F = H_beam*inv(H_beam'*H_beam);
beta = sqrt(P/trace(F'*F));
H_eq = H_beam'*F;
for k = 1:K
sum_inf = sum(abs(H_eq(k,:)).^2)-abs(H_eq(k,k))^2;
temp = temp+log2(1+abs(H_eq(k,k))^2/(sum_inf+1/(SNR*beta^2)));
end
%% IA-BS
F1 = Hr1_e*inv(Hr1_e'*Hr1_e);
beta1 = sqrt(P/trace(F1'*F1));
H_eq1 = Hr1'*F1;
for k = 1:K
sum_inf = sum(abs(H_eq1(k,:)).^2)-abs(H_eq1(k,k))^2;
temp1 = temp1+log2(1+abs(H_eq1(k,k))^2/(sum_inf+1/(SNR*beta1^2)));
end
%% NOMA mmWave
[SE,EE,ite,power1] = PA(Hr3,F3,setf3,K,rf_num3,1/SNR,P,Imax);
temp3 = temp3+SE;
temp33 = temp33+EE;
%% OMA mmWave
beta3 = P/trace(F3'*F3);
H_eq3 = Hr3'*F3;
for i = 1:rf_num3
usern = setf3(i,:);
usern(usern==0) = [];
unum = length(usern);
for m = 1:unum
sum_inf = sum(abs(H_eq3(usern(m),:)).^2)-sum(abs(H_eq3(usern(m),usern(1:end))).^2);
temp5 = temp5+log2(1+abs(H_eq3(usern(m),usern(m)))^2/(sum_inf+1/SNR/unum/beta3))/unum;
temp55 = temp55+log2(1+abs(H_eq3(usern(m),usern(m)))^2/(sum_inf+1/SNR/unum/beta3))/unum/(P+rf_num3*305+200)*10^3;
end
end
end
C(i_snr) = temp/N_iter;
C1(i_snr) = temp1/N_iter;
C3(i_snr) = temp3(end)/N_iter;
C5(i_snr) = temp5/N_iter;
C00(i_snr) = temp/N_iter/(P+n*305+200)*10^3;
C11(i_snr) = temp1/N_iter/(P+K*305+200)*10^3;
C33(i_snr) = temp33(end)/N_iter;
C55(i_snr) = temp55/N_iter;
temp_it = [temp_it;temp3];
end
Unrecognized function or variable 'beamspace_channel'.
%CI3 = temp_it(3,:)/N_iter;
%%
figure;
plot(K_list,C,'k-*','Linewidth',1.5);
hold on
plot(K_list,C1,'r-*','Linewidth',1.5);
hold on
plot(K_list,C3,'g-*','Linewidth',1.5);
hold on
plot(K_list,C5,'c-*','Linewidth',1.5);
grid on
xlabel('The number of users');
ylabel('Spectral efficiency');
legend('Fully digital MIMO', 'The proposed beamspace MIMO-NOMA', 'MIMO-OMA', 'IA beam selection');
figure;
plot(K_list,C00,'k-','Linewidth',1.5);
hold on
plot(K_list,C11,'r-','Linewidth',1.5);
hold on
plot(K_list,C33,'g-','Linewidth',1.5);
hold on
plot(K_list,C55,'c-','Linewidth',1.5);
grid on
xlabel('The number of users');
ylabel('Energy efficiency');
legend( 'The proposed beamspace MIMO-NOMA', 'MIMO-OMA', 'IA beam selection', 'Fully digital MIMO');
% figure;
% plot(ite,CI3,'g-','Linewidth',1.5);
% grid on
% xlabel('Iteration');
% ylabel('Spectral efficiency');

Risposta accettata

Jan
Jan il 1 Feb 2023
Spostato: Jan il 1 Feb 2023
No, the loops do run run infinitely. They are only slow. Check this with displaying a loop counter.
Use the profile to find the bottleneck:
profile on
Then start the code. Press Ctrl-C after some time.
profile report
In which lines does Matlab spent the most time? Start optimizing the code there.
U is constant, isn't it? So avoid to create it in each iteration again, but do this once before the loop:
U = zeros(n,n);
deta = 1/n;
for i = -(n-1)/2:1:(n-1)/2
U(:,i+(n+1)/2) = sqrt(1/n)*exp(1i*[0:n-1]*2*pi*deta*i).';
end
Or do I see correctly that even H_beam is a constant?

Più risposte (0)

Categorie

Scopri di più su Mathematics in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by