From 95e71c35ba7c54f9c6cd248714195cefe17c77cb Mon Sep 17 00:00:00 2001 From: Franz Zotter Date: Tue, 22 Jul 2008 17:23:43 +0000 Subject: corrected [mtx_rfft] and [mtx_rifft] memory bugs. svn path=/trunk/externals/iem/iemmatrix/; revision=10208 --- doc/mtx_rfft-help.pd | 9 ++++++--- doc/mtx_rifft-help.pd | 11 +++++++---- src/mtx_rfft.c | 9 ++++++--- src/mtx_rifft.c | 41 +++++++++++++++++++++++++---------------- 4 files changed, 44 insertions(+), 26 deletions(-) diff --git a/doc/mtx_rfft-help.pd b/doc/mtx_rfft-help.pd index 6548a01..5943dc0 100644 --- a/doc/mtx_rfft-help.pd +++ b/doc/mtx_rfft-help.pd @@ -13,8 +13,8 @@ has to have 2^k columns \; a N/2+1 columns matrix is the result \; #X obj 11 169 mtx_ones; #X obj 11 213 mtx_- 1; #X obj 49 255 mtx_* 0; -#X floatatom 172 234 5 0 0 0 - - -; -#X text 212 232 <-- scroll here to select frequency; +#X floatatom 140 210 5 0 0 0 - - -; +#X text 184 208 <-- scroll here to select frequency; #X obj 172 255 / 8; #X obj 49 306 t a a; #X obj 92 306 mtx_print original; @@ -31,13 +31,14 @@ has to have 2^k columns \; a N/2+1 columns matrix is the result \; #X obj 261 438 mtx_rifft; #X obj 121 353 mtx_+ 0.5; #X obj 49 373 mtx_+ 0.5; +#X obj 140 231 t b f; #X connect 4 0 8 0; #X connect 7 0 10 0; #X connect 8 0 9 0; #X connect 9 0 7 0; #X connect 10 0 11 0; #X connect 11 0 20 0; -#X connect 12 0 14 0; +#X connect 12 0 30 0; #X connect 14 0 21 0; #X connect 15 0 26 0; #X connect 15 1 16 0; @@ -49,3 +50,5 @@ has to have 2^k columns \; a N/2+1 columns matrix is the result \; #X connect 26 1 28 0; #X connect 28 0 23 0; #X connect 29 0 24 0; +#X connect 30 0 4 0; +#X connect 30 1 14 0; diff --git a/doc/mtx_rifft-help.pd b/doc/mtx_rifft-help.pd index be8f908..afad9be 100644 --- a/doc/mtx_rifft-help.pd +++ b/doc/mtx_rifft-help.pd @@ -8,8 +8,8 @@ #X obj 11 169 mtx_ones; #X obj 11 213 mtx_- 1; #X obj 49 255 mtx_* 0; -#X floatatom 177 234 5 0 0 0 - - -; -#X obj 177 255 / 8; +#X floatatom 155 202 5 0 0 0 - - -; +#X obj 187 255 / 8; #X obj 49 278 t a a; #X text 266 443 see also:; #X text 46 34 real ifft in each matrix row; @@ -19,7 +19,7 @@ the result \;; #X msg 11 147 1 9; #X obj 49 307 mtx_cos; #X obj 104 307 mtx_sin; -#X text 217 232 <-- scroll here to select delay; +#X text 199 203 <-- scroll here to select delay; #X obj 49 411 mtx_print rifft; #X obj 49 389 mtx_int; #X obj 103 255 * -3.14159; @@ -27,12 +27,13 @@ the result \;; #X obj 262 463 mtx_rfft; #X obj 49 336 mtx_rifft; #X obj 49 362 mtx_+ 0.5; +#X obj 155 223 t b f; #X connect 4 0 15 0; #X connect 5 0 7 0; #X connect 6 0 5 0; #X connect 7 0 8 0; #X connect 8 0 11 0; -#X connect 9 0 10 0; +#X connect 9 0 26 0; #X connect 10 0 21 0; #X connect 11 0 16 0; #X connect 11 1 17 0; @@ -43,3 +44,5 @@ the result \;; #X connect 21 0 8 1; #X connect 24 0 25 0; #X connect 25 0 20 0; +#X connect 26 0 4 0; +#X connect 26 1 10 0; diff --git a/src/mtx_rfft.c b/src/mtx_rfft.c index d0890c3..2d9d18f 100644 --- a/src/mtx_rfft.c +++ b/src/mtx_rfft.c @@ -137,10 +137,13 @@ static void readFloatFromList (int n, t_atom *l, t_float *f) } #ifdef HAVE_FFTW3_H -static void writeFFTWComplexPartIntoList (int n, t_atom *l, fftw_complex *f, enum ComplexPart p) +static void writeFFTWComplexPartIntoList (int n, t_atom *l, fftw_complex *c, enum ComplexPart p) { - for (;n--;f++, l++) - SETFLOAT (l, ((t_float)*f[p])); + t_float f; + while (n--) { + f=(t_float)c[n][p]; + SETFLOAT (l+n, f); + } } static void readDoubleFromList (int n, t_atom *l, double *f) { diff --git a/src/mtx_rifft.c b/src/mtx_rifft.c index 94ba86c..36f498d 100644 --- a/src/mtx_rifft.c +++ b/src/mtx_rifft.c @@ -33,13 +33,12 @@ typedef struct _MTXRifft_ int columns_re; int size; int size2; + t_float renorm_fac; #ifdef HAVE_FFTW3_H fftw_plan *fftplan; fftw_complex *f_in; double *f_out; #else - t_float renorm_fac; - t_float *f_re; t_float *f_im; #endif @@ -94,14 +93,24 @@ static void ifftPrepareReal (int n, t_float *re, t_float *im) #ifdef HAVE_FFTW3_H static void readFFTWComplexPartFromList (int n, t_atom *l, fftw_complex *f, enum ComplexPart p) { - for (;n--;f++, l++) - *f[p] = (double) atom_getfloat (l); + for (;n--;) + f[n][p] = (double) atom_getfloat (l+n); } -static void writeDoubleIntoList (int n, t_atom *l, double *f) +static void writeDoubleIntoList (int n, t_atom *l, double *d) { - while (n--) - SETFLOAT (l++,((t_float)(*f++))); + t_float f; + while (n--) { + f=(t_float) d[n]; + SETFLOAT (l+n,f); + } } +static void multiplyDoubleVector (int n, double *f, t_float fac) +{ + double fd=(double)fac; + while (n--) + *f++ *= (double)fd; +} + #endif static void *newMTXRifft (t_symbol *s, int argc, t_atom *argv) @@ -152,6 +161,8 @@ static void mTXRifftMatrixCold (MTXRifft *x, t_symbol *s, x->fftplan=(fftw_plan*)realloc(x->fftplan,sizeof(fftw_plan)*rows); f_in=(fftw_complex*)realloc(f_in,sizeof(fftw_complex)*size2); f_out=(double*)realloc(f_out,sizeof(double)*size); + list_re=(t_atom*)realloc(list_re, sizeof(t_atom)*(size+2)); + x->list_re = list_re; x->f_out = f_out; x->f_in = f_in; for (ifft_count=0;ifft_countf_re = f_re; x->f_im = f_im; + list_re=(t_atom*)realloc(list_re, sizeof(t_atom)*(size+2)); + x->list_re = list_re; #endif - 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; /* main part: reading imaginary part */ ifft_count = rows; -#ifndef HAVE_FFTW3_H x->renorm_fac = 1.0f / columns; -#endif - while (ifft_count--) { + for (ifft_count=0;ifft_countf_in; - double *f_out = x->f_out; #else t_float *f_re = x->f_re; t_float *f_im = x->f_im; - t_float renorm_fac = x->renorm_fac; #endif + t_float renorm_fac = x->renorm_fac; /* ifftsize check */ if ((rows != x->rows) || @@ -235,7 +243,6 @@ static void mTXRifftMatrixHot (MTXRifft *x, t_symbol *s, readFloatFromList (columns_re, argv, f_re); ifftPrepareReal (columns, f_re, f_im); mayer_realifft (columns, f_re); - multiplyVector (columns, f_re, renorm_fac); f_im += columns; f_re += columns; #endif @@ -249,8 +256,10 @@ static void mTXRifftMatrixHot (MTXRifft *x, t_symbol *s, SETFLOAT(x->list_re, rows); SETFLOAT(x->list_re+1, x->columns); #ifdef HAVE_FFTW3_H - writeDoubleIntoList (size, x->list_re+2, f_out); + multiplyDoubleVector (size, x->f_out, renorm_fac); + writeDoubleIntoList (size, x->list_re+2, x->f_out); #else + multiplyVector (size, f_re, renorm_fac); writeFloatIntoList (size, x->list_re+2, f_re); #endif outlet_anything(x->list_re_out, gensym("matrix"), size+2, x->list_re); -- cgit v1.2.1