In the previous section, we discussed the amplitude spectrum of the dataset. Now is the time to explore the power spectrum. The power spectrum of any physical signal can display the energy distribution of the signal. We can easily change the code and display the power spectrum by squaring the transformed signal using the following syntax:
plt.plot(transformed ** 2, label="Power Spectrum")
We can also plot the phase spectrum using the following Python syntax:
plt.plot(np.angle(transformed), label="Phase Spectrum")
Let's see the complete code for the power and phase spectrum for the Sunspot dataset:
- Import the libraries and read the dataset:
# Import required library
import numpy as np
import statsmodels.api as sm
from scipy.fftpack import rfft
from scipy.fftpack import fftshift
import matplotlib.pyplot as plt
# Read the dataset
data = sm.datasets.sunspots.load_pandas().data
- Compute FFT, Spectrum, and Phase:
# Compute FFT
transformed = fftshift...