2017-08-08 35 views
1

我實現了一個給出離散值包絡曲線的函數。我覺得可能是一個錯誤,當我測試過的日期,我會爲您提供在帖子的底部,我得到螞蟻的包絡線的實際數據點之間的不同點是在這個圖中播種 example of the code applied to a given data爲什麼包絡曲線在開始時是錯誤的?

from scipy.interpolate import interp1d 
import numpy as np 
import matplotlib.pyplot as plt 

def enveloppe(s): 
    u_x = [0,]   
    u_y = [s[0],] 
    q_u = np.zeros(s.shape) 
    for k in xrange(1,len(s)-1): 
     if (np.sign(s[k]-s[k-1])==1) and (np.sign(s[k]-s[k+1])==1): 
      u_x.append(k) 
      u_y.append(s[k]) 
    u_x.append(len(s)-1) 
    u_y.append(s[-1]) 
    u_p = interp1d(u_x,u_y, kind = 'cubic',bounds_error = False, fill_value=0.0) 
    #Evaluate each model over the domain of (s) 
    for k in xrange(0,len(s)): 
     q_u[k] = u_p(k) 
    return q_u 

fig, ax = plt.subplots() 
ax.plot(S, '-o', label = 'magnitude') 
ax.plot(envelope(S), '-o', label = 'enveloppe magnitude') 
ax.legend() 

Data S : array([ 9.12348621e-11, 6.69568658e-10, 6.55973768e-09, 
     1.26822485e-06, 4.50553316e-09, 5.06526113e-07, 
     2.96728433e-09, 2.36088205e-07, 1.90802318e-09, 
     1.15867354e-07, 1.18504790e-09, 5.72888034e-08, 
     6.98672478e-10, 2.75361324e-08, 3.82391643e-10, 
     1.25393143e-08, 1.96697343e-10, 5.96979943e-09, 
     1.27009013e-10, 4.46365555e-09, 1.31769958e-10, 
     4.42024233e-09, 1.42514400e-10, 4.17757107e-09, 
     1.41640360e-10, 3.65170558e-09, 1.29784598e-10, 
     2.99790514e-09, 1.11732461e-10]) 
+1

爲什麼你插值峯的名單?三次樣條插值會導致您觀察到的效果。 –

+0

你會建議如何做到沒有插值? – Tassou

+0

線性插值怎麼樣,不超調?或者如果這是在信號處理背景中,請查看[分析信號](https://en.wikipedia.org/wiki/Analytic_signal#Envelope_and_instantaneous_phase)和[hilbert transform](https://en.wikipedia.org/wiki/ Hilbert_transform)。 – kazemakase

回答

1

我會做出兩處修改您的ENVELOPPE功能,以獲得更單調輸出

enter image description here

這樣做是爲了避免左側的隱含此外,右兩端用來構建信封

def enveloppe(s): 
    u_x = [] # do not add 0 
    u_y = [] 
    q_u = np.zeros(s.shape) 
    for k in range(1,len(s)-1): 
     if (np.sign(s[k]-s[k-1])==1) and (np.sign(s[k]-s[k+1])==1): 
      u_x.append(k) 
      u_y.append(s[k]) 
    print(u_x) 
    u_p = interp1d(u_x,u_y, kind = 'cubic', 
       bounds_error = False, 
       fill_value="extrapolate") # use fill_value="extrapolate" 
    for k in range(0,len(s)): 
     q_u[k] = u_p(k) 
    return q_u 
相關問題