close all; clc
%% =====================================================================================================
% Parameters to be modified

% Read image with filter
I_signal = imread('Picture 9.jpg');

% Read reference image 
I_reference = imread('Picture 8.jpg');

% Define a vertical region of interest (ROI) over which the data will be averaged
y_start = 275; 
y_end   = 295;

% Filter the transmission to get rid of uninteresting parts of the
% spectrum. Hides the transmission values where the reference data 
% is less than the "filter" value
filter = 10;

% Show ROI (1 = show ROI, 0 = hide ROI)
show_ROI = 1;





% XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
%
%                             DO NOT MODIFY THE SCRIPT BEYOND THIS POINT
%
% XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX




%% =====================================================================================================
% Process data

%parameters
PixNrx          =       1280;
PixNry          =       720;

% convert to B&W
I_signal_BW     = mean(I_signal,3);
I_reference_BW  = mean(I_reference,3);

% caculate the mean of the ROI over several rows
ROI_Line_white  = I_reference_BW(y_start:y_end,:);
N_Avg_Line_white = mean(ROI_Line_white);

ROI_Line_signal  = I_signal_BW(y_start:y_end,:);
N_Avg_Line_signal = mean(ROI_Line_signal);

% calculate the transmission
Transmission = N_Avg_Line_signal./N_Avg_Line_white;

% filter transmission
Transmission(N_Avg_Line_white < filter) = -10;

%% =====================================================================================================
% Plot data

figure('color','w','Position', [250 100 850 630])

% show an image to adjust line positions
subplot(2,2,1)
imagesc(I_reference)
hold on
if show_ROI
    rectangle('Position',[1 y_start PixNrx-1 y_end-y_start],'EdgeColor','w','LineWidth',1.2)
end
title('Reference image')
xlabel('Position x (pixel)')
ylabel('Position y (pixel)')

subplot(2,2,2)
imagesc(I_signal)
hold on
if show_ROI
    rectangle('Position',[1 y_start PixNrx-1 y_end-y_start],'EdgeColor','w','LineWidth',1.2)
end
title('Signal with filter')
xlabel('Position x (pixel)')
ylabel('Position y (pixel)')

% plot the averaged lines
subplot(2,2,3)
hold on
plot(N_Avg_Line_white,'k','LineWidth',2)
plot(N_Avg_Line_signal,'b','LineWidth',2)
plot([1 PixNrx],[filter filter],'r','LineWidth',2)
title('Averaged data and filter line (red)')
legend('Ref.','Signal')
xlabel('Position x (pixel)')
ylabel('Signal (counts)')
xlim([1 PixNrx])

% plot the transmission
subplot(2,2,4)
plot(Transmission,'k','LineWidth',2)
title('Averaged filtered transmission')
xlabel('Position x (pixel)')
ylabel('Signal (counts)')
ylim([0 1])
xlim([1 PixNrx])
