Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 7.07 KB | None | 0 0
  1. close all;
  2. clear;
  3. clc;
  4.  
  5. %% Experiment Data
  6. filenames = "combined_test_case_3.xlsx";
  7. raw_data = xlsread(filenames);
  8. raw_data(:,1:5)=[];
  9.  
  10.  
  11. %% Raw data
  12. raw_temperatures(:,1) = ((raw_data(:,1)-1.25)./0.005)-23;
  13. raw_temperatures(:,2) = ((raw_data(:,2)-1.25)./0.005)-23;
  14. raw_temperatures(:,3) = ((raw_data(:,3)-1.25)./0.005)-23;
  15. raw_temperatures(:,4) = ((raw_data(:,4)-1.25)./0.005)-23;
  16. raw_temp_sum=(raw_temperatures(:,1)+raw_temperatures(:,2)+raw_temperatures(:,3)+raw_temperatures(:,4));
  17. raw_source_location(:,1)=.5+(((raw_temperatures(:,1)+raw_temperatures(:,3))./raw_temp_sum(:)).*9);
  18. raw_source_location(:,2)=.5+(9-((raw_temperatures(:,1)+raw_temperatures(:,2))./raw_temp_sum(:)).*9);
  19.  
  20. %% Kalman filtering data
  21. R=[0.00003,0.00003,0.00003,0.00003]; %Based off test case 3
  22. Q=0.001*ones(1,4); %Assume low noise
  23. F=[1,1,1,1]; %Assume little change
  24. Pc=[0,0,0,0]; %
  25. G=[0,0,0,0]; %
  26. P=[0,0,0,0]; %
  27. Xp=[0,0,0,0]; %
  28. Zp=[0,0,0,0]; %
  29. Xe=[0,0,0,0]; %
  30. filt_temperatures=zeros(length(raw_data(:,1)),4);
  31. for i = 1:length(raw_data(:,1))
  32.     Pc=P+Q;
  33.     G= Pc./(Pc+R); %Kalman gain
  34.     P=(1-G).*Pc;
  35.     Xp=Xe;
  36.     Zp=Xp;
  37.     Xe=G.*(raw_temperatures(i,:)-Zp)+Xp; %Kalman estimate
  38.     filt_temperatures(i,:)=Xe;
  39. end
  40. %filt_temperatures(:,1) = ((filt_data(:,1)-1.25)./0.005);
  41. %filt_temperatures(:,2) = ((filt_data(:,2)-1.25)./0.005);
  42. %filt_temperatures(:,3) = ((filt_data(:,3)-1.25)./0.005);
  43. %filt_temperatures(:,4) = ((filt_data(:,4)-1.25)./0.005);
  44.  
  45. %Zero temperatures to get differential
  46. filt_temp_sum=(filt_temperatures(:,1)+filt_temperatures(:,2)+filt_temperatures(:,3)+filt_temperatures(:,4));
  47. filt_source_location(:,1)=.5+((filt_temperatures(:,1)+filt_temperatures(:,3))./filt_temp_sum(:)).*9;
  48. filt_source_location(:,2)=.5+(9-((filt_temperatures(:,1)+filt_temperatures(:,2))./filt_temp_sum(:)).*9);
  49.  
  50. %%
  51. figure()
  52. hold on
  53. plot(raw_temperatures(:,1));
  54. plot(filt_temperatures(:,1));
  55. title('Plot of raw and filtered T1 thermocouple for test case 1')
  56. xlabel('Time Step (0.001s)')
  57. ylabel('Temperature Differential(C)')
  58. legend('Raw measurement','Filtered measurement')
  59. %% Bayesian Fusion
  60.  
  61. % User inputs
  62. size_x = 0.1; % length of plate [m]
  63. size_y = 0.1; % width of plate [m]
  64. grid_x = 10; %number of cells in x-directions
  65. grid_y = grid_x; %number of cells in y-directions
  66. num_ir = 4;
  67. target_rad = 0.015;
  68. sensor_pos = [1  3; 1 8 ; 2 3 ; 2 8]; % ([x(1),y(2)], position) line of sight
  69.  
  70. % Occupancy Grid
  71. map = robotics.OccupancyGrid(0.1,0.1,grid_x*10);
  72. [x,y] = meshgrid(1:grid_x);
  73. setOccupancy(map,[x(:) y(:)],0.5, "grid");
  74.  
  75. % Initialize grid probabilities
  76. p_occ = 0.5*ones(grid_x,grid_y); % overall grid
  77. l_odds = log10(p_occ./(1-p_occ));
  78.  
  79. lmin = -2.0;
  80. lmax = 3.5;
  81. measured = 0;
  82.  
  83. dx = size_x/grid_x;
  84. dy = size_y/grid_y;
  85.  
  86. delta_t = 0.001;
  87. tau = 0.1;
  88. %% Simulation data
  89. Temp_Var=0.75; %Temperature variance
  90.  
  91. Sim_Temp_Data_1=[(randn(60000,1)*Temp_Var)+linspace(0,6,60000)'...
  92.     (randn(60000,1)*Temp_Var)+linspace(0,2,60000)' (randn(60000,1)*Temp_Var)+linspace(0,2,60000)' (randn(60000,1)*Temp_Var)+linspace(0,2,60000)'];
  93.  
  94. Sim_Temp_Data_2=[(randn(60000,1)*Temp_Var)+linspace(0,2,60000)'...
  95.     (randn(60000,1)*Temp_Var)+linspace(0,2,60000)' (randn(60000,1)*Temp_Var)+linspace(0,2,60000)' (randn(60000,1)*Temp_Var)+linspace(0,6,60000)'];
  96.  
  97. Sim_Temp_Data_3=[(randn(60000,1)*Temp_Var)+linspace(0,2,60000)'...
  98.     (randn(60000,1)*Temp_Var)+linspace(0,6,60000)' (randn(60000,1)*Temp_Var)+linspace(0,2,60000)' (randn(60000,1)*Temp_Var)+linspace(0,2,60000)'];
  99.  
  100. % Sim_Temp_Data_4=[(randn(60000,1)*Temp_Var)+linspace(0,2,60000)'...
  101. %     (randn(60000,1)*Temp_Var)+linspace(0,2,60000)' (randn(60000,1)*Temp_Var)+linspace(0,6,60000)' (randn(60000,1)*Temp_Var)+linspace(0,2,60000)']
  102. %
  103. % Sim_Temp_Data_5=[(randn(60000,1)*Temp_Var)+linspace(0,2,60000)'...
  104. %     (randn(60000,1)*Temp_Var)+linspace(0,6,60000)' (randn(60000,1)*Temp_Var)+linspace(0,6,60000)' (randn(60000,1)*Temp_Var)+linspace(0,2,60000)']
  105. %
  106. % Sim_Temp_Data_6=[(randn(60000,1)*Temp_Var)+linspace(0,6,60000)'...
  107. %     (randn(60000,1)*Temp_Var)+linspace(0,6,60000)' (randn(60000,1)*Temp_Var)+linspace(0,2,60000)' (randn(60000,1)*Temp_Var)+linspace(0,2,60000)']
  108.  
  109. %Get source location from sim data
  110. % Sum_Sim_Temp_Data=(Sim_Temp_Data_3(:,1)+Sim_Temp_Data_3(:,2)+Sim_Temp_Data_3(:,3)+Sim_Temp_Data_3(:,4));
  111. % filt_source_location(:,1)=.5+9-(((Sim_Temp_Data_3(:,1)+Sim_Temp_Data_3(:,2))./Sum_Sim_Temp_Data(:)).*9);
  112. % filt_source_location(:,2)=.5+((Sim_Temp_Data_3(:,1)+Sim_Temp_Data_3(:,3))./Sum_Sim_Temp_Data(:)).*9;
  113.  
  114.  
  115. %%
  116. %for t=1:size(filt_source_location,1)
  117. for t=1:1000
  118.     t;
  119.     % Bivariate Gaussian distribution
  120.     % mu = current estimate position of heat source
  121.     mu = filt_source_location(i,:)/100;
  122.     sigma = [0.0025,0.0025];
  123.     for j=1:grid_x
  124.         for k=1:grid_y
  125.             %apply forgetting factor
  126.             p_occ(j,k) = (p_occ(j,k)-0.5)*exp(-delta_t/tau)+0.5;
  127.             l_odds(j,k) = log10(p_occ(j,k)/(1-p_occ(j,k)));
  128.            
  129.             xy = grid2world(map,[j k]);
  130.             prob_inv = 0.8*(mvnpdf(xy,mu,sigma)/mvnpdf(mu,mu,sigma))+0.1;
  131.             l = l_odds(j,k) + log10(prob_inv/(1-prob_inv));
  132.  
  133.             % log odds update (temporal)
  134.             if l > lmax
  135.                 l = lmax;
  136.             elseif l < lmin
  137.                 l = lmin;
  138.             end
  139.             l_odds(j,k) = l;
  140.            
  141.             prob = 1-(1/(1+exp(l_odds(j,k))));
  142.             p_occ(j,k) = prob;
  143.  
  144.         end
  145.     end
  146.  
  147. end
  148.  
  149. [y,x] = meshgrid(1:grid_x);
  150. setOccupancy(map,[x(:) y(:)],p_occ(:), "grid");
  151. show(map);
  152. hold on
  153. % Get xy values of grid with probability of 0.8 or higher
  154. x = [];
  155. y = [];
  156. for i=1:size(p_occ,1)
  157.     for j=1:size(p_occ,2)
  158.         if p_occ(i,j) >= 0.7
  159.             xy = grid2world(map,[i j]);
  160.             x = vertcat(x,xy(1));
  161.             y = vertcat(y,xy(2));
  162.         end
  163.     end
  164. end
  165. x_loc = mean(x);
  166. y_loc = mean(y);
  167. plot(x_loc,y_loc,'xr','MarkerSize',10)
  168. grid on;
  169. colorbar;
  170. %%
  171. figure()
  172. hold on
  173. c = linspace(1,60000,length(filt_temp_sum));
  174. scatter(filt_source_location(:,1),filt_source_location(:,2),[5],c)
  175. xlim([0 10])
  176. ylim ([0 10])
  177. hcb=colorbar;
  178. title('Plot of heat source centerpoint')
  179. xlabel('X [meters]')
  180. ylabel('Y [meters]')
  181. %%
  182. figure()
  183. hold on
  184. c = linspace(1,60000,length(filt_temp_sum));
  185. scatter(raw_source_location(:,1),raw_source_location(:,2),[5],c)
  186. xlim([0 10])
  187. ylim ([0 10])
  188. hcb=colorbar;
  189. title('Plot of heat source centerpoint')
  190. xlabel('X [meters]')
  191. ylabel('Y [meters]')
  192. %%
  193. x_moving_Variance=movvar(raw_source_location(:,1),600);
  194. y_moving_Variance=movvar(raw_source_location(:,2),600);
  195.  
  196. figure()
  197. hold on
  198. plot(x_moving_Variance)
  199. plot(y_moving_Variance)
  200. plot(1:1:60000,0.75*ones(60000,1))
  201. ylim([0 4])
  202. title('Change in variance over time with 600 point window')
  203. xlabel('Time step (0.001s)')
  204. ylabel('Variance [cm]')
  205. %%
  206. x_moving_Variance=movvar(filt_source_location(:,1),600);
  207. y_moving_Variance=movvar(filt_source_location(:,2),600);
  208.  
  209. figure()
  210. hold on
  211. plot(x_moving_Variance)
  212. plot(y_moving_Variance)
  213. plot(1:1:60000,0.75*ones(60000,1))
  214. ylim([0 4])
  215. title('Change in variance over time with 600 point window')
  216. xlabel('Time step (0.001s)')
  217. ylabel('Variance [cm]')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement