[SciPy-user] Passing numpy array to a c function

Christopher Brown c-b@asu....
Sun Apr 20 15:58:10 CDT 2008

Hi List,

I am trying to pass a numpy array of float64's to a c function, and I am 
not having much luck. I have tried every example I could find, and based 
on everything I have read, this seems (to me) like it should work. 
Here's a minimal version of my function:

#include "Python.h"
#include "Numeric/arrayobject.h"

PyObject* my_c_function(PyObject* self, PyObject* args) {
   PyArrayObject *array;
   int i;
   if (!PyArg_ParseTuple(args, "O|i", &array, &i))
     return NULL;
   printf("length is: %5.0f\n",i);
   printf("type_num is: %3.0f\n",array->descr->type_num);
   return 0;

Here's what I get in ipython:

In [1]: import mymodule

In [2]: from scikits.audiolab import wavread

In [3]: targ = wavread('/home/code-breaker/ding.wav')

In [4]: buff = targ[0][:,0] # take the left channel of a stereo wavefile

In [5]: type(buff)
Out[5]: <type 'numpy.ndarray'>

In [6]: type(buff[0])
Out[6]: <type 'numpy.float64'>

In [7]: len(buff)
Out[7]: 20191

In [8]: buff
array([ -2.44140625e-04,  -3.05175781e-05,   1.52587891e-04, ...,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00])

In [9]: test = mymodule.my_c_function(buff,len(buff))
length is:    -0
type_num is:  -0

In [10]:

What am I doing wrong?


More information about the SciPy-user mailing list