Repository navigation
Pseudo-invert only the kept components in ASR reconstruction - #52
Merged
Merged
Conversation
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]>
This was referenced Nov 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
asr_process.mandasr_process_r.mnow pseudo-invert only the kept rows ofV' * Mand place the result into the kept columns ofRinv(C x C), which equalspinv(bsxfun(@times, keep', V' * M))in exact arithmetic while keeping zeroed rows out of the singular value decomposition.Rinvinstead ofP, becausePalready holds the look-ahead length in samples in both files (overwritingPcaused 13 of 13 recordings insccn/eegprep_parity_testto fail at the first rejected component). Keeps the existing Windows line endings ofasr_process.m.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 in0of500cases (s_zero / cutoff <= 0.069), whereas OpenBLAS 0.3.34 inverted it in266of500cases (scalingRup to1.43e14), which affects GNU Octave builds linked to OpenBLAS as well as Python.pinvandRparity: Across all 500 adversarial seeds and 500 random(C, k)cases (includingk = 0all rejected,k = Call kept, and rank-deficient matrices),Rin MATLAB (Pseudo-invert only the kept components in ASR reconstruction #52) matches Python (sccn/eegprep#832) within1.75e-14relative 2-norm across Apple Accelerate and OpenBLAS (direct pinvwithin2.8e-15on Accelerate and2.2e-14on OpenBLAS, and0on empty and zero matrices).ds003061): Inclean_asrrepair mode (BurstRejectionoff) onsub-001, MATLAB Pseudo-invert only the kept components in ASR reconstruction #52 and Python #832 differ by at most5.46e-11µV (2.92e-13µV RMS) for standard ASR and1.85e-7µV (1.85e-9µV RMS) for Riemannian ASR, identical to themasterversusdevelopdifference before this change (5.56e-11µV and1.85e-7µV). WithBurstRejectionon (sccn/eegprep_parity_test), all 13 recordings are bit for bit identical tomasterand match nativearm64Python (#832 + #833) in channel and sample count on 13 of 13 recordings.Comparison of MATLAB pinv.m and Python eegprep pinv
Both
pinv.min MATLAB R2025b andeegprep.functions.miscfunc.pinv.pinvin Python compute a thin singular value decomposition and drop singular values at or belowmax(size(A)) * eps(s_1). WhenkofCcomponents are kept (0 <= k <= C),V(:, keep)' * Mhas shapek x C, somax(k, C) = Cpreserves the exactC * eps(s_1)cutoff of theC x Cmasked matrix (and returns an emptyC x 0zero matrix whenk = 0, leavingR = zeros(C, C)on both sides). Insideclean_asr,asr_process, andasr_process_r,MandVare alwaysdouble(float64) on both sides. Note that standalonepinv(single(A))in MATLAB uses thesinglecutoff and returnssingle, whereaseegprep's standalonepinvcasts its input tofloat64.Why BurstRejection on is bit for bit identical to master
With
BurstRejectionset to'on',Ris only used to identify which samples ASR modified, and those windows are then removed. Kept samples never pass throughR, so the final output is bit for bit identical tomaster. Repair mode (BurstRejectionset to'off') is the mode whereRenters the output, and theremasterand this branch differ by at most2.47e-11µV (standard) and1.44e-12µV (Riemannian) onsub-001.