Update of /cvsroot/pure-data/externals/iem/iemmatrix/src
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19974/src
Modified Files:
mtx_conv.c
Log Message:
mtx_conv: one or two dimensional convolution for matrices. help-patch
added; object repaired.
Index: mtx_conv.c
===================================================================
RCS file: /cvsroot/pure-data/externals/iem/iemmatrix/src/mtx_conv.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** mtx_conv.c 29 Mar 2006 12:07:52 -0000 1.4
--- mtx_conv.c 23 Aug 2006 15:14:53 -0000 1.5
***************
*** 24,38 ****
int rows;
int columns;
int rows_k;
int columns_k;
int size_k;
int rows_y;
int columns_y;
int size_y;
- t_float renorm_fac;
t_float **x;
t_float **k;
t_float **y;
t_outlet *list_outlet;
--- 24,42 ----
int rows;
int columns;
+
int rows_k;
int columns_k;
int size_k;
+
int rows_y;
int columns_y;
int size_y;
t_float **x;
+ t_float *x_array;
t_float **k;
+ t_float *k_array;
t_float **y;
+ t_float *y_array;
t_outlet *list_outlet;
***************
*** 41,117 ****
};
! static t_float **getTFloatMatrix (int rows, int columns)
{
! t_float **matrix = (t_float **) getbytes
! (sizeof (t_float *) * columns);
! t_float **mtx = matrix;
!
! while (columns--)
! if (!(*matrix++ = (t_float *) getbytes
! (sizeof (t_float) * rows)))
! post("an error has occured :)");
! return mtx;
! }
! static void deleteTFloatMatrix (t_float **matrix, int rows, int columns)
! {
! int n = columns;
! t_float **matr = matrix;
! if (matr) {
! while (n--)
! if (*matr)
! freebytes (*matr++, sizeof(t_float) * rows);
! freebytes (matrix, sizeof(t_float*) * columns);
}
}
!
! static t_float **resizeTFloatMatrix (t_float **old, int rows_old, int columns_old,
! int rows_new, int columns_new)
{
! t_float **mtx = old;
! int count1;
! post("resizing from %dx%d to %dx%d", rows_old, columns_old, rows_new, columns_new);
!
! if ((rows_new == 0)||(columns_new == 0)) {
! deleteTFloatMatrix (old, rows_old, columns_old);
! old = 0;
! return old;
! }
! // 1. if rows_old>rows_new: old row disposal
! if (rows_old>rows_new)
! for (count1 = (rows_old - rows_new), mtx += rows_new;
! count1--; mtx++)
! freebytes (*mtx, sizeof(t_float) * columns_old);
! // 2. resize row (double) pointer
! mtx = old = (t_float **) resizebytes(old, sizeof(t_float*) * rows_old,
! sizeof(t_float*) * rows_new);
! // 3. resizing rows if new size is smaller
! if (rows_old>rows_new)
! for (count1 = rows_new; count1--; mtx++)
! *mtx = (t_float *) resizebytes (*mtx, sizeof(t_float) * columns_old,
! sizeof(t_float) * columns_new);
! else { // 4. resizing old rows if new size is bigger, allocating new rows
! for (count1 = rows_old; count1--; mtx++)
! *mtx = (t_float *) resizebytes (*mtx, sizeof(t_float) * columns_old,
! sizeof(t_float) * columns_new);
! for (count1 = (rows_new - rows_old); count1--; mtx++)
! *mtx = (t_float *) getbytes (sizeof(t_float) * columns_new);
! }
! /* post("return resize"); */
! return old;
}
static void deleteMTXConv (MTXConv *mtx_conv_obj)
{
! deleteTFloatMatrix (mtx_conv_obj->k, mtx_conv_obj->rows_k, mtx_conv_obj->columns_k);
! deleteTFloatMatrix (mtx_conv_obj->x, mtx_conv_obj->rows, mtx_conv_obj->columns);
! deleteTFloatMatrix (mtx_conv_obj->y, mtx_conv_obj->rows_y, mtx_conv_obj->columns_y);
if (mtx_conv_obj->list)
freebytes (mtx_conv_obj->list, sizeof(t_float) * (mtx_conv_obj->size_y + 2));
- mtx_conv_obj->k = 0;
- mtx_conv_obj->x = 0;
- mtx_conv_obj->y = 0;
mtx_conv_obj->list = 0;
}
--- 45,90 ----
};
! static void getTFloatMatrix (int rows, int columns, t_float ***mtx, t_float **array)
{
! int size = rows*columns;
! t_float *ptr;
! t_float **dptr;
! if (!size)
! return;
!
! if (*array=ptr=(t_float *)calloc(sizeof(t_float),size)) {
! if (*mtx=dptr=(t_float **)calloc(sizeof(t_float *),rows)) {
! for(;rows-- ; ptr+=columns) {
! *dptr++ = ptr;
! }
! } else {
! freebytes (*array,sizeof(t_float)*size);
! array=0;
! }
}
}
! static void deleteTFloatMatrix (int rows, int columns, t_float ***mtx, t_float **array)
{
! int size = rows*columns;
!
! if (*mtx)
! freebytes (*mtx, sizeof(t_float*) * columns);
! if (*array)
! freebytes (*array, sizeof(t_float) * size);
! *mtx=0;
! *array=0;
}
+
static void deleteMTXConv (MTXConv *mtx_conv_obj)
{
! deleteTFloatMatrix (mtx_conv_obj->rows_k, mtx_conv_obj->columns_k, &mtx_conv_obj->k, &mtx_conv_obj->k_array);
! deleteTFloatMatrix (mtx_conv_obj->rows, mtx_conv_obj->columns, &mtx_conv_obj->x, &mtx_conv_obj->x_array);
! deleteTFloatMatrix (mtx_conv_obj->rows_y, mtx_conv_obj->columns_y, &mtx_conv_obj->y, &mtx_conv_obj->y_array);
if (mtx_conv_obj->list)
freebytes (mtx_conv_obj->list, sizeof(t_float) * (mtx_conv_obj->size_y + 2));
mtx_conv_obj->list = 0;
}
***************
*** 163,173 ****
static void readMatrixFromList (int rows, int columns, t_atom *l, t_float **mtx)
{
! for (;rows--; l+=columns)
! readFloatFromList (columns, l, *mtx++);
}
static void writeMatrixIntoList (int rows, int columns, t_atom *l, t_float **mtx)
{
! for (;rows--; l+=columns)
! writeFloatIntoList (columns, l, *mtx++);
}
--- 136,150 ----
static void readMatrixFromList (int rows, int columns, t_atom *l, t_float **mtx)
{
! int n,m;
! for (n=0;n<rows; n++)
! for (m=0;m<columns; m++)
! mtx[n][m]=atom_getfloat (l++);
}
static void writeMatrixIntoList (int rows, int columns, t_atom *l, t_float **mtx)
{
! int n,m;
! for (n=0;n<rows; n++)
! for (m=0;m<columns; m++, l++)
! SETFLOAT(l,mtx[n][m]);
}
***************
*** 179,206 ****
int in_size = argc-2;
int size_k = rows_k * columns_k;
- t_float **k = mtx_conv_obj->k;
! if (!size_k)
post ("mtx_conv: invalid matrix dimensions!");
! else if (in_size < size_k)
post("mtx_conv: sparse matrix not yet supported: use \"mtx_check\"");
! else if ((rows_k != mtx_conv_obj->rows_k) || (columns_k != mtx_conv_obj->columns_k)) {
! if (k)
! k = resizeTFloatMatrix (k, mtx_conv_obj->rows_k, mtx_conv_obj->columns_k,
! rows_k, columns_k);
! else
! k = getTFloatMatrix (rows_k, columns_k);
mtx_conv_obj->rows_k = rows_k;
mtx_conv_obj->columns_k = columns_k;
-
- readMatrixFromList (rows_k, columns_k, argv, k);
-
- mtx_conv_obj->k = k;
mtx_conv_obj->size_k = size_k;
}
! else
! readMatrixFromList (rows_k, columns_k, argv, k);
}
static void convolveRow (int columns, int columns_c, t_float *x, t_float *c, t_float *y)
{
--- 156,188 ----
int in_size = argc-2;
int size_k = rows_k * columns_k;
! if (!size_k) {
post ("mtx_conv: invalid matrix dimensions!");
! return;
! }
! if (in_size < size_k) {
post("mtx_conv: sparse matrix not yet supported: use \"mtx_check\"");
! return;
! }
+ if ((rows_k != mtx_conv_obj->rows_k) || (columns_k != mtx_conv_obj->columns_k)) {
+ if (mtx_conv_obj->k)
+ deleteTFloatMatrix (mtx_conv_obj->rows_k, mtx_conv_obj->columns_k,
+ &mtx_conv_obj->k, &mtx_conv_obj->k_array);
+ post("getting %d rows and %d columns for k",rows_k,columns_k);
+ getTFloatMatrix (rows_k, columns_k, &mtx_conv_obj->k, &mtx_conv_obj->k_array);
+ if ((!mtx_conv_obj->k)||(!mtx_conv_obj->k_array)) {
+ post("mtx_conv: memory allocation failed!");
+ return;
+ }
mtx_conv_obj->rows_k = rows_k;
mtx_conv_obj->columns_k = columns_k;
mtx_conv_obj->size_k = size_k;
+
}
!
! readMatrixFromList (rows_k, columns_k, argv, mtx_conv_obj->k);
}
+
static void convolveRow (int columns, int columns_c, t_float *x, t_float *c, t_float *y)
{
***************
*** 211,222 ****
}
! static void convolveMtx (int rows, int columns, int rows_c, int columns_c,
! t_float **x, t_float **c, t_float **y)
{
! int n,k,count;
! zeroTFloatMatrix (y, rows+rows_c-1, columns+columns_c-1);
! for (k = 0; k < rows_c; k++)
! for (n = k, count = rows; count--; n++)
! convolveRow (columns, columns_c, x[n-k], c[k], y[n]);
}
--- 193,210 ----
}
! static void convolveMtx (int rows, int columns, int rows_h, int columns_h,
! t_float **x, t_float **h, t_float **y)
{
! int n,m,k,l;
! int rows_y=rows+rows_h-1;
! int cols_y=columns+columns_h-1;
! zeroTFloatMatrix (y, rows_y, cols_y);
!
!
! for (n=0; n<rows_y; n++)
! for (m=0; m<cols_y; m++)
! for (k=n; (k<rows_h)&&(k-n<rows); k++)
! for (l=m; (l<columns_h)&&(l-m<columns); l++)
! y[n][m]+=x[k-n][l-m]*h[k][l];
}
***************
*** 236,242 ****
int size_y = mtx_conv_obj->size_y;
t_atom *list_ptr = mtx_conv_obj->list;
- t_float **x = mtx_conv_obj->x;
- t_float **y = mtx_conv_obj->y;
- t_float **k = mtx_conv_obj->k;
// fftsize check
--- 224,227 ----
***************
*** 255,264 ****
if ((mtx_conv_obj->rows != rows)||(mtx_conv_obj->columns != columns)) {
! if (x)
! x = resizeTFloatMatrix (x, mtx_conv_obj->rows, mtx_conv_obj->columns,
! rows, columns);
! else
! x = getTFloatMatrix (rows, columns);
! mtx_conv_obj->x = x;
mtx_conv_obj->size = size;
mtx_conv_obj->rows = rows;
--- 240,252 ----
if ((mtx_conv_obj->rows != rows)||(mtx_conv_obj->columns != columns)) {
! if (mtx_conv_obj->x)
! deleteTFloatMatrix (mtx_conv_obj->rows, mtx_conv_obj->columns,
! &mtx_conv_obj->x, &mtx_conv_obj->x_array);
! post("getting %d rows and %d columns for x",rows,columns);
! getTFloatMatrix (rows, columns, &mtx_conv_obj->x, &mtx_conv_obj->x_array);
! if ((!mtx_conv_obj->x)||(!mtx_conv_obj->x_array)) {
! post("mtx_conv: memory allocation failed!");
! return;
! }
mtx_conv_obj->size = size;
mtx_conv_obj->rows = rows;
***************
*** 270,296 ****
if ((mtx_conv_obj->rows_y != rows_y)||(mtx_conv_obj->columns_y != columns_y)) {
size_y = rows_y * columns_y;
! if (y)
! y = resizeTFloatMatrix (y, mtx_conv_obj->rows_y, mtx_conv_obj->columns_y,
! rows_y, columns_y);
! else
! y = getTFloatMatrix (rows_y, columns_y);
! if (list_ptr)
! list_ptr = (t_atom *) resizebytes (list_ptr, sizeof(t_atom) * (mtx_conv_obj->size_y+2),
! sizeof (t_atom) * (size_y+2));
! else
! list_ptr = (t_atom *) getbytes (sizeof (t_atom) * (size_y+2));
mtx_conv_obj->size_y = size_y;
mtx_conv_obj->rows_y = rows_y;
mtx_conv_obj->columns_y = columns_y;
! mtx_conv_obj->y = y;
mtx_conv_obj->list = list_ptr;
}
//post("3");
// main part
! readMatrixFromList (rows, columns, argv, x);
//post("4");
! convolveMtx (rows, columns, rows_k, columns_k, x, k, y);
//post("5");
! writeMatrixIntoList (rows_y, columns_y, list_ptr+2, y);
//post("6");
SETSYMBOL(list_ptr, gensym("matrix"));
--- 258,294 ----
if ((mtx_conv_obj->rows_y != rows_y)||(mtx_conv_obj->columns_y != columns_y)) {
size_y = rows_y * columns_y;
! if (mtx_conv_obj->y)
! deleteTFloatMatrix (mtx_conv_obj->rows_y, mtx_conv_obj->columns_y,
! &mtx_conv_obj->y, &mtx_conv_obj->y_array);
! post("getting %d rows and %d columns for y",rows_y,columns_y);
! getTFloatMatrix (rows_y, columns_y, &mtx_conv_obj->y, &mtx_conv_obj->y_array);
! if ((!mtx_conv_obj->y)||(!mtx_conv_obj->y_array)) {
! post("mtx_conv: memory allocation failed!");
! return;
! }
mtx_conv_obj->size_y = size_y;
mtx_conv_obj->rows_y = rows_y;
mtx_conv_obj->columns_y = columns_y;
!
! if (list_ptr)
! list_ptr = (t_atom *) resizebytes (list_ptr, sizeof(t_atom) * (mtx_conv_obj->size_y+2),
! sizeof (t_atom) * (size_y+2));
! else
! list_ptr = (t_atom *) getbytes (sizeof (t_atom) * (size_y+2));
mtx_conv_obj->list = list_ptr;
+ if (!list_ptr) {
+ post("mtx_conv: memory allocation failed!");
+ return;
+ }
+
}
//post("3");
// main part
! readMatrixFromList (rows, columns, argv, mtx_conv_obj->x);
//post("4");
! convolveMtx (rows, columns, rows_k, columns_k,
! mtx_conv_obj->x, mtx_conv_obj->k, mtx_conv_obj->y);
//post("5");
! writeMatrixIntoList (rows_y, columns_y, list_ptr+2, mtx_conv_obj->y);
//post("6");
SETSYMBOL(list_ptr, gensym("matrix"));