[py] add minimal docstring to fft
[aubio.git] / python / ext / py-fft.c
index 61595b9..a08af4e 100644 (file)
@@ -1,6 +1,24 @@
 #include "aubio-types.h"
 
-static char Py_fft_doc[] = "fft object";
+static char Py_fft_doc[] = ""
+"fft(size=1024)\n"
+"\n"
+"Compute Fast Fourier Transorms.\n"
+"\n"
+"Parameters\n"
+"----------\n"
+"size : int\n"
+"    size of the FFT to compute\n"
+"\n"
+"Example\n"
+"-------\n"
+">>> x = aubio.fvec(512)\n"
+">>> f = aubio.fft(512)\n"
+">>> c = f(x); c\n"
+"aubio cvec of 257 elements\n"
+">>> x2 = f.rdo(c); x2.shape\n"
+"(512,)\n"
+"";
 
 typedef struct
 {
@@ -11,8 +29,8 @@ typedef struct
   fvec_t vecin;
   cvec_t cvecin;
   // do / rdo output results
-  cvec_t *out;
-  fvec_t *rout;
+  PyObject *doout;
+  PyObject *rdoout;
 } Py_fft;
 
 static PyObject *
@@ -51,14 +69,13 @@ Py_fft_init (Py_fft * self, PyObject * args, PyObject * kwds)
 {
   self->o = new_aubio_fft (self->win_s);
   if (self->o == NULL) {
-    char_t errstr[30];
-    sprintf(errstr, "error creating fft with win_s=%d", self->win_s);
-    PyErr_SetString (PyExc_Exception, errstr);
+    // PyErr_Format(PyExc_RuntimeError, ...) was set above by new_ which called
+    // AUBIO_ERR when failing
     return -1;
   }
 
-  self->out = new_cvec(self->win_s);
-  self->rout = new_fvec(self->win_s);
+  self->doout = new_py_cvec(self->win_s);
+  self->rdoout = new_py_fvec(self->win_s);
 
   return 0;
 }
@@ -66,9 +83,11 @@ Py_fft_init (Py_fft * self, PyObject * args, PyObject * kwds)
 static void
 Py_fft_del (Py_fft *self, PyObject *unused)
 {
-  del_aubio_fft(self->o);
-  del_cvec(self->out);
-  del_fvec(self->rout);
+  Py_XDECREF(self->doout);
+  Py_XDECREF(self->rdoout);
+  if (self->o) {
+    del_aubio_fft(self->o);
+  }
   Py_TYPE(self)->tp_free((PyObject *) self);
 }
 
@@ -76,6 +95,7 @@ static PyObject *
 Py_fft_do(Py_fft * self, PyObject * args)
 {
   PyObject *input;
+  cvec_t c_out;
 
   if (!PyArg_ParseTuple (args, "O", &input)) {
     return NULL;
@@ -85,10 +105,20 @@ Py_fft_do(Py_fft * self, PyObject * args)
     return NULL;
   }
 
+  if (self->vecin.length != self->win_s) {
+    PyErr_Format(PyExc_ValueError,
+                 "input array has length %d, but fft expects length %d",
+                 self->vecin.length, self->win_s);
+    return NULL;
+  }
+
+  Py_INCREF(self->doout);
+  if (!PyAubio_PyCvecToCCvec(self->doout, &c_out)) {
+    return NULL;
+  }
   // compute the function
-  aubio_fft_do (((Py_fft *)self)->o, &(self->vecin), self->out);
-  // convert cvec to py_cvec
-  return PyAubio_CCvecToPyCvec(self->out);
+  aubio_fft_do (self->o, &(self->vecin), &c_out);
+  return self->doout;
 }
 
 static PyMemberDef Py_fft_members[] = {
@@ -101,6 +131,7 @@ static PyObject *
 Py_fft_rdo(Py_fft * self, PyObject * args)
 {
   PyObject *input;
+  fvec_t out;
 
   if (!PyArg_ParseTuple (args, "O", &input)) {
     return NULL;
@@ -110,9 +141,20 @@ Py_fft_rdo(Py_fft * self, PyObject * args)
     return NULL;
   }
 
+  if (self->cvecin.length != self->win_s / 2 + 1) {
+    PyErr_Format(PyExc_ValueError,
+                 "input cvec has length %d, but fft expects length %d",
+                 self->cvecin.length, self->win_s / 2 + 1);
+    return NULL;
+  }
+
+  Py_INCREF(self->rdoout);
+  if (!PyAubio_ArrayToCFvec(self->rdoout, &out) ) {
+    return NULL;
+  }
   // compute the function
-  aubio_fft_rdo (self->o, &(self->cvecin), self->rout);
-  return PyAubio_CFvecToArray(self->rout);
+  aubio_fft_rdo (self->o, &(self->cvecin), &out);
+  return self->rdoout;
 }
 
 static PyMethodDef Py_fft_methods[] = {
@@ -160,4 +202,13 @@ PyTypeObject Py_fftType = {
   (initproc) Py_fft_init,
   0,
   Py_fft_new,
+  0,
+  0,
+  0,
+  0,
+  0,
+  0,
+  0,
+  0,
+  0,
 };