[b6ad1a]: mex / comp_filterbank.c  Maximize  Restore  History

Download this file

135 lines (109 with data), 3.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "mex.h"
void comp_filterbank_td(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[] );
void comp_filterbank_fft(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[] );
void comp_filterbank_fftbl(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[] );
// Calling convention:
// comp_filterbank(f,g,a);
void mexFunction( int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[] )
{
const mxArray* mxf = prhs[0];
const mxArray* mxg = prhs[1];
const mxArray* mxa = prhs[2];
// input data length
const mwSize L = mxGetM(mxf);
const mwSize W = mxGetN(mxf);
// filter number
const mwSize M = mxGetNumberOfElements(mxg);
// a col count
mwSize acols = mxGetN(mxa);
// pointer to a
double *a = (double*) mxGetData(mxa);
if(acols>1)
{
int isOnes = 1;
for(mwIndex m=0;m<M;m++)
{
isOnes = isOnes && a[M+m]==1;
}
if(isOnes)
{
acols = 1;
}
}
// Cell output
plhs[0] = mxCreateCellMatrix(M, 1);
// Stuff for sorting the filters
mwSize tdCount = 0;
mwSize fftCount = 0;
mwSize fftblCount = 0;
mwIndex tdArgsIdx[M];
mwIndex fftArgsIdx[M];
mwIndex fftblArgsIdx[M];
// WALK the filters to determine what has to be done
for(mwIndex m =0; m<M; m++)
{
mxArray * gEl = mxGetCell(mxg, m);
if(mxGetField(gEl,0,"h")!=NULL)
{
tdArgsIdx[tdCount++] = m;
continue;
}
if(mxGetField(gEl,0,"H")!=NULL)
{
if(acols==1&&L==mxGetNumberOfElement(mxGetField(gEl,0,"H")))
{
fftArgsIdx[fftCount++] = m;
continue;
}
else
{
fftblArgsIdx[fftblCount++] = m;
continue;
}
}
}
if(tdCount>0)
{
/*
Here, we have to reformat the inputs and pick up results to comply with:
c=comp_filterbank_td(f,g,a,offset,ext);
BEWARE OF THE AUTOMATIC DEALLOCATION!! by the Matlab engine.
Arrays can be very easily freed twice causing segfaults.
This happends particulary when using mxCreateCell* which stores
pointers to other mxArray structs. Setting all such pointers to
NULL after they are used seems to solve it.
*/
mxArray* plhs_td[1];
const mxArray* prhs_td[5];
prhs_td[0] = mxf;
prhs_td[1] = mxCreateCellMatrix(tdCount,1);
prhs_td[2] = mxCreateDoubleMatrix(tdCount,1,mxREAL);
double* aPtr = (double*)mxGetPr(prhs_td[2]);
prhs_td[3] = mxCreateDoubleMatrix(tdCount,1,mxREAL);
double* offsetPtr = (double*)mxGetPr(prhs_td[3]);
prhs_td[4] = mxCreateString("per");
for(mwIndex m=0;m<tdCount;m++)
{
mxArray * gEl = mxGetCell(mxg, tdArgsIdx[m]);
mxSetCell((mxArray*)prhs_td[1],m,mxGetField(gEl,0,"h"));
// This has overhead
//mxSetCell((mxArray*)prhs_td[1],m,mxDuplicateArray(mxGetField(gEl,0,"h")));
aPtr[m] = a[tdArgsIdx[m]];
offsetPtr[m] = mxGetScalar(mxGetField(gEl,0,"offset"));
}
// Finally call it!
comp_filterbank_td(1,plhs_td,5, prhs_td);
// This has overhead:
// mexCallMATLAB(1,plhs_td,5, prhs_td,"comp_filterbank_td");
// Copy pointers to a proper index in the output + unset all duplicate cell elements
for(mwIndex m=0;m<tdCount;m++)
{
mxSetCell(plhs[0],tdArgsIdx[m],mxGetCell(plhs_td[0],m));
mxSetCell(plhs_td[0],m,NULL);
mxSetCell((mxArray*)prhs_td[1],m,NULL);
}
}
if(fftCount>0)
{
}
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks