【问题标题】:creating an upper envelope for broadband, noisy signal using pandas使用 pandas 为宽带噪声信号创建上包络
【发布时间】:2017-05-14 18:28:40
【问题描述】:

我使用的是 Python 2.7。标题提供上下文。我以这种特定的方式对标题进行了表述,以便人们将来可以查询此堆栈交换问题。使用 MATLAB 有很多关于这些东西的文档,但是 Scipy、NumPy、Pandas、matplotlib 等严重缺乏这个过程。

基本上,我有以下数据框:

   time amplitude
 0 1.0  0.1
 1 2.0 -0.3
 2 3.0  1.4
 3 4.0  4.2
 4 5.0  -5.7
 5 6.0  2.3
 6 7.0  -0.2
 7 8.0  -0.3
 8 9.0  1.0
 9 10.0  0.1

现在我想做的是:

  • 以 5 秒为间隔,查找最大值和最小值
  • 用对应的时间值记录最大值和最小值(即对于上述情况,在前 5 秒内,最大值在 4 秒时为 4.2,在 5 秒时为 -5.7)
  • 将值附加到数据框中的适当位置,即

    time amplitude upper lower
    0 1.0  0.1       
    1 2.0 -0.3
    2 3.0  1.4
    3 4.0  4.2       4.2
    4 5.0  -5.7            -5.7
    5 6.0  2.3       2.3
    6 7.0  -0.8            -0.8
    7 8.0  -0.3
    8 9.0   1.0
    9 10.0  0.1
    
  • 在最大值和最小值之间进行插值以清除数据帧

  • 绘制幅度列、上列和下列

我对 python/pandas 足够熟悉,想象一下代码如下所示:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import scipy as scipy

time = [0,1,2,3,4,5,6,7,8,9]
amplitude = [0.1,-0.3,1.4,4.2,-5.7,2.3,-0.2,-0.3,1.0,0.1]
df = pd.DataFrame({'time': time, 'amplitude': amplitude}]
plt.plot(df['time'],df['amplitude])

for seconds in time:
    if <interval == 5>:
        max = []
        time_max = []
        min = []
        time_min = []

        max.append(df.max['amplitude'])
        min.append(df.min['amplitude'])
        time_max.append(<time value in interval>)
        time_min.append(<time value in interval>)

  <build another dataframe>
  <concat to existing dataframe df>
  <interpolate between values in column 'upper'>
  <interpolate between values in column 'lower'> 

感谢任何帮助。

谢谢。

~德文

【问题讨论】:

  • 第 6 行中的 amplitude 值在示例数据的第一次和第二次显示中不同(-0.2-0.8)。是哪个?
  • 需要什么样的插值协议?
  • 具体情况无关紧要。顺便说一句,我回答了我自己的问题

标签: pandas signal-processing


【解决方案1】:

Pandas resample()interpolate() 将在这里提供帮助。要获得 DatetimeIndex 的秒数,请以任意的 Datetime 开头 - 完成后您可以随时取消年份:

df.set_index(pd.to_datetime("2017") + df.time * pd.offsets.Second(), inplace=True)

print(df)
                     time  amplitude
time                                
2017-01-01 00:00:01   1.0        0.1
2017-01-01 00:00:02   2.0       -0.3
2017-01-01 00:00:03   3.0        1.4
2017-01-01 00:00:04   4.0        4.2
2017-01-01 00:00:05   5.0       -5.7
2017-01-01 00:00:06   6.0        2.3
2017-01-01 00:00:07   7.0       -0.2
2017-01-01 00:00:08   8.0       -0.3
2017-01-01 00:00:09   9.0        1.0
2017-01-01 00:00:10  10.0        0.1

每 5 秒重新采样一次,并获得汇总统计 minmax

summary = (df.resample('5S', label='right', closed='right')
             .agg({"amplitude":{"lower":"min","upper":"max"}}))
summary.columns = summary.columns.droplevel(0)

print(summary)
                     upper  lower
time                             
2017-01-01 00:00:05    4.2   -5.7
2017-01-01 00:00:10    2.3   -0.3

与原始df 合并并插入缺失值。 (请注意,只能在两个值之间进行插值,因此前几个条目将是 NaN。)

df2 = df.merge(summary, how='left', left_index=True, right_index=True)
df2.lower.interpolate(inplace=True) 
df2.upper.interpolate(inplace=True) 

print(df2)
                     time  amplitude  upper  lower
time                                              
2017-01-01 00:00:01   1.0        0.1    NaN    NaN
2017-01-01 00:00:02   2.0       -0.3    NaN    NaN
2017-01-01 00:00:03   3.0        1.4    NaN    NaN
2017-01-01 00:00:04   4.0        4.2    NaN    NaN
2017-01-01 00:00:05   5.0       -5.7   4.20  -5.70
2017-01-01 00:00:06   6.0        2.3   3.82  -4.62
2017-01-01 00:00:07   7.0       -0.2   3.44  -3.54
2017-01-01 00:00:08   8.0       -0.3   3.06  -2.46
2017-01-01 00:00:09   9.0        1.0   2.68  -1.38
2017-01-01 00:00:10  10.0        0.1   2.30  -0.30

最后,绘制输出:

plot_cols = ['amplitude','lower','upper']
df2[plot_cols].plot()

注意:如果您希望索引只显示秒,只需使用:

df2.index = df2.index.second

【讨论】:

  • 我以不同的方式回答了我的问题(见帖子),但您的解决方案更符合 Python 风格。谢谢!!!
【解决方案2】:

我希望这可以帮助人们为噪声信号/时间序列数据创建任意包络,就像它帮助了我一样!!!!

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import scipy as scipy

time_array = [0,1,2,3,4,5,6,7,8,9]
value_array = [0.1,-0.3,1.4,4.2,-5.7,2.3,-0.2,-0.3,1.0,0.1]

upper_time = []
upper_value = []
lower_time = []
lower_value = []

df = pd.DataFrame({'time': time_array, 'value': value_array})


for element,df_k in df.groupby(lambda x: x/2):
    df_temp = df_k.reset_index(drop=True)

    upper_time.append(df_temp['time'].loc[df_temp['value'].idxmax()])
    upper_value_raw = df_temp['value'].loc[df_temp['value'].idxmax()]
    upper_value.append(round(upper_value_raw,1))

    lower_time.append(df_temp['time'].loc[df_temp['value'].idxmin()])
    lower_value_raw = df_temp['value'].loc[df_temp['value'].idxmin()]
    lower_value.append(round(lower_value_raw,1))



plt.plot(df['time'],df['value'])
plt.plot(upper_time,upper_value)
plt.plot(lower_time,lower_value)
plt.show()

【讨论】:

  • note**** 我是通过意外查看 2 秒间隔来制作这个信封的。哎呀。只需将 df.groupby(lambda x: x/2) 更改为 df.groupby(lambda x: x/5)
猜你喜欢
  • 1970-01-01
  • 2022-12-01
  • 2014-02-22
  • 2011-10-15
  • 2015-10-11
  • 1970-01-01
  • 1970-01-01
  • 2021-11-21
  • 1970-01-01
相关资源
最近更新 更多