/mc_rec_r4.m

http://github.com/gallamine/Photonator · Objective C · 172 lines · 138 code · 34 blank · 0 comment · 30 complexity · d76a5981c58da01c749e3d5c5ad06f3f MD5 · raw file

  1. % VERSION LOG
  2. % Rev 1 - first creation, record power and count for each receiver
  3. % Rev 2 - add statistics, mean and variance for power, angle(s)
  4. % Rev 3 - changed receiver plane to x/y instead of y/z - photons move along
  5. % the z-axis now
  6. % Rev 3b/4 - 5/3/11 - added window affects, changed power calculations <- SIMPLE BASIC, VERIFIED VERSION
  7. function [power,ph_cnt,angle_mean,angle_var,dist_mean,dist_var,weight_mean,weight_var,reflected,distances,angles,weights] = mc_rec_r4(a,rec_loc_final,total_rec_dist,rec_weights,rec_pos,rec_aperture,rec_fov,numTxPhotons)
  8. sizeRecPos = size(rec_pos);
  9. num_rx = sizeRecPos(1); % total number of receivers
  10. num_photons = length(rec_loc_final); % total number of received photons
  11. ph_cnt = zeros(1,num_rx);
  12. power = zeros(1,num_rx);
  13. angle_mean = zeros(1,num_rx); % mean of the incident angle for each Rx
  14. angle_var = zeros(1,num_rx); % variance of incident angle for each Rx
  15. dist_mean = zeros(1,num_rx); % Mean distance traveled for each Rx
  16. dist_var = zeros(1,num_rx); % Variance of dist traveled for each Rx
  17. weight_mean = zeros(1,num_rx); % Mean weight for each Rx
  18. weight_var = zeros(1,num_rx); % Variance of weight for each Rx
  19. distances = zeros(1,num_photons); % Distances from photon to 0,0 point - Initailize to full size, crop at end
  20. angles = zeros(1,num_photons); % Angle of received photon
  21. weights = zeros(1,num_photons); % Weight of received photon
  22. dWindow = 0.00635; % 0.25 inches
  23. dAir = 0.127; % 5 inches
  24. nWater = 1.33;
  25. nWindow = 1.585; % polycarbonate
  26. nAir = 1;
  27. reflected = 0; % number of photons past critical angle of window
  28. nWaterWindow = nWater/nWindow;
  29. nWindowAir = nWindow/nAir;
  30. critAngSine = nAir/nWater; % sine T1 = nair/nwater sine T3
  31. if (num_rx > 1)
  32. disp('You need to change how the distance array is stored!');
  33. end
  34. for i = 1:num_photons % iterate over every photon on receiver plane
  35. ph_x = rec_loc_final(i,1);
  36. ph_y = rec_loc_final(i,2);
  37. mu_x = rec_loc_final(i,3);
  38. mu_y = rec_loc_final(i,4);
  39. mu_z = rec_loc_final(i,5);
  40. % Find new position from window
  41. sinT = sqrt(1 - mu_z^2);
  42. % if sinT >= critAngSine % if the photon's incident angle > critical angle, then skip this photon
  43. % reflected = reflected + 1;
  44. % continue;
  45. % end
  46. %
  47. % sinT1 = nWaterWindow*sinT; % 1.33/1.585 * sqrt(1 - uz^2)
  48. % mu_z1 = sqrt(1-sinT1^2); % this can be simplified ...
  49. % mu_x1 = nWaterWindow * mu_x;
  50. % mu_y1 = nWaterWindow * mu_y;
  51. % delY1 = dWindow * mu_y1/mu_z1;
  52. % delX1 = dWindow * mu_x1/mu_z1;
  53. % % find new position from air
  54. % sinT2 = nWater*sinT; % this can be simplified ...
  55. % mu_z2 = sqrt(1 - sinT2^2);
  56. % mu_x2 = nWater * mu_x;
  57. % mu_y2 = nWater * mu_y;
  58. % delY2 = dAir * mu_y2/mu_z2;
  59. % delX2 = dAir * mu_x2/mu_z2;
  60. %
  61. % ph_x = ph_x + delX1 + delX2;
  62. % ph_y = ph_y + delY1 + delY2;
  63. for j = 1:num_rx % iterate over every receiver
  64. rx_x = rec_pos(j,1);
  65. rx_y = rec_pos(j,2);
  66. radius = rec_aperture(j)/2; % 1/2 diameter of receiver
  67. angle_sqravg = 0;
  68. dist_sqravg = 0;
  69. cos_rec_fov = cos(rec_fov(j)/2); % cos(fov/2) to compare with photon's incident angle
  70. distance = sqrt((ph_x-rx_x)^2 + (ph_y-rx_y)^2); % Euclidian distance to receiver center
  71. if ((distance <= radius) && (mu_z >= cos_rec_fov)) % Photon received
  72. power(j) = power(j) + rec_weights(i); % Adjust power of received photons
  73. ph_cnt(j) = ph_cnt(j) + 1; % Increment number of received photons
  74. ph_dist = total_rec_dist(i);
  75. angle_delta = rec_loc_final(i,5) - angle_mean(j);
  76. dist_delta = ph_dist - dist_mean(j);
  77. weight_delta = rec_weights(i) - weight_mean(j);
  78. % if j == 1
  79. % weights(ph_cnt(j)) = rec_weights(i);
  80. % angles(ph_cnt(j)) = rec_loc_final(i,5);
  81. % distances(ph_cnt(j)) = ph_dist;
  82. % end
  83. % Record stats on incident angle
  84. angle_mean(j) = angle_mean(j) + (angle_delta) / ph_cnt(j); % E'[uz] = E[uz] + (uz - E[uz]) / N+1 - See Knuth's Art of Comp. Programming & http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
  85. % Record stats on distance traveled
  86. dist_mean(j) = dist_mean(j) + (dist_delta) / ph_cnt(j);
  87. % Record stats on photon weight
  88. weight_mean(j) = weight_mean(j) + (weight_delta)/ph_cnt(j); % E'[D] = E[D] + (D - E[D]) / N+1
  89. angle_var(j) = angle_var(j) + angle_delta*(rec_loc_final(i,5) - angle_mean(j)); % Var[uz] = 1/N-1 *(Var[uz] + (uz - E[uz]_N-1)(uz - E[uz]_N))
  90. dist_var(j) = dist_var(j) + dist_delta*(ph_dist - dist_mean(j));
  91. weight_var(j) = weight_var(j) + weight_delta*(rec_weights(i) - weight_mean(j));
  92. if (num_rx == 1)
  93. distances(ph_cnt(j)) = distance;
  94. angles(ph_cnt(j)) = mu_z;
  95. weights(ph_cnt(j)) = rec_weights(i);
  96. end
  97. end
  98. end
  99. end
  100. for j = 1:num_rx
  101. if (num_rx == 1)
  102. distances = distances(1:ph_cnt(j));
  103. angles = angles(1:ph_cnt(j));
  104. weights = weights(1:ph_cnt(j));
  105. end
  106. weight_var(j) = (weight_var(j) + weight_mean(j).^2*(ph_cnt(j).*(numTxPhotons - ph_cnt(j))/numTxPhotons)) / (numTxPhotons - 1);
  107. weight_mean(j) = ph_cnt(j)*weight_mean(j) / numTxPhotons;
  108. if weight_mean(j) ~= power(j)/numTxPhotons
  109. disp('Problem counting up the weight mean');
  110. weight_mean(j) - power(j)/numTxPhotons
  111. weight_mean(j)
  112. power(j)
  113. end
  114. % if ph_cnt(j) > 1
  115. % angle_var(j) = (1./(ph_cnt(j)-1)).*angle_var(j);
  116. % dist_var(j) = (1./(ph_cnt(j)-1)).*dist_var(j);
  117. % end
  118. %
  119. % if isnan(angle_var(j))
  120. % disp('angle_var(j) is NaN (in correction loop)');
  121. % end
  122. % if isnan(dist_var(j))
  123. % disp('dist_var(j) is NaN (in correction loop)');
  124. % end
  125. % if isnan(weight_var(j))
  126. % disp('weight_var(j) is NaN (in correction loop)');
  127. % end
  128. % if isinf(angle_var(j))
  129. % disp('angle_var(j) is Inf');
  130. % end
  131. % if isinf(dist_var(j))
  132. % disp('dist_var(j) is Inf');
  133. % end
  134. % if isinf(weight_var(j))
  135. % disp('weight_var(j) is Inf');
  136. % end
  137. end
  138. %dist_mean.*ph_cnt - power