Advertisement
makispaiktis

Eidikes Keraies - Ergasia2 - Part3

Jul 9th, 2021 (edited)
695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 5.43 KB | None | 0 0
  1. clear all
  2. clc
  3.  
  4.  
  5. % Main Function
  6. % Standard data
  7. M = 24;
  8. N = 13;
  9. Pg = 1;
  10. SNR = 10;
  11. stepDegrees = 0.1;
  12. SNR_noDB = 10^(SNR / 10);
  13. P_noise = Pg / SNR_noDB;
  14. centerDegrees = 90;
  15. counter = 0;
  16. deltaDegreesList = [];
  17. for i = 5:-0.01:4
  18.     counter = counter + 1;
  19.     deltaDegreesList(counter) = i;
  20. end
  21.  
  22. % Scanning my list for all the values of delta (between 4 and 5 degrees)
  23. for i = 1:length(deltaDegreesList)
  24.     display('*********************************************');
  25.     deltaDegrees = deltaDegreesList(i)
  26.     % if deltaDegrees == 4.21
  27.     delta = deltaDegrees * pi/180;
  28.     thetaList = createThetaList(centerDegrees, deltaDegrees);
  29.     THETA_LIST = thetaList * 180/pi
  30.     A = steeringMatrix(M, thetaList);
  31.     spectrum = draw_LP_Spectrum(M, N, A, THETA_LIST, Pg, P_noise, centerDegrees, deltaDegrees, stepDegrees);
  32.     topicMax = findTopicMax(spectrum, stepDegrees);
  33.     LENGTH = topicMax(1)
  34.     topicMaxList = topicMax([2:LENGTH+1]);
  35.     anglesList = topicMax([LENGTH+2:end])
  36.    
  37.     % Now, we have the angles of topic max, but sometimes their number is not
  38.     % 13, so it's wrong
  39.     if LENGTH ~= 13
  40.         display(strcat('LP method is not efficient for delta=', num2str(deltaDegrees),' degrees'));
  41.     end
  42.     display(' ');
  43.     % end
  44. end
  45.  
  46.  
  47. % ===============================================================
  48. % ===============================================================
  49. % ======================== Functions ============================
  50. % ===============================================================
  51. % ===============================================================
  52.  
  53. % Function 1 - Steering vector
  54. function a = steeringVector(M, theta)
  55.     a = [];    
  56.     for m = 1 : M
  57.         a(m) = exp(i * pi * (m-1) * cos(theta));
  58.     end
  59.     a = a';
  60.     a;
  61. end
  62.  
  63. % Function 2 - Steering Matrix
  64. function A = steeringMatrix(M, thetaList)
  65.     A = [];
  66.     for index = 1 : length(thetaList)
  67.         A(:, index) = steeringVector(M, thetaList(index));
  68.     end
  69.     A;
  70. end
  71.  
  72. % Function 3 - DoA: LP = Linear Prediction
  73. function spectrum = draw_LP_Spectrum(M, N, A, THETA_LIST, Pg, P_noise, centerDegrees, deltaDegrees, stepDegrees)
  74.     Rgg = Pg * eye(N);
  75.     Rnn = P_noise * eye(M);
  76.     Rxx = A * Rgg * A' + Rnn;
  77.     % Draw χωρικό φάσμα ισχύος του εκτιμητή
  78.     % We suppose 1st element as the reference
  79.     e1 = zeros(1, M);
  80.     e1(1) = 1;
  81.     e1 = e1';            % Now, its a column-vector with size = 24x1
  82.     nominator = e1' * inv(Rxx) * e1;
  83.     % For denominator, I need to make a vector "ad" which refers to a random
  84.     % angle signal
  85.     counter = 0;
  86.     angles = [];            % x axis
  87.     spectrum = [];          % y axis
  88.     for angle = 0:stepDegrees:180
  89.         counter = counter + 1;
  90.         angles(counter) = angle;        % In degrees
  91.         angleRad = angle * pi/180;      % In rad
  92.         ad = steeringMatrix(M, angleRad);
  93.         % For calculations, I will need rad angles
  94.         denominator = (norm(e1' * inv(Rxx) * ad))^2;
  95.         spectrum(counter) = nominator / denominator;
  96.     end
  97.     plot(angles, spectrum);
  98.     myTitle = "";
  99.     for i = 1:length(THETA_LIST)
  100.         t = "θ" + int2str(i) + "=" + int2str(THETA_LIST(i));
  101.         if i < length(THETA_LIST)
  102.             t = t + ", ";
  103.         end
  104.         myTitle = myTitle + t;
  105.     end
  106.     title(myTitle);
  107.     xlabel("θ in degrees, delta = " + num2str(deltaDegrees));
  108.     ylabel("10log(P / Pmax)");
  109.     spectrum;
  110. end
  111.  
  112. % Function 4 - createThetaList(center, delta) - output in rad
  113. function thetaList = createThetaList(centerDegrees, deltaDegrees)
  114.     thetaList = [];
  115.     counter = 0;
  116.     for i = -6:6
  117.         counter = counter + 1;
  118.         angle = centerDegrees + i * deltaDegrees;     % Degrees
  119.         theta = angle * pi/180;         % Rad
  120.         thetaList(counter) = theta;
  121.     end
  122.     thetaList;
  123. end
  124.  
  125.  
  126. % Function 5 - Deleting zeros from a list
  127. function newList = deleteZerosFrom(myList)
  128.     counter = 0;    
  129.     newList = [];
  130.     for i = 1:length(myList)
  131.         if myList(i) ~= 0
  132.             counter = counter + 1;
  133.             newList(counter) = myList(i);
  134.         end
  135.     end
  136.     newList;
  137. end
  138.  
  139. % Function 6 - Find the topic max values in my spectrum
  140. function topicMax = findTopicMax(spectrum, stepDegrees)
  141.     lowerBound = 20;
  142.     counter = 0;
  143.     topicMaxList = [];
  144.     anglesList = [];
  145.     % If my sample is below this value, I continue with the next sample
  146.     for i = 2:length(spectrum)-1
  147.         value = spectrum(i);
  148.         if value >= lowerBound
  149.             counter = counter + 1;
  150.             if value > spectrum(i-1) && value > spectrum(i+1)
  151.                 topicMaxList(counter) = value;
  152.                 index = i;
  153.                 angle = (index - 1) * 0.1;
  154.                 anglesList(counter) = angle;
  155.             end
  156.         end
  157.     end
  158.     % Now, I have a list with all my topic max and the positions
  159.     % I will return a big vector with 3 returned values in row
  160.     % 1st_element = length = number of topic max values
  161.     % 2nd_element = a list with all the topic max values (13 elememnts for example)
  162.     % 3rd_element = a list with all the angles of topic max (13 elememnts for example)
  163.    
  164.     % Sometimes my 2 lists have some zeros (depends on lower bound)
  165.     % I have to delete them
  166.     topicMaxList = deleteZerosFrom(topicMaxList);
  167.     anglesList = deleteZerosFrom(anglesList);
  168.     LENGTH = length(topicMaxList);
  169.     topicMax = [LENGTH topicMaxList anglesList];
  170. end
  171.  
  172.  
  173.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement