去趋势化或过滤锯齿波信号 (Python)

Detrend or Filter a sawtooth signal (Python)

我试图通过使用 filfilt 过滤器来消除信号的趋势(锯齿波),但效果不佳。这里 data

core_3 = pd.read_csv(filename_3, sep=";",header=0,index_col = 0,
                     parse_dates=True, names='date','temp'], infer_datetime_format=True, dayfirst=True)

core_7 = pd.read_csv(filename_7 ,sep=";",header=0,index_col = 0,
                    parse_dates=True, names=['date','temp'], infer_datetime_format=True,dayfirst=True)

当我应用 Butterworth 滤波器时

b3, a3 = sg.butter(1, 0.045)
y3     = sg.filtfilt(b3, a3, core_3.temp)
b7, a7 = sg.butter(1, 0.030)
y7     = sg.filtfilt(b7, a7, core_7.temp)

结果是

如您所见,对于 3 THz 信号,存在锯齿趋势。在 21:45 处,信号有一个扰动(我想研究那个扰动)。在 7 THz 处可以清楚地观察到这种扰动。在 3 THz 观察到锯齿波中断。所以,我需要去趋势化或过滤这个信号。我尝试使用 filtfilt 过滤器,但我不知道使用 scipy.detrend.

是否更方便

无论是过滤还是简单的去趋势化都不会对这个信号有任何好处。第一个问题是趋势有些周期性。第二个问题是周期性不稳定。我相信线性方法解决不了问题。

我建议您执行以下操作:

  1. 删除跳跃("unroll the sawtooth")
  2. 然后用低通滤波器或其他任何东西去除信号趋势

这是一个例子:

import numpy as np
import scipy.signal as sps
import matplotlib.pyplot as plt

np.random.seed(123)

# create an example signal
x = []
ofs = 3.4
slope = 0.002
for t in np.linspace(0, 100, 1000):
    ofs += slope    
    x.append(np.sin(t*2) * 0.1 + ofs)
    if x[-1] > 4:
        ofs =3.2
        slope = np.random.rand() * 0.003 + 0.002
x = np.asarray(x)    
plt.plot(x, label='original')

# detect and remove jumps
jmps = np.where(np.diff(x) < -0.5)[0]  # find large, rapid drops in amplitdue
for j in jmps:
    x[j+1:] += x[j] - x[j+1]    
plt.plot(x, label='unrolled')

# detrend with a low-pass
order = 200
x -= sps.filtfilt([1] * order, [order], x)  # this is a very simple moving average filter
plt.plot(x, label='detrended')

plt.legend(loc='best')
plt.show()