2012-01-17 40 views

回答

5

既然你不會插入任何新的值,你不能只是原來的數組,並通過一些用戶定義的包裝訪問索引它?您可以利用numpy數組的非整數索引。

要看我的意思,用x = np.array(range(10)),然後例如(x[i] for i in np.linspace(0, len(x)-1, num=25))會像零階保持一樣。

0

有點遲到了,但這裏是我想出了:

from numpy import zeros, array, sign 

def signal_zoh(x,y,epsilon = 0.001): 
    """ 
     Fills in the data from a Zero-Order Hold (stair-step) signal 
    """ 
    deltaX = array(x[1:],dtype='float') - x[:-1] 
    fudge = min(deltaX) *epsilon 
    retX = zeros((len(x)*2-1,)) 
    retY = zeros((len(y)*2-1,)) 
    retX[0::2] = x 
    retX[1::2] = x[1:]+fudge*sign(deltaX) 
    retY[0::2] = y 
    retY[1::2] = y[:-1] 
    return retX,retY 
0

這裏有一個numpy的免費版本,具有相同簽名。數據需要按遞增順序排列 - 當您通過「巧妙」使用列表作爲嵌套功能默認值(100倍加速因子)時,數據需要按照升序排列 - b/c:

def interp0(x, xp, yp): 
    """Zeroth order hold interpolation w/ same 
    (base) signature as numpy.interp.""" 
    from collections import deque 

    def func(x0, xP=deque(xp), yP=deque(yp)): 
     if x0 <= xP[0]: 
     return yP[0] 
     if x0 >= xP[-1]: 
     return yP[-1]  
     while x0 > xP[0]: 
     xP.popleft()  # get rid of default 
     y = yP.popleft() # data as we go. 
     return y 

return map(func, x)