1
from cmath import phaseimport mathimport numpyimport numpy as npfrom numpy import unwrapimport matplotlib.pyplot as pltimport matplotlib.pyplot as pltimport scipyfrom scipy import interpolatefrom scipy.interpolate import interp1dimport scipy.signal.signaltools as sigtool

I have a data set produced by the the following codes.

for i in xrange(10000):    v = i/10000.0    if v < 0.25:        k=0.4*(math.sin(2*3.14*90*v))        l1.append(k)    elif 0.25 <= v < 0.5:        k=0.8*(math.sin(2*3.14*90*v))        l2.append(k)    elif 0.5 <= v < 0.75:        k=0.6*(math.sin(2*3.14*300*v))        l3.append(k)    elif 0.75 <= v < 1.0:        k=0.9*(math.sin(2*3.14*300*v))        l4.append(k)comb= l1+l2+l3+l4k=[]for i in range(len(comb)):    i1=i/10000.    k.append(i1)    f.write(str(i1)+" "+(str(comb[i])+"\n"))

I am finding the local maxima and local minima along with their corresponding positions with the following codes:

loc_mx=[]loc_mn=[]loc_mnt=[]loc_mxt=[]for i in range(len(comb)-2):    if comb[i] < comb[i+1]:        if comb[i+1] > comb[i+2]:            loc_mx.append(comb[i+1])            loc_mxt.append(i+3)    if comb[i] > comb[i+1]:        if comb[i+1] < comb[i+2]:            loc_mn.append(comb[i+1])            loc_mnt.append(i+3)

Interpolating the the data with the help of local maxima and local minima with following code

loc_mn.append(comb[len(comb)-1])loc_mx.append(comb[len(comb)-1])loc_mnt.append(k[len(comb)-1])loc_mxt.append(k[len(comb)-1])loc_mn.reverseloc_mx.reverseloc_mn.append(comb[0])loc_mx.append(comb[0])loc_mnt.append(k[0])loc_mxt.append(k[0])loc_mn.reverseloc_mx.reversemin_mnt=min(loc_mnt)min_mxt=min(loc_mxt)max_mnt=max(loc_mnt)max_mxt=max(loc_mxt)x1=loc_mxty1=loc_mxf1=interpolate.interp1d(x1,y1,kind="cubic")x2=loc_mnty2=loc_mnf2=interpolate.interp1d(x2,y2,kind="cubic")f1(k)f2(k)

I am getting the following error.

  File "emd.py", line 150, in <module>    int_dt.write(str(k[i])+" "+str(f1(k[i]))+" "+str(f2(k[i])))  File "/usr/lib/python2.7/dist-packages/scipy/interpolate/polyint.py", line 54, in __call__    y = self._evaluate(x)  File "/usr/lib/python2.7/dist-packages/scipy/interpolate/interpolate.py", line 448, in _evaluate    out_of_bounds = self._check_bounds(x_new)  File "/usr/lib/python2.7/dist-packages/scipy/interpolate/interpolate.py", line 475, in _check_bounds    raise ValueError("A value in x_new is below the interpolation "ValueError: A value in x_new is below the interpolation range.

Would be grateful to get any help in this regard.

askedJun 3, 2015 at 11:57
Bob's user avatar
8
  • 1
    What isk? In the first block of codek is a scalar, but in the last block of codek is used like a list. The error is saying thatk[i] is below the range of interpolation which meansf1 orf2 is defined with anx1 orx2 whose values are all greater thank[i]. In other words, the interpolator can not extrapolate.CommentedJun 3, 2015 at 12:07
  • Thanks ubuntu... That was my mistake... i just edited the questionCommentedJun 3, 2015 at 12:10
  • 1
    Please use more descriptive variable names in the future. It will help you out a lot to avoid mistakes like this.CommentedJun 3, 2015 at 12:13
  • 1
    @Bob: The values ink range from 0 to 1 (roughly). The values inx1 andx2 range over values much greater than 1. It doesn't look likex1 andx2 are being chosen from the values inkCommentedJun 3, 2015 at 12:18
  • 1
    Did you mean to divide the values inx1 andx2 by 10000. as you do fork?CommentedJun 3, 2015 at 12:22

1 Answer1

1

The interpolator can not extrapolate.

f1 = interpolate.interp1d(x1,y1,kind="cubic")

definesf1 on the domain[min(x1), max(x1)].

In [62]: [min(x1), max(x1)]Out[62]: [30, 9982]

f1 can not be evaluated outside this domain. Since

In [63]: [min(k), max(k)]Out[63]: [0.0, 0.9999]

f1(k[i]) raises

ValueError: A value in x_new is below the interpolation range.

A similar issue affectsf2(k[i]) since again the values ink lie outside[min(x2), max(x2)].

answeredJun 3, 2015 at 12:16
unutbu's user avatar
Sign up to request clarification or add additional context in comments.

3 Comments

@ubuntu if i add the max and min value corresponding k in x1 along with some value for the y1 can i resolve the problem ??
@Bob, yes, that would avoid the error. You might also want to useloc_mxt.append((i+3)/10000.) to scale the values inloc_mxt down to the range used byk... but you would still need to augmentx1 withmin(k) andmax(k) and add associatedy values. But that may or may not be consistent with what you want the interpolator to be doing....
loc_mn.append(comb[len(comb)-1]) loc_mx.append(comb[len(comb)-1]) loc_mnt.append(k[len(comb)-1]) loc_mxt.append(k[len(comb)-1]) loc_mn.reverse loc_mx.reverse loc_mn.append(comb[0]) loc_mx.append(comb[0]) loc_mnt.append(k[0]) loc_mxt.append(k[0]) loc_mn.reverse loc_mx.reverse @ubuntu

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.