首页 > 解决方案 > 如何将过滤器应用于局部最大值(绘图)

问题描述

下面的代码让我绘制 trc-oscilloscope 数据。此外,我正在标记情节的局部最大值和最小值。

trc 文件:https : //ufile.io/0zd2c (200MB)

import matplotlib.pyplot as plt
import pandas as pd
import readTrc
import numpy as np
from scipy.signal import argrelextrema
from scipy import signal

#100mil datapoints
datX, datY, m = readTrc.readTrc('C220180104_ch2_UHF00014.trc')
srx, sry = pd.Series(datX * 1000), pd.Series(datY * 1000)
df = pd.concat([srx, sry], axis = 1)
df.set_index(0, inplace = True)

#Impulse location
x1 = df[1].idxmax() - 0.0005
x2 = df[1].idxmax() + 0.003
df2 = df.loc[x1:x2]

#Locate Maximum
print('Maximum at:', round(df[1].idxmax(), 6), 'ms')

#Local Maxima
n=10 #Every n maximum a Point will be placed
df3_min = df2.iloc[argrelextrema(df2[1].values, np.less_equal, order=n)[0]][1]
df3_max = df2.iloc[argrelextrema(df2[1].values, np.greater_equal, order=n)[0]][1]
plt.scatter(df3_min.index, df3_min, c='r')
plt.scatter(df3_max.index, df3_max, c='g')

#Plot Impulse
df2[1].plot(grid = 1,
        linewidth = 1,
        figsize = (9,5),
        color = 'blue',
        legend = False,
        xlim = (x1, x2))

plt.xlabel('Time in ms')
plt.ylabel('UHF-Signal in mV')
plt.show()

输出:

在此处输入图像描述

现在我想通过最大值绘制一条曲线。我该怎么做?我尝试使用过滤器,但它们“切断”了最高极限(最大值)。

编辑:

添加这些代码连接最大值:

df3_max.plot()

输出:

在此处输入图像描述

现在,如果我尝试应用黄油过滤器:

b, a = signal.butter(5, 0.1)
y2 = signal.filtfilt(b,a, df3_max[1].values)
df3_max = pd.DataFrame(y2, index=df3_max.index)

我得到一个错误:

Traceback (most recent call last):
  File "/home/artur/.local/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 3078, in get_loc
    return self._engine.get_loc(key)
  File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 379, in pandas._libs.hashtable.Float64HashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 385, in pandas._libs.hashtable.Float64HashTable.get_item
KeyError: 1.0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/artur/Desktop/shard_plot/UHF_impulse_plot.py", line 38, in <module>
    y2 = signal.filtfilt(b,a, df3_max[1].values)
  File "/home/artur/.local/lib/python3.6/site-packages/pandas/core/series.py", line 767, in __getitem__
    result = self.index.get_value(self, key)
  File "/home/artur/.local/lib/python3.6/site-packages/pandas/core/indexes/numeric.py", line 358, in get_value
    loc = self.get_loc(k)
  File "/home/artur/.local/lib/python3.6/site-packages/pandas/core/indexes/numeric.py", line 419, in get_loc
    tolerance=tolerance)
  File "/home/artur/.local/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 3080, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas/_libs/index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 379, in pandas._libs.hashtable.Float64HashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 385, in pandas._libs.hashtable.Float64HashTable.get_item
KeyError: 1.0

标签: pythonpandasnumpymatplotlibscipy

解决方案


plot最大值的方法df3_max应该可以解决问题:

df3_max.plot()

要应用滤波器,您可以使用scipy.signal.butter低通滤波器的功能。

b, a = signal.butter(5, 0.1)
y2 = signal.filtfilt(b, a, df3_max.values)
df3_max_filt = pd.DataFrame(y2, index=df3_max.index)

df3_max_filt.plot()

推荐阅读