PTB-M4D / PyDynamic

Python library for the analysis of dynamic measurements
https://ptb-m4d.github.io/PyDynamic/
GNU Lesser General Public License v3.0
25 stars 13 forks source link

Identity fails for odd signals `GUM_iDFT(GUMDFT(odd_signal, cov_odd_signal))` #310

Closed mgrub closed 1 year ago

mgrub commented 1 year ago

Currently, GUM_DFT and GUM_iDFT are based on numpy.fft.rfft and numpy.fft.irfft respectivly.

Assume two signals of even and odd length:

xe, Uxe = np.ones(6), np.eye(6)
xo, Uxo = np.ones(7), np.eye(7)

As mentioned in the numpy documentation, numpy.fft.irfft requires the length of the original signal for a proper reconstruction, otherwise an original signal of even length is assumed. Therefore, to achieve identity of the reconstruction for the above signals, it is necessary to call:

# get spectrum
Xe = np.fft.rfft(xe)
Xo = np.fft.rfft(xo)

# recover signal
xe_reconstructed = np.fft.irfft(Xe, n=xe.size)
xo_reconstructed = np.fft.irfft(Xo, n=xo.size)

# bad way of recovering
xe_reconstructed_not_good = np.fft.irfft(Xe)  # yields correct result, bc. original signal is even
xo_reconstructed_not_good = np.fft.irfft(Xo)  # yields wrong result, bc. original signal is odd

The n=... parameter of numpy.fft.irfft is not reachable when calling GUM_iDFT. Hence, the reconstruction always assumes an original signal of even length. Also workarounds with zero-padding and removal via the GUM_iDFT(..., Nx=...) cannot yield the desired result.

This might be related to #99 . I would suggest to stick to the numpy behavior, in order to simplify transition from existing routines based on numpy/scipy to the uncertainty-enhanced versions in PyDynamic. However, this would probably require some careful adjustments in the uncertainty evaluation.