[Numpy-discussion] Fwd: ifft padding

Allen Welkie allen.welkie at gmail.com
Wed May 25 15:35:02 EDT 2016


I'd like to get some feedback on my [pull request](
https://github.com/numpy/numpy/pull/7593).

This pull request adds a function `ifftpad` which pads a spectrum by
inserting zeros where the highest frequencies would be. This is necessary
because the padding that `ifft` does simply inserts zeros at the end of the
array. But because of the way the spectrum is laid out, this changes which
bins represent which frequencies and in general messes up the result of
`ifft`. If you pad with the proposed `ifftpad` function, the zeros will be
inserted in the middle of the spectrum and the time signal that results
from `ifft` will be an interpolated version of the unpadded time signal.
See the discussion in [issue #1346](
https://github.com/numpy/numpy/issues/1346).

The following is a script to demonstrate what I mean:

```
import numpy
from numpy import concatenate, zeros
from matplotlib import pyplot

def correct_padding(a, n, scale=True):
    """ A copy of the proposed `ifftpad` function. """
    spectrum = concatenate((a[:len(a) // 2],
                            zeros(n - len(a)),
                            a[len(a) // 2:]))
    if scale:
        spectrum *= (n / len(a))
    return spectrum

def plot_real(signal, label):
    time = numpy.linspace(0, 1, len(signal) + 1)[:-1]
    pyplot.plot(time, signal.real, label=label)

def main():
    spectrum = numpy.zeros(10, dtype=complex)
    spectrum[-1] = 1 + 1j

    signal = numpy.fft.ifft(spectrum)
    signal_bad_padding = numpy.fft.ifft(10 * spectrum, 100)
    signal_good_padding = numpy.fft.ifft(correct_padding(spectrum, 100))

    plot_real(signal, 'No padding')
    plot_real(signal_bad_padding, 'Bad padding')
    plot_real(signal_good_padding, 'Good padding')

    pyplot.legend()
    pyplot.show()


if __name__ == '__main__':
    main()
```
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20160525/507e53fd/attachment.html>


More information about the NumPy-Discussion mailing list