diff options
author | IOhannes m zmölnig <zmoelnig@users.sourceforge.net> | 2005-10-17 16:17:05 +0000 |
---|---|---|
committer | IOhannes m zmölnig <zmoelnig@users.sourceforge.net> | 2005-10-17 16:17:05 +0000 |
commit | 0a741312eea3cf6c06ba3663650ffa3691b642e7 (patch) | |
tree | 1ce4968ead9f7b8f0d1b3fd6ec09183c6458f22e /src | |
parent | bc07020328352ab8a9562a7f5bab295050c5532a (diff) |
made code more readable for me
made object less crashy
svn path=/trunk/externals/iem/iemmatrix/; revision=3720
Diffstat (limited to 'src')
-rw-r--r-- | src/mtx_rowrfft.c | 142 | ||||
-rw-r--r-- | src/mtx_rowrifft.c | 120 |
2 files changed, 115 insertions, 147 deletions
diff --git a/src/mtx_rowrfft.c b/src/mtx_rowrfft.c index e7f0551..763003c 100644 --- a/src/mtx_rowrfft.c +++ b/src/mtx_rowrfft.c @@ -20,12 +20,8 @@ typedef struct _MTXRowrfft_ MTXRowrfft; struct _MTXRowrfft_ { t_object x_obj; - int rows; - int columns; - int columns_re; int size; int size2; - t_float renorm_fac; t_float *f_re; t_float *f_im; @@ -37,31 +33,32 @@ struct _MTXRowrfft_ t_atom *list_im; }; -static void deleteMTXRowrfft (MTXRowrfft *mtx_rowrfft_obj) +static void deleteMTXRowrfft (MTXRowrfft *x) { - if (mtx_rowrfft_obj->f_re) - freebytes (mtx_rowrfft_obj->f_re, sizeof(t_float)*mtx_rowrfft_obj->size); - if (mtx_rowrfft_obj->f_im) - freebytes (mtx_rowrfft_obj->f_im, sizeof(t_float)*mtx_rowrfft_obj->size); - if (mtx_rowrfft_obj->list_re) - freebytes (mtx_rowrfft_obj->list_re, sizeof(t_atom)*(mtx_rowrfft_obj->size2+2)); - if (mtx_rowrfft_obj->list_im) - freebytes (mtx_rowrfft_obj->list_im, sizeof(t_atom)*(mtx_rowrfft_obj->size2+2)); + free (x->f_re); + free (x->f_im); + free (x->list_re); + free (x->list_im); } static void *newMTXRowrfft (t_symbol *s, int argc, t_atom *argv) { - MTXRowrfft *mtx_rowrfft_obj = (MTXRowrfft *) pd_new (mtx_rowrfft_class); - mtx_rowrfft_obj->list_re_out = outlet_new (&mtx_rowrfft_obj->x_obj, gensym("matrix")); - mtx_rowrfft_obj->list_im_out = outlet_new (&mtx_rowrfft_obj->x_obj, gensym("matrix")); - return ((void *) mtx_rowrfft_obj); + MTXRowrfft *x = (MTXRowrfft *) pd_new (mtx_rowrfft_class); + x->list_re_out = outlet_new (&x->x_obj, gensym("matrix")); + x->list_im_out = outlet_new (&x->x_obj, gensym("matrix")); + + x->size=x->size2=0; + x->f_re=x->f_im=0; + x->list_re=x->list_im=0; + + return ((void *) x); } -static void mTXrowrfftBang (MTXRowrfft *mtx_rowrfft_obj) +static void mTXrowrfftBang (MTXRowrfft *x) { - if (mtx_rowrfft_obj->list_im) { - outlet_anything(mtx_rowrfft_obj->list_im_out, gensym("matrix"), mtx_rowrfft_obj->size2+2, mtx_rowrfft_obj->list_im); - outlet_anything(mtx_rowrfft_obj->list_re_out, gensym("matrix"), mtx_rowrfft_obj->size2+2, mtx_rowrfft_obj->list_re); + if (x->list_im) { + outlet_anything(x->list_im_out, gensym("matrix"), x->size2, x->list_im); + outlet_anything(x->list_re_out, gensym("matrix"), x->size2, x->list_re); } } @@ -98,93 +95,80 @@ static void readFloatFromList (int n, t_atom *l, t_float *f) *f++ = atom_getfloat (l++); } -static void mTXrowrfftMatrix (MTXRowrfft *mtx_rowrfft_obj, t_symbol *s, +static void mTXrowrfftMatrix (MTXRowrfft *x, t_symbol *s, int argc, t_atom *argv) { - //mTXrowrfftList (mtx_rowrfft_obj, s, argc-2, argv+2); + //mTXrowrfftList (x, s, argc-2, argv+2); int rows = atom_getint (argv++); int columns = atom_getint (argv++); - int columns_re = (columns>>1)+1; + int columns_re = (columns>>1); int size = rows * columns; int in_size = argc-2; - int size2 = columns_re * rows; + int size2 = columns_re * rows + 2; /* +2 since the list also contains matrix row+col */ int fft_count; - t_atom *ptr_re = mtx_rowrfft_obj->list_re; - t_atom *ptr_im = mtx_rowrfft_obj->list_im; - t_float *f_re = mtx_rowrfft_obj->f_re; - t_float *f_im = mtx_rowrfft_obj->f_im; + t_atom *list_re = x->list_re; + t_atom *list_im = x->list_im; + t_float *f_re = x->f_re; + t_float *f_im = x->f_im; // fftsize check if (!size) post("mtx_rowrfft: invalid dimensions"); else if (in_size<size) post("mtx_rowrfft: sparse matrix not yet supported: use \"mtx_check\""); + else if (columns < 4){ + post("mtx_rowrfft: matrix must have at least 4 columns"); + } else if (columns == (1 << ilog2(columns))) { + /* ok, do the FFT! */ + // memory things - if (f_re) { - if (size != mtx_rowrfft_obj->size) { - f_re = (t_float *) resizebytes (f_re, - sizeof (t_float) * mtx_rowrfft_obj->size, - sizeof (t_float) * size); - f_im = (t_float *) resizebytes (f_im, - sizeof (t_float) * mtx_rowrfft_obj->size, - sizeof (t_float) * size); - ptr_re = (t_atom *) resizebytes (ptr_re, - sizeof (t_atom) * (mtx_rowrfft_obj->size2+2), - sizeof (t_atom) * (size2 + 2)); - ptr_im = (t_atom *) resizebytes (ptr_im, - sizeof (t_atom) * (mtx_rowrfft_obj->size2+2), - sizeof (t_atom) * (size2 + 2)); - } - } - else { - f_re = (t_float *) getbytes (sizeof (t_float) * size); - f_im = (t_float *) getbytes (sizeof (t_float) * size); - ptr_re = (t_atom *) getbytes (sizeof (t_atom) * (size2+2)); - ptr_im = (t_atom *) getbytes (sizeof (t_atom) * (size2+2)); - } - mtx_rowrfft_obj->size = size; - mtx_rowrfft_obj->size2 = size2; - mtx_rowrfft_obj->rows = rows; - mtx_rowrfft_obj->columns = columns; - mtx_rowrfft_obj->columns_re = columns_re; - mtx_rowrfft_obj->list_im = ptr_im; - mtx_rowrfft_obj->list_re = ptr_re; - mtx_rowrfft_obj->f_re = f_re; - mtx_rowrfft_obj->f_im = f_im; - + f_re=(t_float*)realloc(f_re, sizeof(t_float)*size); + f_im=(t_float*)realloc(f_im, sizeof(t_float)*size); + list_re=(t_atom*)realloc(list_re, sizeof(t_atom)*size2); + list_im=(t_atom*)realloc(list_im, sizeof(t_atom)*size2); + + x->size = size; + x->size2 = size2; + x->list_im = list_im; + x->list_re = list_re; + x->f_re = f_re; + x->f_im = f_im; + // main part readFloatFromList (size, argv, f_re); fft_count = rows; - ptr_re += 2; - ptr_im += 2; + list_re += 2; + list_im += 2; while (fft_count--){ mayer_realfft (columns, f_re); fftRestoreImag (columns, f_re, f_im); - writeFloatIntoList (columns_re, ptr_re, f_re); - writeFloatIntoList (columns_re, ptr_im, f_im); + writeFloatIntoList (columns_re, list_re, f_re); + writeFloatIntoList (columns_re, list_im, f_im); f_im += columns; f_re += columns; - ptr_re += columns_re; - ptr_im += columns_re; + list_re += columns_re; + list_im += columns_re; } - ptr_re = mtx_rowrfft_obj->list_re; - ptr_im = mtx_rowrfft_obj->list_im; + + list_re = x->list_re; + list_im = x->list_im; - SETSYMBOL(ptr_re, gensym("matrix")); - SETSYMBOL(ptr_im, gensym("matrix")); - SETFLOAT(ptr_re, rows); - SETFLOAT(ptr_im, rows); - SETFLOAT(&ptr_re[1], columns_re); - SETFLOAT(&ptr_im[1], columns_re); - outlet_anything(mtx_rowrfft_obj->list_im_out, gensym("matrix"), - mtx_rowrfft_obj->size2+2, ptr_im); - outlet_anything(mtx_rowrfft_obj->list_re_out, gensym("matrix"), - mtx_rowrfft_obj->size2+2, ptr_re); + SETSYMBOL(list_re, gensym("matrix")); + SETSYMBOL(list_im, gensym("matrix")); + SETFLOAT(list_re, rows); + SETFLOAT(list_im, rows); + SETFLOAT(list_re+1, columns_re); + SETFLOAT(list_im+1, columns_re); + outlet_anything(x->list_im_out, gensym("matrix"), + x->size2, list_im); + outlet_anything(x->list_re_out, gensym("matrix"), + x->size2, list_re); } else post("mtx_rowfft: rowvector size no power of 2!"); + } void mtx_rowrfft_setup (void) diff --git a/src/mtx_rowrifft.c b/src/mtx_rowrifft.c index 9eaba9c..8776246 100644 --- a/src/mtx_rowrifft.c +++ b/src/mtx_rowrifft.c @@ -79,65 +79,54 @@ static void ifftPrepareReal (int n, t_float *re, t_float *im) static void *newMTXRowrifft (t_symbol *s, int argc, t_atom *argv) { - MTXRowrifft *mtx_rowrifft_obj = (MTXRowrifft *) pd_new (mtx_rowrifft_class); - inlet_new(&mtx_rowrifft_obj->x_obj, &mtx_rowrifft_obj->x_obj.ob_pd, gensym("matrix"),gensym("")); - mtx_rowrifft_obj->list_re_out = outlet_new (&mtx_rowrifft_obj->x_obj, gensym("matrix")); - return ((void *) mtx_rowrifft_obj); + MTXRowrifft *x = (MTXRowrifft *) pd_new (mtx_rowrifft_class); + inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("matrix"),gensym("")); + x->list_re_out = outlet_new (&x->x_obj, gensym("matrix")); + return ((void *) x); } -static void mTXrowrifftMatrixCold (MTXRowrifft *mtx_rowrifft_obj, t_symbol *s, +static void mTXrowrifftMatrixCold (MTXRowrifft *x, t_symbol *s, int argc, t_atom *argv) { - //mTXrowrifftList (mtx_rowrifft_obj, s, argc-2, argv+2); + //mTXrowrifftList (x, s, argc-2, argv+2); int rows = atom_getint (argv++); int columns_re = atom_getint (argv++); int in_size = argc-2; - int columns = (columns_re-1)<<1; + int columns = columns_re<<1; int size2 = columns_re * rows; int size = rows * columns; int ifft_count; - t_atom *ptr_re = mtx_rowrifft_obj->list_re; - t_float *f_re = mtx_rowrifft_obj->f_re; - t_float *f_im = mtx_rowrifft_obj->f_im; + t_atom *list_re = x->list_re; + t_float *f_re = x->f_re; + t_float *f_im = x->f_im; // ifftsize check if (!size) post("mtx_rowrifft: invalid dimensions"); else if (in_size < size2) post("mtx_rowrifft: sparse matrix not yet supported: use \"mtx_check\""); + else if (columns<4) + post("mtx_rowrifft: too small matrices"); else if (columns == (1 << ilog2(columns))) { + // memory things - if (f_re) { - if (size != mtx_rowrifft_obj->size) { - f_re = (t_float *) resizebytes (f_re, - sizeof (t_float) * mtx_rowrifft_obj->size, - sizeof (t_float) * size); - f_im = (t_float *) resizebytes (f_im, - sizeof (t_float) * mtx_rowrifft_obj->size, - sizeof (t_float) * size); - ptr_re = (t_atom *) resizebytes (ptr_re, - sizeof (t_atom) * (mtx_rowrifft_obj->size + 2), - sizeof (t_atom) * (size + 2)); - } - } - else { - f_re = (t_float *) getbytes (sizeof (t_float) * size); - f_im = (t_float *) getbytes (sizeof (t_float) * size); - ptr_re = (t_atom *) getbytes (sizeof (t_atom) * (size + 2)); - } - mtx_rowrifft_obj->size = size; - mtx_rowrifft_obj->size2 = size2; - mtx_rowrifft_obj->rows = rows; - mtx_rowrifft_obj->columns = columns; - mtx_rowrifft_obj->columns_re = columns_re; - mtx_rowrifft_obj->list_re = ptr_re; - mtx_rowrifft_obj->f_re = f_re; - mtx_rowrifft_obj->f_im = f_im; + f_re=(t_float*)realloc(f_re, sizeof(t_float)*size); + f_im=(t_float*)realloc(f_im, sizeof(t_float)*size); + list_re=(t_atom*)realloc(list_re, sizeof(t_atom)*(size+2)); + + x->size = size; + x->size2 = size2; + x->rows = rows; + x->columns = columns; + x->columns_re = columns_re; + x->list_re = list_re; + x->f_re = f_re; + x->f_im = f_im; // main part: reading imaginary part ifft_count = rows; - mtx_rowrifft_obj->renorm_fac = 1.0f / columns; + x->renorm_fac = 1.0f / columns; while (ifft_count--) { readFloatFromList (columns_re, argv, f_im); argv += columns_re; @@ -149,34 +138,33 @@ static void mTXrowrifftMatrixCold (MTXRowrifft *mtx_rowrifft_obj, t_symbol *s, post("mtx_rowrifft: rowvector size no power of 2!"); } -static void mTXrowrifftMatrixHot (MTXRowrifft *mtx_rowrifft_obj, t_symbol *s, +static void mTXrowrifftMatrixHot (MTXRowrifft *x, t_symbol *s, int argc, t_atom *argv) { - //mTXrowrifftList (mtx_rowrifft_obj, s, argc-2, argv+2); + //mTXrowrifftList (x, s, argc-2, argv+2); int rows = atom_getint (argv++); int columns_re = atom_getint (argv++); - int columns = mtx_rowrifft_obj->columns; - int size = mtx_rowrifft_obj->size; + int columns = x->columns; + int size = x->size; int in_size = argc-2; - int size2 = mtx_rowrifft_obj->size2; + int size2 = x->size2; int ifft_count; - t_atom *ptr_re = mtx_rowrifft_obj->list_re; - t_float *f_re = mtx_rowrifft_obj->f_re; - t_float *f_im = mtx_rowrifft_obj->f_im; - t_float renorm_fac; + t_atom *ptr_re = x->list_re; + t_float *f_re = x->f_re; + t_float *f_im = x->f_im; + t_float renorm_fac = x->renorm_fac; // ifftsize check - if ((rows != mtx_rowrifft_obj->rows) || - (columns_re != mtx_rowrifft_obj->columns_re)) + if ((rows != x->rows) || + (columns_re != x->columns_re)) post("mtx_rowrifft: matrix dimensions do not match"); else if (in_size<size2) post("mtx_rowrifft: sparse matrix not yet supported: use \"mtx_check\""); - else if (!mtx_rowrifft_obj->size2) + else if (!x->size2) post("mtx_rowrifft: invalid right side matrix"); else { // main part ifft_count = rows; ptr_re += 2; - renorm_fac = mtx_rowrifft_obj->renorm_fac; while (ifft_count--){ readFloatFromList (columns_re, argv, f_re); ifftPrepareReal (columns, f_re, f_im); @@ -187,36 +175,32 @@ static void mTXrowrifftMatrixHot (MTXRowrifft *mtx_rowrifft_obj, t_symbol *s, ptr_re += columns; argv += columns_re; } - ptr_re = mtx_rowrifft_obj->list_re; - f_re = mtx_rowrifft_obj->f_re; - size2 = mtx_rowrifft_obj->size2; + ptr_re = x->list_re; + f_re = x->f_re; + size2 = x->size2; SETSYMBOL(ptr_re, gensym("matrix")); SETFLOAT(ptr_re, rows); - SETFLOAT(&ptr_re[1], mtx_rowrifft_obj->columns); + SETFLOAT(&ptr_re[1], x->columns); writeFloatIntoList (size, ptr_re+2, f_re); - outlet_anything(mtx_rowrifft_obj->list_re_out, gensym("matrix"), size+2, ptr_re); + outlet_anything(x->list_re_out, gensym("matrix"), size+2, ptr_re); } } -static void mTXrowrifftBang (MTXRowrifft *mtx_rowrifft_obj) +static void mTXrowrifftBang (MTXRowrifft *x) { - if (mtx_rowrifft_obj->list_re) - outlet_anything(mtx_rowrifft_obj->list_re_out, gensym("matrix"), - mtx_rowrifft_obj->size+2, mtx_rowrifft_obj->list_re); + if (x->list_re) + outlet_anything(x->list_re_out, gensym("matrix"), + x->size+2, x->list_re); } -static void deleteMTXRowrifft (MTXRowrifft *mtx_rowrfft_obj) +static void deleteMTXRowrifft (MTXRowrifft *x) { - if (mtx_rowrfft_obj->f_re) - freebytes (mtx_rowrfft_obj->f_re, sizeof(t_float)*mtx_rowrfft_obj->size); - if (mtx_rowrfft_obj->f_im) - freebytes (mtx_rowrfft_obj->f_im, sizeof(t_float)*mtx_rowrfft_obj->size); - if (mtx_rowrfft_obj->list_re) - freebytes (mtx_rowrfft_obj->list_re, sizeof(t_atom)*(mtx_rowrfft_obj->size2+2)); - if (mtx_rowrfft_obj->list_im) - freebytes (mtx_rowrfft_obj->list_im, sizeof(t_atom)*(mtx_rowrfft_obj->size2+2)); + free(x->f_re); + free(x->f_im); + free(x->list_re); + free(x->list_im); } static void mtx_rowrifft_setup (void) |