Следующий код позволяет отображать данные trc-осциллографа.Кроме того, я отмечаю локальные максимумы и минимумы графика.
trc-файл: https://ufile.io/0zd2c (200 МБ)
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