[Numpy-discussion] interpolating arrays (?)

Ray Schumacher rays at blue-cove.com
Mon Mar 22 19:01:10 EST 2004


Howdy,

I'd like to resize an array (1-dimensional) a of length n into an array of 
n+x, and interpolate the values as necessary.
x might be+/-200, len(a) is usually ~500

I tried it in pure Python but it's not quite right yet. (rubberBand, below)
Someone out there must have done this... and linear interpolation for 
values is probably sufficient (instead of bi-cubic or some such)

Cheers,
Ray




==================================================================
         startSlice = 10000
         endOfSlice = 
10000+int(round(self.samplesPerRotation))    #self.samplesPerRotation=661
         ## self.dataArray is about len=200,000 of Int

         for delta in range(-2,3):
             ## interpolate the different slices to the original length
	  ##  0 should return the same array, this is a test
             newArray = self.rubberBand(
                                 self.dataArray[startSlice:endOfSlice+delta],
                                 round(self.samplesPerRotation))
             print len(self.dataArray[startSlice:endOfSlice+delta]), 
len(newArray),
             ## show the result
             print delta, Numeric.sum(self.dataArray[startSlice:endOfSlice] 
- newArray)

     def rubberBand(self, data, desiredLength):
         """ alias/interpolate the data so that it is adjusted to the 
desired array length"""
         currentLength = float(len(data))

         ## positive if the new array is to be longer
         difference = desiredLength - currentLength
         #print  difference, desiredLength, currentLength

         ## set up the desired length array
         newData = Numeric.zeros(desiredLength, Numeric.Float)

         ## accounts for binary rounding errors
         smallNum = 2**-14

         for index in range(desiredLength):
             ## find the ratio of the current index to the desired length
             ratio = index / float(desiredLength)

             ## find the same (float) position in the old data
             currIndex = int(ratio * currentLength)
             ## find the decimal part
             decimalPart = (ratio * currentLength) - currIndex
             print index, currIndex, decimalPart,
             if(decimalPart>smallNum):
                 ## interpolate
                 newData[index] = ((1 - decimalPart) * data[currIndex] + 
decimalPart * data[currIndex+1])
                 print data[currIndex], data[currIndex+1], newData[index]
             else:
                 newData[index] = data[currIndex]
                 print 'else',data[currIndex], newData[index]

         return newData





More information about the NumPy-Discussion mailing list