【问题标题】:Align the peaks of multiple curves对齐多条曲线的峰
【发布时间】:2022-11-20 02:07:34
【问题描述】:

我有一个包含 x 和 y 值的数据集。绘制曲线并且峰值位于沿 x 轴的不同值处。

我正在尝试使用 scipy 的信号对齐所有曲线的峰值。我试着关注这篇文章 Use of pandas.shift() to align datasets based on scipy.signal.correlate,但峰不重叠。

import matplotlib.pyplot as plt
from scipy import signal
import math
import numpy as np

a = [0.0002, 0.0005, 0.009, 0.0207, 0.0307, 0.04, 0.044, 0.05, 0.07, 0.07, 0.07, 0.082, 0.087, 0.089, 0.09, 0.09, 0.097,
     0.1, 0.11, 0.149, 0.153, 0.159, 0.16, 0.16, 0.2, 0.24, 0.24, 0.24, 0.25, 0.27, 0.3, 0.385, 0.46, 0.77, 3.7]
b = [0.4, 0.48, 2.2, 2.2, 3.4, 4.0, 4.7, 7.15, 9.9]
c = [0.006, 0.01, 0.01, 0.01, 0.012, 0.013, 0.0178, 0.018, 0.02, 0.022, 0.022, 0.027, 0.031, 0.035, 0.035, 0.036, 0.04,
     0.04, 0.046, 0.046, 0.047, 0.05, 0.0507, 0.06, 0.062, 0.07, 0.071, 0.08, 0.1, 0.143, 0.18, 0.19, 0.255, 0.3, 0.4,
     0.75, 1.25, 4.8, 35.0, 100.0]
d = [0.002, 0.01, 0.012, 0.018, 0.032, 0.035, 0.042, 0.13, 0.14, 0.172]
e = [0.0033, 0.01, 0.012, 0.023, 0.023]

data = {'a': a, 'b': b, 'c': c, 'd': d, 'e': e}

fig = plt.figure()
xc = [*range(0, len(data['c']), 1)]
for k, v in data.items():

    x = [*range(0, len(data[k]), 1)]
    v = [math.log10(i) for i in v]
    # https://stackoverflow.com/questions/10482684/python-reorder-a-sorted-list-so-the-highest-value-is-in-the-middle
    v = v[len(v) % 2::2] + v[::-2]
    # plt.plot(x, [math.log10(i) for i in v], '*')
    if k == 'c':
        plt.plot(xc, v, '*', linestyle='--')
    dx = np.mean(np.diff(xc))
    shift = (np.argmax(signal.correlate(data['c'], v)) - len(v)) * dx
    if k != 'c':
        plt.plot(x + shift, v)

峰值不以相同的 x 值为中心。有关如何执行此操作的建议将非常有帮助。

【问题讨论】:

    标签: python-3.x matplotlib scipy signal-processing data-analysis


    【解决方案1】:
    import matplotlib.pyplot as plt
    import numpy as np
    
    
    def centralize(v):
        return np.concatenate((v[len(v) % 2::2], v[::-2]))
    
    
    def shift_x(v, largest):
        
        x_max_position = largest.argmax()
        y_max_position = v.argmax()
    
        n = len(v)
        start = x_max_position - y_max_position
        end = start + n
        x = np.arange(start, end)
        return x
    
    
    a = [0.0002, 0.0005, 0.009, 0.0207, 0.0307, 0.04, 0.044, 0.05, 0.07, 0.07, 0.07, 0.082, 0.087, 0.089, 0.09, 0.09, 0.097,
         0.1, 0.11, 0.149, 0.153, 0.159, 0.16, 0.16, 0.2, 0.24, 0.24, 0.24, 0.25, 0.27, 0.3, 0.385, 0.46, 0.77, 3.7]
    
    b = [0.4, 0.48, 2.2, 2.2, 3.4, 4.0, 4.7, 7.15, 9.9]
    
    c = [0.006, 0.01, 0.01, 0.01, 0.012, 0.013, 0.0178, 0.018, 0.02, 0.022, 0.022, 0.027, 0.031, 0.035, 0.035, 0.036, 0.04,
         0.04, 0.046, 0.046, 0.047, 0.05, 0.0507, 0.06, 0.062, 0.07, 0.071, 0.08, 0.1, 0.143, 0.18, 0.19, 0.255, 0.3, 0.4,
         0.75, 1.25, 4.8, 35.0, 100.0]
    
    d = [0.002, 0.01, 0.012, 0.018, 0.032, 0.035, 0.042, 0.13, 0.14, 0.172]
    
    e = [0.0033, 0.01, 0.012, 0.023, 0.023]
    
    data = {'a': a, 'b': b, 'c': c, 'd': d, 'e': e}
    
    fig = plt.figure()
    
    xc = np.arange(len(c))
    vc = centralize(np.log10(c))
    plt.plot(xc, vc, '*', linestyle='--')
    
    for k, v in data.items():
    
        if k != 'c':
            v = centralize(np.log10(v))
            x = shift_x(v, vc)
            plt.plot(x, v)
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-22
      • 2018-07-23
      • 2011-09-13
      • 1970-01-01
      • 2019-08-31
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多