0
$\begingroup$

Consider simple rectangular pulse and FFT of it in Python:

import numpy as np
import matplotlib.pyplot as plt

def rectangular_pulse(t, amplitude, start, stop):
    wave = np.zeros(len(t))+amplitude
    wave[0:start] = 0
    wave[stop:] = 0
    return wave

t = np.linspace(0,1,1001) # time
fs = 1/t[1] # sampling frequency
N = len(t) # number of points
func = rectangular_pulse(t, 1, 1, 100)

plt.plot(t, func)

enter image description here

FFT = np.fft.fft(func)
freqs_fft = np.fft.fftfreq(len(func), t[1])

if I have a look at the frequency resolution $\left(\frac{f_s}{N}\right)$, I'll get:

freqs_fft[1]

which returns $0.9990009990009989$

If I add 1000 zero points (with the same sampling):

enter image description here

I'll get for freqs_fft[1]: $0.49975012493753124$ - obviously, the value has been changed.

In this case, I have a question: if zero-padding doesn't improve frequency resolution, why do I get "better" frequency resolution in case of adding zero-padding to this signal.

$\endgroup$
10
  • $\begingroup$ Take a 100x100 image and blow it up to 1000x1000 pixels. It has more pixels, yes, but does it have better resolution? $\endgroup$
    – MBaz
    Dec 10, 2021 at 15:52
  • $\begingroup$ @MBaz, agree with you, but why np.fft.fftfreq returns the result, which puts me think about pseudo-improved resolution... $\endgroup$
    – Curious
    Dec 10, 2021 at 15:59
  • $\begingroup$ @Curious you seem to have a fundamental misunderstand of what the fftfreq function does. It's really boring: It calculates the range +-(1/second argument)/2, and then divides that into (first argument) equal parts. Nothing more. You don't need it to calculate the spacing of FFT bins - it's always sample rate / N. If you increase N, you decrease the bin spacing by exactly the inverse factor. $\endgroup$ Dec 10, 2021 at 17:01
  • $\begingroup$ @MarcusMüller, do you mean it is incorrect to use fftfreq for calculating sample frequencies? $\endgroup$
    – Curious
    Dec 10, 2021 at 17:55
  • 1
    $\begingroup$ Almost a duplicate of: dsp.stackexchange.com/questions/741/… $\endgroup$
    – hotpaw2
    Dec 10, 2021 at 23:10

2 Answers 2

4
$\begingroup$

why do I get "better" frequency resolution in case of adding zero-padding to this signal.

You do and you don't. Zero padding increases resolution by interpolating between existing data point. The interpolated data doesn't add any new information, it's linearly dependent on the existing data (from the non-zero padded FFT). So you create more data but not more information. That still can be useful, even just for visual representation, i.e. it often "looks nicer".

There are different ways to interpolate, zero-padding is just an efficient way of doing it.

$\endgroup$
4
  • $\begingroup$ thank you for explanation! is it possible to show it somehow mathematically (indeed, the data with and without zero padding differ by some coefficient, but why does it happen)? and how to explain artificial "better" resolution in case of more points? $\endgroup$
    – Curious
    Dec 10, 2021 at 16:17
  • 1
    $\begingroup$ Matter of fact, the data does not differ – it's an interpolation, and since you're even using an integer interpolation factor, the same data happens is even preserved! Your example is actually why it looks likes it does: The Fourier transform of a rectangular window is a sinc function, and multiplication with a rectangular window in time leads to convolution with the sinc in frequency domain.I'm almost certain you'll find that in every single text that introduces zero-padding as means to produce a finer-grained spectrum estimate.You might just need to read another page in your textbook! $\endgroup$ Dec 10, 2021 at 16:58
  • $\begingroup$ @Curious: the mathematical proof is quite simple and a good exercise to do yourself. Start with a DFT transform pair $x[n] \leftrightarrow X[k]$ . Then create a sequence $y[m]$ by zero padding $x[n]$ to twice it's length. Calculate the DFT of $y[m]$ and compare it to $X[k]$ $\endgroup$
    – Hilmar
    Dec 10, 2021 at 19:04
  • $\begingroup$ @Hilmar, thank you! I will try) $\endgroup$
    – Curious
    Dec 16, 2021 at 12:57
1
$\begingroup$

Zero padding before an FFT increases the number of interpolated points to plot from the longer result, by doing a high quality Sinc interpolation.

With a higher density of plot points, the probability that one of them is closer to some random frequency isolated spectral peak increases.

If an isolated spectral peak is right at a low-res plot point, its estimation won't improve by adding some points to the left and right. If the peak is right between low-res plot points, then increasing the plot resolution by doubling the number of plot points might land on it more directly, thus appearing to be a "higher resolution" result. But the peak hasn't moved. You've just done a better job of interpolation to hunt it down.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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