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 sigtoolI 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.
- 1What is
k? In the first block of codekis a scalar, but in the last block of codekis used like a list. The error is saying thatk[i]is below the range of interpolation which meansf1orf2is defined with anx1orx2whose values are all greater thank[i]. In other words, the interpolator can not extrapolate.unutbu– unutbu2015-06-03 12:07:02 +00:00CommentedJun 3, 2015 at 12:07 - Thanks ubuntu... That was my mistake... i just edited the questionBob– Bob2015-06-03 12:10:28 +00:00CommentedJun 3, 2015 at 12:10
- 1Please use more descriptive variable names in the future. It will help you out a lot to avoid mistakes like this.kylieCatt– kylieCatt2015-06-03 12:13:56 +00:00CommentedJun 3, 2015 at 12:13
- 1@Bob: The values in
krange from 0 to 1 (roughly). The values inx1andx2range over values much greater than 1. It doesn't look likex1andx2are being chosen from the values inkunutbu– unutbu2015-06-03 12:18:39 +00:00CommentedJun 3, 2015 at 12:18 - 1Did you mean to divide the values in
x1andx2by 10000. as you do fork?unutbu– unutbu2015-06-03 12:22:49 +00:00CommentedJun 3, 2015 at 12:22
1 Answer1
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)].
3 Comments
loc_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....Explore related questions
See similar questions with these tags.