2017-05-14 34 views
0

我正在使用Python 2.7。標題提供了上下文。我用這種特定的方式說出了標題,以便將來人們可以查詢這個堆棧交換問題。有使用MATLAB這個東西的文檔過多,但這個過程是嚴重缺乏用於SciPy的,NumPy的,熊貓,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塞科NDS,最大爲4.2,在4秒,並且在-5.7 5秒)
  • 追加值在適當的地方變成即

    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 /熊貓足夠的熟悉和想象中的代碼看索姆如下所示:

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'> 

任何幫助表示讚賞。

謝謝。

〜德文

+0

的'第6行amplitude'值是在你的例子的數據的第一和第二顯示不同('-0.2' VS'-0.8')。這是什麼? –

+0

需要什麼樣的插值協議? –

+0

的具體細節非常無關緊要。我回答了我自己的問題btw –

回答

0

熊貓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() 

time series plot with boundaries

注意:如果你想要索引只顯示秒,只需使用:

df2.index = df2.index.second 
+0

我以不同的方式回答了我的問題(請參閱帖子),但是您的解決方案更爲pythonic。謝謝!!! –

0

我希望這有助於人們爲噪音信號/時間序列數據創建任意信封,就像它幫助我一樣!

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() 

enter image description here

+0

請注意****我在事故發生時以2秒的間隔製作了這個信封。哎呀。只需將df.groupby(lambda x:x/2)更改爲df.groupby(lambda x:x/5) –