1
from cmath import phase
import math
import numpy
import numpy as np
from numpy import unwrap
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
import scipy
from scipy import interpolate
from scipy.interpolate import interp1d
import 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+l4

k=[]

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.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


min_mnt=min(loc_mnt)
min_mxt=min(loc_mxt)
max_mnt=max(loc_mnt)
max_mxt=max(loc_mxt)


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

x2=loc_mnt
y2=loc_mn
f2=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.

8
  • 1
    What is k? In the first block of code k is a scalar, but in the last block of code k is used like a list. The error is saying that k[i] is below the range of interpolation which means f1 or f2 is defined with an x1 or x2 whose values are all greater than k[i]. In other words, the interpolator can not extrapolate. Commented Jun 3, 2015 at 12:07
  • Thanks ubuntu... That was my mistake... i just edited the question Commented Jun 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. Commented Jun 3, 2015 at 12:13
  • 1
    @Bob: The values in k range from 0 to 1 (roughly). The values in x1 and x2 range over values much greater than 1. It doesn't look like x1 and x2 are being chosen from the values in k Commented Jun 3, 2015 at 12:18
  • 1
    Did you mean to divide the values in x1 and x2 by 10000. as you do for k? Commented Jun 3, 2015 at 12:22

1 Answer 1

1

The interpolator can not extrapolate.

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

defines f1 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 affects f2(k[i]) since again the values in k lie outside [min(x2), max(x2)].

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 use loc_mxt.append((i+3)/10000.) to scale the values in loc_mxt down to the range used by k... but you would still need to augment x1 with min(k) and max(k) and add associated y 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

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy 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.