pandas - 从 Python 中的信号中删除尖峰

标签 pandas signal-processing

例如,我有一个来自呼吸记录的信号,其中有很多由于打哈欠引起的尖峰。我曾尝试使用 pandas 中的滚动均值函数将其删除,但没有帮助。此图上的绿色空间是使用滚动平均值的结果。

import pandas as pd

RESP=pd.DataFrame(RESP)
RESP_AV=pd.rolling_mean(RESP,50)

我对过滤数据不太了解,我在 Pandas 中找不到任何其他方法来消除这些尖峰,所以我的问题是在哪里寻找答案。
RESP.head() 的结果是:
 0 -2562.863389
1 -2035.020403
2 -2425.538355
3 -2554.280563
4 -2242.438367
6.7636961937

enter image description here

最佳答案

以下函数将从数组 yi 中删除最高尖峰并用抛物线替换尖峰区域:

import numpy as np
def despike(yi, th=1.e-8):
'''Remove spike from array yi, the spike area is where the difference between 
  the neigboring points is higher than th.'''
  y = np.copy(yi) # use y = y1 if it is OK to modify input array
  n = len(y)
  x = np.arange(n)
  c = np.argmax(y)
  d = abs(np.diff(y))
  try:
    l = c - 1 - np.where(d[c-1::-1]<th)[0][0]
    r = c + np.where(d[c:]<th)[0][0] + 1
  except: # no spike, return unaltered array
    return y
  # for fit, use area twice wider then the spike
  if (r-l) <= 3:
    l -= 1
    r += 1
  s = int(round((r-l)/2.))
  lx = l - s
  rx = r + s
  # make a gap at spike area
  xgapped = np.concatenate((x[lx:l],x[r:rx]))
  ygapped = np.concatenate((y[lx:l],y[r:rx]))
  # quadratic fit of the gapped array
  z = np.polyfit(xgapped,ygapped,2)
  p = np.poly1d(z)
  y[l:r] = p(x[l:r])
  return y
要去除许多尖峰:找到最高尖峰的位置,将此功能应用于尖峰周围的狭窄区域,重复。

关于pandas - 从 Python 中的信号中删除尖峰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37556487/

相关文章:

python - Pandas timedelta64[ns] 计算

python - pandas/matplotlib 具有多个 y 轴的图

python - 值错误 : Must pass 2-d input. 形状=(1, 50, 2)

c - 双二阶滤波器输出在 C 实现中被放大

python - 使用 pandas df.query() 过滤列中的字符串值列表

excel - 使用 Pandas 在 Python 中复制 Excel 索引匹配

matlab - 使用倍频程/matlab 代码随时间递增/逐渐改变信号的音高

java - 如何实现均衡器

c++ - 计算二阶巴特沃斯低通滤波器的系数

c - FFTW 单精度库在使用 SIMD 优化时输出不正确的 DFT