Commit 82559adc authored by Christophe Favergeon's avatar Christophe Favergeon
Browse files

Added DTW to the PythonWrapper API

parent b46a2f86
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -68,7 +68,7 @@ jobs:

      - name: Archive documentation
        if: ${{ github.event_name == 'pull_request' }}
        uses: actions/upload-artifact@v2
        uses: actions/upload-artifact@v3
        with:
          name: documentation
          path: Documentation/html/
@@ -92,7 +92,7 @@ jobs:
      
      - name: Archive pack
        if: ${{ github.event_name != 'release' }}
        uses: actions/upload-artifact@v2
        uses: actions/upload-artifact@v3
        with:
          path: output/*.pack
          retention-days: 1
@@ -108,7 +108,7 @@ jobs:
          tag: ${{ github.ref }}
          overwrite: true

      - uses: actions/checkout@v2
      - uses: actions/checkout@v3
        if: ${{ github.event_name == 'release' || github.event_name == 'push' || github.event_name == 'workflow_dispatch' }}
        with:
          ref: gh-pages
+8 −8
Original line number Diff line number Diff line
@@ -371,7 +371,7 @@ arm_status arm_dtw_distance_f32(const arm_matrix_instance_f32 *pDistance,
/**
 * @brief        Mapping between query and template
 * @param[in]    pDTW  Cost matrix (Query rows * Template columns)
 * @param[out]   pPath Warping path in cost matrix 2*nb rows + nb columns)
 * @param[out]   pPath Warping path in cost matrix 2*(nb rows + nb columns)
 * @param[out]   pathLength Length of path in number of points
 * @return none
 * 
+145 −0
Original line number Diff line number Diff line
@@ -30,6 +30,13 @@
#define MODINITNAME cmsisdsp_distance

#include "cmsisdsp_module.h"
MATRIXFROMNUMPY(f32,float32_t,double,NPY_DOUBLE);
CREATEMATRIX(f32,float32_t);
NUMPYARRAYFROMMATRIX(f32,NPY_FLOAT);

MATRIXFROMNUMPY(q7,q7_t,int8_t,NPY_BYTE);
CREATEMATRIX(q7,q7_t);
NUMPYARRAYFROMMATRIX(q7,NPY_BYTE);


NUMPYVECTORFROMBUFFER(f32,float32_t,NPY_FLOAT);
@@ -209,6 +216,140 @@ INTDIST(sokalmichener_distance);
INTDIST(sokalsneath_distance);
INTDIST(yule_distance);

static PyObject *
cmsis_arm_dtw_init_window_q7(PyObject *obj, 
                             PyObject *args)
{

  PyObject *pSrc=NULL; // input
  int32_t winType;
  int32_t winSize;
  arm_matrix_instance_q7 pSrc_converted; // input
   

  if (PyArg_ParseTuple(args,"iiO",&winType,&winSize,&pSrc));
  {

    q7MatrixFromNumpy(&pSrc_converted,pSrc);
    uint32_t row = pSrc_converted.numCols ;
    uint32_t column = pSrc_converted.numRows ;

    arm_status returnValue = 
    arm_dtw_init_window_q7(winType,
                           winSize,
                           &pSrc_converted
                           );
    PyObject* theReturnOBJ=Py_BuildValue("i",returnValue);
    PyObject* dstOBJ=NumpyArrayFromq7Matrix(&pSrc_converted);

    PyObject *pythonResult = Py_BuildValue("OO",theReturnOBJ,dstOBJ);

    Py_DECREF(theReturnOBJ);
    Py_DECREF(dstOBJ);
    return(pythonResult);

  }
  Py_RETURN_NONE;
}

static PyObject *
cmsis_arm_dtw_distance_f32(PyObject *obj, 
                    PyObject *args)
{

  PyObject *pDist=NULL; // input
  arm_matrix_instance_f32 pDist_converted; // input
   
  PyObject *pWin=NULL; // input
  arm_matrix_instance_q7 pWin_converted; // input
  arm_matrix_instance_q7 *pWinMatrix;

  arm_matrix_instance_f32 dtw_converted;


  if (PyArg_ParseTuple(args,"OO",&pDist,&pWin));
  {

    f32MatrixFromNumpy(&pDist_converted,pDist);
    if (pWin != Py_None)
    {
       q7MatrixFromNumpy(&pWin_converted,pWin);
       pWinMatrix = &pWin_converted;
    }
    else
    {
        pWinMatrix = NULL;
    }

    uint32_t column = pDist_converted.numCols ;
    uint32_t row = pDist_converted.numRows ;
    createf32Matrix(&dtw_converted,row,column);
    float32_t distance;

    arm_status returnValue = 
    arm_dtw_distance_f32(&pDist_converted,
                         pWinMatrix,
                         &dtw_converted,
                         &distance
                         );

    
    PyObject* theReturnOBJ=Py_BuildValue("i",returnValue);
    PyObject* distOBJ=Py_BuildValue("f",distance);

    PyObject* dstOBJ=NumpyArrayFromf32Matrix(&dtw_converted);


    PyObject *pythonResult = Py_BuildValue("OOO",theReturnOBJ,distOBJ,dstOBJ);

    Py_DECREF(theReturnOBJ);
    Py_DECREF(distOBJ);

    FREEMATRIX(&pDist_converted);
    if (pWinMatrix)
    {
        FREEMATRIX(pWinMatrix);
    }
    Py_DECREF(dstOBJ);

    return(pythonResult);

  }
  Py_RETURN_NONE;
}

static PyObject *
cmsis_arm_dtw_path_f32(PyObject *obj, 
                       PyObject *args)
{
  PyObject *pCost=NULL; // input
  arm_matrix_instance_f32 pCost_converted; // input
  int16_t *pDst=NULL; // output

  if (PyArg_ParseTuple(args,"O",&pCost))
  {
     f32MatrixFromNumpy(&pCost_converted,pCost);

     uint32_t pathLength;
     int32_t blockSize;
     blockSize=2*(pCost_converted.numRows+pCost_converted.numCols);
     pDst=PyMem_Malloc(sizeof(int16_t)*blockSize);


     arm_dtw_path_f32(&pCost_converted,
                      pDst,
                      &pathLength);

     INT16ARRAY1(pDstOBJ,2*pathLength,pDst);

     PyObject *pythonResult = Py_BuildValue("O",pDstOBJ);

     FREEMATRIX(&pCost_converted);
     Py_DECREF(pDstOBJ);
     return(pythonResult);
  }

}

static PyMethodDef CMSISDSPMethods[] = {

@@ -241,6 +382,10 @@ static PyMethodDef CMSISDSPMethods[] = {
    {"arm_sokalsneath_distance",cmsis_arm_sokalsneath_distance, METH_VARARGS,""},
    {"arm_yule_distance",cmsis_arm_yule_distance, METH_VARARGS,""},

    {"arm_dtw_init_window_q7",  cmsis_arm_dtw_init_window_q7, METH_VARARGS,""},
    {"arm_dtw_distance_f32",  cmsis_arm_dtw_distance_f32, METH_VARARGS,""},
    {"arm_dtw_path_f32",  cmsis_arm_dtw_path_f32, METH_VARARGS,""},

    {"error_out", (PyCFunction)error_out, METH_NOARGS, NULL},
    {NULL, NULL, 0, NULL}        /* Sentinel */
};
+2 −0
Original line number Diff line number Diff line
@@ -2186,6 +2186,7 @@ cmsis_arm_mat_solve_upper_triangular_f64(PyObject *obj, PyObject *args)
  Py_RETURN_NONE;
}


static PyMethodDef CMSISDSPMethods[] = {

{"arm_mat_add_f32",  cmsis_arm_mat_add_f32, METH_VARARGS,""},
@@ -2238,6 +2239,7 @@ static PyMethodDef CMSISDSPMethods[] = {
{"arm_householder_f64",  cmsis_arm_householder_f64, METH_VARARGS,""},
{"arm_mat_qr_f32",  cmsis_arm_mat_qr_f32, METH_VARARGS,""},
{"arm_mat_qr_f64",  cmsis_arm_mat_qr_f64, METH_VARARGS,""},

{"error_out", (PyCFunction)error_out, METH_NOARGS, NULL},

    {NULL, NULL, 0, NULL}        /* Sentinel */
+0 −1
Original line number Diff line number Diff line
@@ -35,7 +35,6 @@
#endif

#include <Python.h>
#define MAX(A,B) ((A) < (B) ? (B) : (A))

#define CAT1(A,B) A##B
#define CAT(A,B) CAT1(A,B)
Loading