function [barcodeGen] = mask_peaks(barcodeGen,mask_lim)
if nargin<2
mask_lim= 3.5; %stds from mean to mask
end

for t = 1:numel(barcodeGen)
    barcode = double(barcodeGen{t}.rawBarcode);   % ensure numeric
    bitmask = barcodeGen{t}.rawBitmask;
    valid_vals = barcode(bitmask == 1);     % Only consider currently unmasked regions
    mu = mean(valid_vals, 'omitnan');
    sigma = std(valid_vals, 'omitnan');
    new_mask = bitmask & abs(barcode - mu) <= mask_lim * sigma;     % Build new bitmask: keep old mask AND remove extreme values
    barcodeGen{t}.peakBitmask = new_mask;  % Replace bitmask    % Save into new struct
end

end
