Files

76 lines
3.5 KiB
Matlab

function DataTable = nappDetermineShiftXps(DataTable, processSelection, options)
% NAPPDETERMINESHIFT Calculate binding energy axis shifts based on a spectrum's own fitted peak.
%
% DataTable = NAPPDETERMINESHIFT(DataTable, processSelection) calculates
% the binding energy shift for spectra where processSelection is true, based
% on their own fitted peak position (internal reference).
%
% NOTE: This function requires that peak fitting (e.g., nappFitGaussXps)
% has already been performed so that the 'peakPosition' column exists.
%
% Name-Value arguments:
% referenceBindingEnergy - The theoretical energy to align to (e.g., 285.0 for C1s)
% peakNumber - Which peak position to use if multiple exist (1 by default)
arguments
DataTable table
processSelection logical {mustBeVector}
% Name-Value arguments
options.referenceBindingEnergy (1,1) double = 284.8 % Default to C1s binding energy
options.peakNumber (1,1) double {mustBeInteger, mustBePositive} = 1
end
numRowsTable = height(DataTable);
% Validate length of the logical array
if length(processSelection) ~= numRowsTable
error('processSelection must have the exact same length as the DataTable height (%d).', numRowsTable);
end
% Ensure peak positions exist (Fit must be run first!)
if ~ismember('peakPosition', DataTable.Properties.VariableNames)
error('The column "peakPosition" does not exist. Please run nappFitGaussXps before calculating shifts.');
end
% Initialize new columns to store the shift data if they don't exist
if ~ismember('bindingEnergyShift', DataTable.Properties.VariableNames)
DataTable.bindingEnergyShift = zeros(numRowsTable, 1);
end
if ~ismember('energyShiftSource', DataTable.Properties.VariableNames)
DataTable.energyShiftSource = cell(numRowsTable, 1);
end
%% Processing Loop
processIdx = find(processSelection);
for i = 1:length(processIdx)
currRow = processIdx(i);
currName = DataTable.name{currRow};
% Extract the peak position
refPositions = DataTable.peakPosition(currRow, :);
if size(refPositions, 2) < options.peakNumber || isnan(refPositions(options.peakNumber))
warning('Row %d (%s): peakNumber %d does not contain a valid fitted position. Skipping.', ...
currRow, currName, options.peakNumber);
continue;
end
% Calculate the shift: Shift = Reference Binding Energy - Measured Energy
measuredEnergy = refPositions(options.peakNumber);
% Validate that xRayEkin_eV exists and contains a valid scalar for this row
if ~ismember('xRayEkin_eV', DataTable.Properties.VariableNames)
error('The column "xRayEkin_eV" does not exist in DataTable. Cannot compute binding energy shift.');
end
xRayVal = DataTable.xRayEkin_eV{currRow};
if isempty(xRayVal) || ~(isnumeric(xRayVal) && isscalar(xRayVal)) || isnan(xRayVal)
error('Row %d (%s): xRayEkin_eV is empty, non-scalar, or NaN. Aborting shift calculation.', currRow, currName);
end
calculatedShift = xRayVal - options.referenceBindingEnergy - measuredEnergy;
% Log the shift and its source
DataTable.bindingEnergyShift(currRow) = calculatedShift;
DataTable.energyShiftSource{currRow} = num2str(options.referenceBindingEnergy);
end
end