I have looked at previous similar questions, but I am still not very clear on this. Most real-time or otherwise fft functions suggest adding zeros at the end of the input to make the sample size an integer exponent of 2 and make the resolution better (as the step size of the output is inversely proportional to the number of samples at the input).

What I do not understand is, wouldn't adding the zeros affect the output spectrum (amplitude at each frequency in output data) if we add zeros to the input signal? My understanding is that the fft function considers the sample data as periodic and thus adding zero will add a sharp fall in signal amplitude and thus would introduce wrong amplitudes/ energy distribution in higher frequencies of the output. The exact reason we add a windowing function to signals for.

I tested this using the rfft function provided by CMSIS library of STM32. I generated a sample sine wave and passed on data to the rfft function. I changed the data length such that in one case I do not need to add zeros to get $2^n$ samples and other case I had to. The results with zero padded data looked very different from the non padded signals.

Spectrum analysis from this page also confirms this observation. The spectrum on left without zero padding gives more realistic amplitudes of frequencies other than the peak, while the one with padding gives higher magnitudes for other frequencies (shown with red lines on left side of spectrum). The right side spectrum is probably distorted in first image too because of the time domain signal not being complete cycles of the waveform.

enter image description here

  • $\begingroup$ Try windowing before zero-padding. $\endgroup$
    – DSP novice
    Nov 24, 2022 at 6:46
  • 2
    $\begingroup$ "to make the sample size multiple of 2" I think you mean to make it some integer exponent of 2 -- i.e., 256, 1024, or some other $n = 2^k$ where $k$ is an integer. This is a lot less important than it used to be -- FFT algorithms have been found that work for arbitrary $n$, even prime numbers. Unless you've got code space limitations or are running a hard-wired FFT in logic, padding to $2^k$ is getting obsolete. $\endgroup$
    – TimWescott
    Nov 24, 2022 at 16:18
  • $\begingroup$ You are right, I meant integer exponent of 2. I will edit the question so as not to mislead others readers. $\endgroup$
    – Nirmala
    Nov 24, 2022 at 22:04

2 Answers 2


First, @OverLordGoldDragon's answer to the same question should be studied carefully.

Here is a different approach to an answer.

  • Frequency precision, which is defined as $$\frac{f_s}{\tt{N_{fft}}}$$ $\tt{N_{fft}}$ being the length of the input (with or without zero-padding), is not to be confused with frequency resolution, which is defined as $$\frac{f_s}{N}$$ $N$ being the length of the actual data, i.e. without the added zeros. With no zero-padding, $N = \tt{N_{fft}}$

  • The $\text{DFT}$ of a signal is a frequency sampled version of the underlying $\text{DTFT}$ of that signal. Where the samples are taken is dependent on the sampling frequency $f_s$ and the length of the input $\tt{N_{fft}}$.

  • Multiplication in the time domain is equivalent to convolution in the frequency domain. The $\text{DTFT}$ of a truncated signal is therefore the convolution of the $\text{DTFT}$ of the signal with the $\text{DTFT}$ of the window used for truncation. If you're using a rectangular window to truncate your signal, the resulting $\text{DTFT}$ will then be the convolution of the $\text{DTFT}$ of your signal with the $\text{DTFT}$ of a rectangular window, which is a $\text{sinc}$ function.

  • Taking the $\text{DFT}$, you're then sampling the resulting convoluted $\text{DTFT}$ at every $f_s/\tt{N_{fft}}$ frequency sample.

Here is the (zoomed in) $\text{DFT}$ of a rectangular window of length $N = 7000$ and $f_s = 100 \,\text{MHz}$. I set $\tt{N_{fft}} = 2^{22}$: enter image description here

Again, by truncating your signal, you're convolving its $\text{DTFT}$ with that of the rectangular window, then by taking the $\text{DFT}$, you're sampling the resulting $\text{DTFT}$ every $f_s/\tt{N_{fft}}$ samples. You can see the sampling in action by looking at the $\text{DFT}$ of your truncated signal with 3 different values of $\tt{N_{fft}}$: $7000$, $8000$ and $2^{22}$. You can think of the blue curve as the underlying convolved $\text{DTFT}$.

enter image description here

For fun, here is the result if you used a Hamming window to truncate: enter image description here


Yes, it can.

But whether that's the most important thing to worry about depends on how you got the signal in the first place, and where you're doing the processing.

If you're actually sampling a periodic signal, and your sampling is exactly synchronous to that signal, then zero padding is bad. As a concrete example, say you've arranged to sample a power line at 1200Hz for 100ms, from a clock that's locked to the power line phase. You have an integer number of cycles, and to the degree that the input is actually periodic, the tail matches the head perfectly.

You almost never do that.

Usually, if you're doing an FFT on collected data, you're either using it for fast convolution or you're analyzing some arbitrary signal taken from the real world.

If you're doing fast convolution, you zero-pad so the output of the fast convolution has died off before it gets to the end of the output vector.

Typically when you're analyzing data with the FFT, you sample over what is, from the world's point of view, a random interval of time. In general, spectral components of the actual signal don't have periods that match the length of your sample.

This distorts your spectrum!

Basically, if this is the reason you're using the FFT, then when you take a continuous-time signal and sample it you're making an approximation (you're throwing away everything between your samples), then when you select a finite-length chunk of that sampled signal you're making another approximation (you're throwing away everything past the ends).

By the time you take that twice-approximated signal and zero-pad it, the spectrum is already pretty battered -- zero padding it isn't going to hurt. At this point, zero-padding, detrending, and windowing your signal before FFT may very well help to get the most accurate representation of the real signal's spectrum.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.