arrays – How to plot fast-fourier transform data as a function of frequencies in Python?

The example in numpy documentation is all you need :

import numpy as np

# Let's say here is your signal
signal = np.array([-2, 8, 6, 4, 1, 0, 3, 5, -3, 4], dtype=float)

fourier = np.fft.rfft(signal)
n = signal.size
sample_rate = 100
freq = np.fft.fftfreq(n, d=1./sample_rate)

>> freq
array([  0.,  10.,  20., ..., -30., -20., -10.])


but when you apply rfftfreq, you get only positive frequencies:

freq = np.fft.rfftfreq(n, d=1./sample_rate)


So if you want to plot something you take only the corresponding half of values (in the fourier transform magnitude for example).

>> freq
array([  0.,  10.,  20., 40., 50.])


To perform zero-padding, you can just use np.pad

np.pad(signal, (2,2), 'constant', constant_values=(0,0))



This added 2 zero values in the beginning and the end of the array. Multiple options are possible. Check this numpy.org/doc/stable/reference/generated/numpy.pad.html