source: python/ext/py-fft.c @ 4c37e87

feature/autosinkfeature/cnnfeature/crepefix/ffmpeg5
Last change on this file since 4c37e87 was e14f7a7, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[py] add minimal docstring to fft

  • Property mode set to 100644
File size: 3.8 KB
RevLine 
[5652a8c]1#include "aubio-types.h"
[615ac7d]2
[e14f7a7]3static char Py_fft_doc[] = ""
4"fft(size=1024)\n"
5"\n"
6"Compute Fast Fourier Transorms.\n"
7"\n"
8"Parameters\n"
9"----------\n"
10"size : int\n"
11"    size of the FFT to compute\n"
12"\n"
13"Example\n"
14"-------\n"
15">>> x = aubio.fvec(512)\n"
16">>> f = aubio.fft(512)\n"
17">>> c = f(x); c\n"
18"aubio cvec of 257 elements\n"
19">>> x2 = f.rdo(c); x2.shape\n"
20"(512,)\n"
21"";
[615ac7d]22
[de81d2b]23typedef struct
24{
25  PyObject_HEAD
26  aubio_fft_t * o;
27  uint_t win_s;
[569b363]28  // do / rdo input vectors
29  fvec_t vecin;
30  cvec_t cvecin;
31  // do / rdo output results
[ede5d38]32  PyObject *doout;
33  PyObject *rdoout;
[de81d2b]34} Py_fft;
[615ac7d]35
36static PyObject *
37Py_fft_new (PyTypeObject * type, PyObject * args, PyObject * kwds)
38{
[96fe713]39  int win_s = 0;
[615ac7d]40  Py_fft *self;
[96fe713]41  static char *kwlist[] = { "win_s", NULL };
[615ac7d]42
[96fe713]43  if (!PyArg_ParseTupleAndKeywords (args, kwds, "|I", kwlist,
44          &win_s)) {
[615ac7d]45    return NULL;
46  }
47
48  self = (Py_fft *) type->tp_alloc (type, 0);
49
50  if (self == NULL) {
51    return NULL;
52  }
53
54  self->win_s = Py_default_vector_length;
55
56  if (win_s > 0) {
57    self->win_s = win_s;
58  } else if (win_s < 0) {
59    PyErr_SetString (PyExc_ValueError,
60        "can not use negative window size");
61    return NULL;
62  }
63
64  return (PyObject *) self;
65}
66
[de81d2b]67static int
68Py_fft_init (Py_fft * self, PyObject * args, PyObject * kwds)
69{
70  self->o = new_aubio_fft (self->win_s);
71  if (self->o == NULL) {
[dab4a4c]72    // PyErr_Format(PyExc_RuntimeError, ...) was set above by new_ which called
73    // AUBIO_ERR when failing
[de81d2b]74    return -1;
75  }
[b5bef11]76
[ede5d38]77  self->doout = new_py_cvec(self->win_s);
78  self->rdoout = new_py_fvec(self->win_s);
[615ac7d]79
[de81d2b]80  return 0;
81}
[615ac7d]82
[de81d2b]83static void
84Py_fft_del (Py_fft *self, PyObject *unused)
85{
[ede5d38]86  Py_XDECREF(self->doout);
87  Py_XDECREF(self->rdoout);
[23982aa]88  if (self->o) {
89    del_aubio_fft(self->o);
90  }
[5c1200a]91  Py_TYPE(self)->tp_free((PyObject *) self);
[de81d2b]92}
[615ac7d]93
[b5bef11]94static PyObject *
[de81d2b]95Py_fft_do(Py_fft * self, PyObject * args)
[615ac7d]96{
97  PyObject *input;
[a138975]98  cvec_t c_out;
[615ac7d]99
100  if (!PyArg_ParseTuple (args, "O", &input)) {
101    return NULL;
102  }
103
[569b363]104  if (!PyAubio_ArrayToCFvec(input, &(self->vecin))) {
[615ac7d]105    return NULL;
106  }
107
[6014dc0]108  if (self->vecin.length != self->win_s) {
109    PyErr_Format(PyExc_ValueError,
[8147e03]110                 "input array has length %d, but fft expects length %d",
[6014dc0]111                 self->vecin.length, self->win_s);
112    return NULL;
113  }
114
[ede5d38]115  Py_INCREF(self->doout);
116  if (!PyAubio_PyCvecToCCvec(self->doout, &c_out)) {
117    return NULL;
118  }
[615ac7d]119  // compute the function
[ede5d38]120  aubio_fft_do (self->o, &(self->vecin), &c_out);
121  return self->doout;
[615ac7d]122}
123
[5652a8c]124static PyMemberDef Py_fft_members[] = {
[615ac7d]125  {"win_s", T_INT, offsetof (Py_fft, win_s), READONLY,
126    "size of the window"},
[5652a8c]127  {NULL}
128};
[615ac7d]129
[b5bef11]130static PyObject *
[965b302]131Py_fft_rdo(Py_fft * self, PyObject * args)
[615ac7d]132{
133  PyObject *input;
[a138975]134  fvec_t out;
[615ac7d]135
136  if (!PyArg_ParseTuple (args, "O", &input)) {
137    return NULL;
138  }
139
[92a8800]140  if (!PyAubio_PyCvecToCCvec (input, &(self->cvecin)) ) {
[615ac7d]141    return NULL;
142  }
143
[1f87c1b]144  if (self->cvecin.length != self->win_s / 2 + 1) {
145    PyErr_Format(PyExc_ValueError,
[8147e03]146                 "input cvec has length %d, but fft expects length %d",
147                 self->cvecin.length, self->win_s / 2 + 1);
[1f87c1b]148    return NULL;
149  }
150
[ede5d38]151  Py_INCREF(self->rdoout);
152  if (!PyAubio_ArrayToCFvec(self->rdoout, &out) ) {
153    return NULL;
154  }
[615ac7d]155  // compute the function
[ede5d38]156  aubio_fft_rdo (self->o, &(self->cvecin), &out);
157  return self->rdoout;
[615ac7d]158}
159
160static PyMethodDef Py_fft_methods[] = {
161  {"rdo", (PyCFunction) Py_fft_rdo, METH_VARARGS,
162    "synthesis of spectral grain"},
163  {NULL}
164};
165
[5652a8c]166PyTypeObject Py_fftType = {
167  PyVarObject_HEAD_INIT (NULL, 0)
168  "aubio.fft",
169  sizeof (Py_fft),
170  0,
171  (destructor) Py_fft_del,
172  0,
173  0,
174  0,
175  0,
176  0,
177  0,
178  0,
179  0,
180  0,
181  (ternaryfunc)Py_fft_do,
182  0,
183  0,
184  0,
185  0,
186  Py_TPFLAGS_DEFAULT,
187  Py_fft_doc,
188  0,
189  0,
190  0,
191  0,
192  0,
193  0,
194  Py_fft_methods,
195  Py_fft_members,
196  0,
197  0,
198  0,
199  0,
200  0,
201  0,
202  (initproc) Py_fft_init,
203  0,
204  Py_fft_new,
[0e70ef9]205  0,
206  0,
207  0,
208  0,
209  0,
210  0,
211  0,
212  0,
213  0,
[5652a8c]214};
Note: See TracBrowser for help on using the repository browser.