can you write a simple python code to generate fft of a signal?
- User
can you write a simple python code to generate fft of a signal?
- Assistant
Certainly! Here's a simple Python code to generate the Fast Fourier Transform (FFT) of a signal using the NumPy library:
import numpy as np import matplotlib.pyplot as plt # Generate a sample signal time = np.linspace(0, 2*np.pi, 1000) signal = np.sin(5*time) + np.sin(10*time) # Perform FFT fft = np.fft.fft(signal) # Calculate the frequency bins freq = np.fft.fftfreq(len(signal), d=time[1]-time[0]) # Plot the frequency spectrum plt.plot(freq, np.abs(fft)) plt.xlabel('Frequency') plt.ylabel('Amplitude') plt.show()In this code, we first generate a sample signal consisting of two sine waves with frequencies 5 and 10 Hz. Then, we perform the FFT using
np.fft.fft()function. The resulting FFT values are stored in thefftvariable. We also calculate the corresponding frequency bins using function. Finally, we plot the frequency spectrum using function.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.