Clear Filters
Clear Filters

What is the use case for ifft's trailing zero padding? Why is that the default?

62 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);
disp(ys) disp(ydefault)

Accepted Answer

Paul on 19 Jul 2022
Edited: Paul on 19 Jul 2022
Hi Kevin,
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.
Paul on 4 Aug 2022
Hi David,
Maybe I'm misreading the statement in [], but I thought that typically (not always) fftshift operates on the frequency array and ifftshift operates on the time array, and that ifftshift shifts the zero of the input array from the center array to the start of the array, i.e., it "undoes" the result of fftshift.
David Goodmanson
David Goodmanson on 4 Aug 2022
HI Paul,
Thanks for the observation and correction. For odd n that's the way it has to be, ifftshift undoes fftshift and those functions are distinct. I mistakenly had only even n in mind, for which the array halves are swapped, both fftshift and ifftshift do the same thing, each is its own inverse, and each simultaneously takes 0 up to the 'center' (n/2+1) should it be the first element, and takes 0 down to the first element should it be at the 'center'.

Sign in to comment.

More Answers (1)

David Goodmanson
David Goodmanson on 19 Jul 2022
Edited: David Goodmanson on 4 Aug 2022
Hi Kevin.
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.




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!