每日时间序列中每一天的振幅

时间:2018-08-04 07:33:40

标签: time-series signal-processing amplitude

我正在尝试确定时间序列中每一天的振幅。频率是恒定的,并且序列仅在幅度上变化。我尝试使用快速傅立叶变换和Lombscargle周期图,但是它们返回每个频率的幅度,并且该幅度似乎是整个时间序列的均值。如果我分割时间序列并计算每天的fft,我将使波从零开始会遇到问题,并且它会返回错误的值。有谁知道我每天如何可靠地计算振幅?

以下是一些示例数据:

import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0, 240, 240) # time
fs = 1/24 # frequency
## Create a time series
series = np.sin(fs * 2 * np.pi * t) 
series = series * np.sin(fs * 1/10 * 2 * np.pi * t) + 25
## Plot
plt.ylabel("Temperature ($^oC$)")
plt.xlabel("Time (hrs)")
plt.plot(t, series)
plt.show()

enter image description here

1 个答案:

答案 0 :(得分:0)

如果您需要每天的平均时间序列,则无需在分割数组后计算各部分的FFT。只需按以下步骤计算每个部分的平均时间(使用numpy mean):

# compute number of full days in the time series
days = int(np.floor(len(series)*fs))

# truncate the series, keeping full days
# (may not be necessary if total samples is a multiple of fs)
M = int(np.floor(days/fs))
series = series[0:M]
t      = t[0:M]

# compute the average for each day
avg  = np.mean(np.split(series, days), axis=1)
tmid = np.mean(np.split(t, days), axis=1)

# plot the results
plt.ylabel("Temperature ($^oC$)")
plt.xlabel("Time (hrs)")
plt.ylim(24, 26)
plt.bar(tmid, avg, 0.9/fs, color=(0.8,0.8,0.8))
plt.plot(t, series)
plt.show()

enter image description here

您可以类似地计算每个时间段的其他特征。例如,要获取特定日期的温度波动幅度,可以使用以下命令:

reshaped  = np.split(series, days)
minvalue  = np.amin(reshaped, axis=1)
maxvalue  = np.amax(reshaped, axis=1)
variation = maxvalue - minvalue
amplitude = 0.5*variation

plt.ylabel("Temperature ($^oC$)")
plt.xlabel("Time (hrs)")
plt.ylim(24, 26)
plt.bar(tmid, 2*amplitude, 1/fs, minvalue, color=(0.8,0.8,0.8), edgecolor=(0.7,0.7,0.7))
plt.plot(t, series)
plt.show()

enter image description here