a b s t r a c tDuring atrial fibrillation (AF), cancellation of ventricular activity from atrial electrograms (AEG) is commonly performed by template matching and subtraction (TMS): a running template, built in correspondence of QRSs, is subtracted from the AEG to uncover atrial activity (AA). However, TMS can produce poor cancellation, leaving high-power residues. In this study, we propose to modulate the templates before subtraction, in order to make the residuals as similar as possible to the nearby atrial activity, avoiding high-power ones. The coefficients used to modulate the template are estimated by maximizing, via Multi-swarm Particle Swarm Optimization, a fitness function. The modulated TMS method (mTMS) was tested on synthetic and real AEGs. Cancellation performances were assessed using: normalized mean squared error (NMSE, computed on simulated data only), reduction of ventricular activity (VDR), and percentage of segments (PP) whose power was outside the standard range of the atrial power. All testings suggested that mTMS is an improvement over TMS alone, being, on simulated data, NMSE and PP significantly decreased while VDR significantly increased. Similar results were obtained on real electrograms (median values of CS1 recordings PP: 2.44 vs. 0.38 p < 0.001; VDR: 6.71 vs. 8.15 p < 0.001).