Previous standalone version of data analysis routines added as part of legacy code archive.
This commit is contained in:
parent
3115554b27
commit
f52666d929
711
Data-Analyzer/Deprecated/analyzeFolder.m
Normal file
711
Data-Analyzer/Deprecated/analyzeFolder.m
Normal file
@ -0,0 +1,711 @@
|
|||||||
|
function results = analyzeFolder(options)
|
||||||
|
% Ensure required fields are defined in options
|
||||||
|
arguments
|
||||||
|
options.scan_parameter (1,:) char
|
||||||
|
options.scan_groups (1,:) double
|
||||||
|
options.cam (1,1) double
|
||||||
|
options.angle (1,1) double
|
||||||
|
options.center (1,2) double
|
||||||
|
options.span (1,2) double
|
||||||
|
options.fraction (1,2) double
|
||||||
|
options.ImagingMode (1,:) char
|
||||||
|
options.PulseDuration (1,1) double
|
||||||
|
options.removeFringes (1,1) logical
|
||||||
|
options.skipUnshuffling (1,1) logical
|
||||||
|
options.pixel_size (1,1) double
|
||||||
|
options.magnification (1,1) double
|
||||||
|
options.zoom_size (1,1) double
|
||||||
|
options.r_min (1,1) double
|
||||||
|
options.r_max (1,1) double
|
||||||
|
options.N_angular_bins (1,1) double
|
||||||
|
options.Angular_Threshold (1,1) double
|
||||||
|
options.Angular_Sigma (1,1) double
|
||||||
|
options.Angular_WindowSize (1,1) double
|
||||||
|
options.theta_min (1,1) double
|
||||||
|
options.theta_max (1,1) double
|
||||||
|
options.N_radial_bins (1,1) double
|
||||||
|
options.Radial_Sigma (1,1) double
|
||||||
|
options.Radial_WindowSize (1,1) double
|
||||||
|
options.k_min (1,1) double
|
||||||
|
options.k_max (1,1) double
|
||||||
|
options.skipPreprocessing (1,1) logical
|
||||||
|
options.skipMasking (1,1) logical
|
||||||
|
options.skipIntensityThresholding (1,1) logical
|
||||||
|
options.skipBinarization (1,1) logical
|
||||||
|
options.folderPath (1,:) char
|
||||||
|
end
|
||||||
|
|
||||||
|
% Assign variables from options
|
||||||
|
scan_parameter = options.scan_parameter;
|
||||||
|
scan_groups = options.scan_groups;
|
||||||
|
folderPath = options.folderPath;
|
||||||
|
center = options.center;
|
||||||
|
span = options.span;
|
||||||
|
fraction = options.fraction;
|
||||||
|
ImagingMode = options.ImagingMode;
|
||||||
|
PulseDuration = options.PulseDuration;
|
||||||
|
removeFringes = options.removeFringes;
|
||||||
|
skipUnshuffling = options.skipUnshuffling;
|
||||||
|
pixel_size = options.pixel_size;
|
||||||
|
magnification = options.magnification;
|
||||||
|
zoom_size = options.zoom_size;
|
||||||
|
r_min = options.r_min;
|
||||||
|
r_max = options.r_max;
|
||||||
|
N_angular_bins = options.N_angular_bins;
|
||||||
|
Angular_Threshold = options.Angular_Threshold;
|
||||||
|
Angular_Sigma = options.Angular_Sigma;
|
||||||
|
Angular_WindowSize = options.Angular_WindowSize;
|
||||||
|
theta_min = options.theta_min;
|
||||||
|
theta_max = options.theta_max;
|
||||||
|
N_radial_bins = options.N_radial_bins;
|
||||||
|
Radial_Sigma = options.Radial_Sigma;
|
||||||
|
Radial_WindowSize = options.Radial_WindowSize;
|
||||||
|
k_min = options.k_min;
|
||||||
|
k_max = options.k_max;
|
||||||
|
skipPreprocessing = options.skipPreprocessing;
|
||||||
|
skipMasking = options.skipMasking;
|
||||||
|
skipIntensityThresholding = options.skipIntensityThresholding;
|
||||||
|
skipBinarization = options.skipBinarization;
|
||||||
|
cam = options.cam;
|
||||||
|
angle = options.angle;
|
||||||
|
|
||||||
|
% Load images and analyze them (keep using the cleaned body of your original function)
|
||||||
|
% Fix the incorrect usage of 'cam' and 'angle' not defined locally
|
||||||
|
groupList = ["/images/MOT_3D_Camera/in_situ_absorption",
|
||||||
|
"/images/ODT_1_Axis_Camera/in_situ_absorption",
|
||||||
|
"/images/ODT_2_Axis_Camera/in_situ_absorption",
|
||||||
|
"/images/Horizontal_Axis_Camera/in_situ_absorption",
|
||||||
|
"/images/Vertical_Axis_Camera/in_situ_absorption"];
|
||||||
|
|
||||||
|
filePattern = fullfile(folderPath, '*.h5');
|
||||||
|
files = dir(filePattern);
|
||||||
|
refimages = zeros(span(1) + 1, span(2) + 1, length(files));
|
||||||
|
absimages = zeros(span(1) + 1, span(2) + 1, length(files));
|
||||||
|
|
||||||
|
for k = 1 : length(files)
|
||||||
|
baseFileName = files(k).name;
|
||||||
|
fullFileName = fullfile(files(k).folder, baseFileName);
|
||||||
|
|
||||||
|
fprintf(1, 'Now reading %s\n', fullFileName);
|
||||||
|
|
||||||
|
atm_img = double(imrotate(h5read(fullFileName, append(groupList(cam), "/atoms")), angle));
|
||||||
|
bkg_img = double(imrotate(h5read(fullFileName, append(groupList(cam), "/background")), angle));
|
||||||
|
dark_img = double(imrotate(h5read(fullFileName, append(groupList(cam), "/dark")), angle));
|
||||||
|
|
||||||
|
if (isempty(atm_img) && isa(atm_img, 'double')) || ...
|
||||||
|
(isempty(bkg_img) && isa(bkg_img, 'double')) || ...
|
||||||
|
(isempty(dark_img) && isa(dark_img, 'double'))
|
||||||
|
|
||||||
|
refimages(:,:,k) = nan(size(refimages(:,:,k))); % fill with NaNs
|
||||||
|
absimages(:,:,k) = nan(size(absimages(:,:,k)));
|
||||||
|
else
|
||||||
|
refimages(:,:,k) = subtractBackgroundOffset(cropODImage(bkg_img, center, span), fraction)';
|
||||||
|
absimages(:,:,k) = subtractBackgroundOffset(cropODImage(calculateODImage(atm_img, bkg_img, dark_img, ImagingMode, PulseDuration), center, span), fraction)';
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
% ===== Fringe removal =====
|
||||||
|
|
||||||
|
if removeFringes
|
||||||
|
optrefimages = removefringesInImage(absimages, refimages);
|
||||||
|
absimages_fringe_removed = absimages(:, :, :) - optrefimages(:, :, :);
|
||||||
|
|
||||||
|
nimgs = size(absimages_fringe_removed,3);
|
||||||
|
od_imgs = cell(1, nimgs);
|
||||||
|
for i = 1:nimgs
|
||||||
|
od_imgs{i} = absimages_fringe_removed(:, :, i);
|
||||||
|
end
|
||||||
|
else
|
||||||
|
nimgs = size(absimages(:, :, :),3);
|
||||||
|
od_imgs = cell(1, nimgs);
|
||||||
|
for i = 1:nimgs
|
||||||
|
od_imgs{i} = absimages(:, :, i);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
% ===== Get rotation angles =====
|
||||||
|
scan_parameter_values = zeros(1, length(files));
|
||||||
|
|
||||||
|
% Get information about the '/globals' group
|
||||||
|
for k = 1 : length(files)
|
||||||
|
baseFileName = files(k).name;
|
||||||
|
fullFileName = fullfile(files(k).folder, baseFileName);
|
||||||
|
info = h5info(fullFileName, '/globals');
|
||||||
|
for i = 1:length(info.Attributes)
|
||||||
|
if strcmp(info.Attributes(i).Name, scan_parameter)
|
||||||
|
if strcmp(scan_parameter, 'rot_mag_fin_pol_angle')
|
||||||
|
scan_parameter_values(k) = 180 - info.Attributes(i).Value;
|
||||||
|
else
|
||||||
|
scan_parameter_values(k) = info.Attributes(i).Value;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
% ===== Unshuffle if necessary to do so =====
|
||||||
|
|
||||||
|
if ~skipUnshuffling
|
||||||
|
n_values = length(scan_groups);
|
||||||
|
n_total = length(scan_parameter_values);
|
||||||
|
|
||||||
|
% Infer number of repetitions
|
||||||
|
n_reps = n_total / n_values;
|
||||||
|
|
||||||
|
% Preallocate ordered arrays
|
||||||
|
ordered_scan_values = zeros(1, n_total);
|
||||||
|
ordered_od_imgs = cell(1, n_total);
|
||||||
|
|
||||||
|
counter = 1;
|
||||||
|
|
||||||
|
for rep = 1:n_reps
|
||||||
|
for val = scan_groups
|
||||||
|
% Find the next unused match for this val
|
||||||
|
idx = find(scan_parameter_values == val, 1, 'first');
|
||||||
|
|
||||||
|
% Assign and remove from list to avoid duplicates
|
||||||
|
ordered_scan_values(counter) = scan_parameter_values(idx);
|
||||||
|
ordered_od_imgs{counter} = od_imgs{idx};
|
||||||
|
|
||||||
|
% Mark as used by removing
|
||||||
|
scan_parameter_values(idx) = NaN; % NaN is safe since original values are 0:5:45
|
||||||
|
od_imgs{idx} = []; % empty cell so it won't be matched again
|
||||||
|
|
||||||
|
counter = counter + 1;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
% Now assign back
|
||||||
|
scan_parameter_values = ordered_scan_values;
|
||||||
|
od_imgs = ordered_od_imgs;
|
||||||
|
end
|
||||||
|
% Extract quantities
|
||||||
|
fft_imgs = cell(1, nimgs);
|
||||||
|
spectral_distribution = cell(1, nimgs);
|
||||||
|
theta_values = cell(1, nimgs);
|
||||||
|
radial_spectral_contrast = zeros(1, nimgs);
|
||||||
|
angular_spectral_weight = zeros(1, nimgs);
|
||||||
|
N_shots = length(od_imgs);
|
||||||
|
|
||||||
|
for k = 1:N_shots
|
||||||
|
IMG = od_imgs{k};
|
||||||
|
if ~(max(IMG(:)) > 1)
|
||||||
|
IMGFFT = NaN(size(IMG));
|
||||||
|
else
|
||||||
|
[IMGFFT, IMGPR] = computeFourierTransform(IMG, skipPreprocessing, skipMasking, skipIntensityThresholding, skipBinarization);
|
||||||
|
end
|
||||||
|
|
||||||
|
% Size of original image (in pixels)
|
||||||
|
[Ny, Nx] = size(IMG);
|
||||||
|
|
||||||
|
% Real-space pixel size in micrometers after magnification
|
||||||
|
dx = pixel_size / magnification;
|
||||||
|
dy = dx; % assuming square pixels
|
||||||
|
|
||||||
|
% Real-space axes
|
||||||
|
x = ((1:Nx) - ceil(Nx/2)) * dx * 1E6;
|
||||||
|
y = ((1:Ny) - ceil(Ny/2)) * dy * 1E6;
|
||||||
|
|
||||||
|
% Reciprocal space increments (frequency domain, μm⁻¹)
|
||||||
|
dvx = 1 / (Nx * dx);
|
||||||
|
dvy = 1 / (Ny * dy);
|
||||||
|
|
||||||
|
% Frequency axes
|
||||||
|
vx = (-floor(Nx/2):ceil(Nx/2)-1) * dvx;
|
||||||
|
vy = (-floor(Ny/2):ceil(Ny/2)-1) * dvy;
|
||||||
|
|
||||||
|
% Wavenumber axes
|
||||||
|
kx_full = 2 * pi * vx * 1E-6; % μm⁻¹
|
||||||
|
ky_full = 2 * pi * vy * 1E-6;
|
||||||
|
|
||||||
|
% Crop FFT image around center
|
||||||
|
mid_x = floor(Nx/2);
|
||||||
|
mid_y = floor(Ny/2);
|
||||||
|
fft_imgs{k} = IMGFFT(mid_y-zoom_size:mid_y+zoom_size, mid_x-zoom_size:mid_x+zoom_size);
|
||||||
|
|
||||||
|
% Crop wavenumber axes to match fft_imgs{k}
|
||||||
|
kx = kx_full(mid_x - zoom_size : mid_x + zoom_size);
|
||||||
|
ky = ky_full(mid_y - zoom_size : mid_y + zoom_size);
|
||||||
|
|
||||||
|
[theta_vals, S_theta] = computeAngularSpectralDistribution(fft_imgs{k}, kx, ky, k_min, k_max, N_angular_bins, Angular_Threshold, Angular_Sigma, []);
|
||||||
|
[k_rho_vals, S_k] = computeRadialSpectralDistribution(fft_imgs{k}, kx, ky, theta_min, theta_max, N_radial_bins);
|
||||||
|
S_k_smoothed = movmean(S_k, Radial_WindowSize); % Compute moving average (use convolution) or use conv for more control
|
||||||
|
spectral_distribution{k} = S_theta;
|
||||||
|
theta_values{k} = theta_vals;
|
||||||
|
radial_spectral_contrast(k) = computeRadialSpectralContrast(k_rho_vals, S_k_smoothed, k_min, k_max);
|
||||||
|
S_theta_norm = S_theta / max(S_theta); % Normalize to 1
|
||||||
|
angular_spectral_weight(k) = trapz(theta_vals, S_theta_norm);
|
||||||
|
end
|
||||||
|
|
||||||
|
% Assuming scan_parameter_values and spectral_weight are column vectors (or row vectors of same length)
|
||||||
|
[unique_scan_parameter_values, ~, idx] = unique(scan_parameter_values);
|
||||||
|
|
||||||
|
% Preallocate arrays
|
||||||
|
mean_rsc = zeros(size(unique_scan_parameter_values));
|
||||||
|
stderr_rsc = zeros(size(unique_scan_parameter_values));
|
||||||
|
|
||||||
|
% Loop through each unique theta and compute mean and standard error
|
||||||
|
for i = 1:length(unique_scan_parameter_values)
|
||||||
|
group_vals = radial_spectral_contrast(idx == i);
|
||||||
|
mean_rsc(i) = mean(group_vals, 'omitnan');
|
||||||
|
stderr_rsc(i) = std(group_vals, 'omitnan') / sqrt(length(group_vals)); % standard error = std / sqrt(N)
|
||||||
|
end
|
||||||
|
|
||||||
|
% Preallocate arrays
|
||||||
|
mean_asw = zeros(size(unique_scan_parameter_values));
|
||||||
|
stderr_asw = zeros(size(unique_scan_parameter_values));
|
||||||
|
|
||||||
|
% Loop through each unique theta and compute mean and standard error
|
||||||
|
for i = 1:length(unique_scan_parameter_values)
|
||||||
|
group_vals = angular_spectral_weight(idx == i);
|
||||||
|
mean_asw(i) = mean(group_vals, 'omitnan');
|
||||||
|
stderr_asw(i) = std(group_vals, 'omitnan') / sqrt(length(group_vals)); % standard error = std / sqrt(N)
|
||||||
|
end
|
||||||
|
|
||||||
|
% Convert spectral distribution to matrix (N_shots x N_angular_bins)
|
||||||
|
delta_nkr_all = zeros(N_shots, N_angular_bins);
|
||||||
|
for k = 1:N_shots
|
||||||
|
delta_nkr_all(k, :) = spectral_distribution{k};
|
||||||
|
end
|
||||||
|
|
||||||
|
% Group by scan parameter values (e.g., alpha, angle, etc.)
|
||||||
|
[unique_scan_parameter_values, ~, idx] = unique(scan_parameter_values);
|
||||||
|
N_params = length(unique_scan_parameter_values);
|
||||||
|
|
||||||
|
% Define angular range and conversion
|
||||||
|
angle_range = 180;
|
||||||
|
angle_per_bin = angle_range / N_angular_bins;
|
||||||
|
max_peak_angle = 180;
|
||||||
|
max_peak_bin = round(max_peak_angle / angle_per_bin);
|
||||||
|
|
||||||
|
% Parameters for search
|
||||||
|
window_size = 10;
|
||||||
|
angle_threshold = 100;
|
||||||
|
|
||||||
|
% Initialize containers for final results
|
||||||
|
mean_max_g2_values = zeros(1, N_params);
|
||||||
|
mean_max_g2_angle_values = zeros(1, N_params);
|
||||||
|
var_max_g2_values = zeros(1, N_params);
|
||||||
|
var_max_g2_angle_values = zeros(1, N_params);
|
||||||
|
std_error_g2_values = zeros(1, N_params);
|
||||||
|
|
||||||
|
% Also store raw data per group
|
||||||
|
g2_all_per_group = cell(1, N_params);
|
||||||
|
angle_all_per_group = cell(1, N_params);
|
||||||
|
|
||||||
|
for i = 1:N_params
|
||||||
|
group_idx = find(idx == i);
|
||||||
|
group_data = delta_nkr_all(group_idx, :);
|
||||||
|
N_reps = size(group_data, 1);
|
||||||
|
|
||||||
|
g2_values = zeros(1, N_reps);
|
||||||
|
angle_at_max_g2 = zeros(1, N_reps);
|
||||||
|
|
||||||
|
for j = 1:N_reps
|
||||||
|
profile = group_data(j, :);
|
||||||
|
|
||||||
|
% Restrict search to 0–60° for highest peak
|
||||||
|
restricted_profile = profile(1:max_peak_bin);
|
||||||
|
[~, peak_idx_rel] = max(restricted_profile);
|
||||||
|
peak_idx = peak_idx_rel;
|
||||||
|
peak_angle = (peak_idx - 1) * angle_per_bin;
|
||||||
|
|
||||||
|
if peak_angle < angle_threshold
|
||||||
|
offsets = round(50 / angle_per_bin) : round(70 / angle_per_bin);
|
||||||
|
else
|
||||||
|
offsets = -round(70 / angle_per_bin) : -round(50 / angle_per_bin);
|
||||||
|
end
|
||||||
|
|
||||||
|
ref_window = mod((peak_idx - window_size):(peak_idx + window_size) - 1, N_angular_bins) + 1;
|
||||||
|
ref = profile(ref_window);
|
||||||
|
|
||||||
|
correlations = zeros(size(offsets));
|
||||||
|
angles = zeros(size(offsets));
|
||||||
|
|
||||||
|
for k = 1:length(offsets)
|
||||||
|
shifted_idx = mod(peak_idx + offsets(k) - 1, N_angular_bins) + 1;
|
||||||
|
sec_window = mod((shifted_idx - window_size):(shifted_idx + window_size) - 1, N_angular_bins) + 1;
|
||||||
|
sec = profile(sec_window);
|
||||||
|
|
||||||
|
num = mean(ref .* sec, 'omitnan');
|
||||||
|
denom = mean(ref.^2, 'omitnan');
|
||||||
|
g2 = num / denom;
|
||||||
|
|
||||||
|
correlations(k) = g2;
|
||||||
|
angles(k) = mod((peak_idx - 1 + offsets(k)) * angle_per_bin, angle_range);
|
||||||
|
end
|
||||||
|
|
||||||
|
[max_corr, max_idx] = max(correlations);
|
||||||
|
g2_values(j) = max_corr;
|
||||||
|
angle_at_max_g2(j) = angles(max_idx);
|
||||||
|
end
|
||||||
|
% Store raw values
|
||||||
|
g2_all_per_group{i} = g2_values;
|
||||||
|
angle_all_per_group{i} = angle_at_max_g2;
|
||||||
|
|
||||||
|
% Final stats
|
||||||
|
mean_max_g2_values(i) = mean(g2_values, 'omitnan');
|
||||||
|
var_max_g2_values(i) = var(g2_values, 0, 'omitnan');
|
||||||
|
mean_max_g2_angle_values(i)= mean(angle_at_max_g2, 'omitnan');
|
||||||
|
var_max_g2_angle_values(i) = var(angle_at_max_g2, 0, 'omitnan');
|
||||||
|
n_i = numel(g2_all_per_group{i}); % Number of repetitions for this param
|
||||||
|
std_error_g2_values(i) = sqrt(var_max_g2_values(i) / n_i);
|
||||||
|
end
|
||||||
|
|
||||||
|
results.folderPath = folderPath;
|
||||||
|
results.scan_parameter = scan_parameter;
|
||||||
|
results.scan_groups = scan_groups;
|
||||||
|
|
||||||
|
results.mean_max_g2_values = mean_max_g2_values;
|
||||||
|
results.std_error_g2_values = std_error_g2_values;
|
||||||
|
results.mean_max_g2_angle = mean_max_g2_angle_values;
|
||||||
|
results.radial_spectral_contrast= mean_rsc;
|
||||||
|
results.angular_spectral_weight = mean_asw;
|
||||||
|
end
|
||||||
|
|
||||||
|
%% Helper Functions
|
||||||
|
function [IMGFFT, IMGPR] = computeFourierTransform(I, skipPreprocessing, skipMasking, skipIntensityThresholding, skipBinarization)
|
||||||
|
% computeFourierSpectrum - Computes the 2D Fourier power spectrum
|
||||||
|
% of binarized and enhanced lattice image features, with optional central mask.
|
||||||
|
%
|
||||||
|
% Inputs:
|
||||||
|
% I - Grayscale or RGB image matrix
|
||||||
|
%
|
||||||
|
% Output:
|
||||||
|
% F_mag - 2D Fourier power spectrum (shifted)
|
||||||
|
|
||||||
|
if ~skipPreprocessing
|
||||||
|
% Preprocessing: Denoise
|
||||||
|
filtered = imgaussfilt(I, 10);
|
||||||
|
IMGPR = I - filtered; % adjust sigma as needed
|
||||||
|
else
|
||||||
|
IMGPR = I;
|
||||||
|
end
|
||||||
|
|
||||||
|
if ~skipMasking
|
||||||
|
[rows, cols] = size(IMGPR);
|
||||||
|
[X, Y] = meshgrid(1:cols, 1:rows);
|
||||||
|
% Elliptical mask parameters
|
||||||
|
cx = cols / 2;
|
||||||
|
cy = rows / 2;
|
||||||
|
|
||||||
|
% Shifted coordinates
|
||||||
|
x = X - cx;
|
||||||
|
y = Y - cy;
|
||||||
|
|
||||||
|
% Ellipse semi-axes
|
||||||
|
rx = 0.4 * cols;
|
||||||
|
ry = 0.2 * rows;
|
||||||
|
|
||||||
|
% Rotation angle in degrees -> radians
|
||||||
|
theta_deg = 30; % Adjust as needed
|
||||||
|
theta = deg2rad(theta_deg);
|
||||||
|
|
||||||
|
% Rotated ellipse equation
|
||||||
|
cos_t = cos(theta);
|
||||||
|
sin_t = sin(theta);
|
||||||
|
|
||||||
|
x_rot = (x * cos_t + y * sin_t);
|
||||||
|
y_rot = (-x * sin_t + y * cos_t);
|
||||||
|
|
||||||
|
ellipseMask = (x_rot.^2) / rx^2 + (y_rot.^2) / ry^2 <= 1;
|
||||||
|
|
||||||
|
% Apply cutout mask
|
||||||
|
IMGPR = IMGPR .* ellipseMask;
|
||||||
|
end
|
||||||
|
|
||||||
|
if ~skipIntensityThresholding
|
||||||
|
% Apply global intensity threshold mask
|
||||||
|
intensity_thresh = 0.20;
|
||||||
|
intensity_mask = IMGPR > intensity_thresh;
|
||||||
|
IMGPR = IMGPR .* intensity_mask;
|
||||||
|
end
|
||||||
|
|
||||||
|
if ~skipBinarization
|
||||||
|
% Adaptive binarization and cleanup
|
||||||
|
IMGPR = imbinarize(IMGPR, 'adaptive', 'Sensitivity', 0.0);
|
||||||
|
IMGPR = imdilate(IMGPR, strel('disk', 2));
|
||||||
|
IMGPR = imerode(IMGPR, strel('disk', 1));
|
||||||
|
IMGPR = imfill(IMGPR, 'holes');
|
||||||
|
F = fft2(double(IMGPR)); % Compute 2D Fourier Transform
|
||||||
|
IMGFFT = abs(fftshift(F))'; % Shift zero frequency to center
|
||||||
|
else
|
||||||
|
F = fft2(double(IMGPR)); % Compute 2D Fourier Transform
|
||||||
|
IMGFFT = abs(fftshift(F))'; % Shift zero frequency to center
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function [k_rho_vals, S_radial] = computeRadialSpectralDistribution(IMGFFT, kx, ky, thetamin, thetamax, num_bins)
|
||||||
|
% IMGFFT : 2D FFT image (fftshifted and cropped)
|
||||||
|
% kx, ky : 1D physical wavenumber axes [μm⁻¹] matching FFT size
|
||||||
|
% thetamin : Minimum angle (in radians)
|
||||||
|
% thetamax : Maximum angle (in radians)
|
||||||
|
% num_bins : Number of radial bins
|
||||||
|
|
||||||
|
[KX, KY] = meshgrid(kx, ky);
|
||||||
|
K_rho = sqrt(KX.^2 + KY.^2);
|
||||||
|
Theta = atan2(KY, KX);
|
||||||
|
|
||||||
|
if thetamin < thetamax
|
||||||
|
angle_mask = (Theta >= thetamin) & (Theta <= thetamax);
|
||||||
|
else
|
||||||
|
angle_mask = (Theta >= thetamin) | (Theta <= thetamax);
|
||||||
|
end
|
||||||
|
|
||||||
|
power_spectrum = abs(IMGFFT).^2;
|
||||||
|
|
||||||
|
r_min = min(K_rho(angle_mask));
|
||||||
|
r_max = max(K_rho(angle_mask));
|
||||||
|
r_edges = linspace(r_min, r_max, num_bins + 1);
|
||||||
|
k_rho_vals = 0.5 * (r_edges(1:end-1) + r_edges(2:end));
|
||||||
|
S_radial = zeros(1, num_bins);
|
||||||
|
|
||||||
|
for i = 1:num_bins
|
||||||
|
r_low = r_edges(i);
|
||||||
|
r_high = r_edges(i + 1);
|
||||||
|
radial_mask = (K_rho >= r_low) & (K_rho < r_high);
|
||||||
|
full_mask = radial_mask & angle_mask;
|
||||||
|
S_radial(i) = sum(power_spectrum(full_mask));
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function [theta_vals, S_theta] = computeAngularSpectralDistribution(IMGFFT, kx, ky, k_min, k_max, num_bins, threshold, sigma, windowSize)
|
||||||
|
% Apply threshold to isolate strong peaks
|
||||||
|
IMGFFT(IMGFFT < threshold) = 0;
|
||||||
|
|
||||||
|
% Create wavenumber meshgrid
|
||||||
|
[KX, KY] = meshgrid(kx, ky);
|
||||||
|
Kmag = sqrt(KX.^2 + KY.^2); % radial wavenumber magnitude
|
||||||
|
Theta = atan2(KY, KX); % range [-pi, pi]
|
||||||
|
|
||||||
|
% Restrict to radial band in wavenumber space
|
||||||
|
radial_mask = (Kmag >= k_min) & (Kmag <= k_max);
|
||||||
|
|
||||||
|
% Initialize angular structure factor
|
||||||
|
S_theta = zeros(1, num_bins);
|
||||||
|
theta_vals = linspace(0, pi, num_bins); % only 0 to pi due to symmetry
|
||||||
|
|
||||||
|
% Loop over angular bins
|
||||||
|
for i = 1:num_bins
|
||||||
|
angle_start = (i - 1) * pi / num_bins;
|
||||||
|
angle_end = i * pi / num_bins;
|
||||||
|
angle_mask = (Theta >= angle_start) & (Theta < angle_end);
|
||||||
|
bin_mask = radial_mask & angle_mask;
|
||||||
|
fft_angle = IMGFFT .* bin_mask;
|
||||||
|
S_theta(i) = sum(sum(abs(fft_angle).^2));
|
||||||
|
end
|
||||||
|
|
||||||
|
% Optional smoothing
|
||||||
|
if exist('sigma', 'var') && ~isempty(sigma)
|
||||||
|
% Gaussian smoothing
|
||||||
|
half_width = ceil(3 * sigma);
|
||||||
|
x = -half_width:half_width;
|
||||||
|
gauss_kernel = exp(-x.^2 / (2 * sigma^2));
|
||||||
|
gauss_kernel = gauss_kernel / sum(gauss_kernel);
|
||||||
|
|
||||||
|
% Circular convolution
|
||||||
|
S_theta = conv([S_theta(end - half_width + 1:end), S_theta, S_theta(1:half_width)], ...
|
||||||
|
gauss_kernel, 'same');
|
||||||
|
S_theta = S_theta(half_width + 1:end - half_width);
|
||||||
|
elseif exist('windowSize', 'var') && ~isempty(windowSize)
|
||||||
|
% Moving average smoothing
|
||||||
|
pad = floor(windowSize / 2);
|
||||||
|
kernel = ones(1, windowSize) / windowSize;
|
||||||
|
S_theta = conv([S_theta(end - pad + 1:end), S_theta, S_theta(1:pad)], kernel, 'same');
|
||||||
|
S_theta = S_theta(pad + 1:end - pad);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function contrast = computeRadialSpectralContrast(k_rho_vals, S_k_smoothed, k_min, k_max)
|
||||||
|
% Computes the ratio of the peak in S_k_smoothed within [k_min, k_max]
|
||||||
|
% to the value at (or near) k = 0.
|
||||||
|
|
||||||
|
% Ensure inputs are column vectors
|
||||||
|
k_rho_vals = k_rho_vals(:);
|
||||||
|
S_k_smoothed = S_k_smoothed(:);
|
||||||
|
|
||||||
|
% Step 1: Find index of k ≈ 0
|
||||||
|
[~, idx_k0] = min(abs(k_rho_vals)); % Closest to zero
|
||||||
|
S_k0 = S_k_smoothed(idx_k0);
|
||||||
|
|
||||||
|
% Step 2: Find indices in specified k-range
|
||||||
|
in_range = (k_rho_vals >= k_min) & (k_rho_vals <= k_max);
|
||||||
|
|
||||||
|
if ~any(in_range)
|
||||||
|
warning('No values found in the specified k-range. Returning NaN.');
|
||||||
|
contrast = NaN;
|
||||||
|
return;
|
||||||
|
end
|
||||||
|
|
||||||
|
% Step 3: Find peak value in the specified k-range
|
||||||
|
S_k_peak = max(S_k_smoothed(in_range));
|
||||||
|
|
||||||
|
% Step 4: Compute contrast
|
||||||
|
contrast = S_k_peak / S_k0;
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
function ret = getBkgOffsetFromCorners(img, x_fraction, y_fraction)
|
||||||
|
% image must be a 2D numerical array
|
||||||
|
[dim1, dim2] = size(img);
|
||||||
|
|
||||||
|
s1 = img(1:round(dim1 * y_fraction), 1:round(dim2 * x_fraction));
|
||||||
|
s2 = img(1:round(dim1 * y_fraction), round(dim2 - dim2 * x_fraction):dim2);
|
||||||
|
s3 = img(round(dim1 - dim1 * y_fraction):dim1, 1:round(dim2 * x_fraction));
|
||||||
|
s4 = img(round(dim1 - dim1 * y_fraction):dim1, round(dim2 - dim2 * x_fraction):dim2);
|
||||||
|
|
||||||
|
ret = mean([mean(s1(:)), mean(s2(:)), mean(s3(:)), mean(s4(:))]);
|
||||||
|
end
|
||||||
|
|
||||||
|
function ret = subtractBackgroundOffset(img, fraction)
|
||||||
|
% Remove the background from the image.
|
||||||
|
% :param dataArray: The image
|
||||||
|
% :type dataArray: xarray DataArray
|
||||||
|
% :param x_fraction: The fraction of the pixels used in x axis
|
||||||
|
% :type x_fraction: float
|
||||||
|
% :param y_fraction: The fraction of the pixels used in y axis
|
||||||
|
% :type y_fraction: float
|
||||||
|
% :return: The image after removing background
|
||||||
|
% :rtype: xarray DataArray
|
||||||
|
|
||||||
|
x_fraction = fraction(1);
|
||||||
|
y_fraction = fraction(2);
|
||||||
|
offset = getBkgOffsetFromCorners(img, x_fraction, y_fraction);
|
||||||
|
ret = img - offset;
|
||||||
|
end
|
||||||
|
|
||||||
|
function ret = cropODImage(img, center, span)
|
||||||
|
% Crop the image according to the region of interest (ROI).
|
||||||
|
% :param dataSet: The images
|
||||||
|
% :type dataSet: xarray DataArray or DataSet
|
||||||
|
% :param center: The center of region of interest (ROI)
|
||||||
|
% :type center: tuple
|
||||||
|
% :param span: The span of region of interest (ROI)
|
||||||
|
% :type span: tuple
|
||||||
|
% :return: The cropped images
|
||||||
|
% :rtype: xarray DataArray or DataSet
|
||||||
|
|
||||||
|
x_start = floor(center(1) - span(1) / 2);
|
||||||
|
x_end = floor(center(1) + span(1) / 2);
|
||||||
|
y_start = floor(center(2) - span(2) / 2);
|
||||||
|
y_end = floor(center(2) + span(2) / 2);
|
||||||
|
|
||||||
|
ret = img(y_start:y_end, x_start:x_end);
|
||||||
|
end
|
||||||
|
|
||||||
|
function imageOD = calculateODImage(imageAtom, imageBackground, imageDark, mode, exposureTime)
|
||||||
|
%CALCULATEODIMAGE Calculates the optical density (OD) image for absorption imaging.
|
||||||
|
%
|
||||||
|
% imageOD = calculateODImage(imageAtom, imageBackground, imageDark, mode, exposureTime)
|
||||||
|
%
|
||||||
|
% Inputs:
|
||||||
|
% imageAtom - Image with atoms
|
||||||
|
% imageBackground - Image without atoms
|
||||||
|
% imageDark - Image without light
|
||||||
|
% mode - 'LowIntensity' (default) or 'HighIntensity'
|
||||||
|
% exposureTime - Required only for 'HighIntensity' [in seconds]
|
||||||
|
%
|
||||||
|
% Output:
|
||||||
|
% imageOD - Computed OD image
|
||||||
|
%
|
||||||
|
|
||||||
|
arguments
|
||||||
|
imageAtom (:,:) {mustBeNumeric}
|
||||||
|
imageBackground (:,:) {mustBeNumeric}
|
||||||
|
imageDark (:,:) {mustBeNumeric}
|
||||||
|
mode char {mustBeMember(mode, {'LowIntensity', 'HighIntensity'})} = 'LowIntensity'
|
||||||
|
exposureTime double = NaN
|
||||||
|
end
|
||||||
|
|
||||||
|
% Compute numerator and denominator
|
||||||
|
numerator = imageBackground - imageDark;
|
||||||
|
denominator = imageAtom - imageDark;
|
||||||
|
|
||||||
|
% Avoid division by zero
|
||||||
|
numerator(numerator == 0) = 1;
|
||||||
|
denominator(denominator == 0) = 1;
|
||||||
|
|
||||||
|
% Calculate OD based on mode
|
||||||
|
switch mode
|
||||||
|
case 'LowIntensity'
|
||||||
|
imageOD = -log(abs(denominator ./ numerator));
|
||||||
|
|
||||||
|
case 'HighIntensity'
|
||||||
|
if isnan(exposureTime)
|
||||||
|
error('Exposure time must be provided for HighIntensity mode.');
|
||||||
|
end
|
||||||
|
imageOD = abs(denominator ./ numerator);
|
||||||
|
imageOD = -log(imageOD) + (numerator - denominator) ./ (7000 * (exposureTime / 5e-6));
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
function [optrefimages] = removefringesInImage(absimages, refimages, bgmask)
|
||||||
|
% removefringesInImage - Fringe removal and noise reduction from absorption images.
|
||||||
|
% Creates an optimal reference image for each absorption image in a set as
|
||||||
|
% a linear combination of reference images, with coefficients chosen to
|
||||||
|
% minimize the least-squares residuals between each absorption image and
|
||||||
|
% the optimal reference image. The coefficients are obtained by solving a
|
||||||
|
% linear set of equations using matrix inverse by LU decomposition.
|
||||||
|
%
|
||||||
|
% Application of the algorithm is described in C. F. Ockeloen et al, Improved
|
||||||
|
% detection of small atom numbers through image processing, arXiv:1007.2136 (2010).
|
||||||
|
%
|
||||||
|
% Syntax:
|
||||||
|
% [optrefimages] = removefringesInImage(absimages,refimages,bgmask);
|
||||||
|
%
|
||||||
|
% Required inputs:
|
||||||
|
% absimages - Absorption image data,
|
||||||
|
% typically 16 bit grayscale images
|
||||||
|
% refimages - Raw reference image data
|
||||||
|
% absimages and refimages are both cell arrays containing
|
||||||
|
% 2D array data. The number of refimages can differ from the
|
||||||
|
% number of absimages.
|
||||||
|
%
|
||||||
|
% Optional inputs:
|
||||||
|
% bgmask - Array specifying background region used,
|
||||||
|
% 1=background, 0=data. Defaults to all ones.
|
||||||
|
% Outputs:
|
||||||
|
% optrefimages - Cell array of optimal reference images,
|
||||||
|
% equal in size to absimages.
|
||||||
|
%
|
||||||
|
|
||||||
|
% Dependencies: none
|
||||||
|
%
|
||||||
|
% Authors: Shannon Whitlock, Caspar Ockeloen
|
||||||
|
% Reference: C. F. Ockeloen, A. F. Tauschinsky, R. J. C. Spreeuw, and
|
||||||
|
% S. Whitlock, Improved detection of small atom numbers through
|
||||||
|
% image processing, arXiv:1007.2136
|
||||||
|
% Email:
|
||||||
|
% May 2009; Last revision: 11 August 2010
|
||||||
|
|
||||||
|
% Process inputs
|
||||||
|
|
||||||
|
% Set variables, and flatten absorption and reference images
|
||||||
|
nimgs = size(absimages,3);
|
||||||
|
nimgsR = size(refimages,3);
|
||||||
|
xdim = size(absimages(:,:,1),2);
|
||||||
|
ydim = size(absimages(:,:,1),1);
|
||||||
|
|
||||||
|
R = single(reshape(refimages,xdim*ydim,nimgsR));
|
||||||
|
A = single(reshape(absimages,xdim*ydim,nimgs));
|
||||||
|
optrefimages=zeros(size(absimages)); % preallocate
|
||||||
|
|
||||||
|
if not(exist('bgmask','var')); bgmask=ones(ydim,xdim); end
|
||||||
|
k = find(bgmask(:)==1); % Index k specifying background region
|
||||||
|
|
||||||
|
% Ensure there are no duplicate reference images
|
||||||
|
% R=unique(R','rows')'; % comment this line if you run out of memory
|
||||||
|
|
||||||
|
% Decompose B = R*R' using singular value or LU decomposition
|
||||||
|
[L,U,p] = lu(R(k,:)'*R(k,:),'vector'); % LU decomposition
|
||||||
|
|
||||||
|
for j=1:nimgs
|
||||||
|
b=R(k,:)'*A(k,j);
|
||||||
|
% Obtain coefficients c which minimise least-square residuals
|
||||||
|
lower.LT = true; upper.UT = true;
|
||||||
|
c = linsolve(U,linsolve(L,b(p,:),lower),upper);
|
||||||
|
|
||||||
|
% Compute optimised reference image
|
||||||
|
optrefimages(:,:,j)=reshape(R*c,[ydim xdim]);
|
||||||
|
end
|
||||||
|
end
|
576
Data-Analyzer/Deprecated/analyzewithPCA.m
Normal file
576
Data-Analyzer/Deprecated/analyzewithPCA.m
Normal file
@ -0,0 +1,576 @@
|
|||||||
|
%% Extract Images
|
||||||
|
clear; close all; clc;
|
||||||
|
|
||||||
|
%% ===== D-S Settings =====
|
||||||
|
groupList = ["/images/MOT_3D_Camera/in_situ_absorption", "/images/ODT_1_Axis_Camera/in_situ_absorption", ...
|
||||||
|
"/images/ODT_2_Axis_Camera/in_situ_absorption", "/images/Horizontal_Axis_Camera/in_situ_absorption", ...
|
||||||
|
"/images/Vertical_Axis_Camera/in_situ_absorption"];
|
||||||
|
|
||||||
|
folderPath = "//DyLabNAS/Data/TwoDGas/2025/06/23/";
|
||||||
|
|
||||||
|
run = '0300';
|
||||||
|
|
||||||
|
folderPath = strcat(folderPath, run);
|
||||||
|
|
||||||
|
cam = 5;
|
||||||
|
|
||||||
|
angle = 0;
|
||||||
|
center = [1410, 2030];
|
||||||
|
span = [200, 200];
|
||||||
|
fraction = [0.1, 0.1];
|
||||||
|
|
||||||
|
pixel_size = 5.86e-6; % in meters
|
||||||
|
magnification = 23.94;
|
||||||
|
removeFringes = false;
|
||||||
|
|
||||||
|
ImagingMode = 'HighIntensity';
|
||||||
|
PulseDuration = 5e-6; % in s
|
||||||
|
|
||||||
|
% Fourier analysis settings
|
||||||
|
|
||||||
|
% Radial Spectral Distribution
|
||||||
|
theta_min = deg2rad(0);
|
||||||
|
theta_max = deg2rad(180);
|
||||||
|
N_radial_bins = 500;
|
||||||
|
Radial_Sigma = 2;
|
||||||
|
Radial_WindowSize = 5; % Choose an odd number for a centered moving average
|
||||||
|
|
||||||
|
% Angular Spectral Distribution
|
||||||
|
r_min = 10;
|
||||||
|
r_max = 20;
|
||||||
|
N_angular_bins = 180;
|
||||||
|
Angular_Threshold = 75;
|
||||||
|
Angular_Sigma = 2;
|
||||||
|
Angular_WindowSize = 5;
|
||||||
|
|
||||||
|
zoom_size = 50; % Zoomed-in region around center
|
||||||
|
|
||||||
|
% Plotting and saving
|
||||||
|
scan_parameter = 'ps_rot_mag_fin_pol_angle';
|
||||||
|
% scan_parameter = 'rot_mag_field';
|
||||||
|
|
||||||
|
savefileName = 'DropletsToStripes';
|
||||||
|
font = 'Bahnschrift';
|
||||||
|
|
||||||
|
if strcmp(savefileName, 'DropletsToStripes')
|
||||||
|
scan_groups = 0:5:45;
|
||||||
|
titleString = 'Droplets to Stripes';
|
||||||
|
elseif strcmp(savefileName, 'StripesToDroplets')
|
||||||
|
scan_groups = 45:-5:0;
|
||||||
|
titleString = 'Stripes to Droplets';
|
||||||
|
end
|
||||||
|
|
||||||
|
% Flags
|
||||||
|
skipNormalization = true;
|
||||||
|
skipUnshuffling = true;
|
||||||
|
skipPreprocessing = true;
|
||||||
|
skipMasking = true;
|
||||||
|
skipIntensityThresholding = true;
|
||||||
|
skipBinarization = true;
|
||||||
|
skipMovieRender = true;
|
||||||
|
skipSaveFigures = true;
|
||||||
|
skipSaveOD = true;
|
||||||
|
|
||||||
|
%% ===== S-D Settings =====
|
||||||
|
groupList = ["/images/MOT_3D_Camera/in_situ_absorption", "/images/ODT_1_Axis_Camera/in_situ_absorption", ...
|
||||||
|
"/images/ODT_2_Axis_Camera/in_situ_absorption", "/images/Horizontal_Axis_Camera/in_situ_absorption", ...
|
||||||
|
"/images/Vertical_Axis_Camera/in_situ_absorption"];
|
||||||
|
|
||||||
|
folderPath = "//DyLabNAS/Data/TwoDGas/2025/06/24/";
|
||||||
|
|
||||||
|
run = '0001';
|
||||||
|
|
||||||
|
folderPath = strcat(folderPath, run);
|
||||||
|
|
||||||
|
cam = 5;
|
||||||
|
|
||||||
|
angle = 0;
|
||||||
|
center = [1410, 2030];
|
||||||
|
span = [200, 200];
|
||||||
|
fraction = [0.1, 0.1];
|
||||||
|
|
||||||
|
pixel_size = 5.86e-6; % in meters
|
||||||
|
magnification = 23.94;
|
||||||
|
removeFringes = false;
|
||||||
|
|
||||||
|
ImagingMode = 'HighIntensity';
|
||||||
|
PulseDuration = 5e-6; % in s
|
||||||
|
|
||||||
|
% Fourier analysis settings
|
||||||
|
|
||||||
|
% Radial Spectral Distribution
|
||||||
|
theta_min = deg2rad(0);
|
||||||
|
theta_max = deg2rad(180);
|
||||||
|
N_radial_bins = 500;
|
||||||
|
Radial_Sigma = 2;
|
||||||
|
Radial_WindowSize = 5; % Choose an odd number for a centered moving average
|
||||||
|
|
||||||
|
% Angular Spectral Distribution
|
||||||
|
r_min = 10;
|
||||||
|
r_max = 20;
|
||||||
|
N_angular_bins = 180;
|
||||||
|
Angular_Threshold = 75;
|
||||||
|
Angular_Sigma = 2;
|
||||||
|
Angular_WindowSize = 5;
|
||||||
|
|
||||||
|
zoom_size = 50; % Zoomed-in region around center
|
||||||
|
|
||||||
|
% Plotting and saving
|
||||||
|
scan_parameter = 'ps_rot_mag_fin_pol_angle';
|
||||||
|
% scan_parameter = 'rot_mag_field';
|
||||||
|
|
||||||
|
savefileName = 'StripesToDroplets';
|
||||||
|
font = 'Bahnschrift';
|
||||||
|
|
||||||
|
if strcmp(savefileName, 'DropletsToStripes')
|
||||||
|
scan_groups = 0:5:45
|
||||||
|
titleString = 'Droplets to Stripes';
|
||||||
|
elseif strcmp(savefileName, 'StripesToDroplets')
|
||||||
|
scan_groups = 45:-5:0;
|
||||||
|
titleString = 'Stripes to Droplets';
|
||||||
|
end
|
||||||
|
|
||||||
|
% Flags
|
||||||
|
skipNormalization = true;
|
||||||
|
skipUnshuffling = false;
|
||||||
|
skipPreprocessing = true;
|
||||||
|
skipMasking = true;
|
||||||
|
skipIntensityThresholding = true;
|
||||||
|
skipBinarization = true;
|
||||||
|
skipMovieRender = true;
|
||||||
|
skipSaveFigures = true;
|
||||||
|
skipSaveOD = true;
|
||||||
|
|
||||||
|
%% ===== Load and compute OD image, rotate and extract ROI for analysis =====
|
||||||
|
% Get a list of all files in the folder with the desired file name pattern.
|
||||||
|
|
||||||
|
filePattern = fullfile(folderPath, '*.h5');
|
||||||
|
files = dir(filePattern);
|
||||||
|
refimages = zeros(span(1) + 1, span(2) + 1, length(files));
|
||||||
|
absimages = zeros(span(1) + 1, span(2) + 1, length(files));
|
||||||
|
|
||||||
|
for k = 1 : length(files)
|
||||||
|
baseFileName = files(k).name;
|
||||||
|
fullFileName = fullfile(files(k).folder, baseFileName);
|
||||||
|
|
||||||
|
fprintf(1, 'Now reading %s\n', fullFileName);
|
||||||
|
|
||||||
|
atm_img = double(imrotate(h5read(fullFileName, append(groupList(cam), "/atoms")), angle));
|
||||||
|
bkg_img = double(imrotate(h5read(fullFileName, append(groupList(cam), "/background")), angle));
|
||||||
|
dark_img = double(imrotate(h5read(fullFileName, append(groupList(cam), "/dark")), angle));
|
||||||
|
|
||||||
|
if (isempty(atm_img) && isa(atm_img, 'double')) || ...
|
||||||
|
(isempty(bkg_img) && isa(bkg_img, 'double')) || ...
|
||||||
|
(isempty(dark_img) && isa(dark_img, 'double'))
|
||||||
|
|
||||||
|
refimages(:,:,k) = nan(size(refimages(:,:,k))); % fill with NaNs
|
||||||
|
absimages(:,:,k) = nan(size(absimages(:,:,k)));
|
||||||
|
else
|
||||||
|
refimages(:,:,k) = subtractBackgroundOffset(cropODImage(bkg_img, center, span), fraction)';
|
||||||
|
absimages(:,:,k) = subtractBackgroundOffset(cropODImage(calculateODImage(atm_img, bkg_img, dark_img, ImagingMode, PulseDuration), center, span), fraction)';
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
% ===== Fringe removal =====
|
||||||
|
|
||||||
|
if removeFringes
|
||||||
|
optrefimages = removefringesInImage(absimages, refimages);
|
||||||
|
absimages_fringe_removed = absimages(:, :, :) - optrefimages(:, :, :);
|
||||||
|
|
||||||
|
nimgs = size(absimages_fringe_removed,3);
|
||||||
|
od_imgs = cell(1, nimgs);
|
||||||
|
for i = 1:nimgs
|
||||||
|
od_imgs{i} = absimages_fringe_removed(:, :, i);
|
||||||
|
end
|
||||||
|
else
|
||||||
|
nimgs = size(absimages(:, :, :),3);
|
||||||
|
od_imgs = cell(1, nimgs);
|
||||||
|
for i = 1:nimgs
|
||||||
|
od_imgs{i} = absimages(:, :, i);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
% ===== Get rotation angles =====
|
||||||
|
scan_parameter_values = zeros(1, length(files));
|
||||||
|
|
||||||
|
% Get information about the '/globals' group
|
||||||
|
for k = 1 : length(files)
|
||||||
|
baseFileName = files(k).name;
|
||||||
|
fullFileName = fullfile(files(k).folder, baseFileName);
|
||||||
|
info = h5info(fullFileName, '/globals');
|
||||||
|
for i = 1:length(info.Attributes)
|
||||||
|
if strcmp(info.Attributes(i).Name, scan_parameter)
|
||||||
|
if strcmp(scan_parameter, 'ps_rot_mag_fin_pol_angle')
|
||||||
|
scan_parameter_values(k) = 180 - info.Attributes(i).Value;
|
||||||
|
else
|
||||||
|
scan_parameter_values(k) = info.Attributes(i).Value;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
% ===== Unshuffle if necessary to do so =====
|
||||||
|
|
||||||
|
if ~skipUnshuffling
|
||||||
|
n_values = length(scan_groups);
|
||||||
|
n_total = length(scan_parameter_values);
|
||||||
|
|
||||||
|
% Infer number of repetitions
|
||||||
|
n_reps = n_total / n_values;
|
||||||
|
|
||||||
|
% Preallocate ordered arrays
|
||||||
|
ordered_scan_values = zeros(1, n_total);
|
||||||
|
ordered_od_imgs = cell(1, n_total);
|
||||||
|
|
||||||
|
counter = 1;
|
||||||
|
|
||||||
|
for rep = 1:n_reps
|
||||||
|
for val = scan_groups
|
||||||
|
% Find the next unused match for this val
|
||||||
|
idx = find(scan_parameter_values == val, 1, 'first');
|
||||||
|
|
||||||
|
% Assign and remove from list to avoid duplicates
|
||||||
|
ordered_scan_values(counter) = scan_parameter_values(idx);
|
||||||
|
ordered_od_imgs{counter} = od_imgs{idx};
|
||||||
|
|
||||||
|
% Mark as used by removing
|
||||||
|
scan_parameter_values(idx) = NaN; % NaN is safe since original values are 0:5:45
|
||||||
|
od_imgs{idx} = []; % empty cell so it won't be matched again
|
||||||
|
|
||||||
|
counter = counter + 1;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
% Now assign back
|
||||||
|
scan_parameter_values = ordered_scan_values;
|
||||||
|
od_imgs = ordered_od_imgs;
|
||||||
|
end
|
||||||
|
|
||||||
|
%% Carry out PCA
|
||||||
|
numPCs = 5;
|
||||||
|
|
||||||
|
% Stack all 600 images into one data matrix [nImages x nPixels]
|
||||||
|
allImgs3D = cat(3, od_imgs{:});
|
||||||
|
[Nx, Ny] = size(allImgs3D(:,:,1));
|
||||||
|
Xall = reshape(allImgs3D, [], numel(od_imgs))'; % [600 x (Nx*Ny)]
|
||||||
|
|
||||||
|
% Global PCA
|
||||||
|
[coeff, score, ~, ~, explained] = pca(Xall);
|
||||||
|
|
||||||
|
%% Visualize PC1
|
||||||
|
% Extract the first principal component vector (eigenimage)
|
||||||
|
pc1_vector = coeff(:,1);
|
||||||
|
|
||||||
|
% Reshape back to original image dimensions
|
||||||
|
pc1_image = reshape(pc1_vector, Nx, Ny);
|
||||||
|
|
||||||
|
% Plot the PC1 image
|
||||||
|
figure(1); clf; set(gcf, 'Color', 'w', 'Position', [100 100 950 750]);
|
||||||
|
imagesc(pc1_image);
|
||||||
|
axis image off;
|
||||||
|
colormap(Colormaps.coolwarm()); % or use 'jet', 'parula', etc.
|
||||||
|
colorbar;
|
||||||
|
title(sprintf('First Principal Component (PC1) Image - Explains %.2f%% Variance', explained(1)));
|
||||||
|
|
||||||
|
%% Distribution scatter plot
|
||||||
|
numGroups = numel(scan_groups);
|
||||||
|
colors = lines(numGroups);
|
||||||
|
|
||||||
|
figure(2); clf; set(gcf, 'Color', 'w', 'Position', [100 100 950 750]); hold on;
|
||||||
|
for g = 1:numGroups
|
||||||
|
idx = scan_parameter_values == scan_groups(g);
|
||||||
|
scatter(repmat(scan_groups(g), sum(idx),1), score(idx,1), 36, colors(g,:), 'filled');
|
||||||
|
end
|
||||||
|
xlabel('Control Parameter');
|
||||||
|
ylabel('PC1 Score');
|
||||||
|
title('Evolution of PC1 Scores');
|
||||||
|
grid on;
|
||||||
|
|
||||||
|
%% Distribution Histogram plot
|
||||||
|
numGroups = length(scan_groups);
|
||||||
|
colors = lines(numGroups);
|
||||||
|
|
||||||
|
% Define number of bins globally
|
||||||
|
numBins = 20;
|
||||||
|
|
||||||
|
% Define common bin edges based on global PC1 score range
|
||||||
|
minScore = min(score(:,1));
|
||||||
|
maxScore = max(score(:,1));
|
||||||
|
binEdges = linspace(minScore, maxScore, numBins+1); % +1 because edges are one more than bins
|
||||||
|
binWidth = binEdges(2) - binEdges(1); % for scaling KDE
|
||||||
|
|
||||||
|
figure(3);
|
||||||
|
clf; set(gcf, 'Color', 'w', 'Position', [100 100 950 750]);
|
||||||
|
tiledlayout(ceil(numGroups/2), 2, 'TileSpacing', 'compact', 'Padding', 'compact');
|
||||||
|
|
||||||
|
for g = 1:numGroups
|
||||||
|
groupVal = scan_groups(g);
|
||||||
|
idx = scan_parameter_values == groupVal;
|
||||||
|
groupPC1 = score(idx,1);
|
||||||
|
|
||||||
|
nexttile;
|
||||||
|
|
||||||
|
% Plot histogram
|
||||||
|
histogram(groupPC1, 'Normalization', 'probability', ...
|
||||||
|
'FaceColor', colors(g,:), 'EdgeColor', 'none', ...
|
||||||
|
'BinEdges', binEdges);
|
||||||
|
hold on;
|
||||||
|
|
||||||
|
% Compute KDE
|
||||||
|
[f, xi] = ksdensity(groupPC1, 'NumPoints', 1000);
|
||||||
|
|
||||||
|
% Scale KDE to histogram probability scale
|
||||||
|
f_scaled = f * binWidth;
|
||||||
|
|
||||||
|
% Overlay KDE curve
|
||||||
|
plot(xi, f_scaled, 'k', 'LineWidth', 1.5);
|
||||||
|
|
||||||
|
% Vertical line at median
|
||||||
|
med = median(groupPC1);
|
||||||
|
yl = ylim;
|
||||||
|
plot([med med], yl, 'k--', 'LineWidth', 1);
|
||||||
|
|
||||||
|
xlabel('PC1 Score');
|
||||||
|
ylabel('Probability');
|
||||||
|
title(sprintf('Control Parameter = %d', groupVal));
|
||||||
|
grid on;
|
||||||
|
hold off;
|
||||||
|
end
|
||||||
|
|
||||||
|
sgtitle('PC1 Score Distributions');
|
||||||
|
|
||||||
|
%% Box plot for PC1 scores by group
|
||||||
|
groupLabels = cell(size(score,1),1);
|
||||||
|
for g = 1:numGroups
|
||||||
|
idx = scan_parameter_values == scan_groups(g);
|
||||||
|
groupLabels(idx) = {sprintf('%d', scan_groups(g))};
|
||||||
|
end
|
||||||
|
|
||||||
|
figure(4);
|
||||||
|
clf; set(gcf, 'Color', 'w', 'Position', [100 100 950 750]);
|
||||||
|
boxplot(score(:,1), groupLabels);
|
||||||
|
xlabel('Control Parameter');
|
||||||
|
ylabel('PC1 Score');
|
||||||
|
title('Evolution of PC1 Scores');
|
||||||
|
grid on;
|
||||||
|
|
||||||
|
%% Mean and SEM plot for PC1 scores
|
||||||
|
numGroups = length(scan_groups);
|
||||||
|
meanPC1Scores = zeros(numGroups,1);
|
||||||
|
semPC1Scores = zeros(numGroups,1);
|
||||||
|
|
||||||
|
for g = 1:numGroups
|
||||||
|
groupVal = scan_groups(g);
|
||||||
|
idx = scan_parameter_values == groupVal;
|
||||||
|
groupPC1 = score(idx,1); % PC1 scores for this group
|
||||||
|
|
||||||
|
meanPC1Scores(g) = mean(groupPC1);
|
||||||
|
semPC1Scores(g) = std(groupPC1)/sqrt(sum(idx)); % Standard error of mean
|
||||||
|
end
|
||||||
|
|
||||||
|
% Plot mean ± SEM with error bars
|
||||||
|
figure(5);
|
||||||
|
clf; set(gcf, 'Color', 'w', 'Position', [100 100 950 750]);
|
||||||
|
errorbar(scan_groups, meanPC1Scores, semPC1Scores, 'o-', ...
|
||||||
|
'LineWidth', 1.5, 'MarkerSize', 8, 'MarkerFaceColor', 'b');
|
||||||
|
xlabel('Control Parameter');
|
||||||
|
ylabel('Mean PC1 Score ± SEM');
|
||||||
|
title('Evolution of PC1 Scores');
|
||||||
|
grid on;
|
||||||
|
|
||||||
|
%% Plot Binder Cumulant
|
||||||
|
maxOrder = 4; % We only need up to order 4 here
|
||||||
|
numGroups = length(scan_groups);
|
||||||
|
kappa4 = NaN(1, numGroups);
|
||||||
|
|
||||||
|
for g = 1:numGroups
|
||||||
|
groupVal = scan_groups(g);
|
||||||
|
idx = scan_parameter_values == groupVal;
|
||||||
|
groupPC1 = score(idx, 1);
|
||||||
|
|
||||||
|
cumulants = computeCumulants(groupPC1, maxOrder);
|
||||||
|
kappa4(g) = cumulants(4); % 4th-order cumulant
|
||||||
|
end
|
||||||
|
|
||||||
|
% Plot
|
||||||
|
figure(6);
|
||||||
|
clf; set(gcf, 'Color', 'w', 'Position', [100 100 950 750]);
|
||||||
|
plot(scan_groups, kappa4 * 1E-5, '-o', 'LineWidth', 1.5, 'MarkerFaceColor', 'b');
|
||||||
|
ylim([-12 12])
|
||||||
|
xlabel('Control Parameter');
|
||||||
|
ylabel('\kappa_4 (\times 10^{5})');
|
||||||
|
grid on;
|
||||||
|
title('Evolution of Binder Cumulant of PC1 Score');
|
||||||
|
|
||||||
|
%% --- ANOVA test ---
|
||||||
|
p = anova1(score(:,1), groupLabels, 'off');
|
||||||
|
fprintf('ANOVA p-value for PC1 score differences between groups: %.4e\n', p);
|
||||||
|
|
||||||
|
%% Helper Functions
|
||||||
|
|
||||||
|
function ret = getBkgOffsetFromCorners(img, x_fraction, y_fraction)
|
||||||
|
% image must be a 2D numerical array
|
||||||
|
[dim1, dim2] = size(img);
|
||||||
|
|
||||||
|
s1 = img(1:round(dim1 * y_fraction), 1:round(dim2 * x_fraction));
|
||||||
|
s2 = img(1:round(dim1 * y_fraction), round(dim2 - dim2 * x_fraction):dim2);
|
||||||
|
s3 = img(round(dim1 - dim1 * y_fraction):dim1, 1:round(dim2 * x_fraction));
|
||||||
|
s4 = img(round(dim1 - dim1 * y_fraction):dim1, round(dim2 - dim2 * x_fraction):dim2);
|
||||||
|
|
||||||
|
ret = mean([mean(s1(:)), mean(s2(:)), mean(s3(:)), mean(s4(:))]);
|
||||||
|
end
|
||||||
|
|
||||||
|
function ret = subtractBackgroundOffset(img, fraction)
|
||||||
|
% Remove the background from the image.
|
||||||
|
% :param dataArray: The image
|
||||||
|
% :type dataArray: xarray DataArray
|
||||||
|
% :param x_fraction: The fraction of the pixels used in x axis
|
||||||
|
% :type x_fraction: float
|
||||||
|
% :param y_fraction: The fraction of the pixels used in y axis
|
||||||
|
% :type y_fraction: float
|
||||||
|
% :return: The image after removing background
|
||||||
|
% :rtype: xarray DataArray
|
||||||
|
|
||||||
|
x_fraction = fraction(1);
|
||||||
|
y_fraction = fraction(2);
|
||||||
|
offset = getBkgOffsetFromCorners(img, x_fraction, y_fraction);
|
||||||
|
ret = img - offset;
|
||||||
|
end
|
||||||
|
|
||||||
|
function ret = cropODImage(img, center, span)
|
||||||
|
% Crop the image according to the region of interest (ROI).
|
||||||
|
% :param dataSet: The images
|
||||||
|
% :type dataSet: xarray DataArray or DataSet
|
||||||
|
% :param center: The center of region of interest (ROI)
|
||||||
|
% :type center: tuple
|
||||||
|
% :param span: The span of region of interest (ROI)
|
||||||
|
% :type span: tuple
|
||||||
|
% :return: The cropped images
|
||||||
|
% :rtype: xarray DataArray or DataSet
|
||||||
|
|
||||||
|
x_start = floor(center(1) - span(1) / 2);
|
||||||
|
x_end = floor(center(1) + span(1) / 2);
|
||||||
|
y_start = floor(center(2) - span(2) / 2);
|
||||||
|
y_end = floor(center(2) + span(2) / 2);
|
||||||
|
|
||||||
|
ret = img(y_start:y_end, x_start:x_end);
|
||||||
|
end
|
||||||
|
|
||||||
|
function imageOD = calculateODImage(imageAtom, imageBackground, imageDark, mode, exposureTime)
|
||||||
|
%CALCULATEODIMAGE Calculates the optical density (OD) image for absorption imaging.
|
||||||
|
%
|
||||||
|
% imageOD = calculateODImage(imageAtom, imageBackground, imageDark, mode, exposureTime)
|
||||||
|
%
|
||||||
|
% Inputs:
|
||||||
|
% imageAtom - Image with atoms
|
||||||
|
% imageBackground - Image without atoms
|
||||||
|
% imageDark - Image without light
|
||||||
|
% mode - 'LowIntensity' (default) or 'HighIntensity'
|
||||||
|
% exposureTime - Required only for 'HighIntensity' [in seconds]
|
||||||
|
%
|
||||||
|
% Output:
|
||||||
|
% imageOD - Computed OD image
|
||||||
|
%
|
||||||
|
|
||||||
|
arguments
|
||||||
|
imageAtom (:,:) {mustBeNumeric}
|
||||||
|
imageBackground (:,:) {mustBeNumeric}
|
||||||
|
imageDark (:,:) {mustBeNumeric}
|
||||||
|
mode char {mustBeMember(mode, {'LowIntensity', 'HighIntensity'})} = 'LowIntensity'
|
||||||
|
exposureTime double = NaN
|
||||||
|
end
|
||||||
|
|
||||||
|
% Compute numerator and denominator
|
||||||
|
numerator = imageBackground - imageDark;
|
||||||
|
denominator = imageAtom - imageDark;
|
||||||
|
|
||||||
|
% Avoid division by zero
|
||||||
|
numerator(numerator == 0) = 1;
|
||||||
|
denominator(denominator == 0) = 1;
|
||||||
|
|
||||||
|
% Calculate OD based on mode
|
||||||
|
switch mode
|
||||||
|
case 'LowIntensity'
|
||||||
|
imageOD = -log(abs(denominator ./ numerator));
|
||||||
|
|
||||||
|
case 'HighIntensity'
|
||||||
|
if isnan(exposureTime)
|
||||||
|
error('Exposure time must be provided for HighIntensity mode.');
|
||||||
|
end
|
||||||
|
imageOD = abs(denominator ./ numerator);
|
||||||
|
imageOD = -log(imageOD) + (numerator - denominator) ./ (7000 * (exposureTime / 5e-6));
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
function [optrefimages] = removefringesInImage(absimages, refimages, bgmask)
|
||||||
|
% removefringesInImage - Fringe removal and noise reduction from absorption images.
|
||||||
|
% Creates an optimal reference image for each absorption image in a set as
|
||||||
|
% a linear combination of reference images, with coefficients chosen to
|
||||||
|
% minimize the least-squares residuals between each absorption image and
|
||||||
|
% the optimal reference image. The coefficients are obtained by solving a
|
||||||
|
% linear set of equations using matrix inverse by LU decomposition.
|
||||||
|
%
|
||||||
|
% Application of the algorithm is described in C. F. Ockeloen et al, Improved
|
||||||
|
% detection of small atom numbers through image processing, arXiv:1007.2136 (2010).
|
||||||
|
%
|
||||||
|
% Syntax:
|
||||||
|
% [optrefimages] = removefringesInImage(absimages,refimages,bgmask);
|
||||||
|
%
|
||||||
|
% Required inputs:
|
||||||
|
% absimages - Absorption image data,
|
||||||
|
% typically 16 bit grayscale images
|
||||||
|
% refimages - Raw reference image data
|
||||||
|
% absimages and refimages are both cell arrays containing
|
||||||
|
% 2D array data. The number of refimages can differ from the
|
||||||
|
% number of absimages.
|
||||||
|
%
|
||||||
|
% Optional inputs:
|
||||||
|
% bgmask - Array specifying background region used,
|
||||||
|
% 1=background, 0=data. Defaults to all ones.
|
||||||
|
% Outputs:
|
||||||
|
% optrefimages - Cell array of optimal reference images,
|
||||||
|
% equal in size to absimages.
|
||||||
|
%
|
||||||
|
|
||||||
|
% Dependencies: none
|
||||||
|
%
|
||||||
|
% Authors: Shannon Whitlock, Caspar Ockeloen
|
||||||
|
% Reference: C. F. Ockeloen, A. F. Tauschinsky, R. J. C. Spreeuw, and
|
||||||
|
% S. Whitlock, Improved detection of small atom numbers through
|
||||||
|
% image processing, arXiv:1007.2136
|
||||||
|
% Email:
|
||||||
|
% May 2009; Last revision: 11 August 2010
|
||||||
|
|
||||||
|
% Process inputs
|
||||||
|
|
||||||
|
% Set variables, and flatten absorption and reference images
|
||||||
|
nimgs = size(absimages,3);
|
||||||
|
nimgsR = size(refimages,3);
|
||||||
|
xdim = size(absimages(:,:,1),2);
|
||||||
|
ydim = size(absimages(:,:,1),1);
|
||||||
|
|
||||||
|
R = single(reshape(refimages,xdim*ydim,nimgsR));
|
||||||
|
A = single(reshape(absimages,xdim*ydim,nimgs));
|
||||||
|
optrefimages=zeros(size(absimages)); % preallocate
|
||||||
|
|
||||||
|
if not(exist('bgmask','var')); bgmask=ones(ydim,xdim); end
|
||||||
|
k = find(bgmask(:)==1); % Index k specifying background region
|
||||||
|
|
||||||
|
% Ensure there are no duplicate reference images
|
||||||
|
% R=unique(R','rows')'; % comment this line if you run out of memory
|
||||||
|
|
||||||
|
% Decompose B = R*R' using singular value or LU decomposition
|
||||||
|
[L,U,p] = lu(R(k,:)'*R(k,:),'vector'); % LU decomposition
|
||||||
|
|
||||||
|
for j=1:nimgs
|
||||||
|
b=R(k,:)'*A(k,j);
|
||||||
|
% Obtain coefficients c which minimise least-square residuals
|
||||||
|
lower.LT = true; upper.UT = true;
|
||||||
|
c = linsolve(U,linsolve(L,b(p,:),lower),upper);
|
||||||
|
|
||||||
|
% Compute optimised reference image
|
||||||
|
optrefimages(:,:,j)=reshape(R*c,[ydim xdim]);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
41
Data-Analyzer/Deprecated/bootstrapCumulants.m
Normal file
41
Data-Analyzer/Deprecated/bootstrapCumulants.m
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
function [cumulants_mean, cumulants_ci, bootstrap_samples] = bootstrapCumulants(x, maxOrder, nBoot)
|
||||||
|
% bootstrapCumulants - compute bootstrap estimates of cumulants and confidence intervals
|
||||||
|
%
|
||||||
|
% Syntax:
|
||||||
|
% [meanC, ciC, allC] = bootstrapCumulants(x, maxOrder, nBoot)
|
||||||
|
%
|
||||||
|
% Inputs:
|
||||||
|
% x - Data vector (may contain NaNs)
|
||||||
|
% maxOrder - Max cumulant order (default: 6)
|
||||||
|
% nBoot - Number of bootstrap samples (default: 1000)
|
||||||
|
%
|
||||||
|
% Outputs:
|
||||||
|
% cumulants_mean - Mean of bootstrap cumulants
|
||||||
|
% cumulants_ci - 95% confidence intervals [2.5th; 97.5th] percentile
|
||||||
|
% bootstrap_samples - All bootstrap cumulants (nBoot x maxOrder)
|
||||||
|
|
||||||
|
if nargin < 2, maxOrder = 6; end
|
||||||
|
if nargin < 3, nBoot = 1000; end
|
||||||
|
|
||||||
|
x = x(:);
|
||||||
|
x = x(~isnan(x)); % Remove NaNs
|
||||||
|
|
||||||
|
if isempty(x)
|
||||||
|
cumulants_mean = NaN(1, maxOrder);
|
||||||
|
cumulants_ci = NaN(2, maxOrder);
|
||||||
|
bootstrap_samples = NaN(nBoot, maxOrder);
|
||||||
|
return;
|
||||||
|
end
|
||||||
|
|
||||||
|
N = numel(x);
|
||||||
|
bootstrap_samples = zeros(nBoot, maxOrder);
|
||||||
|
|
||||||
|
for b = 1:nBoot
|
||||||
|
xb = x(randi(N, [N, 1])); % Resample with replacement
|
||||||
|
bootstrap_samples(b, :) = computeCumulants(xb, maxOrder);
|
||||||
|
end
|
||||||
|
|
||||||
|
cumulants_mean = mean(bootstrap_samples, 1);
|
||||||
|
cumulants_ci = prctile(bootstrap_samples, [2.5, 97.5]);
|
||||||
|
|
||||||
|
end
|
39
Data-Analyzer/Deprecated/compareAngularCorrelation.m
Normal file
39
Data-Analyzer/Deprecated/compareAngularCorrelation.m
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
%% Track spectral weight across the transition
|
||||||
|
|
||||||
|
set(0,'defaulttextInterpreter','latex')
|
||||||
|
set(groot, 'defaultAxesTickLabelInterpreter','latex'); set(groot, 'defaultLegendInterpreter','latex');
|
||||||
|
|
||||||
|
format long
|
||||||
|
|
||||||
|
font = 'Bahnschrift';
|
||||||
|
|
||||||
|
% Load data
|
||||||
|
Data = load('C:/Users/Karthik/Documents/GitRepositories/Calculations/Data-Analyzer/StructuralPhaseTransition/SpectralAnalysisRoutines/Max_g2_DropletsToStripes.mat', 'unique_scan_parameter_values', 'mean_max_g2_values', 'std_error_g2_values');
|
||||||
|
|
||||||
|
dts_scan_parameter_values = Data.unique_scan_parameter_values;
|
||||||
|
dts_mean_mg2 = Data.mean_max_g2_values;
|
||||||
|
dts_stderr_mg2 = Data.std_error_g2_values;
|
||||||
|
|
||||||
|
Data = load('C:/Users/Karthik/Documents/GitRepositories/Calculations/Data-Analyzer/StructuralPhaseTransition/SpectralAnalysisRoutines/Max_g2_StripesToDroplets.mat', 'unique_scan_parameter_values', 'mean_max_g2_values', 'std_error_g2_values');
|
||||||
|
|
||||||
|
std_scan_parameter_values = Data.unique_scan_parameter_values;
|
||||||
|
std_mean_mg2 = Data.mean_max_g2_values;
|
||||||
|
std_stderr_mg2 = Data.std_error_g2_values;
|
||||||
|
|
||||||
|
figure(1);
|
||||||
|
set(gcf,'Position',[100 100 950 750])
|
||||||
|
errorbar(dts_scan_parameter_values, dts_mean_mg2, dts_stderr_mg2, 'o--', ...
|
||||||
|
'LineWidth', 1.5, 'MarkerSize', 6, 'CapSize', 5, 'DisplayName' , 'Droplets to Stripes');
|
||||||
|
hold on
|
||||||
|
errorbar(std_scan_parameter_values, std_mean_mg2, std_stderr_mg2, 'o--', ...
|
||||||
|
'LineWidth', 1.5, 'MarkerSize', 6, 'CapSize', 5, 'DisplayName', 'Stripes to Droplets');
|
||||||
|
set(gca, 'FontSize', 14, 'YLim', [0, 1]);
|
||||||
|
hXLabel = xlabel('$\alpha$ (degrees)', 'Interpreter', 'latex');
|
||||||
|
hYLabel = ylabel('$\mathrm{max}[g^{(2)}_{[50,70]}(\delta\theta)]$', 'Interpreter', 'latex');
|
||||||
|
% hTitle = title('B = 2.42 G', 'Interpreter', 'tex');
|
||||||
|
legend
|
||||||
|
set([hXLabel, hYLabel], 'FontName', font)
|
||||||
|
set([hXLabel, hYLabel], 'FontSize', 14)
|
||||||
|
% set(hTitle, 'FontName', font, 'FontSize', 16, 'FontWeight', 'bold'); % Set font and size for title
|
||||||
|
grid on
|
||||||
|
%%
|
52
Data-Analyzer/Deprecated/computeCumulants.m
Normal file
52
Data-Analyzer/Deprecated/computeCumulants.m
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
function cumulants = computeCumulants(x, maxOrder)
|
||||||
|
% computeCumulants - compute cumulants up to specified order from data vector x
|
||||||
|
%
|
||||||
|
% Syntax: cumulants = computeCumulants(x, maxOrder)
|
||||||
|
%
|
||||||
|
% Inputs:
|
||||||
|
% x - 1D numeric vector (may contain NaNs)
|
||||||
|
% maxOrder - maximum order of cumulants to compute (default: 6)
|
||||||
|
%
|
||||||
|
% Output:
|
||||||
|
% cumulants - vector [kappa_1, ..., kappa_maxOrder]
|
||||||
|
|
||||||
|
if nargin < 2
|
||||||
|
maxOrder = 6;
|
||||||
|
end
|
||||||
|
|
||||||
|
x = x(:);
|
||||||
|
x = x(~isnan(x)); % Remove NaNs
|
||||||
|
|
||||||
|
if isempty(x)
|
||||||
|
cumulants = NaN(1, maxOrder);
|
||||||
|
return;
|
||||||
|
end
|
||||||
|
|
||||||
|
mu1 = mean(x, 'omitnan');
|
||||||
|
x_centered = x - mu1;
|
||||||
|
|
||||||
|
cumulants = zeros(1, maxOrder);
|
||||||
|
cumulants(1) = mu1;
|
||||||
|
|
||||||
|
mu = zeros(1, maxOrder);
|
||||||
|
for k = 2:maxOrder
|
||||||
|
mu(k) = mean(x_centered.^k, 'omitnan');
|
||||||
|
end
|
||||||
|
|
||||||
|
if maxOrder >= 2
|
||||||
|
cumulants(2) = mu(2);
|
||||||
|
end
|
||||||
|
if maxOrder >= 3
|
||||||
|
cumulants(3) = mu(3);
|
||||||
|
end
|
||||||
|
if maxOrder >= 4
|
||||||
|
cumulants(4) = mu(4) - 3 * mu(2)^2;
|
||||||
|
end
|
||||||
|
if maxOrder >= 5
|
||||||
|
cumulants(5) = mu(5) - 10 * mu(3) * mu(2);
|
||||||
|
end
|
||||||
|
if maxOrder >= 6
|
||||||
|
cumulants(6) = mu(6) - 15 * mu(4) * mu(2) - 10 * mu(3)^2 + 30 * mu(2)^3;
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
1372
Data-Analyzer/Deprecated/conductSpectralAnalysis.m
Normal file
1372
Data-Analyzer/Deprecated/conductSpectralAnalysis.m
Normal file
File diff suppressed because it is too large
Load Diff
545
Data-Analyzer/Deprecated/extractAutocorrelation.m
Normal file
545
Data-Analyzer/Deprecated/extractAutocorrelation.m
Normal file
@ -0,0 +1,545 @@
|
|||||||
|
%% ===== Settings =====
|
||||||
|
groupList = ["/images/MOT_3D_Camera/in_situ_absorption", "/images/ODT_1_Axis_Camera/in_situ_absorption", ...
|
||||||
|
"/images/ODT_2_Axis_Camera/in_situ_absorption", "/images/Horizontal_Axis_Camera/in_situ_absorption", ...
|
||||||
|
"/images/Vertical_Axis_Camera/in_situ_absorption"];
|
||||||
|
|
||||||
|
folderPath = "//DyLabNAS/Data/TwoDGas/2025/06/23/";
|
||||||
|
|
||||||
|
run = '0300';
|
||||||
|
|
||||||
|
folderPath = strcat(folderPath, run);
|
||||||
|
|
||||||
|
cam = 5;
|
||||||
|
|
||||||
|
angle = 0;
|
||||||
|
center = [1410, 2030];
|
||||||
|
span = [200, 200];
|
||||||
|
fraction = [0.1, 0.1];
|
||||||
|
|
||||||
|
pixel_size = 5.86e-6; % in meters
|
||||||
|
magnification = 23.94;
|
||||||
|
removeFringes = false;
|
||||||
|
|
||||||
|
ImagingMode = 'HighIntensity';
|
||||||
|
PulseDuration = 5e-6; % in s
|
||||||
|
|
||||||
|
% Fourier analysis settings
|
||||||
|
|
||||||
|
% Radial Spectral Distribution
|
||||||
|
theta_min = deg2rad(0);
|
||||||
|
theta_max = deg2rad(180);
|
||||||
|
N_radial_bins = 500;
|
||||||
|
Radial_Sigma = 2;
|
||||||
|
Radial_WindowSize = 5; % Choose an odd number for a centered moving average
|
||||||
|
|
||||||
|
% Angular Spectral Distribution
|
||||||
|
r_min = 10;
|
||||||
|
r_max = 20;
|
||||||
|
N_angular_bins = 180;
|
||||||
|
Angular_Threshold = 75;
|
||||||
|
Angular_Sigma = 2;
|
||||||
|
Angular_WindowSize = 5;
|
||||||
|
|
||||||
|
zoom_size = 50; % Zoomed-in region around center
|
||||||
|
|
||||||
|
% Plotting and saving
|
||||||
|
scan_parameter = 'ps_rot_mag_fin_pol_angle';
|
||||||
|
% scan_parameter = 'rot_mag_field';
|
||||||
|
scan_parameter_text = 'Angle = ';
|
||||||
|
% scan_parameter_text = 'BField = ';
|
||||||
|
|
||||||
|
savefileName = 'DropletsToStripes';
|
||||||
|
font = 'Bahnschrift';
|
||||||
|
|
||||||
|
if strcmp(savefileName, 'DropletsToStripes')
|
||||||
|
scan_groups = 0:5:45;
|
||||||
|
titleString = 'Droplets to Stripes';
|
||||||
|
elseif strcmp(savefileName, 'StripesToDroplets')
|
||||||
|
scan_groups = 45:-5:0;
|
||||||
|
titleString = 'Stripes to Droplets';
|
||||||
|
end
|
||||||
|
|
||||||
|
% Flags
|
||||||
|
skipUnshuffling = true;
|
||||||
|
skipPreprocessing = true;
|
||||||
|
skipMasking = true;
|
||||||
|
skipIntensityThresholding = true;
|
||||||
|
skipBinarization = true;
|
||||||
|
skipMovieRender = true;
|
||||||
|
skipSaveFigures = true;
|
||||||
|
|
||||||
|
%% ===== Load and compute OD image, rotate and extract ROI for analysis =====
|
||||||
|
% Get a list of all files in the folder with the desired file name pattern.
|
||||||
|
|
||||||
|
filePattern = fullfile(folderPath, '*.h5');
|
||||||
|
files = dir(filePattern);
|
||||||
|
refimages = zeros(span(1) + 1, span(2) + 1, length(files));
|
||||||
|
absimages = zeros(span(1) + 1, span(2) + 1, length(files));
|
||||||
|
|
||||||
|
for k = 1 : length(files)
|
||||||
|
baseFileName = files(k).name;
|
||||||
|
fullFileName = fullfile(files(k).folder, baseFileName);
|
||||||
|
|
||||||
|
fprintf(1, 'Now reading %s\n', fullFileName);
|
||||||
|
|
||||||
|
atm_img = double(imrotate(h5read(fullFileName, append(groupList(cam), "/atoms")), angle));
|
||||||
|
bkg_img = double(imrotate(h5read(fullFileName, append(groupList(cam), "/background")), angle));
|
||||||
|
dark_img = double(imrotate(h5read(fullFileName, append(groupList(cam), "/dark")), angle));
|
||||||
|
|
||||||
|
if (isempty(atm_img) && isa(atm_img, 'double')) || ...
|
||||||
|
(isempty(bkg_img) && isa(bkg_img, 'double')) || ...
|
||||||
|
(isempty(dark_img) && isa(dark_img, 'double'))
|
||||||
|
|
||||||
|
refimages(:,:,k) = nan(size(refimages(:,:,k))); % fill with NaNs
|
||||||
|
absimages(:,:,k) = nan(size(absimages(:,:,k)));
|
||||||
|
else
|
||||||
|
refimages(:,:,k) = subtractBackgroundOffset(cropODImage(bkg_img, center, span), fraction)';
|
||||||
|
absimages(:,:,k) = subtractBackgroundOffset(cropODImage(calculateODImage(atm_img, bkg_img, dark_img, ImagingMode, PulseDuration), center, span), fraction)';
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
%% ===== Fringe removal =====
|
||||||
|
|
||||||
|
if removeFringes
|
||||||
|
optrefimages = removefringesInImage(absimages, refimages);
|
||||||
|
absimages_fringe_removed = absimages(:, :, :) - optrefimages(:, :, :);
|
||||||
|
|
||||||
|
nimgs = size(absimages_fringe_removed,3);
|
||||||
|
od_imgs = cell(1, nimgs);
|
||||||
|
for i = 1:nimgs
|
||||||
|
od_imgs{i} = absimages_fringe_removed(:, :, i);
|
||||||
|
end
|
||||||
|
else
|
||||||
|
nimgs = size(absimages(:, :, :),3);
|
||||||
|
od_imgs = cell(1, nimgs);
|
||||||
|
for i = 1:nimgs
|
||||||
|
od_imgs{i} = absimages(:, :, i);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
%% ===== Get rotation angles =====
|
||||||
|
scan_parameter_values = zeros(1, length(files));
|
||||||
|
|
||||||
|
% Get information about the '/globals' group
|
||||||
|
for k = 1 : length(files)
|
||||||
|
baseFileName = files(k).name;
|
||||||
|
fullFileName = fullfile(files(k).folder, baseFileName);
|
||||||
|
info = h5info(fullFileName, '/globals');
|
||||||
|
for i = 1:length(info.Attributes)
|
||||||
|
if strcmp(info.Attributes(i).Name, scan_parameter)
|
||||||
|
if strcmp(scan_parameter, 'ps_rot_mag_fin_pol_angle')
|
||||||
|
scan_parameter_values(k) = 180 - info.Attributes(i).Value;
|
||||||
|
else
|
||||||
|
scan_parameter_values(k) = info.Attributes(i).Value;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
%% ===== Extract g2 from experiment data =====
|
||||||
|
|
||||||
|
fft_imgs = cell(1, nimgs);
|
||||||
|
spectral_distribution = cell(1, nimgs);
|
||||||
|
theta_values = cell(1, nimgs);
|
||||||
|
|
||||||
|
N_shots = length(od_imgs);
|
||||||
|
|
||||||
|
% Compute FFT
|
||||||
|
for k = 1:N_shots
|
||||||
|
IMG = od_imgs{k};
|
||||||
|
[IMGFFT, IMGPR] = computeFourierTransform(IMG, skipPreprocessing, skipMasking, skipIntensityThresholding, skipBinarization);
|
||||||
|
|
||||||
|
% Size of original image (in pixels)
|
||||||
|
[Ny, Nx] = size(IMG);
|
||||||
|
|
||||||
|
% Real-space pixel size in micrometers after magnification
|
||||||
|
dx = pixel_size / magnification;
|
||||||
|
dy = dx; % assuming square pixels
|
||||||
|
|
||||||
|
% Real-space axes
|
||||||
|
x = ((1:Nx) - ceil(Nx/2)) * dx * 1E6;
|
||||||
|
y = ((1:Ny) - ceil(Ny/2)) * dy * 1E6;
|
||||||
|
|
||||||
|
% Reciprocal space increments (frequency domain, μm⁻¹)
|
||||||
|
dvx = 1 / (Nx * dx);
|
||||||
|
dvy = 1 / (Ny * dy);
|
||||||
|
|
||||||
|
% Frequency axes
|
||||||
|
vx = (-floor(Nx/2):ceil(Nx/2)-1) * dvx;
|
||||||
|
vy = (-floor(Ny/2):ceil(Ny/2)-1) * dvy;
|
||||||
|
|
||||||
|
% Wavenumber axes
|
||||||
|
kx_full = 2 * pi * vx * 1E-6; % μm⁻¹
|
||||||
|
ky_full = 2 * pi * vy * 1E-6;
|
||||||
|
|
||||||
|
% Crop FFT image around center
|
||||||
|
mid_x = floor(Nx/2);
|
||||||
|
mid_y = floor(Ny/2);
|
||||||
|
fft_imgs{k} = IMGFFT(mid_y-zoom_size:mid_y+zoom_size, mid_x-zoom_size:mid_x+zoom_size);
|
||||||
|
|
||||||
|
% Crop wavenumber axes to match fft_imgs{k}
|
||||||
|
kx = kx_full(mid_x - zoom_size : mid_x + zoom_size);
|
||||||
|
ky = ky_full(mid_y - zoom_size : mid_y + zoom_size);
|
||||||
|
|
||||||
|
[theta_values, S_theta] = computeAngularSpectralDistribution(fft_imgs{k}, r_min, r_max, N_angular_bins, Angular_Threshold, Angular_Sigma, []);
|
||||||
|
spectral_distribution{k} = S_theta;
|
||||||
|
end
|
||||||
|
|
||||||
|
% Create matrix of shape (N_shots x N_angular_bins)
|
||||||
|
delta_nkr_all = zeros(N_shots, N_angular_bins);
|
||||||
|
for k = 1:N_shots
|
||||||
|
delta_nkr_all(k, :) = spectral_distribution{k};
|
||||||
|
end
|
||||||
|
|
||||||
|
% Grouping by scan parameter value (e.g., alpha)
|
||||||
|
[unique_scan_parameter_values, ~, idx] = unique(scan_parameter_values);
|
||||||
|
|
||||||
|
% Number of unique parameter values
|
||||||
|
N_params = length(unique_scan_parameter_values);
|
||||||
|
|
||||||
|
% Preallocate result arrays
|
||||||
|
g2_all = zeros(N_params, N_angular_bins);
|
||||||
|
g2_error_all = zeros(N_params, N_angular_bins);
|
||||||
|
|
||||||
|
% Compute g2
|
||||||
|
for i = 1:N_params
|
||||||
|
group_idx = find(idx == i);
|
||||||
|
group_data = delta_nkr_all(group_idx, :);
|
||||||
|
|
||||||
|
for dtheta = 0:N_angular_bins-1
|
||||||
|
temp = zeros(length(group_idx), 1);
|
||||||
|
for j = 1:length(group_idx)
|
||||||
|
profile = group_data(j, :);
|
||||||
|
profile_shifted = circshift(profile, -dtheta, 2);
|
||||||
|
|
||||||
|
num = mean(profile .* profile_shifted);
|
||||||
|
denom = mean(profile.^2);
|
||||||
|
|
||||||
|
temp(j) = num / denom;
|
||||||
|
end
|
||||||
|
g2_all(i, dtheta+1) = mean(temp, 'omitnan');
|
||||||
|
g2_error_all(i, dtheta+1) = std(temp, 'omitnan') / sqrt(length(group_idx)); % Standard error
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
% Number of unique parameter values
|
||||||
|
nParams = size(g2_all, 1);
|
||||||
|
|
||||||
|
% Generate a colormap with enough unique colors
|
||||||
|
cmap = sky(nParams); % You can also try 'jet', 'turbo', 'hot', etc.
|
||||||
|
|
||||||
|
figure(1);
|
||||||
|
clf;
|
||||||
|
set(gcf, 'Color', 'w', 'Position',[100 100 950 750])
|
||||||
|
hold on;
|
||||||
|
legend_entries = cell(nParams, 1);
|
||||||
|
|
||||||
|
for i = 1:nParams
|
||||||
|
errorbar(theta_values/pi, g2_all(i, :), g2_error_all(i, :), ...
|
||||||
|
'o', 'Color', cmap(i,:), ...
|
||||||
|
'MarkerSize', 3, 'MarkerFaceColor', cmap(i,:), ...
|
||||||
|
'CapSize', 4);
|
||||||
|
if strcmp(scan_parameter, 'ps_rot_mag_fin_pol_angle')
|
||||||
|
legend_entries{i} = sprintf('$\\alpha = %g^\\circ$', unique_scan_parameter_values(i));
|
||||||
|
elseif strcmp(scan_parameter, 'rot_mag_field')
|
||||||
|
legend_entries{i} = sprintf('B = %.2f G', unique_scan_parameter_values(i));
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
ylim([0.0 1.0]); % Set y-axis limits here
|
||||||
|
set(gca, 'FontSize', 14);
|
||||||
|
hXLabel = xlabel('$\delta\theta / \pi$', 'Interpreter', 'latex');
|
||||||
|
hYLabel = ylabel('$g^{(2)}(\delta\theta)$', 'Interpreter', 'latex');
|
||||||
|
hTitle = title(titleString, 'Interpreter', 'tex');
|
||||||
|
legend(legend_entries, 'Interpreter', 'latex', 'Location', 'bestoutside');
|
||||||
|
set([hXLabel, hYLabel], 'FontName', font)
|
||||||
|
set([hXLabel, hYLabel], 'FontSize', 14)
|
||||||
|
set(hTitle, 'FontName', font, 'FontSize', 16, 'FontWeight', 'bold'); % Set font and size for title
|
||||||
|
grid on;
|
||||||
|
|
||||||
|
%% Helper Functions
|
||||||
|
function [IMGFFT, IMGPR] = computeFourierTransform(I, skipPreprocessing, skipMasking, skipIntensityThresholding, skipBinarization)
|
||||||
|
% computeFourierSpectrum - Computes the 2D Fourier power spectrum
|
||||||
|
% of binarized and enhanced lattice image features, with optional central mask.
|
||||||
|
%
|
||||||
|
% Inputs:
|
||||||
|
% I - Grayscale or RGB image matrix
|
||||||
|
%
|
||||||
|
% Output:
|
||||||
|
% F_mag - 2D Fourier power spectrum (shifted)
|
||||||
|
|
||||||
|
if ~skipPreprocessing
|
||||||
|
% Preprocessing: Denoise
|
||||||
|
filtered = imgaussfilt(I, 10);
|
||||||
|
IMGPR = I - filtered; % adjust sigma as needed
|
||||||
|
else
|
||||||
|
IMGPR = I;
|
||||||
|
end
|
||||||
|
|
||||||
|
if ~skipMasking
|
||||||
|
[rows, cols] = size(IMGPR);
|
||||||
|
[X, Y] = meshgrid(1:cols, 1:rows);
|
||||||
|
% Elliptical mask parameters
|
||||||
|
cx = cols / 2;
|
||||||
|
cy = rows / 2;
|
||||||
|
|
||||||
|
% Shifted coordinates
|
||||||
|
x = X - cx;
|
||||||
|
y = Y - cy;
|
||||||
|
|
||||||
|
% Ellipse semi-axes
|
||||||
|
rx = 0.4 * cols;
|
||||||
|
ry = 0.2 * rows;
|
||||||
|
|
||||||
|
% Rotation angle in degrees -> radians
|
||||||
|
theta_deg = 30; % Adjust as needed
|
||||||
|
theta = deg2rad(theta_deg);
|
||||||
|
|
||||||
|
% Rotated ellipse equation
|
||||||
|
cos_t = cos(theta);
|
||||||
|
sin_t = sin(theta);
|
||||||
|
|
||||||
|
x_rot = (x * cos_t + y * sin_t);
|
||||||
|
y_rot = (-x * sin_t + y * cos_t);
|
||||||
|
|
||||||
|
ellipseMask = (x_rot.^2) / rx^2 + (y_rot.^2) / ry^2 <= 1;
|
||||||
|
|
||||||
|
% Apply cutout mask
|
||||||
|
IMGPR = IMGPR .* ellipseMask;
|
||||||
|
end
|
||||||
|
|
||||||
|
if ~skipIntensityThresholding
|
||||||
|
% Apply global intensity threshold mask
|
||||||
|
intensity_thresh = 0.20;
|
||||||
|
intensity_mask = IMGPR > intensity_thresh;
|
||||||
|
IMGPR = IMGPR .* intensity_mask;
|
||||||
|
end
|
||||||
|
|
||||||
|
if ~skipBinarization
|
||||||
|
% Adaptive binarization and cleanup
|
||||||
|
IMGPR = imbinarize(IMGPR, 'adaptive', 'Sensitivity', 0.0);
|
||||||
|
IMGPR = imdilate(IMGPR, strel('disk', 2));
|
||||||
|
IMGPR = imerode(IMGPR, strel('disk', 1));
|
||||||
|
IMGPR = imfill(IMGPR, 'holes');
|
||||||
|
F = fft2(double(IMGPR)); % Compute 2D Fourier Transform
|
||||||
|
IMGFFT = abs(fftshift(F))'; % Shift zero frequency to center
|
||||||
|
else
|
||||||
|
F = fft2(double(IMGPR)); % Compute 2D Fourier Transform
|
||||||
|
IMGFFT = abs(fftshift(F))'; % Shift zero frequency to center
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function [theta_vals, S_theta] = computeAngularSpectralDistribution(IMGFFT, r_min, r_max, num_bins, threshold, sigma, windowSize)
|
||||||
|
% Apply threshold to isolate strong peaks
|
||||||
|
IMGFFT(IMGFFT < threshold) = 0;
|
||||||
|
|
||||||
|
% Prepare polar coordinates
|
||||||
|
[ny, nx] = size(IMGFFT);
|
||||||
|
[X, Y] = meshgrid(1:nx, 1:ny);
|
||||||
|
cx = ceil(nx/2);
|
||||||
|
cy = ceil(ny/2);
|
||||||
|
R = sqrt((X - cx).^2 + (Y - cy).^2);
|
||||||
|
Theta = atan2(Y - cy, X - cx); % range [-pi, pi]
|
||||||
|
|
||||||
|
% Choose radial band
|
||||||
|
radial_mask = (R >= r_min) & (R <= r_max);
|
||||||
|
|
||||||
|
% Initialize angular structure factor
|
||||||
|
S_theta = zeros(1, num_bins);
|
||||||
|
theta_vals = linspace(0, pi, num_bins);
|
||||||
|
|
||||||
|
% Loop through angle bins
|
||||||
|
for i = 1:num_bins
|
||||||
|
angle_start = (i-1) * pi / num_bins;
|
||||||
|
angle_end = i * pi / num_bins;
|
||||||
|
angle_mask = (Theta >= angle_start & Theta < angle_end);
|
||||||
|
bin_mask = radial_mask & angle_mask;
|
||||||
|
fft_angle = IMGFFT .* bin_mask;
|
||||||
|
S_theta(i) = sum(sum(abs(fft_angle).^2));
|
||||||
|
end
|
||||||
|
|
||||||
|
% Smooth using either Gaussian or moving average
|
||||||
|
if exist('sigma', 'var') && ~isempty(sigma)
|
||||||
|
% Gaussian convolution
|
||||||
|
half_width = ceil(3 * sigma);
|
||||||
|
x = -half_width:half_width;
|
||||||
|
gauss_kernel = exp(-x.^2 / (2 * sigma^2));
|
||||||
|
gauss_kernel = gauss_kernel / sum(gauss_kernel);
|
||||||
|
% Circular convolution
|
||||||
|
S_theta = conv([S_theta(end-half_width+1:end), S_theta, S_theta(1:half_width)], ...
|
||||||
|
gauss_kernel, 'same');
|
||||||
|
S_theta = S_theta(half_width+1:end-half_width);
|
||||||
|
elseif exist('windowSize', 'var') && ~isempty(windowSize)
|
||||||
|
% Moving average via convolution (circular)
|
||||||
|
pad = floor(windowSize / 2);
|
||||||
|
kernel = ones(1, windowSize) / windowSize;
|
||||||
|
S_theta = conv([S_theta(end-pad+1:end), S_theta, S_theta(1:pad)], kernel, 'same');
|
||||||
|
S_theta = S_theta(pad+1:end-pad);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function ret = getBkgOffsetFromCorners(img, x_fraction, y_fraction)
|
||||||
|
% image must be a 2D numerical array
|
||||||
|
[dim1, dim2] = size(img);
|
||||||
|
|
||||||
|
s1 = img(1:round(dim1 * y_fraction), 1:round(dim2 * x_fraction));
|
||||||
|
s2 = img(1:round(dim1 * y_fraction), round(dim2 - dim2 * x_fraction):dim2);
|
||||||
|
s3 = img(round(dim1 - dim1 * y_fraction):dim1, 1:round(dim2 * x_fraction));
|
||||||
|
s4 = img(round(dim1 - dim1 * y_fraction):dim1, round(dim2 - dim2 * x_fraction):dim2);
|
||||||
|
|
||||||
|
ret = mean([mean(s1(:)), mean(s2(:)), mean(s3(:)), mean(s4(:))]);
|
||||||
|
end
|
||||||
|
|
||||||
|
function ret = subtractBackgroundOffset(img, fraction)
|
||||||
|
% Remove the background from the image.
|
||||||
|
% :param dataArray: The image
|
||||||
|
% :type dataArray: xarray DataArray
|
||||||
|
% :param x_fraction: The fraction of the pixels used in x axis
|
||||||
|
% :type x_fraction: float
|
||||||
|
% :param y_fraction: The fraction of the pixels used in y axis
|
||||||
|
% :type y_fraction: float
|
||||||
|
% :return: The image after removing background
|
||||||
|
% :rtype: xarray DataArray
|
||||||
|
|
||||||
|
x_fraction = fraction(1);
|
||||||
|
y_fraction = fraction(2);
|
||||||
|
offset = getBkgOffsetFromCorners(img, x_fraction, y_fraction);
|
||||||
|
ret = img - offset;
|
||||||
|
end
|
||||||
|
|
||||||
|
function ret = cropODImage(img, center, span)
|
||||||
|
% Crop the image according to the region of interest (ROI).
|
||||||
|
% :param dataSet: The images
|
||||||
|
% :type dataSet: xarray DataArray or DataSet
|
||||||
|
% :param center: The center of region of interest (ROI)
|
||||||
|
% :type center: tuple
|
||||||
|
% :param span: The span of region of interest (ROI)
|
||||||
|
% :type span: tuple
|
||||||
|
% :return: The cropped images
|
||||||
|
% :rtype: xarray DataArray or DataSet
|
||||||
|
|
||||||
|
x_start = floor(center(1) - span(1) / 2);
|
||||||
|
x_end = floor(center(1) + span(1) / 2);
|
||||||
|
y_start = floor(center(2) - span(2) / 2);
|
||||||
|
y_end = floor(center(2) + span(2) / 2);
|
||||||
|
|
||||||
|
ret = img(y_start:y_end, x_start:x_end);
|
||||||
|
end
|
||||||
|
|
||||||
|
function imageOD = calculateODImage(imageAtom, imageBackground, imageDark, mode, exposureTime)
|
||||||
|
%CALCULATEODIMAGE Calculates the optical density (OD) image for absorption imaging.
|
||||||
|
%
|
||||||
|
% imageOD = calculateODImage(imageAtom, imageBackground, imageDark, mode, exposureTime)
|
||||||
|
%
|
||||||
|
% Inputs:
|
||||||
|
% imageAtom - Image with atoms
|
||||||
|
% imageBackground - Image without atoms
|
||||||
|
% imageDark - Image without light
|
||||||
|
% mode - 'LowIntensity' (default) or 'HighIntensity'
|
||||||
|
% exposureTime - Required only for 'HighIntensity' [in seconds]
|
||||||
|
%
|
||||||
|
% Output:
|
||||||
|
% imageOD - Computed OD image
|
||||||
|
%
|
||||||
|
|
||||||
|
arguments
|
||||||
|
imageAtom (:,:) {mustBeNumeric}
|
||||||
|
imageBackground (:,:) {mustBeNumeric}
|
||||||
|
imageDark (:,:) {mustBeNumeric}
|
||||||
|
mode char {mustBeMember(mode, {'LowIntensity', 'HighIntensity'})} = 'LowIntensity'
|
||||||
|
exposureTime double = NaN
|
||||||
|
end
|
||||||
|
|
||||||
|
% Compute numerator and denominator
|
||||||
|
numerator = imageBackground - imageDark;
|
||||||
|
denominator = imageAtom - imageDark;
|
||||||
|
|
||||||
|
% Avoid division by zero
|
||||||
|
numerator(numerator == 0) = 1;
|
||||||
|
denominator(denominator == 0) = 1;
|
||||||
|
|
||||||
|
% Calculate OD based on mode
|
||||||
|
switch mode
|
||||||
|
case 'LowIntensity'
|
||||||
|
imageOD = -log(abs(denominator ./ numerator));
|
||||||
|
|
||||||
|
case 'HighIntensity'
|
||||||
|
if isnan(exposureTime)
|
||||||
|
error('Exposure time must be provided for HighIntensity mode.');
|
||||||
|
end
|
||||||
|
imageOD = abs(denominator ./ numerator);
|
||||||
|
imageOD = -log(imageOD) + (numerator - denominator) ./ (7000 * (exposureTime / 5e-6));
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
function [optrefimages] = removefringesInImage(absimages, refimages, bgmask)
|
||||||
|
% removefringesInImage - Fringe removal and noise reduction from absorption images.
|
||||||
|
% Creates an optimal reference image for each absorption image in a set as
|
||||||
|
% a linear combination of reference images, with coefficients chosen to
|
||||||
|
% minimize the least-squares residuals between each absorption image and
|
||||||
|
% the optimal reference image. The coefficients are obtained by solving a
|
||||||
|
% linear set of equations using matrix inverse by LU decomposition.
|
||||||
|
%
|
||||||
|
% Application of the algorithm is described in C. F. Ockeloen et al, Improved
|
||||||
|
% detection of small atom numbers through image processing, arXiv:1007.2136 (2010).
|
||||||
|
%
|
||||||
|
% Syntax:
|
||||||
|
% [optrefimages] = removefringesInImage(absimages,refimages,bgmask);
|
||||||
|
%
|
||||||
|
% Required inputs:
|
||||||
|
% absimages - Absorption image data,
|
||||||
|
% typically 16 bit grayscale images
|
||||||
|
% refimages - Raw reference image data
|
||||||
|
% absimages and refimages are both cell arrays containing
|
||||||
|
% 2D array data. The number of refimages can differ from the
|
||||||
|
% number of absimages.
|
||||||
|
%
|
||||||
|
% Optional inputs:
|
||||||
|
% bgmask - Array specifying background region used,
|
||||||
|
% 1=background, 0=data. Defaults to all ones.
|
||||||
|
% Outputs:
|
||||||
|
% optrefimages - Cell array of optimal reference images,
|
||||||
|
% equal in size to absimages.
|
||||||
|
%
|
||||||
|
|
||||||
|
% Dependencies: none
|
||||||
|
%
|
||||||
|
% Authors: Shannon Whitlock, Caspar Ockeloen
|
||||||
|
% Reference: C. F. Ockeloen, A. F. Tauschinsky, R. J. C. Spreeuw, and
|
||||||
|
% S. Whitlock, Improved detection of small atom numbers through
|
||||||
|
% image processing, arXiv:1007.2136
|
||||||
|
% Email:
|
||||||
|
% May 2009; Last revision: 11 August 2010
|
||||||
|
|
||||||
|
% Process inputs
|
||||||
|
|
||||||
|
% Set variables, and flatten absorption and reference images
|
||||||
|
nimgs = size(absimages,3);
|
||||||
|
nimgsR = size(refimages,3);
|
||||||
|
xdim = size(absimages(:,:,1),2);
|
||||||
|
ydim = size(absimages(:,:,1),1);
|
||||||
|
|
||||||
|
R = single(reshape(refimages,xdim*ydim,nimgsR));
|
||||||
|
A = single(reshape(absimages,xdim*ydim,nimgs));
|
||||||
|
optrefimages=zeros(size(absimages)); % preallocate
|
||||||
|
|
||||||
|
if not(exist('bgmask','var')); bgmask=ones(ydim,xdim); end
|
||||||
|
k = find(bgmask(:)==1); % Index k specifying background region
|
||||||
|
|
||||||
|
% Ensure there are no duplicate reference images
|
||||||
|
% R=unique(R','rows')'; % comment this line if you run out of memory
|
||||||
|
|
||||||
|
% Decompose B = R*R' using singular value or LU decomposition
|
||||||
|
[L,U,p] = lu(R(k,:)'*R(k,:),'vector'); % LU decomposition
|
||||||
|
|
||||||
|
for j=1:nimgs
|
||||||
|
b=R(k,:)'*A(k,j);
|
||||||
|
% Obtain coefficients c which minimise least-square residuals
|
||||||
|
lower.LT = true; upper.UT = true;
|
||||||
|
c = linsolve(U,linsolve(L,b(p,:),lower),upper);
|
||||||
|
|
||||||
|
% Compute optimised reference image
|
||||||
|
optrefimages(:,:,j)=reshape(R*c,[ydim xdim]);
|
||||||
|
end
|
||||||
|
end
|
738
Data-Analyzer/Deprecated/extractCustomCorrelation.m
Normal file
738
Data-Analyzer/Deprecated/extractCustomCorrelation.m
Normal file
@ -0,0 +1,738 @@
|
|||||||
|
%% ===== D-S Settings =====
|
||||||
|
groupList = ["/images/MOT_3D_Camera/in_situ_absorption", "/images/ODT_1_Axis_Camera/in_situ_absorption", ...
|
||||||
|
"/images/ODT_2_Axis_Camera/in_situ_absorption", "/images/Horizontal_Axis_Camera/in_situ_absorption", ...
|
||||||
|
"/images/Vertical_Axis_Camera/in_situ_absorption"];
|
||||||
|
|
||||||
|
folderPath = "//DyLabNAS/Data/TwoDGas/2025/07/22/";
|
||||||
|
|
||||||
|
run = '0021';
|
||||||
|
|
||||||
|
folderPath = strcat(folderPath, run);
|
||||||
|
|
||||||
|
cam = 5;
|
||||||
|
|
||||||
|
angle = 0;
|
||||||
|
center = [1410, 2030];
|
||||||
|
span = [200, 200];
|
||||||
|
fraction = [0.1, 0.1];
|
||||||
|
|
||||||
|
pixel_size = 5.86e-6; % in meters
|
||||||
|
magnification = 23.94;
|
||||||
|
removeFringes = false;
|
||||||
|
|
||||||
|
ImagingMode = 'HighIntensity';
|
||||||
|
PulseDuration = 5e-6; % in s
|
||||||
|
|
||||||
|
% Fourier analysis settings
|
||||||
|
|
||||||
|
% Radial Spectral Distribution
|
||||||
|
theta_min = deg2rad(0);
|
||||||
|
theta_max = deg2rad(180);
|
||||||
|
N_radial_bins = 500;
|
||||||
|
Radial_Sigma = 2;
|
||||||
|
Radial_WindowSize = 5; % Choose an odd number for a centered moving average
|
||||||
|
|
||||||
|
% Angular Spectral Distribution
|
||||||
|
r_min = 10;
|
||||||
|
r_max = 20;
|
||||||
|
N_angular_bins = 180;
|
||||||
|
Angular_Threshold = 75;
|
||||||
|
Angular_Sigma = 2;
|
||||||
|
Angular_WindowSize = 5;
|
||||||
|
|
||||||
|
zoom_size = 50; % Zoomed-in region around center
|
||||||
|
|
||||||
|
% Plotting and saving
|
||||||
|
scan_parameter = 'ps_rot_mag_fin_pol_angle';
|
||||||
|
% scan_parameter = 'rot_mag_field';
|
||||||
|
|
||||||
|
savefileName = 'DropletsToStripes';
|
||||||
|
font = 'Bahnschrift';
|
||||||
|
|
||||||
|
if strcmp(savefileName, 'DropletsToStripes')
|
||||||
|
scan_groups = 0:1:40;
|
||||||
|
titleString = 'Droplets to Stripes';
|
||||||
|
elseif strcmp(savefileName, 'StripesToDroplets')
|
||||||
|
scan_groups = 40:-1:0;
|
||||||
|
titleString = 'Stripes to Droplets';
|
||||||
|
end
|
||||||
|
|
||||||
|
% Flags
|
||||||
|
skipNormalization = true;
|
||||||
|
skipUnshuffling = false;
|
||||||
|
skipPreprocessing = true;
|
||||||
|
skipMasking = true;
|
||||||
|
skipIntensityThresholding = true;
|
||||||
|
skipBinarization = true;
|
||||||
|
skipMovieRender = true;
|
||||||
|
skipSaveFigures = false;
|
||||||
|
skipSaveOD = true;
|
||||||
|
|
||||||
|
%% ===== Load and compute OD image, rotate and extract ROI for analysis =====
|
||||||
|
% Get a list of all files in the folder with the desired file name pattern.
|
||||||
|
|
||||||
|
filePattern = fullfile(folderPath, '*.h5');
|
||||||
|
files = dir(filePattern);
|
||||||
|
refimages = zeros(span(1) + 1, span(2) + 1, length(files));
|
||||||
|
absimages = zeros(span(1) + 1, span(2) + 1, length(files));
|
||||||
|
|
||||||
|
for k = 1 : length(files)
|
||||||
|
baseFileName = files(k).name;
|
||||||
|
fullFileName = fullfile(files(k).folder, baseFileName);
|
||||||
|
|
||||||
|
fprintf(1, 'Now reading %s\n', fullFileName);
|
||||||
|
|
||||||
|
atm_img = double(imrotate(h5read(fullFileName, append(groupList(cam), "/atoms")), angle));
|
||||||
|
bkg_img = double(imrotate(h5read(fullFileName, append(groupList(cam), "/background")), angle));
|
||||||
|
dark_img = double(imrotate(h5read(fullFileName, append(groupList(cam), "/dark")), angle));
|
||||||
|
|
||||||
|
if (isempty(atm_img) && isa(atm_img, 'double')) || ...
|
||||||
|
(isempty(bkg_img) && isa(bkg_img, 'double')) || ...
|
||||||
|
(isempty(dark_img) && isa(dark_img, 'double'))
|
||||||
|
|
||||||
|
refimages(:,:,k) = nan(size(refimages(:,:,k))); % fill with NaNs
|
||||||
|
absimages(:,:,k) = nan(size(absimages(:,:,k)));
|
||||||
|
else
|
||||||
|
refimages(:,:,k) = subtractBackgroundOffset(cropODImage(bkg_img, center, span), fraction)';
|
||||||
|
absimages(:,:,k) = subtractBackgroundOffset(cropODImage(calculateODImage(atm_img, bkg_img, dark_img, ImagingMode, PulseDuration), center, span), fraction)';
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
% ===== Fringe removal =====
|
||||||
|
|
||||||
|
if removeFringes
|
||||||
|
optrefimages = removefringesInImage(absimages, refimages);
|
||||||
|
absimages_fringe_removed = absimages(:, :, :) - optrefimages(:, :, :);
|
||||||
|
|
||||||
|
nimgs = size(absimages_fringe_removed,3);
|
||||||
|
od_imgs = cell(1, nimgs);
|
||||||
|
for i = 1:nimgs
|
||||||
|
od_imgs{i} = absimages_fringe_removed(:, :, i);
|
||||||
|
end
|
||||||
|
else
|
||||||
|
nimgs = size(absimages(:, :, :),3);
|
||||||
|
od_imgs = cell(1, nimgs);
|
||||||
|
for i = 1:nimgs
|
||||||
|
od_imgs{i} = absimages(:, :, i);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
%% ===== Get rotation angles =====
|
||||||
|
scan_parameter_values = zeros(1, length(files));
|
||||||
|
|
||||||
|
% Get information about the '/globals' group
|
||||||
|
for k = 1 : length(files)
|
||||||
|
baseFileName = files(k).name;
|
||||||
|
fullFileName = fullfile(files(k).folder, baseFileName);
|
||||||
|
info = h5info(fullFileName, '/globals');
|
||||||
|
for i = 1:length(info.Attributes)
|
||||||
|
if strcmp(info.Attributes(i).Name, scan_parameter)
|
||||||
|
if strcmp(scan_parameter, 'ps_rot_mag_fin_pol_angle')
|
||||||
|
scan_parameter_values(k) = 180 - info.Attributes(i).Value;
|
||||||
|
else
|
||||||
|
scan_parameter_values(k) = info.Attributes(i).Value;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
%% ===== Correlation of a single (highest) peak with a possible peak between 50-70 degrees from experiment data =====
|
||||||
|
|
||||||
|
fft_imgs = cell(1, nimgs);
|
||||||
|
spectral_distribution = cell(1, nimgs);
|
||||||
|
theta_values = cell(1, nimgs);
|
||||||
|
|
||||||
|
N_shots = length(od_imgs);
|
||||||
|
|
||||||
|
% Compute FFT for all images
|
||||||
|
for k = 1:N_shots
|
||||||
|
IMG = od_imgs{k};
|
||||||
|
[IMGFFT, IMGPR] = computeFourierTransform(IMG, skipPreprocessing, skipMasking, skipIntensityThresholding, skipBinarization);
|
||||||
|
|
||||||
|
[Ny, Nx] = size(IMG);
|
||||||
|
dx = pixel_size / magnification;
|
||||||
|
dy = dx; % assuming square pixels
|
||||||
|
|
||||||
|
x = ((1:Nx) - ceil(Nx/2)) * dx * 1E6;
|
||||||
|
y = ((1:Ny) - ceil(Ny/2)) * dy * 1E6;
|
||||||
|
|
||||||
|
dvx = 1 / (Nx * dx);
|
||||||
|
dvy = 1 / (Ny * dy);
|
||||||
|
vx = (-floor(Nx/2):ceil(Nx/2)-1) * dvx;
|
||||||
|
vy = (-floor(Ny/2):ceil(Ny/2)-1) * dvy;
|
||||||
|
|
||||||
|
kx_full = 2 * pi * vx * 1E-6;
|
||||||
|
ky_full = 2 * pi * vy * 1E-6;
|
||||||
|
|
||||||
|
mid_x = floor(Nx/2);
|
||||||
|
mid_y = floor(Ny/2);
|
||||||
|
fft_imgs{k} = IMGFFT(mid_y-zoom_size:mid_y+zoom_size, mid_x-zoom_size:mid_x+zoom_size);
|
||||||
|
|
||||||
|
kx = kx_full(mid_x - zoom_size : mid_x + zoom_size);
|
||||||
|
ky = ky_full(mid_y - zoom_size : mid_y + zoom_size);
|
||||||
|
|
||||||
|
[theta_vals, S_theta] = computeAngularSpectralDistribution(fft_imgs{k}, r_min, r_max, N_angular_bins, Angular_Threshold, Angular_Sigma, []);
|
||||||
|
spectral_distribution{k} = S_theta;
|
||||||
|
theta_values{k} = theta_vals;
|
||||||
|
end
|
||||||
|
|
||||||
|
% Convert spectral distribution to matrix (N_shots x N_angular_bins)
|
||||||
|
delta_nkr_all = zeros(N_shots, N_angular_bins);
|
||||||
|
for k = 1:N_shots
|
||||||
|
delta_nkr_all(k, :) = spectral_distribution{k};
|
||||||
|
end
|
||||||
|
|
||||||
|
% Group by scan parameter values (e.g., alpha, angle, etc.)
|
||||||
|
[unique_scan_parameter_values, ~, idx] = unique(scan_parameter_values);
|
||||||
|
N_params = length(unique_scan_parameter_values);
|
||||||
|
|
||||||
|
% Define angular range and conversion
|
||||||
|
angle_range = 180;
|
||||||
|
angle_per_bin = angle_range / N_angular_bins;
|
||||||
|
max_peak_angle = 180;
|
||||||
|
max_peak_bin = round(max_peak_angle / angle_per_bin);
|
||||||
|
|
||||||
|
% Parameters for search
|
||||||
|
window_size = 10;
|
||||||
|
angle_threshold = 100;
|
||||||
|
|
||||||
|
% Initialize containers for final results
|
||||||
|
mean_max_g2_values = zeros(1, N_params);
|
||||||
|
skew_max_g2_angle_values = zeros(1, N_params);
|
||||||
|
var_max_g2_values = zeros(1, N_params);
|
||||||
|
fourth_order_cumulant_max_g2_angle_values= zeros(1, N_params);
|
||||||
|
fifth_order_cumulant_max_g2_angle_values = zeros(1, N_params);
|
||||||
|
sixth_order_cumulant_max_g2_angle_values = zeros(1, N_params);
|
||||||
|
|
||||||
|
% Also store raw data per group
|
||||||
|
max_g2_all_per_group = cell(1, N_params);
|
||||||
|
std_error_g2_values = zeros(1, N_params);
|
||||||
|
|
||||||
|
for i = 1:N_params
|
||||||
|
group_idx = find(idx == i);
|
||||||
|
group_data = delta_nkr_all(group_idx, :);
|
||||||
|
N_reps = size(group_data, 1);
|
||||||
|
|
||||||
|
g2_values = zeros(1, N_reps);
|
||||||
|
|
||||||
|
for j = 1:N_reps
|
||||||
|
profile = group_data(j, :);
|
||||||
|
|
||||||
|
% Restrict search to 0–60° for highest peak
|
||||||
|
restricted_profile = profile(1:max_peak_bin);
|
||||||
|
[~, peak_idx_rel] = max(restricted_profile);
|
||||||
|
peak_idx = peak_idx_rel;
|
||||||
|
peak_angle = (peak_idx - 1) * angle_per_bin;
|
||||||
|
|
||||||
|
if peak_angle < angle_threshold
|
||||||
|
offsets = round(50 / angle_per_bin) : round(70 / angle_per_bin);
|
||||||
|
else
|
||||||
|
offsets = -round(70 / angle_per_bin) : -round(50 / angle_per_bin);
|
||||||
|
end
|
||||||
|
|
||||||
|
ref_window = mod((peak_idx - window_size):(peak_idx + window_size) - 1, N_angular_bins) + 1;
|
||||||
|
ref = profile(ref_window);
|
||||||
|
|
||||||
|
correlations = zeros(size(offsets));
|
||||||
|
|
||||||
|
for k = 1:length(offsets)
|
||||||
|
shifted_idx = mod(peak_idx + offsets(k) - 1, N_angular_bins) + 1;
|
||||||
|
sec_window = mod((shifted_idx - window_size):(shifted_idx + window_size) - 1, N_angular_bins) + 1;
|
||||||
|
sec = profile(sec_window);
|
||||||
|
|
||||||
|
num = mean(ref .* sec);
|
||||||
|
denom = mean(ref.^2);
|
||||||
|
g2 = num / denom;
|
||||||
|
|
||||||
|
correlations(k) = g2;
|
||||||
|
end
|
||||||
|
|
||||||
|
[max_corr, max_idx] = max(correlations);
|
||||||
|
g2_values(j) = max_corr;
|
||||||
|
end
|
||||||
|
|
||||||
|
% Store raw values
|
||||||
|
max_g2_all_per_group{i} = g2_values;
|
||||||
|
|
||||||
|
% Compute cumulants
|
||||||
|
kappa = computeCumulants(g2_values(:), 6);
|
||||||
|
|
||||||
|
% Final stats
|
||||||
|
mean_max_g2_values(i) = kappa(1);
|
||||||
|
var_max_g2_values(i) = kappa(2);
|
||||||
|
|
||||||
|
N_eff = sum(~isnan(g2_values));
|
||||||
|
std_error_g2_values(i) = sqrt(kappa(2)) / sqrt(N_eff);
|
||||||
|
|
||||||
|
skew_max_g2_angle_values(i) = kappa(3);
|
||||||
|
fourth_order_cumulant_max_g2_angle_values(i)= kappa(4);
|
||||||
|
fifth_order_cumulant_max_g2_angle_values(i) = kappa(5);
|
||||||
|
sixth_order_cumulant_max_g2_angle_values(i) = kappa(6);
|
||||||
|
end
|
||||||
|
|
||||||
|
%% Plot PDF of order parameter
|
||||||
|
|
||||||
|
if ~skipSaveFigures
|
||||||
|
% Define folder for saving images
|
||||||
|
saveFolder = [savefileName '_SavedFigures'];
|
||||||
|
if ~exist(saveFolder, 'dir')
|
||||||
|
mkdir(saveFolder);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
figure(2); % one persistent figure
|
||||||
|
set(gcf, 'Color', 'w', 'Position', [100 100 950 750])
|
||||||
|
|
||||||
|
for val = scan_groups
|
||||||
|
% Find the index i that matches this scan parameter value
|
||||||
|
i = find(unique_scan_parameter_values == val, 1);
|
||||||
|
|
||||||
|
% Skip if not found (sanity check)
|
||||||
|
if isempty(i)
|
||||||
|
continue;
|
||||||
|
end
|
||||||
|
|
||||||
|
g2_vals = max_g2_all_per_group{i};
|
||||||
|
g2_vals = g2_vals(~isnan(g2_vals));
|
||||||
|
|
||||||
|
if isempty(g2_vals)
|
||||||
|
continue;
|
||||||
|
end
|
||||||
|
|
||||||
|
% KDE estimation
|
||||||
|
[f, xi] = ksdensity(g2_vals, 'NumPoints', 200);
|
||||||
|
|
||||||
|
clf;
|
||||||
|
histogram(g2_vals, 'Normalization', 'pdf', ...
|
||||||
|
'NumBins', 10, ...
|
||||||
|
'FaceAlpha', 0.3, ...
|
||||||
|
'EdgeColor', 'none', ...
|
||||||
|
'FaceColor', [0.3 0.5 0.8]);
|
||||||
|
|
||||||
|
hold on;
|
||||||
|
plot(xi, f, 'LineWidth', 2, 'Color', [0 0.2 0.6]);
|
||||||
|
|
||||||
|
set(gca, 'FontSize', 16);
|
||||||
|
title(sprintf('%s: \\boldmath$\\alpha = %.1f^{\\circ}$', titleString, val), ...
|
||||||
|
'FontSize', 16, 'Interpreter', 'latex');
|
||||||
|
|
||||||
|
xlabel('$\mathrm{max}[g^{(2)}_{[50,70]}(\delta\theta)]$', 'Interpreter', 'latex', 'FontSize', 14);
|
||||||
|
ylabel('PDF', 'FontSize', 14);
|
||||||
|
xlim([0.0, 1.5]);
|
||||||
|
grid on;
|
||||||
|
|
||||||
|
drawnow;
|
||||||
|
|
||||||
|
% ==== Save Figure ====
|
||||||
|
if ~skipSaveFigures
|
||||||
|
% Create a filename for each averaged plot
|
||||||
|
fileNamePNG = fullfile(saveFolder, sprintf('max_g2_analysis_param_%03d.png', val));
|
||||||
|
|
||||||
|
% Save current figure as PNG with high resolution
|
||||||
|
print(gcf, fileNamePNG, '-dpng', '-r300'); % 300 dpi for high quality
|
||||||
|
else
|
||||||
|
pause(0.5)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
%% Plot all cumulants
|
||||||
|
figure(3)
|
||||||
|
set(gcf, 'Color', 'w', 'Position', [100 100 950 750])
|
||||||
|
|
||||||
|
scan_vals = unique_scan_parameter_values;
|
||||||
|
|
||||||
|
% Define font style for consistency
|
||||||
|
axis_fontsize = 14;
|
||||||
|
label_fontsize = 16;
|
||||||
|
title_fontsize = 16;
|
||||||
|
|
||||||
|
% 1. Mean with error bars
|
||||||
|
subplot(3,2,1);
|
||||||
|
errorbar(scan_vals, mean_max_g2_values, std_error_g2_values, 'o-', ...
|
||||||
|
'LineWidth', 1.5, 'MarkerSize', 6);
|
||||||
|
title('Mean of $\mathrm{max}[g^{(2)}_{[50,70]}(\delta\theta)]$', ...
|
||||||
|
'Interpreter', 'latex', 'FontSize', title_fontsize);
|
||||||
|
xlabel('$\alpha$ (degrees)', 'Interpreter', 'latex', ...
|
||||||
|
'FontSize', label_fontsize);
|
||||||
|
ylabel('$\kappa_1$', 'Interpreter', 'latex', ...
|
||||||
|
'FontSize', label_fontsize);
|
||||||
|
set(gca, 'FontSize', axis_fontsize);
|
||||||
|
grid on;
|
||||||
|
|
||||||
|
% 2. Variance
|
||||||
|
subplot(3,2,2);
|
||||||
|
plot(scan_vals, var_max_g2_values, 's-', 'LineWidth', 1.5, 'MarkerSize', 6);
|
||||||
|
title('Variance of $\mathrm{max}[g^{(2)}_{[50,70]}(\delta\theta)]$', ...
|
||||||
|
'Interpreter', 'latex', 'FontSize', title_fontsize);
|
||||||
|
xlabel('$\alpha$ (degrees)', 'Interpreter', 'latex', ...
|
||||||
|
'FontSize', label_fontsize);
|
||||||
|
ylabel('$\kappa_2$', 'Interpreter', 'latex', ...
|
||||||
|
'FontSize', label_fontsize);
|
||||||
|
set(gca, 'FontSize', axis_fontsize);
|
||||||
|
grid on;
|
||||||
|
|
||||||
|
% 3. Skewness
|
||||||
|
subplot(3,2,3);
|
||||||
|
plot(scan_vals, skew_max_g2_angle_values, 'd-', 'LineWidth', 1.5, 'MarkerSize', 6);
|
||||||
|
title('Skewness of $\mathrm{max}[g^{(2)}_{[50,70]}(\delta\theta)]$', ...
|
||||||
|
'Interpreter', 'latex', 'FontSize', title_fontsize);
|
||||||
|
xlabel('$\alpha$ (degrees)', 'Interpreter', 'latex', ...
|
||||||
|
'FontSize', label_fontsize);
|
||||||
|
ylabel('$\kappa_3$', 'Interpreter', 'latex', ...
|
||||||
|
'FontSize', label_fontsize);
|
||||||
|
set(gca, 'FontSize', axis_fontsize);
|
||||||
|
grid on;
|
||||||
|
|
||||||
|
% 4. Binder Cumulant
|
||||||
|
subplot(3,2,4);
|
||||||
|
plot(scan_vals, fourth_order_cumulant_max_g2_angle_values, '^-', 'LineWidth', 1.5, 'MarkerSize', 6);
|
||||||
|
title('Binder Cumulant of $\mathrm{max}[g^{(2)}_{[50,70]}(\delta\theta)]$', ...
|
||||||
|
'Interpreter', 'latex', 'FontSize', title_fontsize);
|
||||||
|
xlabel('$\alpha$ (degrees)', 'Interpreter', 'latex', ...
|
||||||
|
'FontSize', label_fontsize);
|
||||||
|
ylabel('$\kappa_4$', 'Interpreter', 'latex', ...
|
||||||
|
'FontSize', label_fontsize);
|
||||||
|
set(gca, 'FontSize', axis_fontsize);
|
||||||
|
grid on;
|
||||||
|
|
||||||
|
% 5. 5th-order cumulant
|
||||||
|
subplot(3,2,5);
|
||||||
|
plot(scan_vals, fifth_order_cumulant_max_g2_angle_values, 'v-', 'LineWidth', 1.5, 'MarkerSize', 6);
|
||||||
|
title('Fifth-order cumulant of $\mathrm{max}[g^{(2)}_{[50,70]}(\delta\theta)]$', ...
|
||||||
|
'Interpreter', 'latex', 'FontSize', title_fontsize);
|
||||||
|
xlabel('$\alpha$ (degrees)', 'Interpreter', 'latex', ...
|
||||||
|
'FontSize', label_fontsize);
|
||||||
|
ylabel('$\kappa_5$', 'Interpreter', 'latex', ...
|
||||||
|
'FontSize', label_fontsize);
|
||||||
|
set(gca, 'FontSize', axis_fontsize);
|
||||||
|
grid on;
|
||||||
|
|
||||||
|
% 6. 6th-order cumulant
|
||||||
|
subplot(3,2,6);
|
||||||
|
plot(scan_vals, sixth_order_cumulant_max_g2_angle_values, '>-', 'LineWidth', 1.5, 'MarkerSize', 6);
|
||||||
|
title('Sixth-order cumulant of $\mathrm{max}[g^{(2)}_{[50,70]}(\delta\theta)]$', ...
|
||||||
|
'Interpreter', 'latex', 'FontSize', title_fontsize);
|
||||||
|
xlabel('$\alpha$ (degrees)', 'Interpreter', 'latex', ...
|
||||||
|
'FontSize', label_fontsize);
|
||||||
|
ylabel('$\kappa_6$', 'Interpreter', 'latex', ...
|
||||||
|
'FontSize', label_fontsize);
|
||||||
|
set(gca, 'FontSize', axis_fontsize);
|
||||||
|
grid on;
|
||||||
|
|
||||||
|
% Super title
|
||||||
|
sgtitle(sprintf('Cumulants of Peak Offset Angular Correlation - %s', titleString), ...
|
||||||
|
'FontWeight', 'bold', 'FontSize', 16, 'Interpreter', 'latex');
|
||||||
|
|
||||||
|
%% ── Mean ± Std vs. scan parameter ──────────────────────────────────────
|
||||||
|
|
||||||
|
% Plot mean ± SEM
|
||||||
|
figure(1);
|
||||||
|
set(gcf, 'Color', 'w', 'Position',[100 100 950 750])
|
||||||
|
set(gca, 'FontSize', 14); % For tick labels only
|
||||||
|
errorbar(unique_scan_parameter_values, ... % x-axis
|
||||||
|
mean_max_g2_values, ... % y-axis (mean)
|
||||||
|
std_error_g2_values, ... % ± SEM
|
||||||
|
'--o', 'LineWidth', 1.8, 'MarkerSize', 6 );
|
||||||
|
|
||||||
|
set(gca, 'FontSize', 14, 'YLim', [0, 1]);
|
||||||
|
hXLabel = xlabel('$\alpha$ (degrees)', 'Interpreter', 'latex');
|
||||||
|
hYLabel = ylabel('$\mathrm{max}[g^{(2)}_{[50,70]}(\delta\theta)]$', 'Interpreter', 'latex');
|
||||||
|
hTitle = title(titleString, 'Interpreter', 'tex');
|
||||||
|
% set([hXLabel, hYLabel], 'FontName', font);
|
||||||
|
set([hXLabel, hYLabel], 'FontSize', 14);
|
||||||
|
set(hTitle, 'FontName', font, 'FontSize', 16, 'FontWeight', 'bold');
|
||||||
|
grid on;
|
||||||
|
|
||||||
|
% Define folder for saving images
|
||||||
|
saveFolder = [savefileName '_SavedFigures'];
|
||||||
|
if ~exist(saveFolder, 'dir')
|
||||||
|
mkdir(saveFolder);
|
||||||
|
end
|
||||||
|
save([saveFolder savefileName '.mat'], 'unique_scan_parameter_values', 'mean_max_g2_values', 'std_error_g2_values');
|
||||||
|
|
||||||
|
%% Helper Functions
|
||||||
|
function [IMGFFT, IMGPR] = computeFourierTransform(I, skipPreprocessing, skipMasking, skipIntensityThresholding, skipBinarization)
|
||||||
|
% computeFourierSpectrum - Computes the 2D Fourier power spectrum
|
||||||
|
% of binarized and enhanced lattice image features, with optional central mask.
|
||||||
|
%
|
||||||
|
% Inputs:
|
||||||
|
% I - Grayscale or RGB image matrix
|
||||||
|
%
|
||||||
|
% Output:
|
||||||
|
% F_mag - 2D Fourier power spectrum (shifted)
|
||||||
|
|
||||||
|
if ~skipPreprocessing
|
||||||
|
% Preprocessing: Denoise
|
||||||
|
filtered = imgaussfilt(I, 10);
|
||||||
|
IMGPR = I - filtered; % adjust sigma as needed
|
||||||
|
else
|
||||||
|
IMGPR = I;
|
||||||
|
end
|
||||||
|
|
||||||
|
if ~skipMasking
|
||||||
|
[rows, cols] = size(IMGPR);
|
||||||
|
[X, Y] = meshgrid(1:cols, 1:rows);
|
||||||
|
% Elliptical mask parameters
|
||||||
|
cx = cols / 2;
|
||||||
|
cy = rows / 2;
|
||||||
|
|
||||||
|
% Shifted coordinates
|
||||||
|
x = X - cx;
|
||||||
|
y = Y - cy;
|
||||||
|
|
||||||
|
% Ellipse semi-axes
|
||||||
|
rx = 0.4 * cols;
|
||||||
|
ry = 0.2 * rows;
|
||||||
|
|
||||||
|
% Rotation angle in degrees -> radians
|
||||||
|
theta_deg = 30; % Adjust as needed
|
||||||
|
theta = deg2rad(theta_deg);
|
||||||
|
|
||||||
|
% Rotated ellipse equation
|
||||||
|
cos_t = cos(theta);
|
||||||
|
sin_t = sin(theta);
|
||||||
|
|
||||||
|
x_rot = (x * cos_t + y * sin_t);
|
||||||
|
y_rot = (-x * sin_t + y * cos_t);
|
||||||
|
|
||||||
|
ellipseMask = (x_rot.^2) / rx^2 + (y_rot.^2) / ry^2 <= 1;
|
||||||
|
|
||||||
|
% Apply cutout mask
|
||||||
|
IMGPR = IMGPR .* ellipseMask;
|
||||||
|
end
|
||||||
|
|
||||||
|
if ~skipIntensityThresholding
|
||||||
|
% Apply global intensity threshold mask
|
||||||
|
intensity_thresh = 0.20;
|
||||||
|
intensity_mask = IMGPR > intensity_thresh;
|
||||||
|
IMGPR = IMGPR .* intensity_mask;
|
||||||
|
end
|
||||||
|
|
||||||
|
if ~skipBinarization
|
||||||
|
% Adaptive binarization and cleanup
|
||||||
|
IMGPR = imbinarize(IMGPR, 'adaptive', 'Sensitivity', 0.0);
|
||||||
|
IMGPR = imdilate(IMGPR, strel('disk', 2));
|
||||||
|
IMGPR = imerode(IMGPR, strel('disk', 1));
|
||||||
|
IMGPR = imfill(IMGPR, 'holes');
|
||||||
|
F = fft2(double(IMGPR)); % Compute 2D Fourier Transform
|
||||||
|
IMGFFT = abs(fftshift(F))'; % Shift zero frequency to center
|
||||||
|
else
|
||||||
|
F = fft2(double(IMGPR)); % Compute 2D Fourier Transform
|
||||||
|
IMGFFT = abs(fftshift(F))'; % Shift zero frequency to center
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function [theta_vals, S_theta] = computeAngularSpectralDistribution(IMGFFT, r_min, r_max, num_bins, threshold, sigma, windowSize)
|
||||||
|
% Apply threshold to isolate strong peaks
|
||||||
|
IMGFFT(IMGFFT < threshold) = 0;
|
||||||
|
|
||||||
|
% Prepare polar coordinates
|
||||||
|
[ny, nx] = size(IMGFFT);
|
||||||
|
[X, Y] = meshgrid(1:nx, 1:ny);
|
||||||
|
cx = ceil(nx/2);
|
||||||
|
cy = ceil(ny/2);
|
||||||
|
R = sqrt((X - cx).^2 + (Y - cy).^2);
|
||||||
|
Theta = atan2(Y - cy, X - cx); % range [-pi, pi]
|
||||||
|
|
||||||
|
% Choose radial band
|
||||||
|
radial_mask = (R >= r_min) & (R <= r_max);
|
||||||
|
|
||||||
|
% Initialize angular structure factor
|
||||||
|
S_theta = zeros(1, num_bins);
|
||||||
|
theta_vals = linspace(0, pi, num_bins);
|
||||||
|
|
||||||
|
% Loop through angle bins
|
||||||
|
for i = 1:num_bins
|
||||||
|
angle_start = (i-1) * pi / num_bins;
|
||||||
|
angle_end = i * pi / num_bins;
|
||||||
|
angle_mask = (Theta >= angle_start & Theta < angle_end);
|
||||||
|
bin_mask = radial_mask & angle_mask;
|
||||||
|
fft_angle = IMGFFT .* bin_mask;
|
||||||
|
S_theta(i) = sum(sum(abs(fft_angle).^2));
|
||||||
|
end
|
||||||
|
|
||||||
|
% Smooth using either Gaussian or moving average
|
||||||
|
if exist('sigma', 'var') && ~isempty(sigma)
|
||||||
|
% Gaussian convolution
|
||||||
|
half_width = ceil(3 * sigma);
|
||||||
|
x = -half_width:half_width;
|
||||||
|
gauss_kernel = exp(-x.^2 / (2 * sigma^2));
|
||||||
|
gauss_kernel = gauss_kernel / sum(gauss_kernel);
|
||||||
|
% Circular convolution
|
||||||
|
S_theta = conv([S_theta(end-half_width+1:end), S_theta, S_theta(1:half_width)], ...
|
||||||
|
gauss_kernel, 'same');
|
||||||
|
S_theta = S_theta(half_width+1:end-half_width);
|
||||||
|
elseif exist('windowSize', 'var') && ~isempty(windowSize)
|
||||||
|
% Moving average via convolution (circular)
|
||||||
|
pad = floor(windowSize / 2);
|
||||||
|
kernel = ones(1, windowSize) / windowSize;
|
||||||
|
S_theta = conv([S_theta(end-pad+1:end), S_theta, S_theta(1:pad)], kernel, 'same');
|
||||||
|
S_theta = S_theta(pad+1:end-pad);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function ret = getBkgOffsetFromCorners(img, x_fraction, y_fraction)
|
||||||
|
% image must be a 2D numerical array
|
||||||
|
[dim1, dim2] = size(img);
|
||||||
|
|
||||||
|
s1 = img(1:round(dim1 * y_fraction), 1:round(dim2 * x_fraction));
|
||||||
|
s2 = img(1:round(dim1 * y_fraction), round(dim2 - dim2 * x_fraction):dim2);
|
||||||
|
s3 = img(round(dim1 - dim1 * y_fraction):dim1, 1:round(dim2 * x_fraction));
|
||||||
|
s4 = img(round(dim1 - dim1 * y_fraction):dim1, round(dim2 - dim2 * x_fraction):dim2);
|
||||||
|
|
||||||
|
ret = mean([mean(s1(:)), mean(s2(:)), mean(s3(:)), mean(s4(:))]);
|
||||||
|
end
|
||||||
|
|
||||||
|
function ret = subtractBackgroundOffset(img, fraction)
|
||||||
|
% Remove the background from the image.
|
||||||
|
% :param dataArray: The image
|
||||||
|
% :type dataArray: xarray DataArray
|
||||||
|
% :param x_fraction: The fraction of the pixels used in x axis
|
||||||
|
% :type x_fraction: float
|
||||||
|
% :param y_fraction: The fraction of the pixels used in y axis
|
||||||
|
% :type y_fraction: float
|
||||||
|
% :return: The image after removing background
|
||||||
|
% :rtype: xarray DataArray
|
||||||
|
|
||||||
|
x_fraction = fraction(1);
|
||||||
|
y_fraction = fraction(2);
|
||||||
|
offset = getBkgOffsetFromCorners(img, x_fraction, y_fraction);
|
||||||
|
ret = img - offset;
|
||||||
|
end
|
||||||
|
|
||||||
|
function ret = cropODImage(img, center, span)
|
||||||
|
% Crop the image according to the region of interest (ROI).
|
||||||
|
% :param dataSet: The images
|
||||||
|
% :type dataSet: xarray DataArray or DataSet
|
||||||
|
% :param center: The center of region of interest (ROI)
|
||||||
|
% :type center: tuple
|
||||||
|
% :param span: The span of region of interest (ROI)
|
||||||
|
% :type span: tuple
|
||||||
|
% :return: The cropped images
|
||||||
|
% :rtype: xarray DataArray or DataSet
|
||||||
|
|
||||||
|
x_start = floor(center(1) - span(1) / 2);
|
||||||
|
x_end = floor(center(1) + span(1) / 2);
|
||||||
|
y_start = floor(center(2) - span(2) / 2);
|
||||||
|
y_end = floor(center(2) + span(2) / 2);
|
||||||
|
|
||||||
|
ret = img(y_start:y_end, x_start:x_end);
|
||||||
|
end
|
||||||
|
|
||||||
|
function imageOD = calculateODImage(imageAtom, imageBackground, imageDark, mode, exposureTime)
|
||||||
|
%CALCULATEODIMAGE Calculates the optical density (OD) image for absorption imaging.
|
||||||
|
%
|
||||||
|
% imageOD = calculateODImage(imageAtom, imageBackground, imageDark, mode, exposureTime)
|
||||||
|
%
|
||||||
|
% Inputs:
|
||||||
|
% imageAtom - Image with atoms
|
||||||
|
% imageBackground - Image without atoms
|
||||||
|
% imageDark - Image without light
|
||||||
|
% mode - 'LowIntensity' (default) or 'HighIntensity'
|
||||||
|
% exposureTime - Required only for 'HighIntensity' [in seconds]
|
||||||
|
%
|
||||||
|
% Output:
|
||||||
|
% imageOD - Computed OD image
|
||||||
|
%
|
||||||
|
|
||||||
|
arguments
|
||||||
|
imageAtom (:,:) {mustBeNumeric}
|
||||||
|
imageBackground (:,:) {mustBeNumeric}
|
||||||
|
imageDark (:,:) {mustBeNumeric}
|
||||||
|
mode char {mustBeMember(mode, {'LowIntensity', 'HighIntensity'})} = 'LowIntensity'
|
||||||
|
exposureTime double = NaN
|
||||||
|
end
|
||||||
|
|
||||||
|
% Compute numerator and denominator
|
||||||
|
numerator = imageBackground - imageDark;
|
||||||
|
denominator = imageAtom - imageDark;
|
||||||
|
|
||||||
|
% Avoid division by zero
|
||||||
|
numerator(numerator == 0) = 1;
|
||||||
|
denominator(denominator == 0) = 1;
|
||||||
|
|
||||||
|
% Calculate OD based on mode
|
||||||
|
switch mode
|
||||||
|
case 'LowIntensity'
|
||||||
|
imageOD = -log(abs(denominator ./ numerator));
|
||||||
|
|
||||||
|
case 'HighIntensity'
|
||||||
|
if isnan(exposureTime)
|
||||||
|
error('Exposure time must be provided for HighIntensity mode.');
|
||||||
|
end
|
||||||
|
imageOD = abs(denominator ./ numerator);
|
||||||
|
imageOD = -log(imageOD) + (numerator - denominator) ./ (7000 * (exposureTime / 5e-6));
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
function [optrefimages] = removefringesInImage(absimages, refimages, bgmask)
|
||||||
|
% removefringesInImage - Fringe removal and noise reduction from absorption images.
|
||||||
|
% Creates an optimal reference image for each absorption image in a set as
|
||||||
|
% a linear combination of reference images, with coefficients chosen to
|
||||||
|
% minimize the least-squares residuals between each absorption image and
|
||||||
|
% the optimal reference image. The coefficients are obtained by solving a
|
||||||
|
% linear set of equations using matrix inverse by LU decomposition.
|
||||||
|
%
|
||||||
|
% Application of the algorithm is described in C. F. Ockeloen et al, Improved
|
||||||
|
% detection of small atom numbers through image processing, arXiv:1007.2136 (2010).
|
||||||
|
%
|
||||||
|
% Syntax:
|
||||||
|
% [optrefimages] = removefringesInImage(absimages,refimages,bgmask);
|
||||||
|
%
|
||||||
|
% Required inputs:
|
||||||
|
% absimages - Absorption image data,
|
||||||
|
% typically 16 bit grayscale images
|
||||||
|
% refimages - Raw reference image data
|
||||||
|
% absimages and refimages are both cell arrays containing
|
||||||
|
% 2D array data. The number of refimages can differ from the
|
||||||
|
% number of absimages.
|
||||||
|
%
|
||||||
|
% Optional inputs:
|
||||||
|
% bgmask - Array specifying background region used,
|
||||||
|
% 1=background, 0=data. Defaults to all ones.
|
||||||
|
% Outputs:
|
||||||
|
% optrefimages - Cell array of optimal reference images,
|
||||||
|
% equal in size to absimages.
|
||||||
|
%
|
||||||
|
|
||||||
|
% Dependencies: none
|
||||||
|
%
|
||||||
|
% Authors: Shannon Whitlock, Caspar Ockeloen
|
||||||
|
% Reference: C. F. Ockeloen, A. F. Tauschinsky, R. J. C. Spreeuw, and
|
||||||
|
% S. Whitlock, Improved detection of small atom numbers through
|
||||||
|
% image processing, arXiv:1007.2136
|
||||||
|
% Email:
|
||||||
|
% May 2009; Last revision: 11 August 2010
|
||||||
|
|
||||||
|
% Process inputs
|
||||||
|
|
||||||
|
% Set variables, and flatten absorption and reference images
|
||||||
|
nimgs = size(absimages,3);
|
||||||
|
nimgsR = size(refimages,3);
|
||||||
|
xdim = size(absimages(:,:,1),2);
|
||||||
|
ydim = size(absimages(:,:,1),1);
|
||||||
|
|
||||||
|
R = single(reshape(refimages,xdim*ydim,nimgsR));
|
||||||
|
A = single(reshape(absimages,xdim*ydim,nimgs));
|
||||||
|
optrefimages=zeros(size(absimages)); % preallocate
|
||||||
|
|
||||||
|
if not(exist('bgmask','var')); bgmask=ones(ydim,xdim); end
|
||||||
|
k = find(bgmask(:)==1); % Index k specifying background region
|
||||||
|
|
||||||
|
% Ensure there are no duplicate reference images
|
||||||
|
% R=unique(R','rows')'; % comment this line if you run out of memory
|
||||||
|
|
||||||
|
% Decompose B = R*R' using singular value or LU decomposition
|
||||||
|
[L,U,p] = lu(R(k,:)'*R(k,:),'vector'); % LU decomposition
|
||||||
|
|
||||||
|
for j=1:nimgs
|
||||||
|
b=R(k,:)'*A(k,j);
|
||||||
|
% Obtain coefficients c which minimise least-square residuals
|
||||||
|
lower.LT = true; upper.UT = true;
|
||||||
|
c = linsolve(U,linsolve(L,b(p,:),lower),upper);
|
||||||
|
|
||||||
|
% Compute optimised reference image
|
||||||
|
optrefimages(:,:,j)=reshape(R*c,[ydim xdim]);
|
||||||
|
end
|
||||||
|
end
|
158
Data-Analyzer/Deprecated/extractQuantities.m
Normal file
158
Data-Analyzer/Deprecated/extractQuantities.m
Normal file
@ -0,0 +1,158 @@
|
|||||||
|
%% Parameters
|
||||||
|
|
||||||
|
% === Define folders and settings ===
|
||||||
|
|
||||||
|
baseFolder = '//DyLabNAS/Data/TwoDGas/2025/04/';
|
||||||
|
|
||||||
|
dates = ["01", "02"]; % Example: three folders
|
||||||
|
runs = {
|
||||||
|
["0059", "0060", "0061"],
|
||||||
|
["0007", "0008", "0009", "0010", "0011"]
|
||||||
|
};
|
||||||
|
|
||||||
|
options.scan_parameter = 'rot_mag_fin_pol_angle';
|
||||||
|
options.scan_groups = 0:10:50;
|
||||||
|
options.cam = 5;
|
||||||
|
|
||||||
|
% Image cropping and alignment
|
||||||
|
options.angle = 0;
|
||||||
|
options.center = [1285, 2100];
|
||||||
|
options.span = [200, 200];
|
||||||
|
options.fraction = [0.1, 0.1];
|
||||||
|
|
||||||
|
% Imaging and calibration parameters
|
||||||
|
options.pixel_size = 5.86e-6; % in meters
|
||||||
|
options.magnification = 23.94;
|
||||||
|
options.removeFringes = false;
|
||||||
|
options.ImagingMode = 'HighIntensity';
|
||||||
|
options.PulseDuration = 5e-6;
|
||||||
|
|
||||||
|
% Fourier analysis: Radial
|
||||||
|
options.theta_min = deg2rad(0);
|
||||||
|
options.theta_max = deg2rad(180);
|
||||||
|
options.N_radial_bins = 500;
|
||||||
|
options.Radial_Sigma = 2;
|
||||||
|
options.Radial_WindowSize = 5; % Must be odd
|
||||||
|
|
||||||
|
% Fourier analysis: Angular
|
||||||
|
options.r_min = 10;
|
||||||
|
options.r_max = 20;
|
||||||
|
options.k_min = 1.2771; % in μm⁻¹
|
||||||
|
options.k_max = 2.5541; % in μm⁻¹
|
||||||
|
options.N_angular_bins = 180;
|
||||||
|
options.Angular_Threshold = 75;
|
||||||
|
options.Angular_Sigma = 2;
|
||||||
|
options.Angular_WindowSize = 5;
|
||||||
|
|
||||||
|
% Optional visualization / zooming
|
||||||
|
options.zoom_size = 50;
|
||||||
|
|
||||||
|
% Optional flags or settings struct
|
||||||
|
options.skipUnshuffling = false;
|
||||||
|
options.skipPreprocessing = true;
|
||||||
|
options.skipMasking = true;
|
||||||
|
options.skipIntensityThresholding = true;
|
||||||
|
options.skipBinarization = true;
|
||||||
|
|
||||||
|
% === Loop through folders and collect results ===
|
||||||
|
|
||||||
|
results_all = [];
|
||||||
|
|
||||||
|
assert(length(dates) == length(runs), ...
|
||||||
|
'Each entry in `dates` must correspond to a cell in `runs`.');
|
||||||
|
|
||||||
|
for i = 1:length(dates)
|
||||||
|
currentDate = dates(i);
|
||||||
|
currentRuns = runs{i};
|
||||||
|
|
||||||
|
for j = 1:length(currentRuns)
|
||||||
|
runID = currentRuns(j);
|
||||||
|
folderPath = fullfile(baseFolder, currentDate, runID);
|
||||||
|
|
||||||
|
if ~endsWith(folderPath, filesep)
|
||||||
|
options.folderPath = [char(folderPath) filesep];
|
||||||
|
else
|
||||||
|
options.folderPath = char(folderPath);
|
||||||
|
end
|
||||||
|
|
||||||
|
try
|
||||||
|
% Unpack options struct into name-value pairs
|
||||||
|
args = [fieldnames(options), struct2cell(options)]';
|
||||||
|
args = args(:)';
|
||||||
|
|
||||||
|
results = analyzeFolder(args{:});
|
||||||
|
results_all = [results_all; results];
|
||||||
|
catch ME
|
||||||
|
warning("Error processing %s/%s: %s", currentDate, runID, ME.message);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
%% Plotting heatmap of mean_max_g2_values
|
||||||
|
|
||||||
|
N_x = length(options.scan_groups);
|
||||||
|
N_y = length(results_all);
|
||||||
|
|
||||||
|
BFields = [2.35, 2.15, 2.0, 1.85, 1.7, 1.55, 1.4, 1.35];
|
||||||
|
|
||||||
|
% Preallocate
|
||||||
|
g2_matrix = zeros(N_y, N_x);
|
||||||
|
|
||||||
|
for i = 1:N_y
|
||||||
|
for j = 1:N_x
|
||||||
|
g2_matrix(i, j) = results_all(i).mean_max_g2_values(j);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
% Plot heatmap
|
||||||
|
|
||||||
|
font = 'Bahnschrift';
|
||||||
|
|
||||||
|
figure(1)
|
||||||
|
clf
|
||||||
|
set(gcf,'Position',[50 50 950 750])
|
||||||
|
imagesc(options.scan_groups, BFields, g2_matrix);
|
||||||
|
colormap(sky);
|
||||||
|
clim([0, 1])
|
||||||
|
set(gca, 'FontSize', 14, 'YDir', 'normal');
|
||||||
|
hXLabel = xlabel('\alpha (degrees)', 'Interpreter', 'tex');
|
||||||
|
hYLabel = ylabel('BField (G)', 'Interpreter', 'tex');
|
||||||
|
hTitle = title('$\mathrm{max}[g^{(2)}_{[50,70]}(\delta\theta)]$', 'Interpreter', 'latex');
|
||||||
|
set([hXLabel, hYLabel], 'FontSize', 14)
|
||||||
|
set(hTitle, 'FontSize', 16, 'FontWeight', 'bold'); % Set font and size for title
|
||||||
|
colorbar;
|
||||||
|
|
||||||
|
%% Heat map of radial spectral contrast
|
||||||
|
|
||||||
|
N_x = length(options.scan_groups);
|
||||||
|
N_y = length(results_all);
|
||||||
|
|
||||||
|
BFields = [2.35, 2.15, 2.0, 1.85, 1.7, 1.55, 1.4, 1.35];
|
||||||
|
|
||||||
|
% Preallocate
|
||||||
|
radial_spectral_contrast_matrix = zeros(N_y, N_x);
|
||||||
|
|
||||||
|
for i = 1:N_y
|
||||||
|
for j = 1:N_x
|
||||||
|
radial_spectral_contrast_matrix(i, j) = results_all(i).radial_spectral_contrast(j);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
% Plot heatmap
|
||||||
|
|
||||||
|
font = 'Bahnschrift';
|
||||||
|
|
||||||
|
figure(3)
|
||||||
|
clf
|
||||||
|
set(gcf,'Position',[50 50 950 750])
|
||||||
|
imagesc(options.scan_groups, BFields, radial_spectral_contrast_matrix);
|
||||||
|
colormap(sky);
|
||||||
|
clim([0 0.008])
|
||||||
|
set(gca, 'FontSize', 14, 'YDir', 'normal');
|
||||||
|
hXLabel = xlabel('\alpha (degrees)', 'Interpreter', 'tex');
|
||||||
|
hYLabel = ylabel('BField (G)', 'Interpreter', 'tex');
|
||||||
|
hTitle = title('Radial Spectral Contrast');
|
||||||
|
set([hXLabel, hYLabel], 'FontSize', 14)
|
||||||
|
set(hTitle, 'FontName', font, 'FontSize', 16, 'FontWeight', 'bold'); % Set font and size for title
|
||||||
|
colorbar;
|
416
Data-Analyzer/Deprecated/plotImages.m
Normal file
416
Data-Analyzer/Deprecated/plotImages.m
Normal file
@ -0,0 +1,416 @@
|
|||||||
|
%% Parameters
|
||||||
|
|
||||||
|
groupList = ["/images/MOT_3D_Camera/in_situ_absorption", "/images/ODT_1_Axis_Camera/in_situ_absorption", ...
|
||||||
|
"/images/ODT_2_Axis_Camera/in_situ_absorption", "/images/Horizontal_Axis_Camera/in_situ_absorption", ...
|
||||||
|
"/images/Vertical_Axis_Camera/in_situ_absorption"];
|
||||||
|
|
||||||
|
folderPath = "//DyLabNAS/Data/TwoDGas/2025/07/16/";
|
||||||
|
|
||||||
|
run = '0002';
|
||||||
|
|
||||||
|
folderPath = strcat(folderPath, run);
|
||||||
|
|
||||||
|
cam = 5;
|
||||||
|
|
||||||
|
angle = 0;
|
||||||
|
center = [1430, 2025];
|
||||||
|
span = [200, 200];
|
||||||
|
fraction = [0.1, 0.1];
|
||||||
|
|
||||||
|
pixel_size = 5.86e-6; % in meters
|
||||||
|
magnification = 23.94;
|
||||||
|
removeFringes = false;
|
||||||
|
|
||||||
|
ImagingMode = 'HighIntensity';
|
||||||
|
PulseDuration = 5e-6;
|
||||||
|
|
||||||
|
% Plotting and saving
|
||||||
|
scan_parameter = 'evap_rot_mag_field';
|
||||||
|
scan_groups = 0:10:50;
|
||||||
|
savefileName = 'Droplets';
|
||||||
|
font = 'Bahnschrift';
|
||||||
|
|
||||||
|
% Flags
|
||||||
|
skipUnshuffling = true;
|
||||||
|
|
||||||
|
%% ===== Load and compute OD image, rotate and extract ROI for analysis =====
|
||||||
|
% Get a list of all files in the folder with the desired file name pattern.
|
||||||
|
|
||||||
|
filePattern = fullfile(folderPath, '*.h5');
|
||||||
|
files = dir(filePattern);
|
||||||
|
refimages = zeros(span(1) + 1, span(2) + 1, length(files));
|
||||||
|
absimages = zeros(span(1) + 1, span(2) + 1, length(files));
|
||||||
|
|
||||||
|
for k = 1 : length(files)
|
||||||
|
baseFileName = files(k).name;
|
||||||
|
fullFileName = fullfile(files(k).folder, baseFileName);
|
||||||
|
|
||||||
|
fprintf(1, 'Now reading %s\n', fullFileName);
|
||||||
|
|
||||||
|
atm_img = double(imrotate(h5read(fullFileName, append(groupList(cam), "/atoms")), angle));
|
||||||
|
bkg_img = double(imrotate(h5read(fullFileName, append(groupList(cam), "/background")), angle));
|
||||||
|
dark_img = double(imrotate(h5read(fullFileName, append(groupList(cam), "/dark")), angle));
|
||||||
|
|
||||||
|
refimages(:,:,k) = subtractBackgroundOffset(cropODImage(bkg_img, center, span), fraction)';
|
||||||
|
absimages(:,:,k) = subtractBackgroundOffset(cropODImage(calculateODImage(atm_img, bkg_img, dark_img, ImagingMode, PulseDuration), center, span), fraction)';
|
||||||
|
end
|
||||||
|
|
||||||
|
% ===== Fringe removal =====
|
||||||
|
|
||||||
|
if removeFringes
|
||||||
|
optrefimages = removefringesInImage(absimages, refimages);
|
||||||
|
absimages_fringe_removed = absimages(:, :, :) - optrefimages(:, :, :);
|
||||||
|
|
||||||
|
nimgs = size(absimages_fringe_removed,3);
|
||||||
|
od_imgs = cell(1, nimgs);
|
||||||
|
for i = 1:nimgs
|
||||||
|
od_imgs{i} = absimages_fringe_removed(:, :, i);
|
||||||
|
end
|
||||||
|
else
|
||||||
|
nimgs = size(absimages(:, :, :),3);
|
||||||
|
od_imgs = cell(1, nimgs);
|
||||||
|
for i = 1:nimgs
|
||||||
|
od_imgs{i} = absimages(:, :, i);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
% ===== Get rotation angles =====
|
||||||
|
scan_parameter_values = zeros(1, length(files));
|
||||||
|
|
||||||
|
% Get information about the '/globals' group
|
||||||
|
for k = 1 : length(files)
|
||||||
|
baseFileName = files(k).name;
|
||||||
|
fullFileName = fullfile(files(k).folder, baseFileName);
|
||||||
|
info = h5info(fullFileName, '/globals');
|
||||||
|
for i = 1:length(info.Attributes)
|
||||||
|
if strcmp(info.Attributes(i).Name, scan_parameter)
|
||||||
|
if strcmp(scan_parameter, 'rot_mag_fin_pol_angle')
|
||||||
|
scan_parameter_values(k) = 180 - info.Attributes(i).Value;
|
||||||
|
else
|
||||||
|
scan_parameter_values(k) = info.Attributes(i).Value;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
% ===== Unshuffle if necessary to do so =====
|
||||||
|
|
||||||
|
if ~skipUnshuffling
|
||||||
|
n_values = length(scan_groups);
|
||||||
|
n_total = length(scan_parameter_values);
|
||||||
|
|
||||||
|
% Infer number of repetitions
|
||||||
|
n_reps = n_total / n_values;
|
||||||
|
|
||||||
|
% Preallocate ordered arrays
|
||||||
|
ordered_scan_values = zeros(1, n_total);
|
||||||
|
ordered_od_imgs = cell(1, n_total);
|
||||||
|
|
||||||
|
counter = 1;
|
||||||
|
|
||||||
|
for rep = 1:n_reps
|
||||||
|
for val = scan_groups
|
||||||
|
% Find the next unused match for this val
|
||||||
|
idx = find(scan_parameter_values == val, 1, 'first');
|
||||||
|
|
||||||
|
% Assign and remove from list to avoid duplicates
|
||||||
|
ordered_scan_values(counter) = scan_parameter_values(idx);
|
||||||
|
ordered_od_imgs{counter} = od_imgs{idx};
|
||||||
|
|
||||||
|
% Mark as used by removing
|
||||||
|
scan_parameter_values(idx) = NaN; % NaN is safe since original values are 0:5:45
|
||||||
|
od_imgs{idx} = []; % empty cell so it won't be matched again
|
||||||
|
|
||||||
|
counter = counter + 1;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
% Now assign back
|
||||||
|
scan_parameter_values = ordered_scan_values;
|
||||||
|
od_imgs = ordered_od_imgs;
|
||||||
|
end
|
||||||
|
|
||||||
|
%% Display Images
|
||||||
|
|
||||||
|
figure(1)
|
||||||
|
clf
|
||||||
|
set(gcf,'Position',[50 50 950 750])
|
||||||
|
|
||||||
|
% Get image size in pixels
|
||||||
|
[Ny, Nx] = size(od_imgs{1});
|
||||||
|
|
||||||
|
% Define pixel size and magnification (if not already defined earlier)
|
||||||
|
dx = pixel_size / magnification; % e.g. in meters
|
||||||
|
dy = dx; % assuming square pixels
|
||||||
|
|
||||||
|
% Define x and y axes in μm (centered at image center)
|
||||||
|
x = ((1:Nx) - ceil(Nx/2)) * dx * 1E6; % micrometers
|
||||||
|
y = ((1:Ny) - ceil(Ny/2)) * dy * 1E6;
|
||||||
|
|
||||||
|
% Display the cropped image
|
||||||
|
for k = 1 : length(od_imgs)
|
||||||
|
imagesc(x, y, od_imgs{k});
|
||||||
|
hold on;
|
||||||
|
|
||||||
|
% Convert pixel grid to µm (already done: x and y axes)
|
||||||
|
% Draw ↘ diagonal (top-left to bottom-right)
|
||||||
|
drawODOverlays(x(1), y(1), x(end), y(end));
|
||||||
|
|
||||||
|
% Draw ↙ diagonal (top-right to bottom-left)
|
||||||
|
drawODOverlays(x(end), y(1), x(1), y(end));
|
||||||
|
|
||||||
|
hold off;
|
||||||
|
axis equal tight;
|
||||||
|
colormap(Colormaps.inferno());
|
||||||
|
set(gca, 'FontSize', 14, 'YDir', 'normal');
|
||||||
|
|
||||||
|
if strcmp(scan_parameter, 'rot_mag_fin_pol_angle')
|
||||||
|
text(0.975, 0.975, [num2str(scan_parameter_values(k), '%.1f^\\circ')], ...
|
||||||
|
'Color', 'white', 'FontWeight', 'bold', 'FontSize', 24, ...
|
||||||
|
'Interpreter', 'tex', 'Units', 'normalized', ...
|
||||||
|
'HorizontalAlignment', 'right', 'VerticalAlignment', 'top');
|
||||||
|
else
|
||||||
|
text(0.975, 0.975, [num2str(scan_parameter_values(k), '%.2f'), ' G'], ...
|
||||||
|
'Color', 'white', 'FontWeight', 'bold', 'FontSize', 24, ...
|
||||||
|
'Interpreter', 'tex', 'Units', 'normalized', ...
|
||||||
|
'HorizontalAlignment', 'right', 'VerticalAlignment', 'top');
|
||||||
|
end
|
||||||
|
|
||||||
|
colorbarHandle = colorbar;
|
||||||
|
ylabel(colorbarHandle, 'Optical Density', 'Rotation', -90, 'FontSize', 14, 'FontName', font);
|
||||||
|
|
||||||
|
xlabel('x (\mum)', 'Interpreter', 'tex', 'FontSize', 14, 'FontName', font);
|
||||||
|
ylabel('y (\mum)', 'Interpreter', 'tex', 'FontSize', 14, 'FontName', font);
|
||||||
|
title('OD Image', 'FontSize', 16, 'FontWeight', 'bold', 'Interpreter', 'tex', 'FontName', font);
|
||||||
|
|
||||||
|
drawnow;
|
||||||
|
pause(0.5);
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
%% Helper Functions
|
||||||
|
|
||||||
|
function ret = getBkgOffsetFromCorners(img, x_fraction, y_fraction)
|
||||||
|
% image must be a 2D numerical array
|
||||||
|
[dim1, dim2] = size(img);
|
||||||
|
|
||||||
|
s1 = img(1:round(dim1 * y_fraction), 1:round(dim2 * x_fraction));
|
||||||
|
s2 = img(1:round(dim1 * y_fraction), round(dim2 - dim2 * x_fraction):dim2);
|
||||||
|
s3 = img(round(dim1 - dim1 * y_fraction):dim1, 1:round(dim2 * x_fraction));
|
||||||
|
s4 = img(round(dim1 - dim1 * y_fraction):dim1, round(dim2 - dim2 * x_fraction):dim2);
|
||||||
|
|
||||||
|
ret = mean([mean(s1(:)), mean(s2(:)), mean(s3(:)), mean(s4(:))]);
|
||||||
|
end
|
||||||
|
|
||||||
|
function ret = subtractBackgroundOffset(img, fraction)
|
||||||
|
% Remove the background from the image.
|
||||||
|
% :param dataArray: The image
|
||||||
|
% :type dataArray: xarray DataArray
|
||||||
|
% :param x_fraction: The fraction of the pixels used in x axis
|
||||||
|
% :type x_fraction: float
|
||||||
|
% :param y_fraction: The fraction of the pixels used in y axis
|
||||||
|
% :type y_fraction: float
|
||||||
|
% :return: The image after removing background
|
||||||
|
% :rtype: xarray DataArray
|
||||||
|
|
||||||
|
x_fraction = fraction(1);
|
||||||
|
y_fraction = fraction(2);
|
||||||
|
offset = getBkgOffsetFromCorners(img, x_fraction, y_fraction);
|
||||||
|
ret = img - offset;
|
||||||
|
end
|
||||||
|
|
||||||
|
function ret = cropODImage(img, center, span)
|
||||||
|
% Crop the image according to the region of interest (ROI).
|
||||||
|
% :param dataSet: The images
|
||||||
|
% :type dataSet: xarray DataArray or DataSet
|
||||||
|
% :param center: The center of region of interest (ROI)
|
||||||
|
% :type center: tuple
|
||||||
|
% :param span: The span of region of interest (ROI)
|
||||||
|
% :type span: tuple
|
||||||
|
% :return: The cropped images
|
||||||
|
% :rtype: xarray DataArray or DataSet
|
||||||
|
|
||||||
|
x_start = floor(center(1) - span(1) / 2);
|
||||||
|
x_end = floor(center(1) + span(1) / 2);
|
||||||
|
y_start = floor(center(2) - span(2) / 2);
|
||||||
|
y_end = floor(center(2) + span(2) / 2);
|
||||||
|
|
||||||
|
ret = img(y_start:y_end, x_start:x_end);
|
||||||
|
end
|
||||||
|
|
||||||
|
function imageOD = calculateODImage(imageAtom, imageBackground, imageDark, mode, exposureTime)
|
||||||
|
%CALCULATEODIMAGE Calculates the optical density (OD) image for absorption imaging.
|
||||||
|
%
|
||||||
|
% imageOD = calculateODImage(imageAtom, imageBackground, imageDark, mode, exposureTime)
|
||||||
|
%
|
||||||
|
% Inputs:
|
||||||
|
% imageAtom - Image with atoms
|
||||||
|
% imageBackground - Image without atoms
|
||||||
|
% imageDark - Image without light
|
||||||
|
% mode - 'LowIntensity' (default) or 'HighIntensity'
|
||||||
|
% exposureTime - Required only for 'HighIntensity' [in seconds]
|
||||||
|
%
|
||||||
|
% Output:
|
||||||
|
% imageOD - Computed OD image
|
||||||
|
%
|
||||||
|
|
||||||
|
arguments
|
||||||
|
imageAtom (:,:) {mustBeNumeric}
|
||||||
|
imageBackground (:,:) {mustBeNumeric}
|
||||||
|
imageDark (:,:) {mustBeNumeric}
|
||||||
|
mode char {mustBeMember(mode, {'LowIntensity', 'HighIntensity'})} = 'LowIntensity'
|
||||||
|
exposureTime double = NaN
|
||||||
|
end
|
||||||
|
|
||||||
|
% Compute numerator and denominator
|
||||||
|
numerator = imageBackground - imageDark;
|
||||||
|
denominator = imageAtom - imageDark;
|
||||||
|
|
||||||
|
% Avoid division by zero
|
||||||
|
numerator(numerator == 0) = 1;
|
||||||
|
denominator(denominator == 0) = 1;
|
||||||
|
|
||||||
|
% Calculate OD based on mode
|
||||||
|
switch mode
|
||||||
|
case 'LowIntensity'
|
||||||
|
imageOD = -log(abs(denominator ./ numerator));
|
||||||
|
|
||||||
|
case 'HighIntensity'
|
||||||
|
if isnan(exposureTime)
|
||||||
|
error('Exposure time must be provided for HighIntensity mode.');
|
||||||
|
end
|
||||||
|
imageOD = abs(denominator ./ numerator);
|
||||||
|
imageOD = -log(imageOD) + (numerator - denominator) ./ (7000 * (exposureTime / 5e-6));
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
function drawODOverlays(x1, y1, x2, y2)
|
||||||
|
|
||||||
|
% Parameters
|
||||||
|
tick_spacing = 10; % µm between ticks
|
||||||
|
tick_length = 2; % µm tick mark length
|
||||||
|
line_color = [0.5 0.5 0.5];
|
||||||
|
tick_color = [0.5 0.5 0.5];
|
||||||
|
font_size = 10;
|
||||||
|
|
||||||
|
% Vector from start to end
|
||||||
|
dx = x2 - x1;
|
||||||
|
dy = y2 - y1;
|
||||||
|
L = sqrt(dx^2 + dy^2);
|
||||||
|
|
||||||
|
% Unit direction vector along diagonal
|
||||||
|
ux = dx / L;
|
||||||
|
uy = dy / L;
|
||||||
|
|
||||||
|
% Perpendicular unit vector for ticks
|
||||||
|
perp_ux = -uy;
|
||||||
|
perp_uy = ux;
|
||||||
|
|
||||||
|
% Midpoint (center)
|
||||||
|
xc = (x1 + x2) / 2;
|
||||||
|
yc = (y1 + y2) / 2;
|
||||||
|
|
||||||
|
% Number of positive and negative ticks
|
||||||
|
n_ticks = floor(L / (2 * tick_spacing));
|
||||||
|
|
||||||
|
% Draw main diagonal line
|
||||||
|
plot([x1 x2], [y1 y2], '--', 'Color', line_color, 'LineWidth', 1.2);
|
||||||
|
|
||||||
|
for i = -n_ticks:n_ticks
|
||||||
|
d = i * tick_spacing;
|
||||||
|
xt = xc + d * ux;
|
||||||
|
yt = yc + d * uy;
|
||||||
|
|
||||||
|
% Tick line endpoints
|
||||||
|
xt1 = xt - 0.5 * tick_length * perp_ux;
|
||||||
|
yt1 = yt - 0.5 * tick_length * perp_uy;
|
||||||
|
xt2 = xt + 0.5 * tick_length * perp_ux;
|
||||||
|
yt2 = yt + 0.5 * tick_length * perp_uy;
|
||||||
|
|
||||||
|
% Draw tick
|
||||||
|
plot([xt1 xt2], [yt1 yt2], '--', 'Color', tick_color, 'LineWidth', 1);
|
||||||
|
|
||||||
|
% Label: centered at tick, offset slightly along diagonal
|
||||||
|
if d ~= 0
|
||||||
|
text(xt, yt, sprintf('%+d', d), ...
|
||||||
|
'Color', tick_color, ...
|
||||||
|
'FontSize', font_size, ...
|
||||||
|
'HorizontalAlignment', 'center', ...
|
||||||
|
'VerticalAlignment', 'bottom', ...
|
||||||
|
'Rotation', atan2d(dy, dx));
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function [optrefimages] = removefringesInImage(absimages, refimages, bgmask)
|
||||||
|
% removefringesInImage - Fringe removal and noise reduction from absorption images.
|
||||||
|
% Creates an optimal reference image for each absorption image in a set as
|
||||||
|
% a linear combination of reference images, with coefficients chosen to
|
||||||
|
% minimize the least-squares residuals between each absorption image and
|
||||||
|
% the optimal reference image. The coefficients are obtained by solving a
|
||||||
|
% linear set of equations using matrix inverse by LU decomposition.
|
||||||
|
%
|
||||||
|
% Application of the algorithm is described in C. F. Ockeloen et al, Improved
|
||||||
|
% detection of small atom numbers through image processing, arXiv:1007.2136 (2010).
|
||||||
|
%
|
||||||
|
% Syntax:
|
||||||
|
% [optrefimages] = removefringesInImage(absimages,refimages,bgmask);
|
||||||
|
%
|
||||||
|
% Required inputs:
|
||||||
|
% absimages - Absorption image data,
|
||||||
|
% typically 16 bit grayscale images
|
||||||
|
% refimages - Raw reference image data
|
||||||
|
% absimages and refimages are both cell arrays containing
|
||||||
|
% 2D array data. The number of refimages can differ from the
|
||||||
|
% number of absimages.
|
||||||
|
%
|
||||||
|
% Optional inputs:
|
||||||
|
% bgmask - Array specifying background region used,
|
||||||
|
% 1=background, 0=data. Defaults to all ones.
|
||||||
|
% Outputs:
|
||||||
|
% optrefimages - Cell array of optimal reference images,
|
||||||
|
% equal in size to absimages.
|
||||||
|
%
|
||||||
|
|
||||||
|
% Dependencies: none
|
||||||
|
%
|
||||||
|
% Authors: Shannon Whitlock, Caspar Ockeloen
|
||||||
|
% Reference: C. F. Ockeloen, A. F. Tauschinsky, R. J. C. Spreeuw, and
|
||||||
|
% S. Whitlock, Improved detection of small atom numbers through
|
||||||
|
% image processing, arXiv:1007.2136
|
||||||
|
% Email:
|
||||||
|
% May 2009; Last revision: 11 August 2010
|
||||||
|
|
||||||
|
% Process inputs
|
||||||
|
|
||||||
|
% Set variables, and flatten absorption and reference images
|
||||||
|
nimgs = size(absimages,3);
|
||||||
|
nimgsR = size(refimages,3);
|
||||||
|
xdim = size(absimages(:,:,1),2);
|
||||||
|
ydim = size(absimages(:,:,1),1);
|
||||||
|
|
||||||
|
R = single(reshape(refimages,xdim*ydim,nimgsR));
|
||||||
|
A = single(reshape(absimages,xdim*ydim,nimgs));
|
||||||
|
optrefimages=zeros(size(absimages)); % preallocate
|
||||||
|
|
||||||
|
if not(exist('bgmask','var')); bgmask=ones(ydim,xdim); end
|
||||||
|
k = find(bgmask(:)==1); % Index k specifying background region
|
||||||
|
|
||||||
|
% Ensure there are no duplicate reference images
|
||||||
|
% R=unique(R','rows')'; % comment this line if you run out of memory
|
||||||
|
|
||||||
|
% Decompose B = R*R' using singular value or LU decomposition
|
||||||
|
[L,U,p] = lu(R(k,:)'*R(k,:),'vector'); % LU decomposition
|
||||||
|
|
||||||
|
for j=1:nimgs
|
||||||
|
b=R(k,:)'*A(k,j);
|
||||||
|
% Obtain coefficients c which minimise least-square residuals
|
||||||
|
lower.LT = true; upper.UT = true;
|
||||||
|
c = linsolve(U,linsolve(L,b(p,:),lower),upper);
|
||||||
|
|
||||||
|
% Compute optimised reference image
|
||||||
|
optrefimages(:,:,j)=reshape(R*c,[ydim xdim]);
|
||||||
|
end
|
||||||
|
end
|
767
Data-Analyzer/Deprecated/plotPhaseDiagram.m
Normal file
767
Data-Analyzer/Deprecated/plotPhaseDiagram.m
Normal file
@ -0,0 +1,767 @@
|
|||||||
|
% === Parameters ===
|
||||||
|
baseFolder = '//DyLabNAS/Data/TwoDGas/2025/04/';
|
||||||
|
|
||||||
|
dates = ["01", "02"];
|
||||||
|
runs = {
|
||||||
|
["0059", "0060", "0061"],
|
||||||
|
["0007", "0008", "0009", "0010", "0011"]
|
||||||
|
};
|
||||||
|
|
||||||
|
scan_groups = 0:10:50;
|
||||||
|
scan_parameter = 'rot_mag_fin_pol_angle';
|
||||||
|
cam = 5;
|
||||||
|
|
||||||
|
% Image cropping and alignment
|
||||||
|
angle = 0;
|
||||||
|
center = [1285, 2100];
|
||||||
|
span = [200, 200];
|
||||||
|
fraction = [0.1, 0.1];
|
||||||
|
|
||||||
|
% Imaging and calibration parameters
|
||||||
|
pixel_size = 5.86e-6; % in meters
|
||||||
|
magnification = 23.94;
|
||||||
|
removeFringes = false;
|
||||||
|
ImagingMode = 'LowIntensity';
|
||||||
|
PulseDuration = 5e-6;
|
||||||
|
|
||||||
|
% Optional visualization / zooming
|
||||||
|
options.zoom_size = 50;
|
||||||
|
|
||||||
|
% Optional flags or settings struct
|
||||||
|
skipUnshuffling = false;
|
||||||
|
skipPreprocessing = true;
|
||||||
|
skipMasking = true;
|
||||||
|
skipIntensityThresholding = true;
|
||||||
|
skipBinarization = true;
|
||||||
|
|
||||||
|
groupList = ["/images/MOT_3D_Camera/in_situ_absorption", "/images/ODT_1_Axis_Camera/in_situ_absorption", ...
|
||||||
|
"/images/ODT_2_Axis_Camera/in_situ_absorption", "/images/Horizontal_Axis_Camera/in_situ_absorption", ...
|
||||||
|
"/images/Vertical_Axis_Camera/in_situ_absorption"];
|
||||||
|
%%
|
||||||
|
|
||||||
|
allData = {}; % now a growing list of structs per B field
|
||||||
|
dataCounter = 1;
|
||||||
|
|
||||||
|
for i = 1:length(dates)
|
||||||
|
dateStr = dates(i);
|
||||||
|
runList = runs{i};
|
||||||
|
|
||||||
|
for j = 1:length(runList)
|
||||||
|
folderPath = fullfile(baseFolder, dateStr, runList{j});
|
||||||
|
filePattern = fullfile(folderPath, '*.h5');
|
||||||
|
files = dir(filePattern);
|
||||||
|
refimages = zeros(span(1) + 1, span(2) + 1, length(files));
|
||||||
|
absimages = zeros(span(1) + 1, span(2) + 1, length(files));
|
||||||
|
|
||||||
|
for k = 1 : length(files)
|
||||||
|
baseFileName = files(k).name;
|
||||||
|
fullFileName = fullfile(files(k).folder, baseFileName);
|
||||||
|
|
||||||
|
fprintf(1, 'Now reading %s\n', fullFileName);
|
||||||
|
|
||||||
|
atm_img = double(imrotate(h5read(fullFileName, append(groupList(cam), "/atoms")), angle));
|
||||||
|
bkg_img = double(imrotate(h5read(fullFileName, append(groupList(cam), "/background")), angle));
|
||||||
|
dark_img = double(imrotate(h5read(fullFileName, append(groupList(cam), "/dark")), angle));
|
||||||
|
|
||||||
|
refimages(:,:,k) = subtractBackgroundOffset(cropODImage(bkg_img, center, span), fraction)';
|
||||||
|
absimages(:,:,k) = subtractBackgroundOffset(cropODImage(calculateODImage(atm_img, bkg_img, dark_img, ImagingMode, PulseDuration), center, span), fraction)';
|
||||||
|
end
|
||||||
|
|
||||||
|
% ===== Fringe removal =====
|
||||||
|
|
||||||
|
if removeFringes
|
||||||
|
optrefimages = removefringesInImage(absimages, refimages);
|
||||||
|
absimages_fringe_removed = absimages(:, :, :) - optrefimages(:, :, :);
|
||||||
|
|
||||||
|
nimgs = size(absimages_fringe_removed,3);
|
||||||
|
od_imgs = cell(1, nimgs);
|
||||||
|
for k = 1:nimgs
|
||||||
|
od_imgs{k} = absimages_fringe_removed(:, :, k);
|
||||||
|
end
|
||||||
|
else
|
||||||
|
nimgs = size(absimages(:, :, :),3);
|
||||||
|
od_imgs = cell(1, nimgs);
|
||||||
|
for k = 1:nimgs
|
||||||
|
od_imgs{k} = absimages(:, :, k);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
%% ===== Get rotation angles =====
|
||||||
|
scan_parameter_values = zeros(1, length(files));
|
||||||
|
|
||||||
|
% Get information about the '/globals' group
|
||||||
|
for k = 1 : length(files)
|
||||||
|
baseFileName = files(k).name;
|
||||||
|
fullFileName = fullfile(files(k).folder, baseFileName);
|
||||||
|
info = h5info(fullFileName, '/globals');
|
||||||
|
for i = 1:length(info.Attributes)
|
||||||
|
if strcmp(info.Attributes(i).Name, scan_parameter)
|
||||||
|
if strcmp(scan_parameter, 'rot_mag_fin_pol_angle')
|
||||||
|
scan_parameter_values(k) = 180 - info.Attributes(i).Value;
|
||||||
|
else
|
||||||
|
scan_parameter_values(k) = info.Attributes(i).Value;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if strcmp(info.Attributes(i).Name, "rot_mag_field")
|
||||||
|
B = info.Attributes(i).Value;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
% ===== Unshuffle if necessary to do so =====
|
||||||
|
|
||||||
|
if ~skipUnshuffling
|
||||||
|
n_values = length(scan_groups);
|
||||||
|
n_total = length(scan_parameter_values);
|
||||||
|
|
||||||
|
% Infer number of repetitions
|
||||||
|
n_reps = n_total / n_values;
|
||||||
|
|
||||||
|
% Preallocate ordered arrays
|
||||||
|
ordered_scan_values = zeros(1, n_total);
|
||||||
|
ordered_od_imgs = cell(1, n_total);
|
||||||
|
|
||||||
|
counter = 1;
|
||||||
|
|
||||||
|
for rep = 1:n_reps
|
||||||
|
for val = scan_groups
|
||||||
|
% Find the next unused match for this val
|
||||||
|
idx = find(scan_parameter_values == val, 1, 'first');
|
||||||
|
|
||||||
|
% Assign and remove from list to avoid duplicates
|
||||||
|
ordered_scan_values(counter) = scan_parameter_values(idx);
|
||||||
|
ordered_od_imgs{counter} = od_imgs{idx};
|
||||||
|
|
||||||
|
% Mark as used by removing
|
||||||
|
scan_parameter_values(idx) = NaN; % NaN is safe since original values are 0:5:45
|
||||||
|
od_imgs{idx} = []; % empty cell so it won't be matched again
|
||||||
|
|
||||||
|
counter = counter + 1;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
% Now assign back
|
||||||
|
scan_parameter_values = ordered_scan_values;
|
||||||
|
od_imgs = ordered_od_imgs;
|
||||||
|
end
|
||||||
|
% === Reshape ===
|
||||||
|
od_imgs_reshaped = reshape(od_imgs, [length(scan_groups), n_reps]);
|
||||||
|
|
||||||
|
% === Store ===
|
||||||
|
allData{dataCounter} = struct(...
|
||||||
|
'B', B, ...
|
||||||
|
'theta_vals', scan_groups, ...
|
||||||
|
'od_imgs', od_imgs_reshaped ...
|
||||||
|
);
|
||||||
|
dataCounter = dataCounter + 1;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
%% === % Plot PD - 1st rep of each θ per B-field ===
|
||||||
|
[theta_vals, ~, idx] = unique(scan_parameter_values);
|
||||||
|
nB = numel(allData);
|
||||||
|
nTheta = numel(theta_vals);
|
||||||
|
|
||||||
|
% Select every 2nd B-field index
|
||||||
|
idxToPlot = 1:2:nB; % indices 1, 3, 5, ...
|
||||||
|
|
||||||
|
% Update number of B-fields to plot
|
||||||
|
nB_new = numel(idxToPlot);
|
||||||
|
|
||||||
|
figure(101); clf;
|
||||||
|
|
||||||
|
% Make the figure wider to fit the colorbar comfortably
|
||||||
|
set(gcf, 'Position', [100, 100, 1300, 800]);
|
||||||
|
|
||||||
|
% Create tiled layout with some right padding to reserve space for colorbar
|
||||||
|
t = tiledlayout(nB_new, nTheta, 'TileSpacing', 'compact', 'Padding', 'compact');
|
||||||
|
|
||||||
|
font = 'Bahnschrift';
|
||||||
|
allAxes = gobjects(nB_new, nTheta);
|
||||||
|
|
||||||
|
for new_i = 1:nB_new
|
||||||
|
i = idxToPlot(new_i); % original index in allData
|
||||||
|
data = allData{i};
|
||||||
|
for j = 1:nTheta
|
||||||
|
ax = nexttile((new_i-1)*nTheta + j);
|
||||||
|
allAxes(new_i,j) = ax;
|
||||||
|
|
||||||
|
od = data(j).od_imgs;
|
||||||
|
imagesc(od, 'Parent', ax);
|
||||||
|
set(ax, 'YDir', 'normal');
|
||||||
|
axis(ax, 'image');
|
||||||
|
ax.XTick = [];
|
||||||
|
ax.YTick = [];
|
||||||
|
|
||||||
|
colormap(ax, Colormaps.inferno());
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
% Use colorbar associated with the last image tile
|
||||||
|
cb = colorbar('Location', 'eastoutside');
|
||||||
|
cb.Layout.Tile = 'east'; % Attach it to the layout edge
|
||||||
|
cb.FontName = font;
|
||||||
|
cb.FontSize = 18;
|
||||||
|
cb.Label.FontSize = 20;
|
||||||
|
cb.Label.Rotation = 90;
|
||||||
|
cb.Label.VerticalAlignment = 'bottom';
|
||||||
|
cb.Label.HorizontalAlignment = 'center';
|
||||||
|
cb.Direction = 'normal'; % Ensure ticks go bottom-to-top
|
||||||
|
|
||||||
|
|
||||||
|
% Add x and y tick labels along bottom and left
|
||||||
|
% Use bottom row for θ ticks
|
||||||
|
for j = 1:nTheta
|
||||||
|
ax = allAxes(end, j);
|
||||||
|
ax.XTick = size(od,2)/2;
|
||||||
|
ax.XTickLabel = sprintf('%d°', theta_vals(j));
|
||||||
|
ax.XTickLabelRotation = 0;
|
||||||
|
ax.FontName = font;
|
||||||
|
ax.FontSize = 20;
|
||||||
|
end
|
||||||
|
|
||||||
|
% Use first column for B ticks (only the plotted subset)
|
||||||
|
for new_i = 1:nB_new
|
||||||
|
i = idxToPlot(new_i);
|
||||||
|
ax = allAxes(new_i, 1);
|
||||||
|
ax.YTick = size(od,1)/2;
|
||||||
|
ax.YTickLabel = sprintf('%.2f G', allData{i}(1).B);
|
||||||
|
ax.FontName = font;
|
||||||
|
ax.FontSize = 20;
|
||||||
|
end
|
||||||
|
|
||||||
|
%% Helper Functions
|
||||||
|
function [IMGFFT, IMGPR] = computeFourierTransform(I, skipPreprocessing, skipMasking, skipIntensityThresholding, skipBinarization)
|
||||||
|
% computeFourierSpectrum - Computes the 2D Fourier power spectrum
|
||||||
|
% of binarized and enhanced lattice image features, with optional central mask.
|
||||||
|
%
|
||||||
|
% Inputs:
|
||||||
|
% I - Grayscale or RGB image matrix
|
||||||
|
%
|
||||||
|
% Output:
|
||||||
|
% F_mag - 2D Fourier power spectrum (shifted)
|
||||||
|
|
||||||
|
if ~skipPreprocessing
|
||||||
|
% Preprocessing: Denoise
|
||||||
|
filtered = imgaussfilt(I, 10);
|
||||||
|
IMGPR = I - filtered; % adjust sigma as needed
|
||||||
|
else
|
||||||
|
IMGPR = I;
|
||||||
|
end
|
||||||
|
|
||||||
|
if ~skipMasking
|
||||||
|
[rows, cols] = size(IMGPR);
|
||||||
|
[X, Y] = meshgrid(1:cols, 1:rows);
|
||||||
|
% Elliptical mask parameters
|
||||||
|
cx = cols / 2;
|
||||||
|
cy = rows / 2;
|
||||||
|
|
||||||
|
% Shifted coordinates
|
||||||
|
x = X - cx;
|
||||||
|
y = Y - cy;
|
||||||
|
|
||||||
|
% Ellipse semi-axes
|
||||||
|
rx = 0.4 * cols;
|
||||||
|
ry = 0.2 * rows;
|
||||||
|
|
||||||
|
% Rotation angle in degrees -> radians
|
||||||
|
theta_deg = 30; % Adjust as needed
|
||||||
|
theta = deg2rad(theta_deg);
|
||||||
|
|
||||||
|
% Rotated ellipse equation
|
||||||
|
cos_t = cos(theta);
|
||||||
|
sin_t = sin(theta);
|
||||||
|
|
||||||
|
x_rot = (x * cos_t + y * sin_t);
|
||||||
|
y_rot = (-x * sin_t + y * cos_t);
|
||||||
|
|
||||||
|
ellipseMask = (x_rot.^2) / rx^2 + (y_rot.^2) / ry^2 <= 1;
|
||||||
|
|
||||||
|
% Apply cutout mask
|
||||||
|
IMGPR = IMGPR .* ellipseMask;
|
||||||
|
end
|
||||||
|
|
||||||
|
if ~skipIntensityThresholding
|
||||||
|
% Apply global intensity threshold mask
|
||||||
|
intensity_thresh = 0.20;
|
||||||
|
intensity_mask = IMGPR > intensity_thresh;
|
||||||
|
IMGPR = IMGPR .* intensity_mask;
|
||||||
|
end
|
||||||
|
|
||||||
|
if ~skipBinarization
|
||||||
|
% Adaptive binarization and cleanup
|
||||||
|
IMGPR = imbinarize(IMGPR, 'adaptive', 'Sensitivity', 0.0);
|
||||||
|
IMGPR = imdilate(IMGPR, strel('disk', 2));
|
||||||
|
IMGPR = imerode(IMGPR, strel('disk', 1));
|
||||||
|
IMGPR = imfill(IMGPR, 'holes');
|
||||||
|
F = fft2(double(IMGPR)); % Compute 2D Fourier Transform
|
||||||
|
IMGFFT = abs(fftshift(F))'; % Shift zero frequency to center
|
||||||
|
else
|
||||||
|
F = fft2(double(IMGPR)); % Compute 2D Fourier Transform
|
||||||
|
IMGFFT = abs(fftshift(F))'; % Shift zero frequency to center
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function [k_rho_vals, S_radial] = computeRadialSpectralDistribution(IMGFFT, kx, ky, thetamin, thetamax, num_bins)
|
||||||
|
% IMGFFT : 2D FFT image (fftshifted and cropped)
|
||||||
|
% kx, ky : 1D physical wavenumber axes [μm⁻¹] matching FFT size
|
||||||
|
% thetamin : Minimum angle (in radians)
|
||||||
|
% thetamax : Maximum angle (in radians)
|
||||||
|
% num_bins : Number of radial bins
|
||||||
|
|
||||||
|
[KX, KY] = meshgrid(kx, ky);
|
||||||
|
K_rho = sqrt(KX.^2 + KY.^2);
|
||||||
|
Theta = atan2(KY, KX);
|
||||||
|
|
||||||
|
if thetamin < thetamax
|
||||||
|
angle_mask = (Theta >= thetamin) & (Theta <= thetamax);
|
||||||
|
else
|
||||||
|
angle_mask = (Theta >= thetamin) | (Theta <= thetamax);
|
||||||
|
end
|
||||||
|
|
||||||
|
power_spectrum = abs(IMGFFT).^2;
|
||||||
|
|
||||||
|
r_min = min(K_rho(angle_mask));
|
||||||
|
r_max = max(K_rho(angle_mask));
|
||||||
|
r_edges = linspace(r_min, r_max, num_bins + 1);
|
||||||
|
k_rho_vals = 0.5 * (r_edges(1:end-1) + r_edges(2:end));
|
||||||
|
S_radial = zeros(1, num_bins);
|
||||||
|
|
||||||
|
for i = 1:num_bins
|
||||||
|
r_low = r_edges(i);
|
||||||
|
r_high = r_edges(i + 1);
|
||||||
|
radial_mask = (K_rho >= r_low) & (K_rho < r_high);
|
||||||
|
full_mask = radial_mask & angle_mask;
|
||||||
|
S_radial(i) = sum(power_spectrum(full_mask));
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function [theta_vals, S_theta] = computeAngularSpectralDistribution(IMGFFT, r_min, r_max, num_bins, threshold, sigma, windowSize)
|
||||||
|
% Apply threshold to isolate strong peaks
|
||||||
|
IMGFFT(IMGFFT < threshold) = 0;
|
||||||
|
|
||||||
|
% Prepare polar coordinates
|
||||||
|
[ny, nx] = size(IMGFFT);
|
||||||
|
[X, Y] = meshgrid(1:nx, 1:ny);
|
||||||
|
cx = ceil(nx/2);
|
||||||
|
cy = ceil(ny/2);
|
||||||
|
R = sqrt((X - cx).^2 + (Y - cy).^2);
|
||||||
|
Theta = atan2(Y - cy, X - cx); % range [-pi, pi]
|
||||||
|
|
||||||
|
% Choose radial band
|
||||||
|
radial_mask = (R >= r_min) & (R <= r_max);
|
||||||
|
|
||||||
|
% Initialize angular structure factor
|
||||||
|
S_theta = zeros(1, num_bins);
|
||||||
|
theta_vals = linspace(0, pi, num_bins);
|
||||||
|
|
||||||
|
% Loop through angle bins
|
||||||
|
for i = 1:num_bins
|
||||||
|
angle_start = (i-1) * pi / num_bins;
|
||||||
|
angle_end = i * pi / num_bins;
|
||||||
|
angle_mask = (Theta >= angle_start & Theta < angle_end);
|
||||||
|
bin_mask = radial_mask & angle_mask;
|
||||||
|
fft_angle = IMGFFT .* bin_mask;
|
||||||
|
S_theta(i) = sum(sum(abs(fft_angle).^2));
|
||||||
|
end
|
||||||
|
|
||||||
|
% Smooth using either Gaussian or moving average
|
||||||
|
if exist('sigma', 'var') && ~isempty(sigma)
|
||||||
|
% Gaussian convolution
|
||||||
|
half_width = ceil(3 * sigma);
|
||||||
|
x = -half_width:half_width;
|
||||||
|
gauss_kernel = exp(-x.^2 / (2 * sigma^2));
|
||||||
|
gauss_kernel = gauss_kernel / sum(gauss_kernel);
|
||||||
|
% Circular convolution
|
||||||
|
S_theta = conv([S_theta(end-half_width+1:end), S_theta, S_theta(1:half_width)], ...
|
||||||
|
gauss_kernel, 'same');
|
||||||
|
S_theta = S_theta(half_width+1:end-half_width);
|
||||||
|
elseif exist('windowSize', 'var') && ~isempty(windowSize)
|
||||||
|
% Moving average via convolution (circular)
|
||||||
|
pad = floor(windowSize / 2);
|
||||||
|
kernel = ones(1, windowSize) / windowSize;
|
||||||
|
S_theta = conv([S_theta(end-pad+1:end), S_theta, S_theta(1:pad)], kernel, 'same');
|
||||||
|
S_theta = S_theta(pad+1:end-pad);
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function contrast = computeRadialSpectralContrast(IMGFFT, r_min, r_max, threshold)
|
||||||
|
% Apply threshold to isolate strong peaks
|
||||||
|
IMGFFT(IMGFFT < threshold) = 0;
|
||||||
|
|
||||||
|
% Prepare polar coordinates
|
||||||
|
[ny, nx] = size(IMGFFT);
|
||||||
|
[X, Y] = meshgrid(1:nx, 1:ny);
|
||||||
|
cx = ceil(nx/2);
|
||||||
|
cy = ceil(ny/2);
|
||||||
|
R = sqrt((X - cx).^2 + (Y - cy).^2);
|
||||||
|
|
||||||
|
% Ring region (annulus) mask
|
||||||
|
ring_mask = (R >= r_min) & (R <= r_max);
|
||||||
|
|
||||||
|
% Squared magnitude in the ring
|
||||||
|
ring_power = abs(IMGFFT).^2 .* ring_mask;
|
||||||
|
|
||||||
|
% Maximum power in the ring
|
||||||
|
ring_max = max(ring_power(:));
|
||||||
|
|
||||||
|
% Power at the DC component
|
||||||
|
dc_power = abs(IMGFFT(cy, cx))^2;
|
||||||
|
|
||||||
|
% Avoid division by zero
|
||||||
|
if dc_power == 0
|
||||||
|
contrast = Inf; % or NaN or 0, depending on how you want to handle this
|
||||||
|
else
|
||||||
|
contrast = ring_max / dc_power;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function ret = getBkgOffsetFromCorners(img, x_fraction, y_fraction)
|
||||||
|
% image must be a 2D numerical array
|
||||||
|
[dim1, dim2] = size(img);
|
||||||
|
|
||||||
|
s1 = img(1:round(dim1 * y_fraction), 1:round(dim2 * x_fraction));
|
||||||
|
s2 = img(1:round(dim1 * y_fraction), round(dim2 - dim2 * x_fraction):dim2);
|
||||||
|
s3 = img(round(dim1 - dim1 * y_fraction):dim1, 1:round(dim2 * x_fraction));
|
||||||
|
s4 = img(round(dim1 - dim1 * y_fraction):dim1, round(dim2 - dim2 * x_fraction):dim2);
|
||||||
|
|
||||||
|
ret = mean([mean(s1(:)), mean(s2(:)), mean(s3(:)), mean(s4(:))]);
|
||||||
|
end
|
||||||
|
|
||||||
|
function ret = subtractBackgroundOffset(img, fraction)
|
||||||
|
% Remove the background from the image.
|
||||||
|
% :param dataArray: The image
|
||||||
|
% :type dataArray: xarray DataArray
|
||||||
|
% :param x_fraction: The fraction of the pixels used in x axis
|
||||||
|
% :type x_fraction: float
|
||||||
|
% :param y_fraction: The fraction of the pixels used in y axis
|
||||||
|
% :type y_fraction: float
|
||||||
|
% :return: The image after removing background
|
||||||
|
% :rtype: xarray DataArray
|
||||||
|
|
||||||
|
x_fraction = fraction(1);
|
||||||
|
y_fraction = fraction(2);
|
||||||
|
offset = getBkgOffsetFromCorners(img, x_fraction, y_fraction);
|
||||||
|
ret = img - offset;
|
||||||
|
end
|
||||||
|
|
||||||
|
function ret = cropODImage(img, center, span)
|
||||||
|
% Crop the image according to the region of interest (ROI).
|
||||||
|
% :param dataSet: The images
|
||||||
|
% :type dataSet: xarray DataArray or DataSet
|
||||||
|
% :param center: The center of region of interest (ROI)
|
||||||
|
% :type center: tuple
|
||||||
|
% :param span: The span of region of interest (ROI)
|
||||||
|
% :type span: tuple
|
||||||
|
% :return: The cropped images
|
||||||
|
% :rtype: xarray DataArray or DataSet
|
||||||
|
|
||||||
|
x_start = floor(center(1) - span(1) / 2);
|
||||||
|
x_end = floor(center(1) + span(1) / 2);
|
||||||
|
y_start = floor(center(2) - span(2) / 2);
|
||||||
|
y_end = floor(center(2) + span(2) / 2);
|
||||||
|
|
||||||
|
ret = img(y_start:y_end, x_start:x_end);
|
||||||
|
end
|
||||||
|
|
||||||
|
function imageOD = calculateODImage(imageAtom, imageBackground, imageDark, mode, exposureTime)
|
||||||
|
%CALCULATEODIMAGE Calculates the optical density (OD) image for absorption imaging.
|
||||||
|
%
|
||||||
|
% imageOD = calculateODImage(imageAtom, imageBackground, imageDark, mode, exposureTime)
|
||||||
|
%
|
||||||
|
% Inputs:
|
||||||
|
% imageAtom - Image with atoms
|
||||||
|
% imageBackground - Image without atoms
|
||||||
|
% imageDark - Image without light
|
||||||
|
% mode - 'LowIntensity' (default) or 'HighIntensity'
|
||||||
|
% exposureTime - Required only for 'HighIntensity' [in seconds]
|
||||||
|
%
|
||||||
|
% Output:
|
||||||
|
% imageOD - Computed OD image
|
||||||
|
%
|
||||||
|
|
||||||
|
arguments
|
||||||
|
imageAtom (:,:) {mustBeNumeric}
|
||||||
|
imageBackground (:,:) {mustBeNumeric}
|
||||||
|
imageDark (:,:) {mustBeNumeric}
|
||||||
|
mode char {mustBeMember(mode, {'LowIntensity', 'HighIntensity'})} = 'LowIntensity'
|
||||||
|
exposureTime double = NaN
|
||||||
|
end
|
||||||
|
|
||||||
|
% Compute numerator and denominator
|
||||||
|
numerator = imageBackground - imageDark;
|
||||||
|
denominator = imageAtom - imageDark;
|
||||||
|
|
||||||
|
% Avoid division by zero
|
||||||
|
numerator(numerator == 0) = 1;
|
||||||
|
denominator(denominator == 0) = 1;
|
||||||
|
|
||||||
|
% Calculate OD based on mode
|
||||||
|
switch mode
|
||||||
|
case 'LowIntensity'
|
||||||
|
imageOD = -log(abs(denominator ./ numerator));
|
||||||
|
|
||||||
|
case 'HighIntensity'
|
||||||
|
if isnan(exposureTime)
|
||||||
|
error('Exposure time must be provided for HighIntensity mode.');
|
||||||
|
end
|
||||||
|
imageOD = abs(denominator ./ numerator);
|
||||||
|
imageOD = -log(imageOD) + (numerator - denominator) ./ (7000 * (exposureTime / 5e-6));
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
function drawODOverlays(x1, y1, x2, y2)
|
||||||
|
|
||||||
|
% Parameters
|
||||||
|
tick_spacing = 10; % µm between ticks
|
||||||
|
tick_length = 2; % µm tick mark length
|
||||||
|
line_color = [0.5 0.5 0.5];
|
||||||
|
tick_color = [0.5 0.5 0.5];
|
||||||
|
font_size = 10;
|
||||||
|
|
||||||
|
% Vector from start to end
|
||||||
|
dx = x2 - x1;
|
||||||
|
dy = y2 - y1;
|
||||||
|
L = sqrt(dx^2 + dy^2);
|
||||||
|
|
||||||
|
% Unit direction vector along diagonal
|
||||||
|
ux = dx / L;
|
||||||
|
uy = dy / L;
|
||||||
|
|
||||||
|
% Perpendicular unit vector for ticks
|
||||||
|
perp_ux = -uy;
|
||||||
|
perp_uy = ux;
|
||||||
|
|
||||||
|
% Midpoint (center)
|
||||||
|
xc = (x1 + x2) / 2;
|
||||||
|
yc = (y1 + y2) / 2;
|
||||||
|
|
||||||
|
% Number of positive and negative ticks
|
||||||
|
n_ticks = floor(L / (2 * tick_spacing));
|
||||||
|
|
||||||
|
% Draw main diagonal line
|
||||||
|
plot([x1 x2], [y1 y2], '--', 'Color', line_color, 'LineWidth', 1.2);
|
||||||
|
|
||||||
|
for i = -n_ticks:n_ticks
|
||||||
|
d = i * tick_spacing;
|
||||||
|
xt = xc + d * ux;
|
||||||
|
yt = yc + d * uy;
|
||||||
|
|
||||||
|
% Tick line endpoints
|
||||||
|
xt1 = xt - 0.5 * tick_length * perp_ux;
|
||||||
|
yt1 = yt - 0.5 * tick_length * perp_uy;
|
||||||
|
xt2 = xt + 0.5 * tick_length * perp_ux;
|
||||||
|
yt2 = yt + 0.5 * tick_length * perp_uy;
|
||||||
|
|
||||||
|
% Draw tick
|
||||||
|
plot([xt1 xt2], [yt1 yt2], '--', 'Color', tick_color, 'LineWidth', 1);
|
||||||
|
|
||||||
|
% Label: centered at tick, offset slightly along diagonal
|
||||||
|
if d ~= 0
|
||||||
|
text(xt, yt, sprintf('%+d', d), ...
|
||||||
|
'Color', tick_color, ...
|
||||||
|
'FontSize', font_size, ...
|
||||||
|
'HorizontalAlignment', 'center', ...
|
||||||
|
'VerticalAlignment', 'bottom', ...
|
||||||
|
'Rotation', atan2d(dy, dx));
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function drawPSOverlays(kx, ky, r_min, r_max)
|
||||||
|
% drawFFTOverlays - Draw overlays on existing FFT plot:
|
||||||
|
% - Radial lines every 30°
|
||||||
|
% - Annular highlight with white (upper half) and gray (lower half) circles between r_min and r_max
|
||||||
|
% - Horizontal white bands at ky=0 in annulus region
|
||||||
|
% - Scale ticks and labels every 1 μm⁻¹ along each radial line
|
||||||
|
%
|
||||||
|
% Inputs:
|
||||||
|
% kx, ky - reciprocal space vectors (μm⁻¹)
|
||||||
|
% r_min - inner annulus radius offset index (integer)
|
||||||
|
% r_max - outer annulus radius offset index (integer)
|
||||||
|
%
|
||||||
|
% Example:
|
||||||
|
% hold on;
|
||||||
|
% drawFFTOverlays(kx, ky, 10, 30);
|
||||||
|
|
||||||
|
hold on
|
||||||
|
|
||||||
|
% === Overlay Radial Lines + Scales ===
|
||||||
|
[kx_grid, ky_grid] = meshgrid(kx, ky);
|
||||||
|
[~, kr_grid] = cart2pol(kx_grid, ky_grid); % kr_grid in μm⁻¹
|
||||||
|
|
||||||
|
max_kx = max(kx);
|
||||||
|
max_ky = max(ky);
|
||||||
|
|
||||||
|
for angle = 0 : pi/6 : pi
|
||||||
|
x_line = [0, max_kx] * cos(angle);
|
||||||
|
y_line = [0, max_ky] * sin(angle);
|
||||||
|
|
||||||
|
% Plot radial lines
|
||||||
|
plot(x_line, y_line, '--', 'Color', [0.5 0.5 0.5], 'LineWidth', 1.2);
|
||||||
|
plot(x_line, -y_line, '--', 'Color', [0.5 0.5 0.5], 'LineWidth', 1.2);
|
||||||
|
|
||||||
|
% Draw scale ticks along positive radial line
|
||||||
|
drawTicksAlongLine(0, 0, x_line(2), y_line(2));
|
||||||
|
|
||||||
|
% Draw scale ticks along negative radial line (reflect y)
|
||||||
|
drawTicksAlongLine(0, 0, x_line(2), -y_line(2));
|
||||||
|
end
|
||||||
|
|
||||||
|
% === Overlay Annular Highlight: White (r_min to r_max), Gray elsewhere ===
|
||||||
|
theta_full = linspace(0, 2*pi, 500);
|
||||||
|
|
||||||
|
center_x = ceil(size(kr_grid, 2) / 2);
|
||||||
|
center_y = ceil(size(kr_grid, 1) / 2);
|
||||||
|
|
||||||
|
k_min = kr_grid(center_y, center_x + r_min);
|
||||||
|
k_max = kr_grid(center_y, center_x + r_max);
|
||||||
|
|
||||||
|
% Upper half: white dashed circles
|
||||||
|
x1_upper = k_min * cos(theta_full(theta_full <= pi));
|
||||||
|
y1_upper = k_min * sin(theta_full(theta_full <= pi));
|
||||||
|
x2_upper = k_max * cos(theta_full(theta_full <= pi));
|
||||||
|
y2_upper = k_max * sin(theta_full(theta_full <= pi));
|
||||||
|
plot(x1_upper, y1_upper, 'k--', 'LineWidth', 1.2);
|
||||||
|
plot(x2_upper, y2_upper, 'k--', 'LineWidth', 1.2);
|
||||||
|
|
||||||
|
% Lower half: gray dashed circles
|
||||||
|
x1_lower = k_min * cos(theta_full(theta_full > pi));
|
||||||
|
y1_lower = k_min * sin(theta_full(theta_full > pi));
|
||||||
|
x2_lower = k_max * cos(theta_full(theta_full > pi));
|
||||||
|
y2_lower = k_max * sin(theta_full(theta_full > pi));
|
||||||
|
plot(x1_lower, y1_lower, '--', 'Color', [0.5 0.5 0.5], 'LineWidth', 1.0);
|
||||||
|
plot(x2_lower, y2_lower, '--', 'Color', [0.5 0.5 0.5], 'LineWidth', 1.0);
|
||||||
|
|
||||||
|
% === Highlight horizontal band across k_y = 0 ===
|
||||||
|
x_vals = kx;
|
||||||
|
xW1 = x_vals((x_vals >= -k_max) & (x_vals < -k_min));
|
||||||
|
xW2 = x_vals((x_vals > k_min) & (x_vals <= k_max));
|
||||||
|
|
||||||
|
plot(xW1, zeros(size(xW1)), 'k--', 'LineWidth', 1.2);
|
||||||
|
plot(xW2, zeros(size(xW2)), 'k--', 'LineWidth', 1.2);
|
||||||
|
|
||||||
|
hold off
|
||||||
|
|
||||||
|
|
||||||
|
% --- Nested helper function to draw ticks along a radial line ---
|
||||||
|
function drawTicksAlongLine(x_start, y_start, x_end, y_end)
|
||||||
|
% Tick parameters
|
||||||
|
tick_spacing = 1; % spacing between ticks in μm⁻¹
|
||||||
|
tick_length = 0.05 * sqrt((x_end - x_start)^2 + (y_end - y_start)^2); % relative tick length
|
||||||
|
line_color = [0.5 0.5 0.5];
|
||||||
|
tick_color = [0.5 0.5 0.5];
|
||||||
|
font_size = 8;
|
||||||
|
|
||||||
|
% Vector along the line
|
||||||
|
dx = x_end - x_start;
|
||||||
|
dy = y_end - y_start;
|
||||||
|
L = sqrt(dx^2 + dy^2);
|
||||||
|
ux = dx / L;
|
||||||
|
uy = dy / L;
|
||||||
|
|
||||||
|
% Perpendicular vector for ticks
|
||||||
|
perp_ux = -uy;
|
||||||
|
perp_uy = ux;
|
||||||
|
|
||||||
|
% Number of ticks (from 0 up to max length)
|
||||||
|
n_ticks = floor(L / tick_spacing);
|
||||||
|
|
||||||
|
for i = 1:n_ticks
|
||||||
|
% Position of tick along the line
|
||||||
|
xt = x_start + i * tick_spacing * ux;
|
||||||
|
yt = y_start + i * tick_spacing * uy;
|
||||||
|
|
||||||
|
% Tick endpoints
|
||||||
|
xt1 = xt - 0.5 * tick_length * perp_ux;
|
||||||
|
yt1 = yt - 0.5 * tick_length * perp_uy;
|
||||||
|
xt2 = xt + 0.5 * tick_length * perp_ux;
|
||||||
|
yt2 = yt + 0.5 * tick_length * perp_uy;
|
||||||
|
|
||||||
|
% Draw tick
|
||||||
|
plot([xt1 xt2], [yt1 yt2], '-', 'Color', tick_color, 'LineWidth', 1);
|
||||||
|
|
||||||
|
% Label with distance (integer)
|
||||||
|
text(xt, yt, sprintf('%d', i), ...
|
||||||
|
'Color', tick_color, ...
|
||||||
|
'FontSize', font_size, ...
|
||||||
|
'HorizontalAlignment', 'center', ...
|
||||||
|
'VerticalAlignment', 'bottom', ...
|
||||||
|
'Rotation', atan2d(dy, dx));
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
function [optrefimages] = removefringesInImage(absimages, refimages, bgmask)
|
||||||
|
% removefringesInImage - Fringe removal and noise reduction from absorption images.
|
||||||
|
% Creates an optimal reference image for each absorption image in a set as
|
||||||
|
% a linear combination of reference images, with coefficients chosen to
|
||||||
|
% minimize the least-squares residuals between each absorption image and
|
||||||
|
% the optimal reference image. The coefficients are obtained by solving a
|
||||||
|
% linear set of equations using matrix inverse by LU decomposition.
|
||||||
|
%
|
||||||
|
% Application of the algorithm is described in C. F. Ockeloen et al, Improved
|
||||||
|
% detection of small atom numbers through image processing, arXiv:1007.2136 (2010).
|
||||||
|
%
|
||||||
|
% Syntax:
|
||||||
|
% [optrefimages] = removefringesInImage(absimages,refimages,bgmask);
|
||||||
|
%
|
||||||
|
% Required inputs:
|
||||||
|
% absimages - Absorption image data,
|
||||||
|
% typically 16 bit grayscale images
|
||||||
|
% refimages - Raw reference image data
|
||||||
|
% absimages and refimages are both cell arrays containing
|
||||||
|
% 2D array data. The number of refimages can differ from the
|
||||||
|
% number of absimages.
|
||||||
|
%
|
||||||
|
% Optional inputs:
|
||||||
|
% bgmask - Array specifying background region used,
|
||||||
|
% 1=background, 0=data. Defaults to all ones.
|
||||||
|
% Outputs:
|
||||||
|
% optrefimages - Cell array of optimal reference images,
|
||||||
|
% equal in size to absimages.
|
||||||
|
%
|
||||||
|
|
||||||
|
% Dependencies: none
|
||||||
|
%
|
||||||
|
% Authors: Shannon Whitlock, Caspar Ockeloen
|
||||||
|
% Reference: C. F. Ockeloen, A. F. Tauschinsky, R. J. C. Spreeuw, and
|
||||||
|
% S. Whitlock, Improved detection of small atom numbers through
|
||||||
|
% image processing, arXiv:1007.2136
|
||||||
|
% Email:
|
||||||
|
% May 2009; Last revision: 11 August 2010
|
||||||
|
|
||||||
|
% Process inputs
|
||||||
|
|
||||||
|
% Set variables, and flatten absorption and reference images
|
||||||
|
nimgs = size(absimages,3);
|
||||||
|
nimgsR = size(refimages,3);
|
||||||
|
xdim = size(absimages(:,:,1),2);
|
||||||
|
ydim = size(absimages(:,:,1),1);
|
||||||
|
|
||||||
|
R = single(reshape(refimages,xdim*ydim,nimgsR));
|
||||||
|
A = single(reshape(absimages,xdim*ydim,nimgs));
|
||||||
|
optrefimages=zeros(size(absimages)); % preallocate
|
||||||
|
|
||||||
|
if not(exist('bgmask','var')); bgmask=ones(ydim,xdim); end
|
||||||
|
k = find(bgmask(:)==1); % Index k specifying background region
|
||||||
|
|
||||||
|
% Ensure there are no duplicate reference images
|
||||||
|
% R=unique(R','rows')'; % comment this line if you run out of memory
|
||||||
|
|
||||||
|
% Decompose B = R*R' using singular value or LU decomposition
|
||||||
|
[L,U,p] = lu(R(k,:)'*R(k,:),'vector'); % LU decomposition
|
||||||
|
|
||||||
|
for j=1:nimgs
|
||||||
|
b=R(k,:)'*A(k,j);
|
||||||
|
% Obtain coefficients c which minimise least-square residuals
|
||||||
|
lower.LT = true; upper.UT = true;
|
||||||
|
c = linsolve(U,linsolve(L,b(p,:),lower),upper);
|
||||||
|
|
||||||
|
% Compute optimised reference image
|
||||||
|
optrefimages(:,:,j)=reshape(R*c,[ydim xdim]);
|
||||||
|
end
|
||||||
|
end
|
304
Data-Analyzer/Deprecated/simulateDistribution.m
Normal file
304
Data-Analyzer/Deprecated/simulateDistribution.m
Normal file
@ -0,0 +1,304 @@
|
|||||||
|
%% Evolve across a second-order-like transition
|
||||||
|
clear; clc;
|
||||||
|
|
||||||
|
N_params = 50;
|
||||||
|
N_reps = 50;
|
||||||
|
alpha_values = linspace(0, 45, N_params);
|
||||||
|
all_data = cell(1, N_params);
|
||||||
|
|
||||||
|
% Transition control
|
||||||
|
alpha_start = 5; % where sigma starts changing
|
||||||
|
alpha_widen_end = 15; % when sigma finishes first change
|
||||||
|
alpha_shift_start = 15; % when mean starts shifting
|
||||||
|
alpha_end = 40; % when mean finishes shifting and sigma narrows
|
||||||
|
|
||||||
|
mu_start = 1.2; % high initial mean
|
||||||
|
mu_end = 0.2; % low final mean
|
||||||
|
|
||||||
|
sigma_start = 0.25; % wide std at start
|
||||||
|
sigma_mid = 0.15; % mid-range std in middle
|
||||||
|
sigma_end = 0.07; % narrow std at end
|
||||||
|
|
||||||
|
max_skew = 5; % peak skew strength
|
||||||
|
|
||||||
|
for i = 1:N_params
|
||||||
|
alpha = alpha_values(i);
|
||||||
|
|
||||||
|
% === Sigma evolution (variance large -> small) ===
|
||||||
|
if alpha < alpha_start
|
||||||
|
sigma = sigma_start; % wide at start
|
||||||
|
elseif alpha < alpha_widen_end
|
||||||
|
% Smooth transition from wide to mid
|
||||||
|
t_sigma = (alpha - alpha_start) / (alpha_widen_end - alpha_start);
|
||||||
|
sigma = sigma_start * (1 - t_sigma) + sigma_mid * t_sigma;
|
||||||
|
elseif alpha < alpha_end
|
||||||
|
% Smooth transition from mid to narrow
|
||||||
|
t_sigma = (alpha - alpha_widen_end) / (alpha_end - alpha_widen_end);
|
||||||
|
sigma = sigma_mid * (1 - t_sigma) + sigma_end * t_sigma;
|
||||||
|
else
|
||||||
|
sigma = sigma_end; % narrow at end
|
||||||
|
end
|
||||||
|
|
||||||
|
% === Mean evolution ===
|
||||||
|
if alpha < alpha_shift_start
|
||||||
|
mu = mu_start; % fixed at high initially
|
||||||
|
elseif alpha <= alpha_end
|
||||||
|
% Smooth cosine shift
|
||||||
|
t_mu = (alpha - alpha_shift_start) / (alpha_end - alpha_shift_start);
|
||||||
|
smooth_t_mu = (1 - cos(pi * t_mu)) / 2;
|
||||||
|
mu = mu_start * (1 - smooth_t_mu) + mu_end * smooth_t_mu;
|
||||||
|
else
|
||||||
|
mu = mu_end;
|
||||||
|
end
|
||||||
|
|
||||||
|
% === Skew evolution ===
|
||||||
|
if alpha < alpha_end
|
||||||
|
t_skew = (alpha - alpha_start) / (alpha_end - alpha_start);
|
||||||
|
skew_strength = max_skew * (1 - t_skew); % fade out
|
||||||
|
else
|
||||||
|
skew_strength = 0;
|
||||||
|
end
|
||||||
|
|
||||||
|
% Generate data
|
||||||
|
if abs(skew_strength) < 1e-2
|
||||||
|
data = normrnd(mu, sigma, [N_reps, 1]);
|
||||||
|
else
|
||||||
|
data = skewnormrnd(mu, sigma, skew_strength, N_reps);
|
||||||
|
end
|
||||||
|
|
||||||
|
all_data{i} = data;
|
||||||
|
|
||||||
|
% Cumulants
|
||||||
|
kappa = computeCumulants(data, 6);
|
||||||
|
mean_vals(i) = kappa(1);
|
||||||
|
var_vals(i) = kappa(2);
|
||||||
|
skew_vals(i) = kappa(3);
|
||||||
|
kappa4_vals(i) = kappa(4);
|
||||||
|
kappa5_vals(i) = kappa(5);
|
||||||
|
kappa6_vals(i) = kappa(6);
|
||||||
|
end
|
||||||
|
|
||||||
|
%% Evolve across a first-order-like transition
|
||||||
|
% First-order-like distribution evolution with significant bimodality
|
||||||
|
clear; clc;
|
||||||
|
|
||||||
|
N_params = 50;
|
||||||
|
N_reps = 50;
|
||||||
|
alpha_values = linspace(0, 45, N_params);
|
||||||
|
|
||||||
|
all_data = cell(1, N_params);
|
||||||
|
|
||||||
|
% Define transition regions
|
||||||
|
skewed_start = 10;
|
||||||
|
bimodal_start = 20;
|
||||||
|
bimodal_end = 35;
|
||||||
|
final_narrow_start = 40;
|
||||||
|
|
||||||
|
% Peak positions and widths
|
||||||
|
mu_high = 1.2; % Initial metastable peak
|
||||||
|
mu_low = 0.2; % Final stable peak
|
||||||
|
mu_new_peak = 0.8; % New peak appears slightly lower
|
||||||
|
sigma_initial = 0.08;
|
||||||
|
|
||||||
|
for i = 1:N_params
|
||||||
|
alpha = alpha_values(i);
|
||||||
|
|
||||||
|
if alpha < skewed_start
|
||||||
|
% Region I: Narrow unimodal at high mean
|
||||||
|
data = normrnd(mu_high, sigma_initial, [N_reps, 1]);
|
||||||
|
|
||||||
|
elseif alpha < bimodal_start
|
||||||
|
% Region II: Slightly skewed
|
||||||
|
t_skew = (alpha - skewed_start) / (bimodal_start - skewed_start);
|
||||||
|
mu = mu_high - 0.15 * t_skew;
|
||||||
|
sigma = sigma_initial + 0.02 * t_skew;
|
||||||
|
skew_strength = 3 * t_skew;
|
||||||
|
data = skewnormrnd(mu, sigma, skew_strength, N_reps);
|
||||||
|
|
||||||
|
elseif alpha < bimodal_end
|
||||||
|
% Region III: Bimodal with fixed or slowly drifting peak positions
|
||||||
|
t = (alpha - bimodal_start) / (bimodal_end - bimodal_start); % t in [0, 1]
|
||||||
|
|
||||||
|
% Increased separation between peaks
|
||||||
|
drift_amount = 0.3; % larger = more drift toward final mean
|
||||||
|
sep_offset = 0.25; % larger = more initial separation between peaks
|
||||||
|
|
||||||
|
% Peaks start separated and move toward mu_low
|
||||||
|
mu1 = mu_high * (1 - t)^drift_amount + mu_low * (1 - (1 - t)^drift_amount); % Right peak drifts to left
|
||||||
|
mu2 = (mu_new_peak - sep_offset) * (1 - t)^drift_amount + mu_low * (1 - (1 - t)^drift_amount); % Left peak moves slightly
|
||||||
|
|
||||||
|
sigma1 = sigma_initial + 0.02 * (1 - abs(0.5 - t) * 2);
|
||||||
|
sigma2 = sigma1;
|
||||||
|
|
||||||
|
% Weight shift: right peak dies out, left peak grows
|
||||||
|
w2 = 0.5 + 0.5 * t; % left peak grows: 0.5 → 1
|
||||||
|
w1 = 1 - w2; % right peak fades: 0.5 → 0
|
||||||
|
|
||||||
|
N1 = round(N_reps * w1);
|
||||||
|
N2 = N_reps - N1;
|
||||||
|
|
||||||
|
mode1 = normrnd(mu1, sigma1, [N1, 1]);
|
||||||
|
mode2 = normrnd(mu2, sigma2, [N2, 1]);
|
||||||
|
|
||||||
|
data = [mode1; mode2];
|
||||||
|
data = data(randperm(length(data)));
|
||||||
|
|
||||||
|
else
|
||||||
|
% Region IV: Final stable low-mean Gaussian
|
||||||
|
data = normrnd(mu_low, sigma_initial, [N_reps, 1]);
|
||||||
|
end
|
||||||
|
|
||||||
|
% Store data and compute cumulants
|
||||||
|
all_data{i} = data;
|
||||||
|
kappa = computeCumulants(data, 6);
|
||||||
|
mean_vals(i) = kappa(1);
|
||||||
|
var_vals(i) = kappa(2);
|
||||||
|
skew_vals(i) = kappa(3);
|
||||||
|
kappa4_vals(i) = kappa(4);
|
||||||
|
kappa5_vals(i) = kappa(5);
|
||||||
|
kappa6_vals(i) = kappa(6);
|
||||||
|
end
|
||||||
|
|
||||||
|
%% === Compute 2D PDF heatmap: f(x, alpha) ===
|
||||||
|
x_grid = linspace(0.0, 1.8, 200); % max[g²] values on y-axis
|
||||||
|
pdf_matrix = zeros(numel(x_grid), N_params); % Now: rows = y, columns = alpha
|
||||||
|
|
||||||
|
for i = 1:N_params
|
||||||
|
data = all_data{i};
|
||||||
|
f = ksdensity(data, x_grid, 'Bandwidth', 0.025);
|
||||||
|
pdf_matrix(:, i) = f; % Transpose for y-axis to be vertical
|
||||||
|
end
|
||||||
|
|
||||||
|
% === Plot PDF vs. alpha heatmap ===
|
||||||
|
figure(2); clf;
|
||||||
|
set(gcf, 'Color', 'w', 'Position',[100 100 950 750])
|
||||||
|
|
||||||
|
imagesc(alpha_values, x_grid, pdf_matrix);
|
||||||
|
set(gca, 'YDir', 'normal'); % Flip y-axis to normal orientation
|
||||||
|
xlabel('$\alpha$ (degrees)', 'Interpreter', 'latex', 'FontSize', 14);
|
||||||
|
ylabel('$\mathrm{max}[g^{(2)}]$', 'Interpreter', 'latex', 'FontSize', 14);
|
||||||
|
title('Evolving PDF of $\mathrm{max}[g^{(2)}]$', ...
|
||||||
|
'Interpreter', 'latex', 'FontSize', 16);
|
||||||
|
|
||||||
|
colormap(Colormaps.coolwarm()); % More aesthetic than default
|
||||||
|
colorbar;
|
||||||
|
c = colorbar;
|
||||||
|
ylabel(c, 'PDF', 'FontSize', 14, 'Interpreter', 'latex');
|
||||||
|
set(gca, 'FontSize', 14);
|
||||||
|
|
||||||
|
%% Animate evolving distribution and cumulant value
|
||||||
|
figure(1); clf;
|
||||||
|
set(gcf, 'Color', 'w', 'Position',[100 100 1300 750])
|
||||||
|
|
||||||
|
for i = 1:N_params
|
||||||
|
clf;
|
||||||
|
|
||||||
|
% PDF
|
||||||
|
subplot(1,2,1); cla; hold on;
|
||||||
|
data = all_data{i};
|
||||||
|
|
||||||
|
% Plot histogram with normalized PDF
|
||||||
|
histogram(data, 'Normalization', 'pdf', 'BinWidth', 0.03, ...
|
||||||
|
'FaceColor', [0.3 0.5 0.8], 'EdgeColor', 'k', 'FaceAlpha', 0.6);
|
||||||
|
|
||||||
|
title(sprintf('Histogram at $\\alpha = %.1f^\\circ$', alpha_values(i)), ...
|
||||||
|
'Interpreter', 'latex', 'FontSize', 16);
|
||||||
|
xlabel('$\mathrm{max}[g^{(2)}]$', 'Interpreter', 'latex', 'FontSize', 14);
|
||||||
|
ylabel('PDF', 'FontSize', 14);
|
||||||
|
set(gca, 'FontSize', 12); grid on;
|
||||||
|
xlim([0.0, 2.0]);
|
||||||
|
|
||||||
|
|
||||||
|
% Cumulant evolution
|
||||||
|
subplot(1,2,2); hold on;
|
||||||
|
plot(alpha_values(1:i), kappa4_vals(1:i), 'bo-', 'LineWidth', 2);
|
||||||
|
title('Binder Cumulant Tracking', 'Interpreter', 'latex', 'FontSize', 16);
|
||||||
|
xlabel('$\alpha$ (degrees)', 'Interpreter', 'latex', 'FontSize', 14);
|
||||||
|
ylabel('$\kappa_4$', 'Interpreter', 'latex', 'FontSize', 14);
|
||||||
|
xlim([0, 45]); grid on;
|
||||||
|
set(gca, 'FontSize', 12);
|
||||||
|
|
||||||
|
pause(0.3);
|
||||||
|
end
|
||||||
|
|
||||||
|
%% === Plotting ===
|
||||||
|
figure(1)
|
||||||
|
set(gcf, 'Color', 'w', 'Position', [100 100 950 750])
|
||||||
|
t = tiledlayout(2, 2, 'TileSpacing', 'compact', 'Padding', 'compact');
|
||||||
|
|
||||||
|
scan_vals = alpha_values; % your parameter sweep values
|
||||||
|
|
||||||
|
% Define font style for consistency
|
||||||
|
axis_fontsize = 14;
|
||||||
|
label_fontsize = 16;
|
||||||
|
title_fontsize = 16;
|
||||||
|
|
||||||
|
% 1. Mean with error bars (if you have error data, else just plot)
|
||||||
|
% If no error, replace errorbar with plot or omit error data
|
||||||
|
% For now, no error bars assumed
|
||||||
|
nexttile;
|
||||||
|
plot(scan_vals, mean_vals, 'o-', 'LineWidth', 1.5, 'MarkerSize', 6);
|
||||||
|
title('Mean', 'FontSize', title_fontsize, 'Interpreter', 'latex');
|
||||||
|
xlabel('$\alpha$ (degrees)', 'Interpreter', 'latex', 'FontSize', label_fontsize);
|
||||||
|
ylabel('$\kappa_1$', 'Interpreter', 'latex', 'FontSize', label_fontsize);
|
||||||
|
set(gca, 'FontSize', axis_fontsize);
|
||||||
|
grid on;
|
||||||
|
|
||||||
|
% 2. Variance
|
||||||
|
nexttile;
|
||||||
|
plot(scan_vals, var_vals, 's-', 'LineWidth', 1.5, 'MarkerSize', 6);
|
||||||
|
title('Variance', 'FontSize', title_fontsize, 'Interpreter', 'latex');
|
||||||
|
xlabel('$\alpha$ (degrees)', 'Interpreter', 'latex', 'FontSize', label_fontsize);
|
||||||
|
ylabel('$\kappa_2$', 'Interpreter', 'latex', 'FontSize', label_fontsize);
|
||||||
|
set(gca, 'FontSize', axis_fontsize);
|
||||||
|
grid on;
|
||||||
|
|
||||||
|
% 3. Skewness
|
||||||
|
nexttile;
|
||||||
|
plot(scan_vals, skew_vals, 'd-', 'LineWidth', 1.5, 'MarkerSize', 6);
|
||||||
|
title('Skewness', 'FontSize', title_fontsize, 'Interpreter', 'latex');
|
||||||
|
xlabel('$\alpha$ (degrees)', 'Interpreter', 'latex', 'FontSize', label_fontsize);
|
||||||
|
ylabel('$\kappa_3$', 'Interpreter', 'latex', 'FontSize', label_fontsize);
|
||||||
|
set(gca, 'FontSize', axis_fontsize);
|
||||||
|
grid on;
|
||||||
|
|
||||||
|
% 4. Binder Cumulant
|
||||||
|
nexttile;
|
||||||
|
plot(scan_vals, kappa4_vals, '^-', 'LineWidth', 1.5, 'MarkerSize', 6);
|
||||||
|
title('Binder Cumulant', 'FontSize', title_fontsize, 'Interpreter', 'latex');
|
||||||
|
xlabel('$\alpha$ (degrees)', 'Interpreter', 'latex', 'FontSize', label_fontsize);
|
||||||
|
ylabel('$\kappa_4$', 'Interpreter', 'latex', 'FontSize', label_fontsize);
|
||||||
|
set(gca, 'FontSize', axis_fontsize);
|
||||||
|
grid on;
|
||||||
|
|
||||||
|
% Super title (you can customize the string)
|
||||||
|
sgtitle('Cumulants of a simulated evolving distribution', ...
|
||||||
|
'FontWeight', 'bold', 'FontSize', 18, 'Interpreter', 'latex');
|
||||||
|
|
||||||
|
%% === Helper: Cumulant Calculation ===
|
||||||
|
function kappa = computeCumulants(data, max_order)
|
||||||
|
data = data(:);
|
||||||
|
mu = mean(data);
|
||||||
|
c = zeros(1, max_order);
|
||||||
|
centered = data - mu;
|
||||||
|
for n = 1:max_order
|
||||||
|
c(n) = mean(centered.^n);
|
||||||
|
end
|
||||||
|
kappa = zeros(1, max_order);
|
||||||
|
kappa(1) = mu;
|
||||||
|
kappa(2) = c(2);
|
||||||
|
kappa(3) = c(3);
|
||||||
|
kappa(4) = c(4) - 3*c(2)^2;
|
||||||
|
kappa(5) = c(5) - 10*c(3)*c(2);
|
||||||
|
kappa(6) = c(6) - 15*c(4)*c(2) - 10*c(3)^2 + 30*c(2)^3;
|
||||||
|
end
|
||||||
|
|
||||||
|
%% === Helper: Skewed Normal Distribution ===
|
||||||
|
function x = skewnormrnd(mu, sigma, alpha, n)
|
||||||
|
% Skew-normal using Azzalini's method
|
||||||
|
delta = alpha / sqrt(1 + alpha^2);
|
||||||
|
u0 = randn(n,1);
|
||||||
|
v = randn(n,1);
|
||||||
|
u1 = delta * u0 + sqrt(1 - delta^2) * v;
|
||||||
|
x = mu + sigma * u1 .* sign(u0);
|
||||||
|
end
|
223
Data-Analyzer/Deprecated/understandingCumulants.m
Normal file
223
Data-Analyzer/Deprecated/understandingCumulants.m
Normal file
@ -0,0 +1,223 @@
|
|||||||
|
%% Main script: Sweep different parameter pairs
|
||||||
|
% Default parameters
|
||||||
|
defaults.mu1 = 0.5;
|
||||||
|
defaults.mu2 = 1.0;
|
||||||
|
defaults.sigma1 = 0.1;
|
||||||
|
defaults.sigma2 = 0.1;
|
||||||
|
defaults.weight1 = 0.5;
|
||||||
|
|
||||||
|
% Parameter pair definitions
|
||||||
|
%{
|
||||||
|
param_pairs = {
|
||||||
|
'mu1', linspace(0.7, 1.0, 40), ...
|
||||||
|
'mu2', linspace(1.0, 1.3, 40);
|
||||||
|
|
||||||
|
'mu1', linspace(0.7, 1.0, 40), ...
|
||||||
|
'weight1', linspace(0.2, 0.8, 40);
|
||||||
|
|
||||||
|
'sigma1', linspace(0.05, 0.2, 40), ...
|
||||||
|
'sigma2', linspace(0.05, 0.2, 40);
|
||||||
|
|
||||||
|
'mu1', linspace(0.7, 1.0, 40), ...
|
||||||
|
'sigma1', linspace(0.05, 0.2, 40);
|
||||||
|
|
||||||
|
'mu2', linspace(1.0, 1.3, 40), ...
|
||||||
|
'weight1', linspace(0.2, 0.8, 40);
|
||||||
|
};
|
||||||
|
%}
|
||||||
|
param_pairs = {
|
||||||
|
'mu1', linspace(0.1, 1.5, 40), ...
|
||||||
|
'mu2', linspace(0.1, 1.5, 40);
|
||||||
|
};
|
||||||
|
|
||||||
|
% Cumulant index to visualize (2=variance, 3=skewness, 4=kurtosis)
|
||||||
|
cumulant_to_plot = 4;
|
||||||
|
|
||||||
|
% Run sweep for each pair
|
||||||
|
for i = 1:size(param_pairs,1)
|
||||||
|
param1_name = param_pairs{i,1};
|
||||||
|
param1_vals = param_pairs{i,2};
|
||||||
|
param2_name = param_pairs{i,3};
|
||||||
|
param2_vals = param_pairs{i,4};
|
||||||
|
|
||||||
|
fprintf('Sweeping %s and %s...\n', param1_name, param2_name);
|
||||||
|
|
||||||
|
Z = sweepBimodalCumulants(param1_name, param1_vals, ...
|
||||||
|
param2_name, param2_vals, ...
|
||||||
|
defaults, cumulant_to_plot);
|
||||||
|
end
|
||||||
|
|
||||||
|
%%
|
||||||
|
% Parameters
|
||||||
|
N_total = 10000;
|
||||||
|
mu1 = 0.5;
|
||||||
|
sigma1 = 0.1;
|
||||||
|
mu2 = 1.0;
|
||||||
|
sigma2 = 0.1;
|
||||||
|
weight1 = 0.7;
|
||||||
|
|
||||||
|
% Generate data
|
||||||
|
data = generateBimodalDistribution(N_total, mu1, mu2, sigma1, sigma2, weight1);
|
||||||
|
|
||||||
|
% Plot histogram
|
||||||
|
figure(3);
|
||||||
|
clf
|
||||||
|
set(gcf, 'Color', 'w', 'Position', [100 100 950 750]);
|
||||||
|
histogram(data, 'Normalization', 'pdf', 'EdgeColor', 'none', 'FaceAlpha', 0.5);
|
||||||
|
hold on;
|
||||||
|
|
||||||
|
% Overlay smooth density estimate
|
||||||
|
[xi, f] = ksdensity(data);
|
||||||
|
plot(f, xi, 'r-', 'LineWidth', 2);
|
||||||
|
|
||||||
|
% Labels and title
|
||||||
|
xlabel('Value');
|
||||||
|
ylabel('Probability Density');
|
||||||
|
legend('Histogram', 'Smoothed Density');
|
||||||
|
grid on;
|
||||||
|
|
||||||
|
%%
|
||||||
|
N = size(Z, 1);
|
||||||
|
|
||||||
|
main_diag_values = diag(Z);
|
||||||
|
anti_diag_values = diag(flipud(Z));
|
||||||
|
|
||||||
|
param1_diag_main = param1_vals;
|
||||||
|
param2_diag_main = param2_vals;
|
||||||
|
|
||||||
|
param1_diag_anti = param1_vals;
|
||||||
|
param2_diag_anti = flip(param2_vals);
|
||||||
|
|
||||||
|
% For example, plot the main diagonal cumulants:
|
||||||
|
figure(4);
|
||||||
|
set(gcf, 'Color', 'w', 'Position', [100 100 950 750]);
|
||||||
|
plot(1:N, main_diag_values, '-o');
|
||||||
|
xlabel('Index along diagonal');
|
||||||
|
ylabel('$\kappa_4$', 'Interpreter', 'latex');
|
||||||
|
title('$\kappa_4$ along anti-diagonal', 'Interpreter', 'latex');
|
||||||
|
|
||||||
|
% Plot anti-diagonal cumulants:
|
||||||
|
figure(5);
|
||||||
|
set(gcf, 'Color', 'w', 'Position', [100 100 950 750]);
|
||||||
|
plot(1:N, anti_diag_values, '-o');
|
||||||
|
xlabel('Index along anti-diagonal');
|
||||||
|
ylabel('$\kappa_4$', 'Interpreter', 'latex');
|
||||||
|
title('$\kappa_4$ along anti-diagonal', 'Interpreter', 'latex');
|
||||||
|
|
||||||
|
%% === Helper: Bimodal Distribution ===
|
||||||
|
function data = generateBimodalDistribution(N_total, mu1, mu2, sigma1, sigma2, weight1)
|
||||||
|
%GENERATEBIMODALDISTRIBUTION Generates a single bimodal distribution.
|
||||||
|
%
|
||||||
|
% data = generateBimodalDistribution(N_total, mu1, mu2, sigma1, sigma2, weight1)
|
||||||
|
%
|
||||||
|
% Inputs:
|
||||||
|
% N_total - total number of samples
|
||||||
|
% mu1, mu2 - means of the two modes
|
||||||
|
% sigma1, sigma2 - standard deviations of the two modes
|
||||||
|
% weight1 - fraction of samples from mode 1 (between 0 and 1)
|
||||||
|
%
|
||||||
|
% Output:
|
||||||
|
% data - shuffled samples from the bimodal distribution
|
||||||
|
|
||||||
|
% Validate weight
|
||||||
|
weight1 = min(max(weight1, 0), 1);
|
||||||
|
weight2 = 1 - weight1;
|
||||||
|
|
||||||
|
% Determine number of samples for each mode
|
||||||
|
N1 = round(N_total * weight1);
|
||||||
|
N2 = N_total - N1;
|
||||||
|
|
||||||
|
% Generate samples
|
||||||
|
mode1_samples = normrnd(mu1, sigma1, [N1, 1]);
|
||||||
|
mode2_samples = normrnd(mu2, sigma2, [N2, 1]);
|
||||||
|
|
||||||
|
% Combine and shuffle
|
||||||
|
data = [mode1_samples; mode2_samples];
|
||||||
|
data = data(randperm(length(data)));
|
||||||
|
end
|
||||||
|
|
||||||
|
%% === Helper: Cumulant Calculation ===
|
||||||
|
function kappa = computeCumulants(data, max_order)
|
||||||
|
data = data(:);
|
||||||
|
mu = mean(data);
|
||||||
|
centered = data - mu;
|
||||||
|
|
||||||
|
% Preallocate
|
||||||
|
c = zeros(1, max_order);
|
||||||
|
kappa = zeros(1, max_order);
|
||||||
|
|
||||||
|
% Compute central moments up to max_order
|
||||||
|
for n = 1:max_order
|
||||||
|
c(n) = mean(centered.^n);
|
||||||
|
end
|
||||||
|
|
||||||
|
% Assign cumulants based on available order
|
||||||
|
if max_order >= 1, kappa(1) = mu; end
|
||||||
|
if max_order >= 2, kappa(2) = c(2); end
|
||||||
|
if max_order >= 3, kappa(3) = c(3); end
|
||||||
|
if max_order >= 4, kappa(4) = c(4) - 3*c(2)^2; end
|
||||||
|
if max_order >= 5, kappa(5) = c(5) - 10*c(3)*c(2); end
|
||||||
|
if max_order >= 6
|
||||||
|
kappa(6) = c(6) - 15*c(4)*c(2) - 10*c(3)^2 + 30*c(2)^3;
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
%% === Helper: Cumulant Calculation ===
|
||||||
|
function Z = sweepBimodalCumulants(param1_name, param1_vals, ...
|
||||||
|
param2_name, param2_vals, ...
|
||||||
|
fixed_params, ...
|
||||||
|
cumulant_index)
|
||||||
|
%SWEEPBIMODALCUMULANTS Sweep 2 parameters and return a chosen cumulant.
|
||||||
|
%
|
||||||
|
% Z = sweepBimodalCumulants(...)
|
||||||
|
% Returns a matrix Z of cumulant values at each grid point.
|
||||||
|
|
||||||
|
% Setup grid
|
||||||
|
[P1, P2] = meshgrid(param1_vals, param2_vals);
|
||||||
|
Z = zeros(size(P1));
|
||||||
|
|
||||||
|
N_samples = 1000;
|
||||||
|
maxOrder = max(4, cumulant_index);
|
||||||
|
|
||||||
|
for i = 1:numel(P1)
|
||||||
|
% Copy fixed parameters
|
||||||
|
params = fixed_params;
|
||||||
|
|
||||||
|
% Override swept parameters
|
||||||
|
params.(param1_name) = P1(i);
|
||||||
|
params.(param2_name) = P2(i);
|
||||||
|
|
||||||
|
% Generate and compute cumulants
|
||||||
|
data = generateBimodalDistribution(N_samples, ...
|
||||||
|
params.mu1, params.mu2, ...
|
||||||
|
params.sigma1, params.sigma2, ...
|
||||||
|
params.weight1);
|
||||||
|
|
||||||
|
kappa = computeCumulants(data, maxOrder);
|
||||||
|
Z(i) = kappa(cumulant_index);
|
||||||
|
end
|
||||||
|
|
||||||
|
% Plot full heatmap
|
||||||
|
figure;
|
||||||
|
set(gcf, 'Color', 'w', 'Position', [100 100 950 750]);
|
||||||
|
imagesc(param1_vals, param2_vals, Z);
|
||||||
|
set(gca, 'YDir', 'normal');
|
||||||
|
xlabel(param1_name);
|
||||||
|
ylabel(param2_name);
|
||||||
|
title(['Cumulant \kappa_', num2str(cumulant_index)]);
|
||||||
|
colorbar;
|
||||||
|
axis tight;
|
||||||
|
|
||||||
|
% Optional binary colormap (red = ≥0, blue = <0)
|
||||||
|
figure;
|
||||||
|
set(gcf, 'Color', 'w', 'Position', [100 100 950 750]);
|
||||||
|
imagesc(param1_vals, param2_vals, Z);
|
||||||
|
set(gca, 'YDir', 'normal');
|
||||||
|
xlabel(param1_name);
|
||||||
|
ylabel(param2_name);
|
||||||
|
title(['Binary color split of \kappa_', num2str(cumulant_index)]);
|
||||||
|
clim([-1 1]);
|
||||||
|
colormap([0 0 1; 1 0 0]); % Blue (neg), Red (pos & zero)
|
||||||
|
colorbar;
|
||||||
|
axis tight;
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user