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)
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):
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.
np.fft.fftfreq
returns the result, which puts me think about pseudo-improved resolution... $\endgroup$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$fftfreq
for calculating sample frequencies? $\endgroup$