m32 · Capstone: ECG imaging end to end (EDGAR)
Education only. Not for diagnosis, treatment or any clinical decision.
Draft, stage 2 of 6. The derivations are written, and each equation is checked by tests in tests/test_m32_ecgi.py. Demos, the guided code lab, questions and the remaining sections follow in later stages (plan: docs/outlines/m32-outline.md). Nothing here has had expert review yet. How the lab obtains EDGAR data is still an open question (outline question 1).
In 60 seconds
Electrocardiographic imaging (ECGI) estimates electrical activity on the surface of the heart from many electrodes on the body. The forward step, from heart to body, is linear and well behaved. The inverse step is not: the torso smooths away fine detail, so tiny noise on the body can produce wild errors on the heart unless the solution is regularized. This capstone builds the pipeline twice: first on nested spheres, where everything is known in closed form, then on an EDGAR dataset in which the heart surface was also measured directly, so the reconstruction can be scored.
From the field
Planned in the outline (docs/outlines/m32-outline.md); written in a later stage.
Derivations
Symbols used throughout. \(\boldsymbol\varphi_H\) potentials at \(n_H\) points on the heart surface, \(\boldsymbol\varphi_B\) potentials at \(n_B\) body-surface electrodes. \(A\) the forward (transfer) matrix. \(\mathbf{e}\) measurement noise. \(\lambda\) the regularization weight and \(L\) the regularization operator. \(s_i\), \(\mathbf{u}_i\), \(\mathbf{v}_i\) the singular values and singular vectors of \(A\). \(r_H\), \(r_T\) heart and torso radii, \(\rho = r_H/r_T\), \(l\) the degree of a spherical harmonic.
The data
EDGAR, the Experimental Data and Geometric Analysis Repository, is a curated archive of experimental, clinical and simulated data for testing ECGI methods, run for the Consortium for ECG Imaging (Aras et al. 2015). Its user guide, checked on 2026-11-04, says that downloads need an account and agreement to an access statement, and that users should cite each dataset’s source and acknowledge the Consortium (Good et al. 2015). Datasets hold time signals, geometry in millimetres, forward and inverse transforms as matrices, and sometimes images. The guide does not name a license or say whether data may be redistributed, so this course does not commit EDGAR data.
The forward problem
Between the heart surface and the body surface the torso is a passive volume conductor (m02, m12). Potentials there satisfy Laplace’s equation, and the body surface carries no current out. For a fixed geometry the relation between the two sets of potentials is therefore linear:
\[ \boldsymbol\varphi_B = A\,\boldsymbol\varphi_H + \mathbf{e} \tag{1}\]
Computing \(A\) for a real torso takes a boundary-element or finite-element model built from images. Choosing heart-surface potentials as the unknown is one standard formulation; it allows the reconstruction to be checked against measured potentials on the heart (Rudy and Messinger-Rapport 1988).
Why the inverse is hard: nested spheres. Let the heart be a sphere of radius \(r_H\) inside a homogeneous torso sphere of radius \(r_T\), with no current leaving the torso. Expand the heart-surface potential in spherical harmonics. A harmonic of degree \(l\) solves Laplace’s equation in the shell as \(a r^l + b r^{-(l+1)}\); the no-current condition at \(r_T\) fixes \(b\). The ratio of torso to heart amplitude is then
\[ g_l = \frac{\varphi_l(r_T)}{\varphi_l(r_H)} = \frac{(2l+1)\,\rho^{\,l+1}}{(l+1)\,\rho^{\,2l+1} + l} \tag{2}\]
For \(l = 0\) (a uniform potential) \(g_0 = 1\). For large \(l\), \(g_l \approx 2\rho^{\,l+1}\): fine spatial detail on the heart reaches the body attenuated geometrically. With \(\rho = 0.4\), degree 20 arrives smaller than \(10^{-7}\) (a test checks this). Any noise on the body at that scale, divided by \(g_l\), becomes a huge error on the heart. The inverse problem is ill-posed, and naive reconstructions are wildly oscillatory (Rudy and Messinger-Rapport 1988).
Key takeaways
- Forward: linear, smoothing, well behaved.
- Inverse: each spatial frequency must be multiplied by \(1/g_l\), which grows geometrically with degree, so noise explodes.
Tikhonov regularization
Regularization trades fidelity to the data for a well-behaved solution. Tikhonov’s method chooses the potentials that fit the body data while keeping \(L\boldsymbol\varphi_H\) small:
\[ \hat{\boldsymbol\varphi}_H = \arg\min_{\boldsymbol\varphi_H} \lVert A\boldsymbol\varphi_H - \boldsymbol\varphi_B \rVert^2 + \lambda^2 \lVert L\boldsymbol\varphi_H \rVert^2 = \left(A^\top A + \lambda^2 L^\top L\right)^{-1} A^\top \boldsymbol\varphi_B \tag{3}\]
The second form follows from setting the gradient of the objective to zero. With \(L = I\) the method limits the size of the solution; with \(L\) a surface gradient or Laplacian it limits its roughness. Tikhonov regularization reconstructed the main features of measured epicardial potentials in a human-torso tank with a canine heart (Messinger-Rapport and Rudy 1990).
What it does to each component. Write \(A = \sum_i s_i\,\mathbf{u}_i\mathbf{v}_i^\top\). For \(L = I\) the solution is
\[ \hat{\boldsymbol\varphi}_H = \sum_i f_i\,\frac{\mathbf{u}_i^\top\boldsymbol\varphi_B}{s_i}\,\mathbf{v}_i, \qquad f_i = \frac{s_i^2}{s_i^2 + \lambda^2} \tag{4}\]
Components with \(s_i \gg \lambda\) pass almost unchanged (\(f_i \approx 1\)); components with \(s_i \ll \lambda\) are damped (\(f_i \approx s_i^2/\lambda^2\)). \(\lambda\) is a cut-off between the spatial detail the data can support and the detail that would be noise.
Key takeaways
- Tikhonov = least squares + a penalty of weight \(\lambda^2\).
- In the SVD basis it is a filter with factors \(s_i^2/(s_i^2 + \lambda^2)\).
Choosing \(\lambda\)
Too small a \(\lambda\) lets noise through; too large a \(\lambda\) smooths away real features. Two standard rules:
\[ \text{L-curve: } \lambda \text{ at maximum curvature of } \left(\log\lVert A\hat{\boldsymbol\varphi} - \boldsymbol\varphi_B\rVert,\ \log\lVert L\hat{\boldsymbol\varphi}\rVert\right); \qquad \text{discrepancy: } \lVert A\hat{\boldsymbol\varphi} - \boldsymbol\varphi_B\rVert = \lVert\mathbf{e}\rVert \tag{5}\]
The L-curve plots the size of the solution against the size of the residual for all \(\lambda\); its corner balances the two (Hansen 1992; Hansen and O’Leary 1993). The discrepancy principle asks the model to fit the data no more closely than the noise allows, so by its definition it needs an estimate of the noise level. On synthetic problems both rules land near the error-minimizing \(\lambda\) (a test checks they are within a factor of three of the best error on a grid). Neither is guaranteed on real data, so report how sensitive the result is to \(\lambda\).
Key takeaways
- The L-curve needs no noise estimate; the discrepancy principle does.
- Always show results for a range of \(\lambda\), not only the chosen one.
Scoring a reconstruction
When the heart-surface potentials were also measured, a reconstruction can be scored:
\[ \mathrm{CC} = \frac{\operatorname{cov}\!\left(\hat\varphi, \varphi\right)}{\sigma_{\hat\varphi}\,\sigma_{\varphi}}, \qquad \mathrm{RE} = \frac{\lVert \hat{\boldsymbol\varphi} - \boldsymbol\varphi \rVert}{\lVert \boldsymbol\varphi \rVert}, \qquad \tau = \arg\min_t \frac{d\hat\varphi(t)}{dt} \tag{6}\]
The correlation coefficient CC compares shape and ignores scale; the relative error RE penalizes both. Activation time \(\tau\) at each heart site is taken at the steepest downstroke of the reconstructed electrogram, and is compared with the measured one. A scaled copy of the truth has CC = 1 but RE > 0, which is why both are reported.
Key takeaways
- Report CC and RE together, plus activation-time error.
- Score against measured heart signals, never against another reconstruction.
Clinical correlate
Planned in the outline. Requires sign-off by an EP physician.
Visual summary
Planned in the outline.
How experts think
Planned in the outline.
Cheat sheet
Planned in the outline.
Interview questions
Planned in the outline.
Code lab
Planned in the outline.
Self-check
Planned in the outline.
Literature figures
Planned in the outline.
Further reading
Planned in the outline.