s = array([1, 4, 3, 5, 3, 2, 4, 3, 4, 5, 4, 3, 2, 5, 6, 7, 8, 7, 8])
q_u = zeros(s.shape)
q_l = zeros(s.shape)
u_x = [0]
u_y = [s[0]]
l_x = [0]
l_y = [s[0]]
for k in range(1, len(s) - 1):
    if sign(s[k] - s[k - 1]) == 1 and sign(s[k] - s[k + 1]) == 1:
        u_x.append(k)
u_y.append(s[k])
if sign(s[k] - s[k - 1]) == -1 and sign(s[k] - s[k + 1]) == -1:
    l_x.append(k)
l_y.append(s[k])
u_x.append(len(s) - 1)
u_y.append(s[-1])
l_x.append(len(s) - 1)
l_y.append(s[-1])
u_p = interp1d(u_x, u_y, kind='cubic', bounds_error=False, fill_value=0.0)
l_p = interp1d(l_x, l_y, kind='cubic', bounds_error=False, fill_value=0.0)
for k in range(0, len(s)):
    q_u[k] = u_p(k)
q_l[k] = l_p(k)
plot(s)
hold(True)
plot(q_u, 'r')
plot(q_l, 'g')
grid(True)
show()
python - How to get high and low envelope of a signal?