提取局部最大值和最小值并绘制包络函数
Extract local max and min and Plot an envelope function
我的数据集:
我有以下 dataset 当使用 Plotly 绘制时产生以下结果(简化):
我的任务:
我想创建一个包络函数,从上述数据集中的数据中提取局部最大值和最小值并绘制包络曲线。它大致看起来像这样:
我已经尝试接近提供的解决方案 and the one for the envelope provided . However, none of them works for me in this approach. For some strange reason, the scatter plots are producing the following results(对于本地最小值),这并不是我所需要的。
这是我的初始情节代码:
import plotly.express as px
import plotly.graph_objects as go
fig= go.Figure()
fig.add_traces(go.Scatter(x= mem_df['time'], y=mem_df['volatge']))
fig.update_layout(xaxis_title = r'$\text{Time } T \text{ in s} $',
yaxis_title = r'$\text{Deflection angle } \text{ in radians}$')
fig.update_layout(title= r'$\text{Deflection angles versus time for the damping sheet}$')
fig.show()
在这种情况下,我们将不胜感激!
提前致谢!
问题是您的数据包含一些噪音。一种可能的方法是找到合适的曲线拟合,然后找到拟合的局部最大值和最小值。 scipy
前来帮忙:
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import numpy as np
from scipy.optimize import curve_fit
from scipy.signal import argrelextrema
mem_df = pd.read_csv("path/to/file")
x = mem_df['time'].values
y = mem_df['deflection'].values
# dampened oscillator
func = lambda x, xi, k, phi, a, b: a * np.sin(k * x - phi) * np.exp(-xi * x) + b
popt, pcov = curve_fit(func, x, y)
fig= go.Figure()
fig.add_traces(go.Scatter(x=x, y=y, name="data"))
# plot the curve fit
xx = np.linspace(0, 20, 150)
yy = func(xx, *popt)
fig.add_traces(go.Scatter(x=xx, y=yy, name="fit"))
idx_max = argrelextrema(yy, np.greater)
idx_min = argrelextrema(yy, np.less)
fig.add_traces(go.Scatter(x=xx[idx_max], y=yy[idx_max], name="top"))
fig.add_traces(go.Scatter(x=xx[idx_min], y=yy[idx_min], name="bottom"))
fig.update_layout(xaxis_title = r'$\text{Time } T \text{ in s} $',
yaxis_title = r'$\text{Deflection angle } \text{ in radians}$')
fig.update_layout(title= r'$\text{Deflection angles versus time for the damping sheet}$')
fig.show()
我的数据集:
我有以下 dataset 当使用 Plotly 绘制时产生以下结果(简化):
我的任务:
我想创建一个包络函数,从上述数据集中的数据中提取局部最大值和最小值并绘制包络曲线。它大致看起来像这样:
我已经尝试接近提供的解决方案
这是我的初始情节代码:
import plotly.express as px
import plotly.graph_objects as go
fig= go.Figure()
fig.add_traces(go.Scatter(x= mem_df['time'], y=mem_df['volatge']))
fig.update_layout(xaxis_title = r'$\text{Time } T \text{ in s} $',
yaxis_title = r'$\text{Deflection angle } \text{ in radians}$')
fig.update_layout(title= r'$\text{Deflection angles versus time for the damping sheet}$')
fig.show()
在这种情况下,我们将不胜感激! 提前致谢!
问题是您的数据包含一些噪音。一种可能的方法是找到合适的曲线拟合,然后找到拟合的局部最大值和最小值。 scipy
前来帮忙:
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import numpy as np
from scipy.optimize import curve_fit
from scipy.signal import argrelextrema
mem_df = pd.read_csv("path/to/file")
x = mem_df['time'].values
y = mem_df['deflection'].values
# dampened oscillator
func = lambda x, xi, k, phi, a, b: a * np.sin(k * x - phi) * np.exp(-xi * x) + b
popt, pcov = curve_fit(func, x, y)
fig= go.Figure()
fig.add_traces(go.Scatter(x=x, y=y, name="data"))
# plot the curve fit
xx = np.linspace(0, 20, 150)
yy = func(xx, *popt)
fig.add_traces(go.Scatter(x=xx, y=yy, name="fit"))
idx_max = argrelextrema(yy, np.greater)
idx_min = argrelextrema(yy, np.less)
fig.add_traces(go.Scatter(x=xx[idx_max], y=yy[idx_max], name="top"))
fig.add_traces(go.Scatter(x=xx[idx_min], y=yy[idx_min], name="bottom"))
fig.update_layout(xaxis_title = r'$\text{Time } T \text{ in s} $',
yaxis_title = r'$\text{Deflection angle } \text{ in radians}$')
fig.update_layout(title= r'$\text{Deflection angles versus time for the damping sheet}$')
fig.show()