clear all; clc; close all;
T_para = zeros(5, 17, 20);
T_per = zeros(5, 17, 20);
T_para_noisy = zeros(5, 17, 20);
T_per_noisy = zeros(5, 17, 20);
add_noise = @(signal, snr) awgn(signal, snr, 'measured');
cost_function = @(original, noisy, variance) sum(((original(:) - noisy(:))./ (var(original(:)))).^2);
lookup_table_interp = [];
a_range = 0.01:0.01:0.05;
cost_matrix = zeros(length(a_range), length(r_range));
for fov = [0.2, 0.5, 1, 2, 5, 10]
for a_idx = 1:length(a_range)
for r_idx = 1:length(r_range)
load(['./Tabledata_', cloud, '/', num2str(h), 'm-', num2str(fov), 'mrad/TABLE.mat']);
T_para_noisy = add_noise(T_para, 30);
T_per_noisy = add_noise(T_per, 30);
var_T_para = var(T_para, 0, 'all');
var_T_per = var(T_per, 0, 'all');
cost_para = cost_function(T_para, T_para_noisy, var_T_para);
cost_per = cost_function(T_per, T_per_noisy, var_T_per);
total_cost = cost_para + cost_per;
cost_matrix(a_idx, r_idx) = total_cost;
if total_cost < global_min_cost
global_min_cost= total_cost;
lookup_table = [lookup_table; optimal_a, optimal_r, global_min_cost];
save('LookupTable.mat', 'lookup_table');
global_min_cost_interp = Inf;
a_interp = 0.01:0.001:0.05;
[a_grid, r_grid] = meshgrid(a_range, r_range);
[a_interp_grid, r_interp_grid] = meshgrid(a_interp, r_interp);
cost_matrix = cost_matrix';
cost_matrix_interp = interp2(a_grid, r_grid, cost_matrix, a_interp_grid, r_interp_grid, 'spline');
for a_interp_idx = 1:length(a_interp)
for r_interp_idx = 1:length(r_interp)
interpolated_cost = cost_matrix_interp(r_interp_idx, a_interp_idx);
if interpolated_cost < global_min_cost_interp
global_min_cost_interp = interpolated_cost;
optimal_a_interp = a_interp(a_interp_idx);
optimal_r_interp = r_interp(r_interp_idx);
lookup_table_interp = [lookup_table_interp; optimal_a_interp, optimal_r_interp, global_min_cost_interp];
save('LookupTable_Interp.mat', 'lookup_table_interp');
disp('Global minimum interpolated cost:');
disp(global_min_cost_interp);
min_cost = min(cost_matrix_interp(:));
max_cost = max(cost_matrix_interp(:));
normalized_cost_matrix_interp = (cost_matrix_interp - min_cost) / (max_cost - min_cost);
surf(a_interp_grid, r_interp_grid, cost_matrix_interp);
set(gca, 'color', 'w', 'FontSize', 12, 'LineWidth', 1, 'FontWeight', 'normal');
ylabel('R_{e} interp (\mum)');
xlabel('\alpha_{e} interp (m^{-1})');
zlabel('Normalized interpolated cost function');
title('Normalized interpolated CF over \alpha_{e} interp and R_{e} interp');
set(gca, 'box', 'on', 'LineWidth', 1, 'FontName', 'Arial', 'FontSmoothing', 'on');
set(gca, 'xlim', [0.01 0.05], 'xtick', [0.01 0.02 0.03 0.04 0.05], 'ylim', [4 20], 'ytick', [4 8 12 16 20]);
[A, R] = meshgrid(a_interp, r_interp);
A_vector = reshape(A, [], 1);
R_vector = reshape(R, [], 1);
Re_cost_matrix_interp = reshape(cost_matrix_interp, [], 1);
[global_min_cost_interp, min_idx] = min(Re_cost_matrix_interp);
optimal_a_interp = A_vector(min_idx);
optimal_r_interp = R_vector(min_idx);
surf_handle=plot3(optimal_a_interp, optimal_r_interp, global_min_cost_interp, 'ro', 'MarkerSize', 12, 'MarkerFaceColor', 'r');
uistack(surf_handle, 'top');
contourf(a_interp_grid, r_interp_grid, normalized_cost_matrix_interp, 20);
xlabel('\alpha_{e} interp (m^{-1})');
ylabel('R_{e} interp (\mum)');
title('2D Contour Plot of interpolated normalized CF');
set(gca,'FontSize', 12, 'LineWidth', 1, 'FontWeight', 'normal');
set(gca, 'box', 'on', 'LineWidth', 1, 'FontName', 'Arial', 'FontSmoothing', 'on');
set(gca, 'xlim', [0.01 0.05], 'xtick', [0.01 0.02 0.03 0.04 0.05], 'ylim', [4 20], 'ytick', [4 8 12 16 20]);
plot(optimal_a_interp, optimal_r_interp, 'ro', 'MarkerSize', 12, 'MarkerFaceColor', 'r');
scatter3(R_vector, A_vector, Re_cost_matrix_interp, 'filled');
xlabel('R_{e} interp (\mum)');
ylabel('\alpha_{e} interp (m^{-1})');
zlabel('Interpolated cost function');
title('Normalized interpolated CF over \alpha_{e} interp and R_{e} interp');
set(gca, 'color', 'w', 'FontSize', 12, 'LineWidth', 1, 'FontWeight', 'normal');
set(gca, 'box', 'on', 'FontName', 'Arial', 'FontSmoothing', 'on');
set(gca, 'ylim', [0.01 0.05], 'ytick', [0.01 0.02 0.03 0.04 0.05], 'xlim', [4 20], 'xtick', [4 8 12 16 20]);
surf_handle = plot3(optimal_r_interp, optimal_a_interp, global_min_cost_interp, 'ro', 'MarkerSize', 12, 'MarkerFaceColor', 'r');
uistack(surf_handle, 'top');