Help with water-filling code (Revised Question)
2 次查看(过去 30 天)
显示 更早的评论
Hi
I wrote-in earlier but i was asked to review the question.
Question: 1. How do i make a “for loop” to occur in sequence without repeating the “for loop” over again? N:B in the iterative water-filling code i have repeated the “for loop” a number of times
for i=1:2 %number of systems
if R(i)>(R_T(i)+(R_T(i))/100)
ini_power3(i)=ini_power3(i)-increase;
end
if R(i)<R_T(i)
ini_power3(i)=ini_power3(i)+(increase);
end
end
2. If you run this code and then type R1, R2...R22 on the matlab command interface, you will get the corresponding values. From Question (1) above, how do i display the following? User 1: R1,R3,R5,R7,R9,R11,R13,R15,R17,R21 User 2: R2,R4,R6,R8,R10,R12,R14,R16,18,R22
3. How do i plot? User 1: y-axis: R1,R3,R5,R7,R9,R11,R13,R15,R17,R21 x-axis: 1:10 %number of iterations User 2: y-axis: R2,R4,R6,R8,R10,R12,R14,R16,18,R22 x-axis: 1:10 %number of iterations
%Iterative Water-Filling Algorithm
% =========================================================================
% Definition of Parameters
% ========================================================================
clear all
clc
% Defines the Target data rates to be occupied in two channels
R1_T = 0.512; % Mbps
R2_T = 0.512; % Mbps
R_T=[R1_T,R2_T];
% Initial Power Spectral Densities
ini_power1 = 0;
ini_power2 = 0;
ini_power3 = [ini_power1,ini_power2];
% Channel Matrix
h11 = 1e-6;
h12 = 1.1e-6;
h21 = 0.9e-6;
h22 = 1.3e-6;
gamma = 2;% snr_gap corresponding to Symbol error probability of 2e-4 and %coding gain of 4dB
% Cross-coupling = alpha1 & alpha2
alpha1 = gamma *(h11)^2/(h22)^2;
alpha2 = gamma *(h21)^2/(h11)^2;
% -80 dBm; the particular parts of noise-floor's %spectral density that %defines the spectral content of spectrum hole1 and hole2
noiseDensity = 1e-11;
% Noise terms = N1 & N2
N1 = gamma*noiseDensity/(h11)^2;
N2 = gamma*noiseDensity/(h22)^2;
% Resulting data = total capacity of a channel based on shanon theory
R1 = log2(1 + (ini_power1)/(N1+alpha2*ini_power2));
R2 = log2(1 + (ini_power2)/(N2+alpha1*ini_power1));
R=[R1,R2];
% Increase PSD by a factor of 3dB
increasedB=1.9952; % 3dB
increase=10^(increasedB/10);
for i=1:2 %number of systems
if R(i)>(R_T(i)+(R_T(i))/100)
ini_power3(i)=ini_power3(i)-increase;
end
if R(i)<R_T(i)
ini_power3(i)=ini_power3(i)+(increase);
end
end
R3 = log2(1 + (ini_power3(1))/(N1+alpha2*ini_power3(2)));
R4 = log2(1 + (ini_power3(2))/(N2+alpha1*ini_power3(1)));
R = [R3,R4];
ini_power4=[ini_power3(1),ini_power3(2)];
for i=1:2 %number of systems
if R(i)>(R_T(i)+(R_T(i))/100)
ini_power4(i)=ini_power4(i)-increase;
end
if R(i)<R_T(i)
ini_power4(i)=ini_power4(i)+(increase);
end
end
R5 = log2(1 + (ini_power4(1))/(N1+alpha2*ini_power4(2)));
R6 = log2(1 + (ini_power4(2))/(N2+alpha1*ini_power4(1)));
R = [R5,R6];
ini_power5=[ini_power4(1),ini_power4(2)];
for i=1:2 %number of systems
if R(i)>(R_T(i)+(R_T(i))/100)
ini_power5(i)=ini_power5(i)-increase;
end
if R(i)<R_T(i)
ini_power5(i)=ini_power5(i)+(increase);
end
end
R7 = log2(1 + (ini_power5(1))/(N1+alpha2*ini_power5(2)));
R8 = log2(1 + (ini_power5(2))/(N2+alpha1*ini_power5(1)));
R = [R7,R8];
ini_power6=[ini_power5(1),ini_power5(2)];
for i=1:2 %number of systems
if R(i)>(R_T(i)+(R_T(i))/100)
ini_power6(i)=ini_power6(i)-increase;
end
if R(i)<R_T(i)
ini_power6(i)=ini_power6(i)+(increase);
end
end
R9 = log2(1 + (ini_power6(1))/(N1+alpha2*ini_power6(2)));
R10 = log2(1 + (ini_power6(2))/(N2+alpha1*ini_power6(1)));
R = [R9,R10];
ini_power7=[ini_power6(1),ini_power6(2)];
for i=1:2 %number of systems
if R(i)>(R_T(i)+(R_T(i))/100)
ini_power7(i)=ini_power7(i)-increase;
end
if R(i)<R_T(i)
ini_power7(i)=ini_power7(i)+(increase);
end
end
R11 = log2(1 + (ini_power7(1))/(N1+alpha2*ini_power7(2)));
R12 = log2(1 + (ini_power7(2))/(N2+alpha1*ini_power7(1)));
R = [R11,R12];
ini_power8=[ini_power7(1),ini_power7(2)];
for i=1:2 %number of systems
if R(i)>(R_T(i)+(R_T(i))/100)
ini_power8(i)=ini_power8(i)-increase;
end
if R(i)<R_T(i)
ini_power8(i)=ini_power8(i)+(increase);
end
end
R13 = log2(1 + (ini_power8(1))/(N1+alpha2*ini_power8(2)));
R14 = log2(1 + (ini_power8(2))/(N2+alpha1*ini_power8(1)));
R = [R13,R14];
ini_power9=[ini_power8(1),ini_power8(2)];
for i=1:2 %number of systems
if R(i)>(R_T(i)+(R_T(i))/100)
ini_power9(i)=ini_power9(i)-increase;
end
if R(i)<R_T(i)
ini_power9(i)=ini_power9(i)+(increase);
end
end
R15 = log2(1 + (ini_power9(1))/(N1+alpha2*ini_power9(2)));
R16 = log2(1 + (ini_power9(2))/(N2+alpha1*ini_power9(1)));
R = [R15,R16];
ini_power10=[ini_power9(1),ini_power9(2)];
for i=1:2 %number of systems
if R(i)>(R_T(i)+(R_T(i))/100)
ini_power10(i)=ini_power10(i)-increase;
end
if R(i)<R_T(i)
ini_power10(i)=ini_power10(i)+(increase);
end
end
R17 = log2(1 + (ini_power10(1))/(N1+alpha2*ini_power10(2)));
R18 = log2(1 + (ini_power10(2))/(N2+alpha1*ini_power10(1)));
R = [R17,R18];
ini_power11=[ini_power10(1),ini_power10(2)];
for i=1:2 %number of systems
if R(i)>(R_T(i)+(R_T(i))/100)
ini_power11(i)=ini_power11(i)-increase;
end
if R(i)<R_T(i)
ini_power11(i)=ini_power11(i)+(increase);
end
end
R19 = log2(1 + (ini_power11(1))/(N1+alpha2*ini_power11(2)));
R20 = log2(1 + (ini_power11(2))/(N2+alpha1*ini_power11(1)));
R = [R19,R20];
ini_power12=[ini_power11(1),ini_power11(2)];
for i=1:2 %number of systems
if R(i)>(R_T(i)+(R_T(i))/100)
ini_power12(i)=ini_power12(i)-increase;
end
if R(i)<R_T(i)
ini_power12(i)=ini_power12(i)+(increase);
end
end
R21 = log2(1 + (ini_power12(1))/(N1+alpha2*ini_power12(2)));
R22 = log2(1 + (ini_power12(2))/(N2+alpha1*ini_power12(1)));
R = [R21,R22];
ini_power13=[ini_power12(1),ini_power12(2)];
for i=1:2 %number of systems
if R(i)>(R_T(i)+(R_T(i))/100)
ini_power13(i)=ini_power13(i)-increase;
end
if R(i)<R_T(i)
ini_power13(i)=ini_power13(i)+(increase);
end
end
R21 = log2(1 + (ini_power13(1))/(N1+alpha2*ini_power13(2)));
R22 = log2(1 + (ini_power13(2))/(N2+alpha1*ini_power13(1)));
R = [R21,R22];
ini_power14=[ini_power13(1),ini_power13(2)];
for i=1:2 %number of systems
if R(i)>(R_T(i)+(R_T(i))/100)
ini_power14(i)=ini_power14(i)-increase;
end
if R(i)<R_T(i)
ini_power14(i)=ini_power14(i)+(increase);
end
end
2 个评论
Walter Roberson
2011-8-15
Please take in to account time-zone differences. You posted your revised question after midnight everywhere in North America (other than Alaska); your repeated requests for assistance were at times when most people in North America were asleep, but people in Western Europe had not yet woken up.
回答(1 个)
Jorrit
2011-8-15
1. You can probably make a double loop, a loop inside another loop. A second option is maybe to make a function out of it. I haven't looked at your code in depth, but it looks like there is a large amount of repetition.
2. & 3. Use logical indexing. Works for both displaying the numbers and plotting. You can use mod(n,2) for checking for even/odd numbers. to do this: n = 1:9;R(mod(n,2)==0) gives the 2nd, 4th, 6th etc entry. R(mod(n,2)==1) gives the 1st, 3rd, 5th etc entry.
2 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!