2017-05-14 5 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は、maxは5秒で4秒で4.2及び-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 /パンダと十分よく知っているし、コードを見ソムを想像次のようなものがあります。

    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' -0.8' VS)あなたの例のデータの第1及び第2の表示が異なっています。どちらですか? –

    +0

    どのような補間プロトコルが必要ですか? –

    +0

    詳細はあまり関係ありません。私は自分自身の質問に答えました。 –

    答えて

    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とマージし、欠損値を補間します。 (補間は2つの値の間でのみ可能であるため、最初の数エントリは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

    私は私の質問に違った答えをしましたが(ポストを参照)、あなたの解決策はもっと有名です。ありがとうございました!!! –

    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)に変更するだけです。 –

    関連する問題