python – How to plot FFT of non periodci signal with correct frequencies on x-axis?
I have a temperature signal with sampling rate of 10 Hz (0.1 s time step) and I would like to plot the amplitude vs frequency using FFT in Python. The final aim is to understand how to filter the signal based on the frequencies of the signal.
Both the data and the script are attached below. The problem is the the Amplitude vs Frenquency plot that shows a single frequency at 0 Hz and an amplitude in million and I think this is not correct.
How can I improve the scipt to generate a realistic distribution of frequencies?
Here is the Amplitude vs Frequency plot:
Here is the Python code I am trying:
def variables(filename, tab):
data = pd.read_excel(filename, sheet_name=tab)
return data
datafile = variables('data.xlsx')
time = datafile['Time_[s]']
temp = datafile['Temperature_[degC]']
# Interpolate to get evenly spaced time values
dt = 0.1 # time step
time_inter = np.arange(time[0], time.iloc[-1]+1, dt)
temp_inter = np.interp(time_inter, time, temp)
n = len(temp_inter)
plt.plot(time_inter, temp_inter)
plt.title('Temp vs Time - time step = 0.1 s')
plt.xlabel('Time [s]')
plt.ylabel('Temperature [$^\circ$C]')
plt.show()
# Calculate the FFT
fft_output = np.fft.fft(temp_inter)
# Compute the frequencies corresponding to the FFT output
frequencies = np.fft.fftfreq(n, dt)
# Calculate the power spectrum
power_spectrum = np.abs(fft_output)
# Plot Amplitude vs Frenquency
plt.plot(frequencies, power_spectrum)
plt.xlabel("Frequency [Hz]")
plt.ylabel("Amplitude")
plt.show()
I tried with np.fft.fftfreq to obtaine the frequencies of the signal but the resulting plot I think is wrong.
Read more here: Source link