Thursday, 27 September 2018

Calling The SigLib Digital Signal Processing Library From Python

I recently wrote a blog post about

Calling The SigLib Digital Signal Processing Library From Julia

and it made me reflect on my main development environment for prototyping DSP code in Python. I've used SWIG (http://www.swig.org/) as an interface for many years and like its portability but I wondered about how to access a shared library from Python directly so did a bit of investigation and found the ctypes API to be very light weight and very easy to use.

To run SigLib (or any other shared library) from Python is very easy using ctypes.

For SigLib, copy the following code into a file "pythonSigLib.py" :

import ctypes
import numpy as np
import platform as _platform

A = np.array([3.4, 1.8, -2.8, 6.4])
B = np.zeros((A.size), dtype=np.double)

if _platform.system() == "Linux":
   lib = ctypes.cdll.LoadLibrary('./siglib.so')
elif _platform.system() == "Darwin":
   lib = ctypes.cdll.LoadLibrary('./siglib.dylib')
elif _platform.system() == "Windows":
    if _platform.machine().endswith('64'):
        lib = ctypes.cdll.LoadLibrary('.\siglib64.dll')
    else:
        lib = ctypes.cdll.LoadLibrary('.\siglib.dll')

SDA_AbsMax = lib.SDA_AbsMax
SDA_AbsMax.restype = ctypes.c_double
absMax = SDA_AbsMax(ctypes.c_void_p(A.ctypes.data), ctypes.c_int(A.size))

SDA_SortMinToMax = lib.SDA_SortMinToMax
SDA_SortMinToMax(ctypes.c_void_p(A.ctypes.data), ctypes.c_void_p(B.ctypes.data), ctypes.c_int(A.size))

print ('absMax: ', absMax)
print ('B: %s' % B)

Now copy the SigLib shared object (.so, .dll or .dylib) file from /siglib/lib to the current working directory and run :

python pythonSigLib.py

For further information about using SigLib, Python and SWIG, please see this blog post :

How To Access A Windows Library From Python, Using SWIG


To download the evaluation version of the Numerix-DSP Libraries : http://www.numerix-dsp.com/eval/.

1 comment: