frequency domain in python

You can convert a signal from time domain to frequency domain in Python using the Fast Fourier Transform (FFT) algorithm, which is provided in the numpy library. Here is an example code snippet to demonstrate this:

main.py
import numpy as np
import matplotlib.pyplot as plt

# Generate a sample signal
Fs = 1000  # Sampling frequency
T = 1/Fs   # Sampling interval
t = np.arange(0, 1, T)   # Time vector
f = 5     # Frequency of the signal
x = np.sin(2*np.pi*f*t)

# Perform FFT to convert to frequency domain
n = len(x)  # Length of the signal
frequencies = np.fft.fftfreq(n, T)[:n//2]  # Frequency components
X = np.fft.fft(x)[:n//2]  # FFT of the signal

# Plot the frequency domain representation
plt.figure()
plt.stem(frequencies, np.abs(X))
plt.xlabel('Frequency [Hz]')
plt.ylabel('Magnitude')
plt.title('Frequency Domain')
plt.show()
618 chars
23 lines

This code snippet generates a sine wave signal, converts it to frequency domain using FFT, and then plots the magnitude of the frequency components.

related categories

gistlibby LogSnag