Skip to content

Pseudo-invert only the kept components in ASR reconstruction - #52

Merged
arnodelorme merged 1 commit into
masterfrom
fix/asr-reconstruction-kept-rows
Nov 28, 2026
Merged

arnodelorme merged 1 commit into
masterfrom
fix/asr-reconstruction-kept-rows

Conversation

@arnodelorme

@arnodelorme arnodelorme commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #51 and pairs with the Python change in sccn/eegprep#832. Please merge the two pull requests together so both sides compute the same reconstruction matrix.

  • What changed: asr_process.m and asr_process_r.m now pseudo-invert only the kept rows of V' * M and place the result into the kept columns of Rinv (C x C), which equals pinv(bsxfun(@times, keep', V' * M)) in exact arithmetic while keeping zeroed rows out of the singular value decomposition.
  • Variable name: Stores the pseudo-inverse in Rinv instead of P, because P already holds the look-ahead length in samples in both files (overwriting P caused 13 of 13 recordings in sccn/eegprep_parity_test to fail at the first rejected component). Keeps the existing Windows line endings of asr_process.m.
  • MATLAB and Octave LAPACK behavior: Over 500 adversarial seeds (27 x 27, 1 rejected component, kept singular values in [31.8, 31.9]), MATLAB R2025b on Apple silicon (NAG Performance Components 1.2.1, LAPACK 3.9.1) inverted the zeroed row in 0 of 500 cases (s_zero / cutoff <= 0.069), whereas OpenBLAS 0.3.34 inverted it in 266 of 500 cases (scaling R up to 1.43e14), which affects GNU Octave builds linked to OpenBLAS as well as Python.
  • MATLAB versus Python pinv and R parity: Across all 500 adversarial seeds and 500 random (C, k) cases (including k = 0 all rejected, k = C all kept, and rank-deficient matrices), R in MATLAB (Pseudo-invert only the kept components in ASR reconstruction #52) matches Python (sccn/eegprep#832) within 1.75e-14 relative 2-norm across Apple Accelerate and OpenBLAS (direct pinv within 2.8e-15 on Accelerate and 2.2e-14 on OpenBLAS, and 0 on empty and zero matrices).
  • End-to-end checks (ds003061): In clean_asr repair mode (BurstRejection off) on sub-001, MATLAB Pseudo-invert only the kept components in ASR reconstruction #52 and Python #832 differ by at most 5.46e-11 µV (2.92e-13 µV RMS) for standard ASR and 1.85e-7 µV (1.85e-9 µV RMS) for Riemannian ASR, identical to the master versus develop difference before this change (5.56e-11 µV and 1.85e-7 µV). With BurstRejection on (sccn/eegprep_parity_test), all 13 recordings are bit for bit identical to master and match native arm64 Python (#832 + #833) in channel and sample count on 13 of 13 recordings.
Comparison of MATLAB pinv.m and Python eegprep pinv

Both pinv.m in MATLAB R2025b and eegprep.functions.miscfunc.pinv.pinv in Python compute a thin singular value decomposition and drop singular values at or below max(size(A)) * eps(s_1). When k of C components are kept (0 <= k <= C), V(:, keep)' * M has shape k x C, so max(k, C) = C preserves the exact C * eps(s_1) cutoff of the C x C masked matrix (and returns an empty C x 0 zero matrix when k = 0, leaving R = zeros(C, C) on both sides). Inside clean_asr, asr_process, and asr_process_r, M and V are always double (float64) on both sides. Note that standalone pinv(single(A)) in MATLAB uses the single cutoff and returns single, whereas eegprep's standalone pinv casts its input to float64.

Why BurstRejection on is bit for bit identical to master

With BurstRejection set to 'on', R is only used to identify which samples ASR modified, and those windows are then removed. Kept samples never pass through R, so the final output is bit for bit identical to master. Repair mode (BurstRejection set to 'off') is the mode where R enters the output, and there master and this branch differ by at most 2.47e-11 µV (standard) and 1.44e-12 µV (Riemannian) on sub-001.

The reconstruction matrix was R = M*pinv(diag(keep)*V'*M)*V'. Each
rejected component zeroes one row of that matrix, so its singular value
is 0 in exact arithmetic, but the SVD inside pinv returns it as roundoff
of the order of pinv's own cutoff and can invert it, which scales R by
about 1e13 (issue #51). Pseudo-inverting only the kept rows and placing
the result in the kept columns gives the same matrix in exact arithmetic
and cannot invert a rejected component. The new variable is Rinv because
P already holds the look-ahead length in samples.

Applied to asr_process.m and asr_process_r.m.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ASR reconstruction can invert a rejected component's roundoff singular value

1 participant