Data interpolation in python

Quite often in my day to day work I end up having to flag out bad data. In many cases this just creates a gap and normally this is fine just to throw away. There are occasions however, such as when creating the bandpass of reciever that its still quite useful to have some idea as to what is going on in the normally flagged out channels – especially when its large chunks of data. To solve this we use interpolation. This post isn’t about explaining data interpolation – for that I suggest you take a look at wiki.
When interpolating I like to use splines. In linear interpolation we use a linear function for each intervale,s with splines we use low-degree polynomials in each of the intervals chosing a polynomial that nicely fits the sections together. Generally works quite well.
Python has many different ways todo interpolation, be it spline or not. There is a huge variety of interpolation functions and I’d urge you to explore the scipy help for an exhaustive list. In this example I’m going to use the spline functions from scipy.signal
Firstly, import the functions required (asciidata is to read in the data and is not needed):
from scipy import *
from numpy import r_, sin
from scipy.signal import cspline1d, cspline1d_eval
I’ve defined a function to do the actual work:
def spline_lowchan(beamdata):
   lastzero = max(where(beamdata == 0)[0]) #first 146 channels are 0
  &nbspstartchan = lastzero+1
  &nbspx = r_[startchan:len(beamdata)]
  &nbspdx = x[1]-x[0]
  &nbspnewx = r_[0:startchan:1] # notice outside the original domain
  &nbspy = beamdata[startchan:]
  &nbspcj = cspline1d(y,lamb=0)
  &nbspnewy = cspline1d_eval(cj, newx, dx=dx,x0=x[0])
  &nbspaa = list(newy)
  &nbspbb = list(y)
  &nbspmod = aa + bb
  &nbspreturn mod
The function is called like:
output = spline_lowchan(originaldata)
Where originaldata is the initial bandpass (just a sequence of numbers in an array or list)
In the above example of interpolating the function is assuming that the first set of data is all set to 0, hence the where is 0. We then interpolate the data over the range where there is no signal. We then add this to the original signal, hence allowing the region without any useful data to have an interpolated data set. Not perfect but gives a better idea of what might be happening. Of course this is not really how you should treat the edges but the cspline1d method very well when you say lose a few data points out of a bandpass.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: