What is the use case for ifft's trailing zero padding? Why is that the default?
61 views (last 30 days)
Hi all, I'm trying to figure out what the use case is for ifft(Y,n) According to the documentation: X = ifft(Y,n) returns the n-point inverse Fourier transform of Y by padding Y with trailing zeros to length n.
These trailing zeros give incorrect results for every case I've seen. Is there something I'm not understanding? Shouldn't they put the padding zeros in the center: after npoints/2? This seems to be a common default as well. I've noticed python fft codes do this too. This is particularly relevant to me because I want to solve a differential equation spectrally using a limited number of modes, but then plot the solutions in real space with high fidelity.
Example Code Demonstrating For simplicity's sake: if I have 10 original data points of a periodic signal, but I want to replot it with 20 points, I should be able to use this code to do so. Instead, I get complex solutions. nsamples = 10; xs = linspace(0,2*pi*(1-1/nsamples) ,nsamples);
ys = sin(xs); ydefault = ifft(fft(ys),20);
Paul on 19 Jul 2022
Edited: Paul on 19 Jul 2022
I think the title question is misleading. The result of zero padding ifft is not incorrect. It does exactly what it claims to do. Nothing more, nothing less.Even if it's not what's desired for a specific problem, it's not incorrect.
The real question is in the text of the question, specifically: "what the use case is for ifft(Y,n)"
One possbility is that different fields of study use different conventions for the sign of the exponent in the transform. So if my field of study defines the DFT with a positive exponent, I can use ifft() to compute my DFT, in which case zero padding in ifft does exactly what I want (interpolate in the frequency domain).
To be sure, zero padding in the middle of the ifft array can be useful as well to interpolate in the time domain, but that's a different problem. And if zero padding in the middle is desired, we can alwasy do fftshift -> zero pad the ends -> ifftshift.
More Answers (1)
David Goodmanson on 19 Jul 2022
Edited: David Goodmanson on 4 Aug 2022
Zeropaddinig has consequences. It also has its uses, but sometimes the results are unexpected.
First of all, whatever ys is, ifft(fft(ys)) comes out the same as ys (not counting tiny numerical error). Zeropadding any signal is going to change the result. In your case the initial ys is a periodic function in the xs window as it should be. For that sine wave, the fft of ys is nonzero at two frequency points. One is at pos. frequency +1. The other is at neg. frequency -1, which for a 10 point fft is the same as pos. frequency +9 (the freq array runs from 0 to 9).
Padding the frequency array changes the positions of the frequency peaks in such a way that you end up with one real sine wave and one imaginary cosine wave, as you can see with
Each of those waves are every-other-point samples in a new 20 point x array.
All of this is totally independent of whether you later choose to cut the frequency response down by a factor of 2 to look at, say, positive frequencies only.