Python Forum
scipy fftfreq parameters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
scipy fftfreq parameters
#1
I am trying to get the fft of my pulse.
My total pulse duration is 1 microsecond and I have 100000 time slot in this pulse. So each interval corresponds 10 picoseconds.
I am trying to find my x axis with fftfreq function. However, I am not sure, If I use the function correctly or not

When I tried to get fft of my pulse, I simply run the code:
from scipy.fft import fft,fftfreq,ifft
yf = fft(v_t)
#freq = n/T
xf = fftfreq(len(result.time[:-1]), 1e-11) 
plt.plot(xf[range(50000)], yf[range(50000)])
plt.show()
plt.plot(result.time[:-1],ift)
plt.show()
where v_t is my pulse. However, I am not sure for my fftfreq parameters. First parameter corresponds the size of my pulse which is 100000 and the second parameters is my time interval between each discrete time. (The total time is 1e-6 and number of time slots is 100000 so 1e-6/400000=1e-11). Did I use the fftfreq correctly?
Thank you in advance
Reply
#2
Yes, you are using the fftfreq function correctly. The first argument to fftfreq is the length of the input array (in your case, the length of v_t), and the second argument is the sampling interval between each time point (in your case, 1e-11).

Your code should produce a plot of the magnitude of the Fourier coefficients up to a frequency of 50000 (since you are taking only the first 50000 elements of xf and yf). Note that the frequency axis in this plot is in units of cycles per second (Hz), not in units of time. To convert the frequency axis to units of time, you can take the reciprocal of the frequency values. For example, if you want the frequency axis in units of GHz, you can divide the frequency values by 1e9. If you then take the reciprocal of the frequency axis, you will get the time axis in units of seconds.

Here's an example of how to plot the Fourier transform with a time axis:

from scipy.fft import fft, fftfreq, ifft
import numpy as np
import matplotlib.pyplot as plt

# Generate a pulse with duration 1 microsecond and 100000 time slots
dt = 1e-11  # Time interval between each time slot
t = np.arange(0, 1e-6, dt)  # Time array
v_t = np.sin(2*np.pi*1e6*t)  # Pulse signal

# Compute the Fourier transform
yf = fft(v_t)
xf = fftfreq(len(v_t), dt)

# Plot the magnitude of the Fourier coefficients with a time axis
freq = xf / 1e9  # Convert frequency axis to GHz
time = 1 / freq  # Convert frequency axis to time axis in seconds
plt.plot(time, np.abs(yf))
plt.xlabel('Time (s)')
plt.ylabel('Magnitude')
plt.show()
In this example, I've generated a sine wave pulse signal with a frequency of 1 MHz. You can replace v_t with your own pulse signal. The resulting plot should show the magnitude of the Fourier coefficients as a function of time.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020