Logic of the closed-loop stimulation system that synchronizes the onset of rTMS to EEG alpha phase. The system continuously processes EEG in real-time, where the EEG is sampled at 10 kHz. To optimize throughput, data is read from the amplifier in chunks of 20 data points (i.e., samples). Subsequently, a low-pass antialiasing filter is applied with a cut-off at 50 Hz and the signal is downsampled to 500 Hz. For EEG processing, the logic switches between three operation modes, SCAN MODE (blue arrows), TRIGGER MODE (red arrows) and REFRACTORY MODE (grey arrow). Model fitting in SCAN MODE is performed in parallel to reading new data (multi-threading) and every new fitting attempt is always performed on the newest available data. Starting in SCAN MODE, the system fits multiple single-sine function models on to the individual’s prefrontal quasi-alpha signal (αind, 6 to 13 Hz; spatial average of FP1, F7 and F3) in a time window [−300, ~−100] ms relative to the newest EEG sample (see S.2 in supplementary material). The resulting model that achieves the lowest root mean square error (RMSE) on that training signal is used for prediction on a more recent test signal in the time window [−100,0]ms, again relative to the newest EEG sample. If the RMSE on that test signal does not reach below a pre-determined, subject-specific threshold (see S.4 in supplementary material), the logic continues with a new fitting attempt, but now again using data relative to the newest EEG data that arrived in real-time. Otherwise, if and only if the RMSE on that test signal is below this threshold, the single-sine model is used to predict the prefrontal quasi-alpha wave up to 123 ms into the future. The targeted phase, ϕtarg, then depends on the randomized treatment arm for that patient. For SYNC, ϕtarg is the subject specific preferred phase ϕpre that was determined in an initial combined fMRI-EEG-TMS experiment (see S.1 in supplementary material). For UNSYNC, ϕtarg is drawn from a uniform random distribution over the range [0,2π] at every prediction (ϕtarg ~U(0,2π)). Taking into account the group delay of causal filtering and processing time, the logic then schedules the rTMS trigger onset at the predicted future time of ϕtarg and switches into TRIGGER MODE. In TRIGGER MODE, no model fitting is attempted. Instead the logic keeps reading new data samples. Whenever the scheduled trigger time has arrived, a train of 40 TMS pulses is triggered where the inter-pulse-interval is the reciprocal of the subject’s individual alpha frequency (IAF, Δtipi = 1/IAF). Directly after the 40th pulse has been triggered, the logic switches into REFRACTORY MODE, where the system does nothing other than reading in new EEG samples for or twice the amount of time it took to deliver 40 TMS pulses, after which the logic again switches into SCAN MODE.