libiir/python/pyiir.i

143 lines
3.4 KiB
OpenEdge ABL

%module iir
%include <complex.i>
/*
* List conversion
*
* SWIG knows how to turn std::vector<T> func() into a function that returns a
* Python list. We do need to tell it about all <T> types with %template,
* however.
*/
%include <std_vector.i>
%template(vectord) std::vector<double>;
%{
#include "iir++.h"
%}
/*
* Add bounds checking
*
* The Python library is targetted at experimentation. It is therefore
* reasonable to protect its object query and creation interfaces with code
* that performs bounds checking on arguments.
*/
%exception IIR::Coeff::c {
if(arg2 < 0 || arg2 >= arg1->nc()) {
char buf[80];
snprintf(buf, sizeof(buf), "Index of c coefficient (%d) out of bounds "
"(0,%d).", arg2, arg1->nc() - 1);
PyErr_SetString(PyExc_RuntimeError, buf);
return NULL;
}
$action
}
%exception IIR::Coeff::d {
if(arg2 < 0 || arg2 >= arg1->nd()) {
char buf[80];
snprintf(buf, sizeof(buf), "Index of d coefficient (%d) out of bounds "
"(0,%d).", arg2, arg1->nd() - 1);
PyErr_SetString(PyExc_RuntimeError, buf);
return NULL;
}
$action
}
%exception IIR::Filter::getCoeffSet {
if(arg2 < 0 || arg2 >= arg1->numCoeffSet()) {
char buf[80];
snprintf(buf, sizeof(buf), "Index of coefficient set (%d) out of bounds "
"(0,%d).", arg2, arg1->numCoeffSet() - 1);
PyErr_SetString(PyExc_RuntimeError, buf);
return NULL;
}
$action
}
%exception IIR::ButterworthLowPass {
if(arg1 < 1) {
char buf[80];
snprintf(buf, sizeof(buf), "Invalid filter order (%d). Must be > 0.",
arg1);
PyErr_SetString(PyExc_RuntimeError, buf);
return NULL;
}
$action
}
%exception IIR::ButterworthHighPass {
if(arg1 < 1) {
char buf[80];
snprintf(buf, sizeof(buf), "Invalid filter order (%d). Must be > 0.",
arg1);
PyErr_SetString(PyExc_RuntimeError, buf);
return NULL;
}
$action
}
%exception IIR::ButterworthBandPass {
if(arg1 < 1) {
char buf[80];
snprintf(buf, sizeof(buf), "Invalid filter order (%d). Must be > 0.",
arg1);
PyErr_SetString(PyExc_RuntimeError, buf);
return NULL;
}
if(arg3 > arg4) {
PyErr_SetString(PyExc_RuntimeError, "Low corner frequency exceeds "
"high corner frequency.");
return NULL;
}
$action
}
%exception IIR::ButterworthBandStop {
if(arg1 < 1) {
char buf[80];
snprintf(buf, sizeof(buf), "Invalid filter order (%d). Must be > 0.",
arg1);
PyErr_SetString(PyExc_RuntimeError, buf);
return NULL;
}
if(arg3 > arg4) {
PyErr_SetString(PyExc_RuntimeError, "Low corner frequency exceeds "
"high corner frequency.");
return NULL;
}
$action
}
/*
* Invalid filter string handling
*
* The C++ library uses a "valid()" member in IIR::Filter to avoid throwing an
* exception if the constructor is passed an invalid filter string. Python's
* exceptions are a much nicer fit, however, so use those instead.
*
* Notes:
* 1. Doesn't match if we prepend it with the namespace.
* 2. Applies to all constructors.
*/
%exception Filter {
$action
if(!result->valid()) {
PyErr_SetString(PyExc_RuntimeError, "Invalid filter description string.");
delete result;
return NULL;
}
}
%include "iir++.h"
// vim: ts=4:sw=4