function [contrast, periodX, periodY] = analyzeGSWavefunction(folder_path, run_index, LatticePropertyDisplayFlag)
    
    arguments
        folder_path                (1,:) char 
        run_index                  (1,:) {mustBeNumeric,mustBeReal}
        LatticePropertyDisplayFlag (1,:) logical = true
    end

    set(0,'defaulttextInterpreter','latex')
    set(groot, 'defaultAxesTickLabelInterpreter','latex'); set(groot, 'defaultLegendInterpreter','latex');
    
    % Load data
    Data    = load(sprintf(horzcat(folder_path, '/Run_%03i/psi_gs.mat'),run_index),'psi','Transf','Observ','Params','VParams');
    
    Transf  = Data.Transf;
    Observ  = Data.Observ; 
    Params  = Data.Params;
    VParams = Data.VParams;

    if isgpuarray(Data.psi)
        psi = gather(Data.psi);
    else
        psi = Data.psi;
    end
    
    if isgpuarray(Data.Observ.residual)
        Observ.residual = gather(Data.Observ.residual);
    else
        Observ.residual = Data.Observ.residual;
    end

    % Set long format for output
    format long
    
    % Coordinates in micrometers
    x            = Transf.x * Params.l0; 
    y            = Transf.y * Params.l0; 
    
    % Compute probability density |psi|^2
    nxy          = abs(psi).^2;
    nxyScaled    = nxy*(Params.add*10^6)^2;

    %------------------ Calculate contrast ------------------ %
    
    % Find unique maximum intensity values
    uniqueMaxValues = unique(nxyScaled(nxyScaled == max(nxyScaled(:))));
    if length(uniqueMaxValues) > 1
        maxIntensity = median(uniqueMaxValues);  % Choose the median of max values
    else
        maxIntensity = uniqueMaxValues;          % If only one, take the unique value
    end

    % Find unique minimum intensity values
    uniqueMinValues = unique(nxyScaled(nxyScaled == min(nxyScaled(:))));
    if length(uniqueMinValues) > 1
        minIntensity = median(uniqueMinValues);  % Choose the median of min values
    else
        minIntensity = uniqueMinValues;          % If only one, take the unique value
    end

    contrast = (maxIntensity - minIntensity) / (maxIntensity + minIntensity);
    
    %------------------ Lattice Properties ------------------ %

    [fftMagnitude, lattice_type, periodX, periodY, freq_x, freq_y, kx, ky] = Scripts.extractLatticeProperties(nxyScaled, x, y);
    
    %------------------ Plotting ------------------ %

    figure('Position', [100, 100, 1600, 800]);
    clf
    t = tiledlayout(1, 2, 'TileSpacing', 'compact', 'Padding', 'compact'); % 2x3 grid
    
    % Plot |psi(x,y)|^2 (density in x and y plane)
    nexttile;  % Equivalent to subplot('Position', [0.05, 0.55, 0.28, 0.4]);
    plotxy    = pcolor(x/Params.l0,y/Params.l0,nxyScaled');
    set(plotxy, 'EdgeColor', 'none');
    cbar1 = colorbar;
    cbar1.Label.Interpreter = 'latex';
    colormap(gca, Helper.Colormaps.plasma())
    % clim(ax1,[0.00,0.3]);
    
    if LatticePropertyDisplayFlag
        % Define normalized positions (relative to axis limits)
        x_offset = 0.025; % 5% offset from the edges
        y_offset = 0.025; % 5% offset from the edges
        
        % Top-left corner (normalized axis coordinates)
        text(0 + x_offset, 1 - y_offset, lattice_type, ...
            'Color', 'white', 'FontWeight', 'bold', 'Interpreter', 'tex', 'FontSize', 30, 'Units', 'normalized', 'HorizontalAlignment', 'left', 'VerticalAlignment', 'top');
        
        % Top-right corner (normalized axis coordinates)
        text(1 - x_offset, 1 - y_offset, ['C: ', num2str(contrast, '%.3f')], ...
            'Color', 'white', 'FontWeight', 'bold', 'Interpreter', 'tex', 'FontSize', 30, 'Units', 'normalized', 'HorizontalAlignment', 'right', 'VerticalAlignment', 'top');
        
        % Bottom-left corner (normalized axis coordinates)
        text(0 + x_offset, 0 + y_offset, ['dx: ', num2str(periodX, '%.2f'), ' \mum'], ...
            'Color', 'white', 'FontWeight', 'bold', 'Interpreter', 'tex', 'FontSize', 30, 'Units', 'normalized', 'HorizontalAlignment', 'left', 'VerticalAlignment', 'bottom');
        
        % Bottom-right corner (normalized axis coordinates)
        text(1 - x_offset, 0 + y_offset, ['dy: ', num2str(periodY, '%.2f'), ' \mum'], ...
            'Color', 'white', 'FontWeight', 'bold', 'Interpreter', 'tex', 'FontSize', 30, 'Units', 'normalized', 'HorizontalAlignment', 'right', 'VerticalAlignment', 'bottom');
    end

    ylabel(cbar1,'$na_{dd}^2$','FontSize',16,'Rotation',270)
    xlabel('$x$ ($\mu$m)', 'Interpreter', 'latex', 'FontSize', 14)
    ylabel('$y$ ($\mu$m)', 'Interpreter', 'latex', 'FontSize', 14)
    title('$|\Psi(x,y)|^2$ ', 'Interpreter', 'latex', 'FontSize', 14)
    
    % Plot 2-D FFT with detected peaks
    nexttile;  % Equivalent to subplot('Position', [0.05, 0.55, 0.28, 0.4]);
    imagesc(kx*Params.l0,ky*Params.l0, log(1 + fftMagnitude));
    cbar1 = colorbar;
    cbar1.Label.Interpreter = 'latex';
    colormap(gca, 'jet')
    hold on;
    plot(2*pi*freq_x*Params.l0, 2*pi*freq_y*Params.l0, 'ro', 'MarkerSize', 10, 'LineWidth', 2);
    hold off;
    xlabel('$k_x l_o$', 'Interpreter', 'latex', 'FontSize', 14)
    ylabel('$k_y l_o$', 'Interpreter', 'latex', 'FontSize', 14)
    title('$\mathcal{F}\{|\Psi(x,y)|^2\}$', 'Interpreter', 'latex', 'FontSize', 16);
    
    sgtitle(['$\omega_z = 2 \pi \times$', num2str(round(Params.wz/(2*pi)), '%.2f'), ' Hz; $\theta = $', num2str(rad2deg(Params.theta), '%.2f'), '$^\circ$'], 'Interpreter', 'latex', 'FontSize', 18, 'FontWeight', 'bold')

end