Exemplo n.º 1
0
void
LTFAT_NAME(ltfatMexFnc)( int UNUSED(nlhs), mxArray *plhs[],
                         int UNUSED(nrhs), const mxArray *prhs[] )
{
    const mxArray* mxf = prhs[0];
    const mxArray* mxg = prhs[1];
    double* aDouble = (double*) mxGetData(prhs[2]);
    double* offsetDouble = (double*) mxGetData(prhs[3]);
    ltfatExtType ext = ltfatExtStringToEnum( mxArrayToString(prhs[4]) );


    // input data length
    mwSize L = mxGetM(mxf);
    // number of channels
    mwSize W = mxGetN(mxf);
    // filter number
    mwSize M = mxGetNumberOfElements(mxg);

    // POINTER TO THE INPUT
    LTFAT_TYPE* fPtr =  mxGetData(prhs[0]);

    // POINTER TO THE FILTERS
    const LTFAT_TYPE* gPtrs[M];
    ltfat_int filtLen[M];
    ltfat_int a[M];
    ltfat_int offset[M];

    // POINTER TO OUTPUTS
    LTFAT_TYPE* cPtrs[M]; // C99 feature
    plhs[0] = mxCreateCellMatrix(M, 1);
    for(mwIndex m=0; m<M; ++m)
    {
        a[m]= (ltfat_int) aDouble[m];
        offset[m] = (ltfat_int) offsetDouble[m];
        filtLen[m] = (ltfat_int) mxGetNumberOfElements(mxGetCell(mxg,m));
        mwSize outLen = (mwSize) filterbank_td_size(L,a[m],filtLen[m],
                                                offset[m],ext);
        mxSetCell(plhs[0], m,
                  ltfatCreateMatrix(outLen,
                                    W,LTFAT_MX_CLASSID,
                                    LTFAT_MX_COMPLEXITY));
        cPtrs[m] = mxGetData(mxGetCell(plhs[0],m));
        gPtrs[m] = mxGetData(mxGetCell(mxg, m));
    }


    LTFAT_NAME(filterbank_td)(fPtr,gPtrs,L,filtLen,W,a,offset,M,cPtrs,ext);

}
void LTFAT_NAME(ltfatMexFnc)( int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[] )
{
  #ifdef _DEBUG
  static int atExitFncRegistered = 0;
  if(!atExitFncRegistered)
  {
     LTFAT_NAME(ltfatMexAtExit)(LTFAT_NAME(tdMexAtExitFnc));
     atExitFncRegistered = 1;
  }
  #endif

  const mxArray* mxf = prhs[0];
  const mxArray* mxg = prhs[1];
  double* a = (double*) mxGetData(prhs[2]);
  double* offset = (double*) mxGetData(prhs[3]);
  char* ext = mxArrayToString(prhs[4]);


  // input data length
  mwSize L = mxGetM(mxf);
  // number of channels
  mwSize W = mxGetN(mxf);
  // filter number
  mwSize M = mxGetNumberOfElements(mxg);
  // filter lengths
  mwSize filtLen[M];
  //mwSize* filtLen = mxMalloc(M*sizeof(mwSize));
  for(unsigned int m=0;m<M;m++)
  {
     filtLen[m] = (mwSize) mxGetNumberOfElements(mxGetCell(mxg,m));
  }

  // output lengths
  mwSize outLen[M];
  //mwSize* outLen = mxMalloc(M*sizeof(mwSize));
  if(!strcmp(ext,"per"))
  {
     for(unsigned int m = 0; m < M; m++)
     {
        outLen[m] = (mwSize) ceil( L/a[m] );
     }
  }
  else if(!strcmp(ext,"valid"))
  {
     for(unsigned int m = 0; m < M; m++)
     {
        outLen[m] = (mwSize) ceil( (L-(filtLen[m]-1))/a[m] );
     }
  }
  else
  {
     for(unsigned int m = 0; m < M; m++)
     {
        outLen[m] = (mwSize) ceil( (L + filtLen[m] - 1 + offset[m] )/a[m] );
     }
  }

     // POINTER TO THE INPUT
     LTFAT_TYPE* fPtr = (LTFAT_TYPE*) mxGetData(prhs[0]);

     // POINTER TO THE FILTERS
     LTFAT_TYPE* gPtrs[M];
     // LTFAT_TYPE** gPtrs = (LTFAT_TYPE**) mxMalloc(M*sizeof(LTFAT_TYPE*));
     for(mwIndex m=0;m<M;m++)
     {
        gPtrs[m] = (LTFAT_TYPE*) mxGetPr(mxGetCell(mxg, m));
     }

     // POINTER TO OUTPUTS
     LTFAT_TYPE* cPtrs[M]; // C99 feature
     //LTFAT_TYPE** cPtrs = (LTFAT_TYPE**) mxMalloc(M*sizeof(LTFAT_TYPE*));
     plhs[0] = mxCreateCellMatrix(M, 1);
     for(mwIndex m=0;m<M;++m)
     {
        mxSetCell(plhs[0], m, ltfatCreateMatrix(outLen[m], W,LTFAT_MX_CLASSID,LTFAT_MX_COMPLEXITY));
        cPtrs[m] = (LTFAT_TYPE*) mxGetData(mxGetCell(plhs[0],m));
        memset(cPtrs[m],0,outLen[m]*W*sizeof(LTFAT_TYPE));
     }

     // over all channels
   //  #pragma omp parallel for private(m)

        for(mwIndex m =0; m<M; m++)
        {
          for(mwIndex w =0; w<W; w++)
          {
           // Obtain pointer to w-th column in input
           LTFAT_TYPE *fPtrCol = fPtr + w*L;
           // Obtaing pointer to w-th column in m-th element of output cell-array
           LTFAT_TYPE *cPtrCol = cPtrs[m] + w*outLen[m];
           //conv_td_sub(fPtrCol,L,&cPtrCol,outLen[m],(const double**)&gPtrs[m],filtLen[m],1,a[m],skip[m],ltfatExtStringToEnum(ext),0);
           LTFAT_NAME(convsub_td)(fPtrCol,L,cPtrCol,outLen[m],gPtrs[m],filtLen[m],a[m],-offset[m],ltfatExtStringToEnum(ext));
          }
        }
}
/*
%COMP_ATROUSFILTERBANK_TD   Uniform filterbank by conv2
%   Usage:  c=comp_atrousfilterbank_fft(f,g,a,offset);
%
%   Input parameters:
%         f   : Input data - L*W array.
%         g   : Filterbank filters - filtLen*M array.
%         a   : Filter upsampling factor - scalar.
%         offset: Delay of the filters - scalar or array of length M.
%
%   Output parameters:
%         c  : L*M*W array of coefficients
%
*/
void LTFAT_NAME(ltfatMexFnc)( int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[] )
{
    const mxArray* mxf = prhs[0];
    const mxArray* mxg = prhs[1];
    double* a = mxGetPr(prhs[2]);
    double* offset = mxGetPr(prhs[3]);

    // input data length
    unsigned int L = mxGetM(mxf);
    // number of channels
    unsigned int W = mxGetN(mxf);
    // filter number
    unsigned int M = mxGetN(mxg);
    // filter length
    unsigned int filtLen = mxGetM(mxg);


        // POINTER TO THE INPUT
        LTFAT_TYPE* fPtr = (LTFAT_TYPE*) mxGetPr(prhs[0]);

        // POINTER TO THE FILTERS
        LTFAT_TYPE** gPtrs = (LTFAT_TYPE**) mxMalloc(M*sizeof(LTFAT_TYPE*));
        for(unsigned int m=0; m<M; m++)
        {
            gPtrs[m] = ((LTFAT_TYPE*) mxGetData(mxg)) + m*filtLen;
        }

        mwSize ndim = 3;
        mwSize dims[3];
        dims[0] =  L;
        dims[1] =  M;
        dims[2] =  W;
        plhs[0] = ltfatCreateNdimArray(ndim,dims,LTFAT_MX_CLASSID,LTFAT_MX_COMPLEXITY);

        // over all filters
        for(unsigned int m =0; m<M; m++)
        {
            // over all channels
            for(unsigned int w =0; w<W; w++)
            {
                // Obtain pointer to w-th column in input
                LTFAT_TYPE *fPtrCol = fPtr + w*L;
                LTFAT_TYPE *cPtrPlane = ((LTFAT_TYPE*) mxGetData(plhs[0])) + w*L*M;
                // Obtaing pointer to w-th column in m-th element of output cell-array
                LTFAT_TYPE *cPtrCol = cPtrPlane + m*L;
                LTFAT_NAME(atrousconvsub_td)(fPtrCol, L, cPtrCol, L, gPtrs[m], filtLen, (int)*a, -offset[m], ltfatExtStringToEnum("per"));
            }
        }
}
/*
%COMP_IFILTERBANK_TD   Synthesis filterbank
%   Usage:  f=comp_ifilterbank_fft(c,g,a,Ls,offset,ext);
%
%   Input parameters:
%         c    : Cell array of length M, each element is N(m)*W matrix.
%         g    : Filterbank filters - length M cell-array, each element is vector of length filtLen(m)
%         a    : Upsampling factors - array of length M.
%         offset : Delay of the filters - scalar or array of length M.
%         Ls   : Output length.
%         ext  : Border exension technique.
%
%   Output parameters:
%         f  : Output Ls*W array.
%
*/
void LTFAT_NAME(ltfatMexFnc)( int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[] )
{
  // printf("Filename: %s, Function name %s, %d \n.",__FILE__,__func__,mxIsDouble(prhs[0]));
  const mxArray* mxc = prhs[0];
  const mxArray* mxg = prhs[1];
  double* a = mxGetPr(prhs[2]);
  double* Lsdouble = mxGetPr(prhs[3]);
  unsigned int Ls = (unsigned int) *Lsdouble;
  double* offset = mxGetPr(prhs[4]);
  char* ext = mxArrayToString(prhs[5]);

  // number of channels
  unsigned int W = mxGetN(mxGetCell(mxc,0));

  // filter number
  unsigned int M = mxGetNumberOfElements(mxg);

  // input data length
  unsigned int* Lc = mxMalloc(M*sizeof(unsigned int));
  for(unsigned int m=0;m<M;m++)
  {
     Lc[m] = (unsigned int) mxGetM(mxGetCell(mxc,m));
  }

  // filter lengths
  unsigned int* filtLen = mxMalloc(M*sizeof(unsigned int));
  for(unsigned int m=0;m<M;m++)
  {
     filtLen[m] = (unsigned int) mxGetNumberOfElements(mxGetCell(mxg,m));
  }

     // POINTER TO THE INPUT
     LTFAT_TYPE** cPtrs = (LTFAT_TYPE**) mxMalloc(M*sizeof(LTFAT_TYPE*));
     for(unsigned int m=0;m<M;++m)
     {
        cPtrs[m] = (LTFAT_TYPE*) mxGetData(mxGetCell(mxc,m));
     }

     // allocate output
     plhs[0] = ltfatCreateMatrix(Ls, W,LTFAT_MX_CLASSID,LTFAT_MX_COMPLEXITY);


      // POINTER TO OUTPUT
     LTFAT_TYPE* fPtr = (LTFAT_TYPE*) mxGetData(plhs[0]);
     // Set to zeros
     memset(fPtr,0,Ls*W*sizeof(LTFAT_TYPE));

     // POINTER TO THE FILTERS
     LTFAT_TYPE** gPtrs = (LTFAT_TYPE**) mxMalloc(M*sizeof(LTFAT_TYPE*));
     for(unsigned int m=0;m<M;m++)
     {
        gPtrs[m] = (LTFAT_TYPE*) mxGetData(mxGetCell(mxg, m));
     }

     // over all channels
   //  #pragma omp parallel for private(m)

        for(unsigned int m =0; m<M; m++)
        {
          for(unsigned int w =0; w<W; w++)
          {
           // Obtain pointer to w-th column in input
           LTFAT_TYPE *fPtrCol = fPtr + w*Ls;
           // Obtaing pointer to w-th column in m-th element of output cell-array
           LTFAT_TYPE *cPtrCol = cPtrs[m] + w*Lc[m];
           //(upconv_td)(const LTFAT_TYPE *in, int inLen, LTFAT_TYPE *out, const int outLen, const LTFAT_TYPE *filts, int fLen, int up, int skip, enum ltfatWavExtType ext)
           LTFAT_NAME(upconv_td)(cPtrCol,Lc[m],fPtrCol,Ls,gPtrs[m],filtLen[m],a[m],-offset[m],ltfatExtStringToEnum(ext));
          }
       }

}
/*
%COMP_IATROUSFILTERBANK_TD   Synthesis Uniform filterbank by conv2
%   Usage:  f=comp_iatrousfilterbank_fft(c,g,a,offset);
%
%   Input parameters:
%         c    : L*M*W array of coefficients.
%         g    : Filterbank filters - filtLen*M array.
%         a    : Filters upsampling factor - scalar.
%         offset : Delay of the filters - scalar or array of length M.
%
%   Output parameters:
%         f  : Output L*W array.
%
*/
void LTFAT_NAME(ltfatMexFnc)( int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[] )
{
    // printf("Filename: %s, Function name %s, %d \n.",__FILE__,__func__,mxIsDouble(prhs[0]));
    const mxArray* mxc = prhs[0];
    const mxArray* mxg = prhs[1];
    double* a = mxGetPr(prhs[2]);
    double* offset = mxGetPr(prhs[3]);

    // number of channels
    const mwSize *dims = mxGetDimensions(mxc);
    unsigned int L = dims[0];
    unsigned int M = dims[1];
    unsigned int W = 1;
    if(mxGetNumberOfDimensions(mxc)>2)
    {
        W = dims[2];
    }

    // filter number
    //unsigned int M = mxGetNumberOfElements(mxg);

    // filter length
    unsigned int filtLen = mxGetM(mxg);

        // allocate output
        mwSize ndim2 = 2;
        mwSize dims2[2];
        dims2[0] =  L;
        dims2[1] =  W;
        plhs[0] = ltfatCreateNdimArray(ndim2,dims2,LTFAT_MX_CLASSID,LTFAT_MX_COMPLEXITY);

        // POINTER TO OUTPUT
        LTFAT_TYPE* fPtr = (LTFAT_TYPE*) mxGetData(plhs[0]);
        // Set to zeros
        memset(fPtr,0,L*W*sizeof(LTFAT_TYPE));

        // POINTER TO THE FILTERS
        LTFAT_TYPE** gPtrs = (LTFAT_TYPE**) mxMalloc(M*sizeof(LTFAT_TYPE*));
        for(unsigned int m=0; m<M; m++)
        {
            gPtrs[m] = ((LTFAT_TYPE*) mxGetData(mxg)) + m*filtLen;
        }

        // over all channels
        //  #pragma omp parallel for private(m)
        for(unsigned int m =0; m<M; m++)
        {
            for(unsigned int w =0; w<W; w++)
            {
                // Obtain pointer to w-th column in input
                LTFAT_TYPE *fPtrCol = fPtr + w*L;
                LTFAT_TYPE *cPtrPlane = ((LTFAT_TYPE*) mxGetData(mxc)) + w*L*M;

                // Obtaing pointer to w-th column in m-th element of output cell-array
                LTFAT_TYPE *cPtrCol = cPtrPlane + m*L;
                //(upconv_td)(const LTFAT_TYPE *in, int inLen, LTFAT_TYPE *out, const int outLen, const LTFAT_TYPE *filts, int fLen, int up, int skip, enum ltfatWavExtType ext)
                LTFAT_NAME(atrousupconv_td)(cPtrCol,L,fPtrCol,L,gPtrs[m],filtLen,(int)*a,-offset[m],ltfatExtStringToEnum("per"));
            }
        }

}