You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I just had a few questions about the default filter design (more of signal processing questions than directly about the code).
How does bandwidth translate to the filter? From the code, it's clear the input bandwidth effects the order of the filter, with larger bandwidths corresponding to higher order (and therefore wider) filters. Was this done with any specific benchmark in mind (something like, the order is set so that the stopband of the filter is equal to the center frequency + / - the bandwidth)?
What was the reasoning for choosing a filter based off a blackman window? More specifically, here's the filter response of a filter from pactools with a center frequency of 50Hz and a bandwidth of 20Hz:
The filter has a gradual slope, so it looks like it still includes a lot of frequencies above or below 40-60Hz. Why not use a filter with a sharper transition?
On a related note, do you have any criteria for excluding certain frequency pairs where there is overlap between the low and high frequencies? For the filter above, my first instinct would be to exclude any comparisons between that 50Hz signal and anything above around 16Hz, where the filter response becomes bumpy -- but maybe at frequencies where the response is say -30 dB or less, the frequency can reasonably be considered filtered out.
The filter order is computed so that the cutoff frequencies are at (center frequency) +/- (bandwidth / 2). The cutoff frequency is defined as the frequency with a transfer function of 0.5 (=-3dB).
The design of a filter is always a tradeoff between localization in time and in frequency. A sharper transition in frequency means a longer impulse response in time (at the extreme, a Fourier transform gives a very precise localization in frequency, at the cost of no localization in time). In the case of PAC, I chose to maximize the localization in time, since we then consider correlations over time.
The frequencies can be considered filtered out much before -30dB, though I agree -3dB is maybe not enough. I don't have a criterion, but keep in mind that to observe any amplitude modulation, the bandwidth of the fast oscillation has to be at least twice as high as the frequency of the slow oscillations, otherwise the amplitude modulation is filtered out.
Adding a quantitative, synthetic follow-up to the bandwidth/overlap questions here. Thank you for the earlier explanation of the time-frequency tradeoff and the need to preserve modulation sidebands.
A pure 20-Hz sinusoid produces a twice-per-cycle envelope and nonzero Tort MI in the cell labeled 20/70 Hz when the native amplitude width is 60 Hz. This is an accepted configuration: phase centers [20, 30] make high_fq_width="auto" resolve to 60 Hz. The input contains no high-frequency component. This example scores only the interior 2–10 seconds of a 12-second signal, avoiding outer filter edges.
Tested against upstream commit 24dc3ed1917b (source reports 0.3.2), with Python 3.10.18, NumPy 2.2.6, SciPy 1.14.1, scikit-learn 1.6.1, and Matplotlib 3.10.3. The pure-sinusoid example also reproduces with the PyPI 0.3.1 distribution. The pinned upstream source was imported directly; no package code was modified.
Minimal reproduction — save as filter_repro.py:
importnumpyasnpfrompactoolsimportComodulogram, multiple_band_passfs=1000.0t=np.arange(12000) /fsx=np.cos(2*np.pi*20*t)[None, :]
mask=np.ones_like(x, dtype=bool)
mask[:, 2000:10000] =False# Score only the interior 2–10 seconds.est=Comodulogram(
fs=fs, low_fq_range=[20.0, 30.0], low_fq_width=2.0,
high_fq_range=[70.0], high_fq_width="auto", method="tort",
n_surrogates=0, progress_bar=False,
)
est.fit(x, mask=mask)
z=multiple_band_pass(x, fs, [70.0], est.high_fq_width)[0, 0]
amp=np.abs(z[2000:10000])
print(f"resolved amplitude width: {est.high_fq_width:.1f} Hz")
print(f"20/70-Hz MI: {est.comod_[0, 0]:.12f}")
print(f"mean extracted amplitude: {amp.mean():.12f}")
envelope_fft=np.abs(np.fft.rfft(amp-amp.mean()))
freqs=np.fft.rfftfreq(amp.size, 1/fs)
print(f"largest envelope component: {freqs[envelope_fft.argmax()]:.1f} Hz")
# Exact complex response of the same native filter, including signed frequencies.impulse=np.zeros((1, 4097))
impulse[0, 2048] =1.0kernel=multiple_band_pass(impulse, fs, [70.0], est.high_fq_width)[0, 0]
lags=np.arange(4097) -2048probe_hz=np.array([-20.0, 20.0, 70.0])
gain=np.abs(np.sum(np.exp(-2j*np.pi*probe_hz[:, None] *lags/fs) *kernel, axis=1))
print("gain at [-20, +20, +70] Hz / gain at +70 Hz:")
print(gain/gain[-1])
Output:
resolved amplitude width: 60.0 Hz
20/70-Hz MI: 0.003009782504
mean extracted amplitude: 0.314984517019
largest envelope component: 40.0 Hz
gain at [-20, +20, +70] Hz / gain at +70 Hz:
[0.05864118 0.31534746 1. ]
The complex response transmits both signed components of the real 20-Hz input. An output of the form a*exp(i*phi) + b*exp(-i*phi) has an envelope whose square contains a 2*phi term. This explains a phase-dependent envelope without a separate high-frequency signal; it does not imply that the filter creates a genuine 70-Hz oscillator. The native quadrature filter construction and filter dispatch are the relevant paths.
This is a deterministic measurement example, not a surrogate-significance result or a false-positive-rate estimate. Finite filter leakage is expected; I am asking about the intended operating range and safeguards for substantial leakage in configurations the API accepts, rather than assuming every nonzero out-of-band response is a bug.
Plot the signed filter response and extracted envelope
Append this to the reproduction above. The left panel normalizes complex-filter gain to the gain at +70 Hz; the right panel shows the retained interior envelope from the unit-amplitude 20-Hz input.
Related documented behavior: the automatic bandwidth changes an unchanged cell when the overall phase scan changes. For the same signal cos(2*pi*6*t) + (1 + 0.5*cos(2*pi*6*t))*cos(2*pi*100*t) and the same interior scoring window:
Requested phase centers (Hz)
Resolved amplitude width (Hz)
MI at 6/100 Hz
Recovered 6-Hz envelope modulation depth
[6]
12
0.01098455
0.35533
[6, 10]
20
0.01718619
0.44237
[6, 30]
60
0.02157089
0.49393
The injected depth is 0.5. This follows the documented 2 * max(low_fq_range) rule; it is not presented as a separate implementation bug. An explicit comparison warning would help users realize that the same plotted cell may represent a different amplitude filter.
Is the 20/70-Hz configuration above within the native filter's intended operating range? Could the documentation give an example of checking phase-band transmission and signed-frequency response before interpreting a cell?
Could bandwidth documentation explicitly distinguish the approximate half-power width from a flat passband or stopband guarantee, with a warning or diagnostic for poorly separated configurations?
Could the auto-width documentation flag scan dependence and recommend fixing explicit filter specifications for matched comparisons?
The sideband and time-frequency constraints discussed by Dvorak & Fenton (2014), sections 3.1.2–3.1.4 and Aru et al. (2015), recommendation 2 motivate these checks; they do not specify a universally safe rejection threshold. Narrowing the amplitude filter indiscriminately can also remove genuine modulation sidebands. This report is confined to the native filtered Tort pathway and does not infer the same behavior for DAR.
I also read the related discussion in #33 and the open alternative-filter PR #34. This example isolates native-filter behavior using synthetic input only, without a MATLAB/MNE comparison or neural data.
Activity
TomDLT commentedon Jan 5, 2021
mgm248 commentedon Jan 12, 2021
Ok, thank you, this is really helpful!
RebeccaStevenson commentedon Oct 5, 2026
Adding a quantitative, synthetic follow-up to the bandwidth/overlap questions here. Thank you for the earlier explanation of the time-frequency tradeoff and the need to preserve modulation sidebands.
A pure 20-Hz sinusoid produces a twice-per-cycle envelope and nonzero Tort MI in the cell labeled 20/70 Hz when the native amplitude width is 60 Hz. This is an accepted configuration: phase centers
[20, 30]makehigh_fq_width="auto"resolve to 60 Hz. The input contains no high-frequency component. This example scores only the interior 2–10 seconds of a 12-second signal, avoiding outer filter edges.Tested against upstream commit
24dc3ed1917b(source reports0.3.2), with Python 3.10.18, NumPy 2.2.6, SciPy 1.14.1, scikit-learn 1.6.1, and Matplotlib 3.10.3. The pure-sinusoid example also reproduces with the PyPI 0.3.1 distribution. The pinned upstream source was imported directly; no package code was modified.Minimal reproduction — save as
filter_repro.py:Output:
The complex response transmits both signed components of the real 20-Hz input. An output of the form
a*exp(i*phi) + b*exp(-i*phi)has an envelope whose square contains a2*phiterm. This explains a phase-dependent envelope without a separate high-frequency signal; it does not imply that the filter creates a genuine 70-Hz oscillator. The native quadrature filter construction and filter dispatch are the relevant paths.This is a deterministic measurement example, not a surrogate-significance result or a false-positive-rate estimate. Finite filter leakage is expected; I am asking about the intended operating range and safeguards for substantial leakage in configurations the API accepts, rather than assuming every nonzero out-of-band response is a bug.
Plot the signed filter response and extracted envelope
Append this to the reproduction above. The left panel normalizes complex-filter gain to the gain at +70 Hz; the right panel shows the retained interior envelope from the unit-amplitude 20-Hz input.
Related documented behavior: the automatic bandwidth changes an unchanged cell when the overall phase scan changes. For the same signal
cos(2*pi*6*t) + (1 + 0.5*cos(2*pi*6*t))*cos(2*pi*100*t)and the same interior scoring window:[6][6, 10][6, 30]The injected depth is 0.5. This follows the documented
2 * max(low_fq_range)rule; it is not presented as a separate implementation bug. An explicit comparison warning would help users realize that the same plotted cell may represent a different amplitude filter.Standalone reproduction of scan dependence
Questions / possible improvements
The sideband and time-frequency constraints discussed by Dvorak & Fenton (2014), sections 3.1.2–3.1.4 and Aru et al. (2015), recommendation 2 motivate these checks; they do not specify a universally safe rejection threshold. Narrowing the amplitude filter indiscriminately can also remove genuine modulation sidebands. This report is confined to the native filtered Tort pathway and does not infer the same behavior for DAR.
I also read the related discussion in #33 and the open alternative-filter PR #34. This example isolates native-filter behavior using synthetic input only, without a MATLAB/MNE comparison or neural data.
Addendum: This post was created by OpenAI Codex.