【问题标题】:Why is the envelope curve erroneous at the beginning?为什么包络线一开始是错误的?
【发布时间】:2023-04-04 12:03:01
【问题描述】:

我实现了一个函数,它给出了离散值的包络曲线。我认为可能会出现错误,因为当我在帖子底部提供给您的日期内对其进行测试时,我发现实际数据点与包络曲线之间存在差异,如图所示

from scipy.interpolate import interp1d
import numpy as np
import matplotlib.pyplot as plt

def enveloppe(s):
    u_x = [0,]        
    u_y = [s[0],]
    q_u = np.zeros(s.shape)
    for k in xrange(1,len(s)-1):
        if (np.sign(s[k]-s[k-1])==1) and (np.sign(s[k]-s[k+1])==1):
            u_x.append(k)
            u_y.append(s[k])
    u_x.append(len(s)-1)
    u_y.append(s[-1]) 
    u_p = interp1d(u_x,u_y, kind = 'cubic',bounds_error = False, fill_value=0.0)
    #Evaluate each model over the domain of (s)
    for k in xrange(0,len(s)):
        q_u[k] = u_p(k)
    return q_u   

fig, ax = plt.subplots()
ax.plot(S, '-o', label = 'magnitude')
ax.plot(envelope(S), '-o', label = 'enveloppe magnitude')
ax.legend()

Data S : array([  9.12348621e-11,   6.69568658e-10,   6.55973768e-09,
         1.26822485e-06,   4.50553316e-09,   5.06526113e-07,
         2.96728433e-09,   2.36088205e-07,   1.90802318e-09,
         1.15867354e-07,   1.18504790e-09,   5.72888034e-08,
         6.98672478e-10,   2.75361324e-08,   3.82391643e-10,
         1.25393143e-08,   1.96697343e-10,   5.96979943e-09,
         1.27009013e-10,   4.46365555e-09,   1.31769958e-10,
         4.42024233e-09,   1.42514400e-10,   4.17757107e-09,
         1.41640360e-10,   3.65170558e-09,   1.29784598e-10,
         2.99790514e-09,   1.11732461e-10])

【问题讨论】:

  • 为什么要进行插值?三次样条插值导致您观察到的效果。
  • 能否请您建议如何在没有插值的情况下进行操作?
  • 线性插值怎么样,不会超调?或者,如果这是在信号处理背景中,请查看 analytic signalhilbert transform
  • 您能否提供我可以在代码中更改的内容,以便插值中不会出现过冲?

标签: python numpy envelope


【解决方案1】:

我会对你的包络函数进行两次修改以获得更单调的输出

这个想法是避免将左右两端隐式添加到用于构造包络的峰值列表中

def enveloppe(s):
    u_x = [] # do not add 0
    u_y = []
    q_u = np.zeros(s.shape)
    for k in range(1,len(s)-1):
        if (np.sign(s[k]-s[k-1])==1) and (np.sign(s[k]-s[k+1])==1):
            u_x.append(k)
            u_y.append(s[k])
    print(u_x)
    u_p = interp1d(u_x,u_y, kind = 'cubic',
              bounds_error = False, 
              fill_value="extrapolate") # use fill_value="extrapolate"
    for k in range(0,len(s)):
        q_u[k] = u_p(k)
    return q_u   

【讨论】:

    猜你喜欢
    • 2022-08-11
    • 1970-01-01
    • 2017-02-27
    • 2021-09-23
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多