Advertisement
makispaiktis

Eidikes Keraies - Ergasia2 - Part1

Jul 9th, 2021 (edited)
1,192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 10.09 KB | None | 0 0
  1. clear all
  2. clc
  3.  
  4.  
  5. % Main Function
  6. % Standard data
  7. M = 24;
  8. Pg = 1;    
  9. minAngle = 30*pi/180;
  10. maxAngle = 150*pi/180;
  11. stepDegrees = 0.1;
  12. % Delta and SNR
  13. %{
  14. deltaList = [6, 8, 10, 12, 14, 16] * pi/180;
  15. SNRlist = [0, 5, 10, 15, 20, 25, 30];
  16. for i = 1:length(deltaList)
  17.     delta = deltaList(i)
  18.     for j = 1:length(SNRlist)
  19.         SNR = SNRlist(j)
  20.         run(M, delta, SNR, Pg, minAngle, maxAngle);
  21.     end
  22. end
  23. %}
  24. SNR = 20;
  25. delta = 10*pi/180;
  26. run(M, delta, SNR, Pg, minAngle, maxAngle, stepDegrees);
  27.  
  28.  
  29.  
  30. % ===============================================================
  31. % ===============================================================
  32. % ======================== Functions ============================
  33. % ===============================================================
  34. % ===============================================================
  35.  
  36. % Function 1 - Steering vector
  37. function a = steeringVector(M, theta)
  38.     a = [];    
  39.     for m = 1 : M
  40.         a(m) = exp(i * pi * (m-1) * cos(theta));
  41.     end
  42.     a = a';
  43.     a;
  44. end
  45.  
  46. % Function 2 - Steering Matrix
  47. function A = steeringMatrix(M, thetaList)
  48.     A = [];
  49.     for index = 1 : length(thetaList)
  50.         A(:, index) = steeringVector(M, thetaList(index));
  51.     end
  52.     A;
  53. end
  54.  
  55. % Function 3 - Find the maximum angle0 (minimum is 30 degrees always)
  56. function maxAngle0 = findMaxAngle0(maxAngle, delta)
  57.     maxAngle0 = maxAngle - 5 * delta;
  58. end
  59.  
  60. % Function 4 - Create arxiki exada
  61. function arxikiExada = createArxikixada(angle0, delta)
  62.     arxikiExada = [angle0, angle0 + delta, angle0 + 2*delta, angle0 + 3*delta, angle0 + 4*delta, angle0 + 5*delta];
  63. end
  64.  
  65. % Function 5 - Having a single exada, I will create the new 6 exades
  66. % icluding the 6 differents combinations for theta0
  67. function exades = createExades(arxikiExada)
  68.     exada1 = arxikiExada;
  69.     exada2 = [arxikiExada(2) arxikiExada([1, 3:end])];
  70.     exada3 = [arxikiExada(3) arxikiExada([1:2, 4:end])];
  71.     exada4 = [arxikiExada(4) arxikiExada([1:3, 5:end])];
  72.     exada5 = [arxikiExada(5) arxikiExada([1:4, end])];
  73.     exada6 = [arxikiExada(6) arxikiExada([1:5])];
  74.     % Exada_i = a row vector with dimensions 1x6
  75.     % My output will be an matrix 6x6, each row is a new exada
  76.     exades = [exada1; exada2; exada3; exada4; exada5; exada6];
  77. end
  78.  
  79.  
  80. % Function 6 - Calculate the NSB weights given an exada = thetaList
  81. function weights = NSBweights(M, P_noise, exada)
  82.     A = steeringMatrix(M, exada);
  83.     tempMatrix = A' * A + P_noise * eye(6);
  84.     invTemp = inv(tempMatrix);
  85.     e1 = [1; 0; 0; 0; 0; 0];
  86.     weights = A * invTemp * e1
  87. end
  88.  
  89.  
  90. % Function 7 - Radiation diagram and calculation of SINR
  91. function AF = af(M, weights, exada, stepDegrees)
  92.     counter = 0;
  93.     angles = [];
  94.     AF = [];
  95.     for theta = 0: stepDegrees*pi/180: pi
  96.         counter = counter + 1;
  97.         angles(counter) = theta * 180/pi;
  98.         AF_complex = weights' * steeringVector(M, theta);
  99.         AF(counter) = abs(AF_complex);
  100.     end
  101.     % Info in the title
  102.     theta0 = exada(1)*180/pi;
  103.     theta1 = exada(2)*180/pi;
  104.     theta2 = exada(3)*180/pi;
  105.     theta3 = exada(4)*180/pi;
  106.     theta4 = exada(5)*180/pi;
  107.     theta5 = exada(6)*180/pi;
  108.     titleInfo = "θ0 = " + int2str(theta0) + ", θ1 = " +...
  109.         int2str(theta1) +  ", θ2 = " + int2str(theta2) +...
  110.         ", θ3 = " + int2str(theta3) + ", θ4 = " +...
  111.         int2str(theta4) + ", θ5 = " + int2str(theta5);
  112.     plot(angles, AF);
  113.     title(titleInfo);
  114.     xlabel((in degrees)");
  115.     ylabel("|AF(θ)|");
  116.     AF;
  117. end
  118.  
  119. % Function 8 - Calculate SINR
  120. function SINR = calculateSINR(M, weights, exada, Pg, P_noise)
  121.     % I need to find Rdd (theta0) and Ruu (theta1 to theta5)
  122.     % Rdd = Psd * ad * ad'
  123.     desiredTheta = exada(1);
  124.     ad = steeringVector(M, desiredTheta);
  125.     undesiredAngles = exada([2:6]);
  126.     Ai = steeringMatrix(M, undesiredAngles);
  127.    
  128.     Rdd = Pg * ad * ad';
  129.     Rgigi = Pg * eye(5);        % Because, interferences have same power
  130.     Ruu = Ai * Rgigi * Ai' + P_noise * eye(M);
  131.    
  132.     Pyd = weights' * Rdd * weights;
  133.     Pyu = weights' * Ruu * weights;
  134.     SINR_noDB = abs(floor(Pyd / Pyu));
  135.     SINR = 10 * log10(SINR_noDB);
  136. end
  137.  
  138. % Function 9 - Calculate maximum side lobes level
  139. function SLL_and_theta0_divergence = calculateSLL(AF, exada, stepDegrees)
  140.     % TO find SLL, first I will find all the topic max
  141.     topicMaxList = [];
  142.     counterMax = 0;
  143.     for i = 2:length(AF)-1
  144.         if AF(i) > AF(i-1)&& AF(i) > AF(i+1)
  145.             counterMax = counterMax+1;
  146.             topicMaxList(counterMax) = AF(i);
  147.         end
  148.     end
  149.     % After for-loop, I will scan topicMaxList to find the 2 max values
  150.     % 1st one is the height of main lobe and the 2nd one is for the
  151.     % most powerful side lobe
  152.     mainLobe = max(topicMaxList)
  153.     index1 = find(topicMaxList == mainLobe, 1);
  154.     % Delete element in AF
  155.     topicNew = topicMaxList([1:index1-1, index1+1:end]);
  156.     sideLobe = max(topicNew)
  157.     index2 = find(topicNew == sideLobe, 1);
  158.     SLL = 10 * log10(mainLobe / sideLobe);
  159.     % After that I have to find the theta in which I have the mainLobe
  160.     % value scanning the AF array
  161.     index = find(AF == mainLobe, 1);
  162.     thetaDegrees = (index - 1) * stepDegrees;   % This is not the desired one
  163.     desired = exada(1) * 180/pi;                % This is the desired one
  164.     theta0_divergence = abs(desired - thetaDegrees);
  165.     SLL_and_theta0_divergence = [SLL theta0_divergence];  % Return 2 values
  166.     % Return mainLobe height and position
  167. end
  168.  
  169.  
  170. % Function 10 - Calculate divergences
  171. function divergences = calculateDivergences(AF, exada, stepDegrees)
  172.     undesired = exada([2:6]) * 180/pi      % Degrees (1x5 array)
  173.     epsilon = 0.001;                % Upper bound (lower = 0)
  174.     counterMin = 0;
  175.     zeroThetaList = [];
  176.     for i = 1:length(AF)
  177.         if AF(i) <= epsilon
  178.             counterMin = counterMin + 1;
  179.             zeroThetaList(counterMin) = i * stepDegrees;
  180.             % index i indicates degree
  181.         end
  182.     end
  183.     zeroThetaList;
  184.     % Now, my list contains the angles with the lowest possible values of AF
  185.     % I have to find the distance between them and my undesired thetas
  186.     divergences = [];
  187.     for i = 1:length(undesired)
  188.         undesiredTheta = undesired(i);
  189.         minDistance = 1000;
  190.         for j = 1:length(zeroThetaList)
  191.             zeroTheta = zeroThetaList(j);
  192.             if abs(undesiredTheta - zeroTheta) <= minDistance
  193.                 minDistance = abs(undesiredTheta - zeroTheta);
  194.                 % It is about this specific undesiredTheta
  195.             end
  196.         end
  197.         divergences(i) = minDistance;
  198.     end
  199.     divergences
  200. end
  201.  
  202.  
  203. % Function 11 - Statistics into a given vector
  204. function statistics = calculateStatistics(myList)
  205.     maxElement = max(myList);
  206.     minElement = min(myList);
  207.     SUM = sum(myList);
  208.     mean = SUM / length(myList);
  209.     % STD
  210.     SUM2 = 0;
  211.     for i = 1:length(myList)
  212.         SUM2 = SUM2 + (myList(i) - mean)^2;
  213.     end
  214.     std = sqrt(SUM2 / length(myList));
  215.     statistics = [minElement maxElement mean std];
  216. end
  217.  
  218.  
  219.  
  220.  
  221. % ===============================================================
  222. % ===============================================================
  223. % Function 12 - Run with data
  224. function run(M, delta, SNR, Pg, minAngle, maxAngle, stepDegrees)
  225.     % Example for angle delta = 10 moires
  226.     % counter = 1  <----> 30 and 40,50,60,70,80
  227.     % counter = 7  <----> 40 and 50,60,70,80,90
  228.     % counter = 13 <----> 50 and 60,70,80,90,100
  229.     % counter = 19 <----> 60 and 70,80,90,100,110
  230.     counter = 0;
  231.     SNR_noDB = 10 ^ (SNR / 10);
  232.     P_noise = Pg / SNR_noDB;
  233.     % Testing 3
  234.     minAngle0 = minAngle;
  235.     maxAngle0 = findMaxAngle0(maxAngle, delta);
  236.     % For statistic purposes
  237.     theta0_divergencesList = [];      % pe 71 elements
  238.     divergencesList = [];             % pe 355 = 71 * 5 elements
  239.     SINRlist = [];                    % 71 elements
  240.     SLLlist = [];                     % 71 elements
  241.     % Testing 4, 5, 6, 7, 8, 9
  242.     % Variable "i" will describe EACH UNIQUE SET OF 6 FIRST ANGLES!!!!
  243.     for angle0 = minAngle0 : pi/180 : maxAngle0   % We work with rads, so 1 degree = pi/180 rad
  244.         display(angle0 * 180/pi);  
  245.         arxikiExada = createArxikixada(angle0, delta);    % Connected with "i"
  246.         exades = createExades(arxikiExada);
  247.         for row = 1:6
  248.             counter = counter + 1
  249.             exada = exades(row, :);
  250.             display(exada * 180/pi);
  251.            
  252.             weights = NSBweights(M, P_noise, exada);
  253.             AF = af(M, weights, exada, stepDegrees);
  254.             SINR = calculateSINR(M, weights, exada, Pg, P_noise);
  255.             SLL_and_theta0_divergence = calculateSLL(AF, exada, stepDegrees);
  256.             SLL = SLL_and_theta0_divergence(1)
  257.             theta0_divergence = SLL_and_theta0_divergence(2)
  258.             divergences = calculateDivergences(AF, exada, stepDegrees);
  259.    
  260.             % For statistic purposes
  261.             theta0_divergencesList(counter) = theta0_divergence;
  262.             D = length(divergences);
  263.             LENGTH = length(divergencesList);
  264.             for j = 1:D
  265.                 divergencesList(LENGTH + j) = divergences(j);
  266.             end
  267.             SINRlist(counter) = SINR;
  268.             SLLlist(counter) = SLL;
  269.            
  270.             display('******************************************************');
  271.             display(' ');
  272.         end
  273.     end
  274.     theta0_divergencesList;
  275.     divergencesList;
  276.     SINRlist;
  277.     SLLlist;
  278.     display(' ');
  279.     display(' ');
  280.     display('******************************************************');
  281.     display(strcat('Delta=', int2str(delta*180/pi), ' and SNR=', int2str(SNR)));
  282.     display(' ');
  283.     theta0_divergences_stats = calculateStatistics(theta0_divergencesList)
  284.     divergences_stats = calculateStatistics(divergencesList)
  285.     SINR_stats = calculateStatistics(SINRlist)
  286.     SLL_stats = calculateStatistics(SLLlist)
  287. end
  288. % ===============================================================
  289. % ===============================================================
  290.  
  291.  
  292. % Gia ta prin, esvisa
  293. % delta (3), maxExades (3), arxikiExada (4), exades (5), AF(7)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement