% Demonstration of Electrical Impedance Tomography using a 2D diffusion
% model in a rectangular grid. We study how the location of an inclusion
% shows up in the data. It is hard to see deeper into the domain!
%
% This routine creates images from the results of
% FD_EIT_2D_demoC_deeper_look_comp.m
%
% Samuli Siltanen March 2025

% Load precomputed results from file
load data/FD_EIT_2D_demoC N I currmat voltmat0 condmatV0 condmatH0 cond_info

% Determine maximal absolute voltage and maximal voltage difference
VoltMAX = max(abs(voltmat0(:)));
VoltdiffMAX = 0;
for iii = 1:((N-1)/2)
    tmp = cond_info(iii,4);
    voltmat = tmp{1};

    % Update maximal absolute voltage
    VoltMAX = max([VoltMAX,max(abs(voltmat(:)))]);

    % Update maximal absolute voltage difference
    voltdiff = voltmat-voltmat0;
    VoltdiffMAX = max(VoltdiffMAX,max(abs(voltdiff(:))));
end

% Plot voltage matrix of the constant conductivity
figure(100)
clf
imagesc(imresize(voltmat0,10,'nearest'),[-VoltMAX,VoltMAX])
axis image
axis off
% Initial image
im1 = print('-r500','-RGBImage');
[row1,col1] = size(im1(:,:,1));
% Crop
imN = 2500;
im2 = im1(190+[1:imN],730+[1:imN],:);
% Save
imwrite(im2,'images/FD_EIT_2D_demoC_voltmat0.png')

% Plot voltage matrices of the conductivities with inclusions
for iii = 1:((N-1)/2)
    tmp = cond_info(iii,4);
    voltmat = tmp{1};
    figure(iii)
    clf
    imagesc(imresize(voltmat,10,'nearest'),[-VoltMAX,VoltMAX])
    axis image
    axis off
    % Initial image
    im1 = print('-r500','-RGBImage');
    [row1,col1] = size(im1(:,:,1));
    % Crop
    imN = 2500;
    im2 = im1(190+[1:imN],730+[1:imN],:);
    % Save
    filename = ['images/FD_EIT_2D_demoC_voltmat',num2str(iii),'.png'];
    imwrite(im2,filename)
end

% Plot the conductivities
for iii = 1:((N-1)/2)
    tmp = cond_info(iii,1);
    condmatH = tmp{1};
    tmp = cond_info(iii,2);
    condmatV = tmp{1};
    EIT_FD_demo2D_conductivity_plot(N,condmatH,condmatV,1000+iii)
    % Initial image
    im1 = print('-r500','-RGBImage');
    [row1,col1] = size(im1(:,:,1));
    % Crop
    imN = 1600;
    im2 = im1(640+[1:imN],1170+[1:imN],:);
    % Save
    filename = ['images/FD_EIT_2D_demoC_cond',num2str(iii),'.png'];
    imwrite(im2,filename)
end



% Plot voltage differences in a common scale
for iii = 1:((N-1)/2)
    tmp = cond_info(iii,4);
    voltmat = tmp{1};
    voltdiff = voltmat-voltmat0;
    figure(iii)
    clf
    imagesc(imresize(voltdiff,10,'nearest'),[-VoltdiffMAX,VoltdiffMAX])
    axis image
    axis off
    % Initial image
    im1 = print('-r500','-RGBImage');
    [row1,col1] = size(im1(:,:,1));
    % Crop
    imN = 2500;
    im2 = im1(190+[1:imN],730+[1:imN],:);
    % Save
    filename = ['images/FD_EIT_2D_demoC_voltdiff',num2str(iii),'.png'];
    imwrite(im2,filename)
end




