python - 如何自动提取信号的周期性
问题描述
我有一个包含大约 1000 个不同时间序列的数据集。其中一些显示出明显的周期性,而另一些则没有。
我希望能够自动确定时间序列是否具有明确的周期性,因此我知道在应用一些异常值方法之前是否需要对其进行季节性分解。
这是一个具有每日周期性的信号,每个样本以 15 分钟的间隔采集。
为了让我尝试自动确定是否存在每日周期性,我尝试使用不同的方法。第一种方法是使用 kats 库中的检测功能。
from kats.consts import TimeSeriesData
from kats.detectors.seasonality import FFTDetector, ACFDetector
def detect_seasonality(df,feature,time_col,detector_type):
df_kpi = df[[feature]].reset_index().rename(columns={feature:'value'})
ts = TimeSeriesData(df_kpi,time_col_name=time_col)
if detector_type == 'fft':
detector = FFTDetector(ts)
elif detector_type == 'acf':
detector = ACFDetector(ts)
else:
raise Exception("Detector types are fft or acf")
detection = detector.detector()
seasonality_presence = detection['seasonality_presence']
return seasonality_presence
这种方法使用 fft 和 acf 检测器返回“错误”的季节性存在。
另一种方法是使用 fft
import numpy as np
import scipy.signal
from matplotlib import pyplot as plt
L = np.array(df[kpi_of_interest].values)
L -= np.mean(L)
# Window signal
L *= scipy.signal.windows.hann(len(L))
fft = np.fft.rfft(L, norm="ortho")
plt.figure()
plt.plot(abs(fft))
但是在这里我们看不到任何明确的方法来确定我所期望的每日周期。
那么为了让我自动检测每日周期,还有其他更好的方法可以在这里应用吗?我是否需要事先进行任何必要的预处理步骤?还是仅仅是缺乏数据?对于每个时间序列,我只有大约 10 天的数据。