[파이썬] scipy에서 IFFT

In signal processing and data analysis, the Fast Fourier Transform (FFT) is a widely used algorithm for efficiently computing the discrete Fourier transform (DFT) of a sequence or signal. The Inverse Fast Fourier Transform (IFFT) is the opposite process, which transforms the Fourier coefficients back into the time domain.

In Python, you can perform FFT and IFFT using the scipy.fft module, which is part of the SciPy library. SciPy is a popular open-source library used for scientific computing and data analysis in Python.

Installation

Before using the scipy.fft module, make sure you have the SciPy library installed. You can install it using pip by running the following command:

pip install scipy

FFT in SciPy

Now, let’s see how you can use the scipy.fft module to perform FFT on a sequence of data. First, import the necessary module:

import scipy.fft as fft

Next, create an input sequence, which can be a list, NumPy array, or even a pandas Series:

sequence = [2, 3, 4, 5, 6]

To perform FFT on this sequence, simply call the fft.fft function:

fft_result = fft.fft(sequence)

The resulting fft_result will be an array of complex numbers representing the Fourier coefficients. The length of the array will be the same as the length of the input sequence.

IFFT in SciPy

To perform the inverse FFT (IFFT) using SciPy, you need to call the fft.ifft function and pass in the Fourier coefficients obtained from the FFT:

ifft_result = fft.ifft(fft_result)

The ifft_result will be an array of complex numbers representing the time-domain signal reconstructed from the Fourier coefficients.

Example: FFT and IFFT using SciPy

Let’s see a complete example where we perform FFT and IFFT on a sinusoidal signal:

import numpy as np
import scipy.fft as fft

# Generate a sinusoidal signal
t = np.linspace(0, 1, 1000)
frequency = 10  # 10 Hz
amplitude = 2
signal = amplitude * np.sin(2 * np.pi * frequency * t)

# Perform FFT on the signal
fft_result = fft.fft(signal)

# Perform IFFT on the Fourier coefficients
ifft_result = fft.ifft(fft_result)

# Plot the original signal and the IFFT result
import matplotlib.pyplot as plt

plt.subplot(2, 1, 1)
plt.plot(t, signal)
plt.title('Original Signal')

plt.subplot(2, 1, 2)
plt.plot(t, ifft_result)
plt.title('Reconstructed Signal')

plt.tight_layout()
plt.show()

In this example, we generate a sinusoidal signal with a frequency of 10 Hz and an amplitude of 2. We then perform FFT on the signal to obtain the Fourier coefficients. Finally, we perform IFFT on the Fourier coefficients to reconstruct the original signal. The two subplots plot the original signal and the reconstructed signal, allowing us to visualize the accuracy of the IFFT process.

Conclusion

The scipy.fft module in SciPy provides a convenient and efficient way to perform FFT and IFFT in Python. Whether you are working with audio signals, time series data, or any other type of data that requires Fourier analysis, SciPy’s FFT and IFFT functions can help you extract meaningful information from the frequency domain and reconstruct the time-domain signal.