使用pandas为宽带、噪声信号创建上包络
我正在使用Python 2.7。标题提供了上下文。我用这种特殊的方式表达了标题,这样人们将来可以查询这个堆栈交换问题。使用MATLAB有大量关于这方面的文档,但是对于Scipy、NumPy、Pandas、matplotlib等,这一过程严重缺乏 基本上,我有以下数据帧:使用pandas为宽带、噪声信号创建上包络,pandas,signal-processing,Pandas,Signal Processing,我正在使用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
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
现在我想做的是:
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'>
- 每隔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
- 在最大值和最小值之间插值以清除数据帧
- 绘制振幅列、上列和下列
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'>
将熊猫作为pd导入
将matplotlib.pyplot作为plt导入
将numpy作为np导入
将scipy导入为scipy
时间=[0,1,2,3,4,5,6,7,8,9]
振幅=[0.1,-0.3,1.4,4.2,-5.7,2.3,-0.2,-0.3,1.0,0.1]
df=pd.DataFrame({'time':时间,'振幅':振幅}]
plt.绘图(df['时间],df['振幅])
秒的时间:
如果:
最大值=[]
时间_max=[]
最小值=[]
时间_min=[]
最大附加值(df.max[“振幅])
最小附加(df.min[‘振幅’])
时间_max.append()
时间\分钟追加()
感谢您的帮助
多谢各位
~devin熊猫,将在这里提供帮助。要获得秒数作为日期时间索引
,请从任意的日期时间开始
-完成后,您可以随时删除年份:
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秒重新采样一次,并获取摘要统计信息min
和max
:
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
)
最后,绘制输出:
plot_cols = ['amplitude','lower','upper']
df2[plot_cols].plot()
注意:如果希望索引仅显示秒,请使用:
df2.index = df2.index.second
- 几乎复制了这一点:(但以更面向熊猫/数据帧的方式)
- 也使用了这个:
- 我第一次遇到这个问题:
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()
第6行中的
振幅值在示例数据的第一次和第二次显示中是不同的(-0.2
vs-0.8
)。是哪一个?需要什么样的插值协议?细节非常不相关。我回答了我自己的问题,但我的回答不同(见帖子),但您的解决方案更具python风格。谢谢!!!注意****我是通过查看Confect.oops上的2秒间隔来创建此信封的。只需将df.groupby(lambda x:x/2)更改为df.groupby(lambda x:x/5)