Wednesday, August 26, 2009

Interpolation with SciPy

This didn't take long, thanks to SciPy.


from scipy.interpolate import interpolate as interp

xa = [i * 10 for i in range(10)]
ya = [200.0 + 1.5 * x for x in xa]

iobj = interp.interp1d(xa, ya)
i_xa = [i for i in range(90)]
i_ya = [iobj(x) for x in i_xa]

import pylab as pl
pl.plot(xa, ya, "o")
pl.plot(i_xa, i_ya, "-")
pl.ylim(0, 1000)
pl.show()

Friday, August 7, 2009

A portable way to pass a string to C as a macro


#define STRINGIZE_(x) #x
#define STRINGIZE(x) STRINGIZE_(x)


And call STRINGIZE(x) in subsequent code. This is useful when using the -D option in gcc -- without STRINGIZE(), extra double-quotes (\") must be added in order to pass strings as macros from the command line.