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/.

Tuesday 25 September 2018

Calling The SigLib Digital Signal Processing Library From Julia

SigLib DSP functions can be called from many languages, such as Python, Java, C#, Perl etc., using the SWIG API.

I've been tracking Julia for a while however V1.0 of Julia was released recently so I thought I would evaluate some of its cool features for DSP prototyping.

Integrating SigLib with Julia was unbelievably easy using the ccall API. If only other languages contained a similarly simple API ;-)

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

For SigLib, copy the following code into a file "juliaSigLib.jl" :

A = [3.4, 1.8, -2.8, 6.4]
B = Array{Float64,1}(undef, length(A))

absMax = ccall((:SDA_AbsMax, "siglib"), Float64, (Ptr{Cdouble},Cint), A, length(A))
println("absMax: ", absMax)

ccall((:SDA_SortMinToMax, "siglib"), Cvoid, (Ptr{Cdouble},Ptr{Cdouble},Cint), A, B, length(A))
println("B: ", B)


Now copy the appropriate shared library to the current working directory where you will run juliaSigLib.jl.

OS Source File Destination File
32 Bit Linux siglib/lib/linux/siglib.so.1.0 siglib.so
64 Bit Linux siglib/lib/linux_64/siglib.so.1.0 siglib.so
Raspberry Pi Linux siglib/lib/RaspberryPi/siglib.so.1.0 siglib.so
32 Bit Windows siglib\lib\Microsoft\dynamic_library\Release\siglib.dll siglib.dll
64 Bit Windows siglib\lib\Microsoft\dynamic_library_64\Release\siglib64.dll siglib.dll


To execute the script use :

julia juliaSigLib.jl

Awesome!

Reference : https://docs.julialang.org/en/v0.6.1/manual/calling-c-and-fortran-code/

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