Why does MNE resample method does not sample the data point to point?

External Email - Use Caution

While trying with the mne downsampling module, I encountered some behavior
that I am unable to explain myself.

My understanding of downsampling is that it is an operation to decrease the
sample rate of x by keeping the first sample and then every nth sample after
the first.

The example provided from the resample method of scipy package clearly
illustrated about this operation as depicted the picture which is accessible
from the link (
<https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.resample.
html)>
https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.resample.h
tml) or as extracted below

In an enlarged view, it is evident that the original data points were
resampled point by point.

However, using the mne example of downsampling which accessible via the link

: <https://mne.tools/dev/auto_examples/preprocessing/plot_resample.html>
https://mne.tools/dev/auto_examples/preprocessing/plot_resample.html

, I notice that the data points were not resampled point by point as
illustrated visually below

This given that, mne resample is based on the resample method of scipy
package as indicated from mne resample function as shown:

<https://github.com/mne-tools/mne-python/blob/607fb4613fb5a80dd225132a4a53fe
43b8fde0fb/mne/filter.py#L1342>
https://github.com/mne-tools/mne-python/blob/607fb4613fb5a80dd225132a4a53fe4
3b8fde0fb/mne/filter.py#L1342

May I know whether this issue is due to the ringing artifacts or due to
other problems?

Also, are there remedies to mitigate this problem.

Thanks for any insight. Appreciate it

Cheers

Rodney

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20200723/092e4d51/attachment-0001.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/jpeg
Size: 15565 bytes
Desc: not available
Url : http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20200723/092e4d51/attachment-0001.jpe
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: image/png
Size: 51215 bytes
Desc: not available
Url : http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20200723/092e4d51/attachment-0001.png

External Email - Use Caution

My understanding of downsampling is that it is an operation to decrease
the sample rate of x by keeping the first sample and then every nth sample
after the first.

Resampling typically consists of two steps: low-pass filtering to avoid
aliasing, then sample rate reduction (subselecting samples from the
resulting signal). The low-passing actually changes the values, so the
subselection-of-filtered-data step will not necessarily yield points that
were "on" the original signal.

May I know whether this issue is due to the ringing artifacts or due to
other problems?

In this case it's likely due to the (implicit) low-pass filtering in the
frequency-domain resampling of the signal. It looks pretty reasonable to
me. If you want to play around with it a bit, you can

1. Call scipy.signal.resample directly on your data and see how closely it
matches.
2. Pad your signal, call scipy.signal.resample, and remove the (now
reduced-length) padding -- this is what MNE does internally.
3. Use scipy.signal.resample_poly directly on your data.
4. Manually low-pass filter and then directly subselect samples from the
low-passed signal, which is what resample_poly does internally.

Hopefully these all give similar results for your signal(s).

Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20200723/a8812d4e/attachment.html

External Email - Use Caution

Also note that the resample example (https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.resample.html) shows upsampling, i.e. the data has a lower sampling rate than the resampled result. However, in the case of downsampling it is usually necessary to avoid aliasing of frequencies above the resampled Nyquist frequency. Therefore, the signal is typically low-pass filtered before the resampling step. As Eric mentioned, this anti-aliasing filter is what actually changes the signal values, but it is necessary to avoid aliasing artifacts.

AFAIK, scipy.signal.resample doesn't include an anti-aliasing filter, but both scipy.signal.resample_poly as well as scipy.signal.decimate apply such a low-pass filter before resampling. That's also what MNE does.

Clemens

External Email - Use Caution

I think the up- vs. downsampling distinction is also really important
for expectations here, as is the distinction between decimating and
resampling (I recall there was a thread about that a few years back with
similar confusion, if somebody wants to do the effort of searching for it)

Phillip

External Email - Use Caution

> AFAIK, scipy.signal.resample doesn't include an anti-aliasing filter,
but both scipy.signal.resample_poly as well as scipy.signal.decimate apply
such a low-pass filter before resampling. That's also what MNE does.

scipy.signal.resample does frequency-domain resampling, so implicitly uses
a brick-wall filter at Nyquist when downsampling (unless you specify
something for the `window` argument, which gets applied in the frequency
domain in addition to the effective brick-wall filter).

Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.nmr.mgh.harvard.edu/pipermail/mne_analysis/attachments/20200723/d2ce2e88/attachment.html