python 中的时间序列分割

time-series segmentation in python

我正在尝试对时间序列数据进行分段,如图所示。我有很多来自传感器的数据,这些数据中的任何一个都可以有不同数量的孤立峰区域。在这个图中,我有 3 个。我想要一个函数,它将时间序列作为输入,returns 等长的分段部分。

我最初的想法是有一个滑动 window 来计算振幅的相对变化。由于带峰的 window 会有相对较高的变化,我可以为相对变化定义一定的阈值,这将帮助我采用带孤立峰的 window 。但是,这会在选择阈值时产生问题,因为相对变化对数据中的噪声非常敏感。

有什么建议吗?

1,这取决于你想怎么定义一个"region",但看起来你只是感觉,而不是严格定义。如果你对要剪出什么样的部分有非常明确的定义,你可以尝试一些方法,比如"matched filter"

2,您可能想要检测绝对幅度的峰值。如果不起作用,请尝试一阶差分绝对幅度的峰值,甚至是二阶差分。

3,这样的噪声数据很难处理。我的建议是在选择部分(针对未过滤的数据)之前进行过滤。过滤会给你平滑的峰,这样峰的位置就可以通过导数符号的变化来检测。对于过滤,请先尝试 "low-pass filter"。如果不行,我也建议"Hilbert–Huang transform".

*,看起来你正在使用 matlab。提到的方法都包含在matlab中。

为此,您需要从噪音中找出信号。

  1. 获取信号的平均值并添加一些在噪声顶部和底部放置边界的多人游戏 - 绿色虚线
  2. 找到噪声底部以下的峰值 -> 数组 2 组数据
  3. 找到噪声之上的峰值 -> 数组 2 组数据
  4. 获取第一个峰底部的最小索引和第一个峰顶部的最大索引以找到第一个峰范围
  5. 获取第二个峰顶部的最小索引和第二个峰底部的最大索引以找到第二个峰范围

代码中的一些描述。使用这种方法,您可以找到其他峰。 您需要手动输入的一件事是告诉程序 x 峰值之间的值以将数据分成几部分。

请参阅图表了解摘要。

import numpy as np
from matplotlib import pyplot as plt


# create noise data
def function(x, noise):
    y = np.sin(7*x+2) + noise
    return y

def function2(x, noise):
    y = np.sin(6*x+2) + noise
    return y


noise = np.random.uniform(low=-0.3, high=0.3, size=(100,))
x_line0 = np.linspace(1.95,2.85,100)
y_line0 = function(x_line0, noise)
x_line = np.linspace(0, 1.95, 100)
x_line2 = np.linspace(2.85, 3.95, 100)
x_pik = np.linspace(3.95, 5, 100)
y_pik = function2(x_pik, noise)
x_line3 = np.linspace(5, 6, 100)

# concatenate noise data
x = np.linspace(0, 6, 500)
y = np.concatenate((noise, y_line0, noise, y_pik, noise), axis=0)

# plot data
noise_band = 1.1
top_noise = y.mean()+noise_band*np.amax(noise)
bottom_noise = y.mean()-noise_band*np.amax(noise)
fig, ax = plt.subplots()
ax.axhline(y=y.mean(), color='red', linestyle='--')
ax.axhline(y=top_noise, linestyle='--', color='green')
ax.axhline(y=bottom_noise, linestyle='--', color='green')
ax.plot(x, y)

# split data into 2 signals
def split(arr, cond):
  return [arr[cond], arr[~cond]]

# find bottom noise data indexes
botom_data_indexes = np.argwhere(y < bottom_noise)
# split by visual x value
splitted_bottom_data = split(botom_data_indexes, botom_data_indexes < np.argmax(x > 3))

# find top noise data indexes
top_data_indexes = np.argwhere(y > top_noise)
# split by visual x value
splitted_top_data = split(top_data_indexes, top_data_indexes < np.argmax(x > 3))

# get first signal range
first_signal_start = np.amin(splitted_bottom_data[0])
first_signal_end = np.amax(splitted_top_data[0])

# get x index of first signal
x_first_signal = np.take(x, [first_signal_start, first_signal_end])
ax.axvline(x=x_first_signal[0], color='orange')
ax.axvline(x=x_first_signal[1], color='orange')

# get second signal range
second_signal_start = np.amin(splitted_top_data[1])
second_signal_end = np.amax(splitted_bottom_data[1])

# get x index of first signal
x_second_signal = np.take(x, [second_signal_start, second_signal_end])
ax.axvline(x=x_second_signal[0], color='orange')
ax.axvline(x=x_second_signal[1], color='orange')

plt.show()

输出:

红线=所有数据的平均值

绿线 - 顶部和底部噪声边界

橙色线 - 选定的峰值数据