python中是否有任何模块/方法可以滤除图中的瞬态部分? 我有一个包含瞬态组件和稳态组件的图表。我试图从使用matplotlib生成的图中提取稳态值。 使用的代码和结果图如下:
from numpy import genfromtxt
import matplotlib.pyplot as plt
per_data=genfromtxt('output-3.csv',delimiter=',',names=['x', 'y'])
print(type(per_data))
plt.xlabel ('x stuff')
plt.ylabel ('y stuff')
plt.title('my test result')
plt.plot(per_data['y'])
plt.show()
答案 0 :(得分:1)
如果我理解正确,你可以有效地将稳态响应定义为图的一部分,其中该点的导数的绝对值接近0,也就是说变化率接近0.同样我们可以说相反,如果导数的绝对值大于接近0,则其瞬态。
这应该很容易过滤掉,但当你说"分离稳态"你可能意味着一百万件事。你想只保持稳定状态吗?你只想保持连续的稳定状态吗?你怎么处理稳定状态之间的部分?你忽略了在这里指定你想要的东西,所以我只是告诉你如何将两个部分分开,然后你可以决定你想要用它们做些什么。
首先确定你想要的衍生范围。你可以试试这个,我只想选0.1。然后我们需要弄清楚如何计算导数。我们的数据显然(来自你所展示的)不连续。通过找到斜率来近似相邻点之间的导数的简单数值方法应该适合您的问题。
您可以将此导数应用于图表上的每对,然后您可以根据这些导数值过滤列表。
以下是一个例子:
#spacing is the distance between each point on the x axis
def find_derivative(y1, y2, spacing)
return (y2 - y1)/ spacing
# note index should obviously be < len and not negative, but garbage
# in garbage out, so no need for an assertion on that point
def consecutive_derivative(datapoints, index, spacing)
assert len(datapoints) >= 2, "Error, why would we need the derivative of one point?"
if index < (len(datapoints)):
y1 = list[index]
y2 = list[index+1]
else:
y1 = list[index-1]
y2 = list[index]
return find_derivative(y1, y2, spacing)
def to_derivative_list(datapoints, spacing):
return [consecutive_derivative(datapoints, i, spacing) for i in range(len(datapoints)]
# ... some code for list creation ...
steady_state_cutoff = 0.1
derivatives = to_derivative_list(per_data, per_data_spacing)
steady_state_indicies = []
transient_state_indicies = []
for index, derivative in enumerate(derivatives):
if abs(derivative) < steady_state_cutoff
steady_state_indicies.append(index)
else
transient_state_indicies.append(index)
#now you even have each index which is a steady state and
#those that are transient, so you can form your own new graphs off of
#these