Mercurial > hg > libxtract
comparison src/vector.c @ 113:72a9a393d5bd
- Fixed bugs in xtract_flatness(), or at least added necessary
documentation and error checking to avoid problems
- Added xtract_is_denormal() helper function and XTRACT_DENORMAL_FOUND
return code
- Replaced all instances of log, sqrt, exp etc. with respective
floating point counterparts (logf etc.)
- Added check for architecture endianness to configure script
- Bug fix to PD example, now no longer crashes if no arguments are
given
- Minor documentation updates
author | Jamie Bullock <jamie@postlude.co.uk> |
---|---|
date | Fri, 15 Feb 2008 12:43:13 +0000 |
parents | a76501dc5307 |
children | f5040ed4e555 |
comparison
equal
deleted
inserted
replaced
112:a76501dc5307 | 113:72a9a393d5bd |
---|---|
35 else | 35 else |
36 return (float)((int)f); | 36 return (float)((int)f); |
37 } | 37 } |
38 #endif | 38 #endif |
39 | 39 |
40 #ifndef powf | |
41 #define powf pow | |
42 #endif | |
43 | |
44 #ifndef expf | |
45 #define expf exp | |
46 #endif | |
47 | |
48 #ifndef sqrtf | |
49 #define sqrtf sqrt | |
50 #endif | |
51 | |
52 #ifndef fabsf | |
53 #define fabsf fabs | |
54 #endif | |
55 | |
40 #ifdef XTRACT_FFT | 56 #ifdef XTRACT_FFT |
41 | 57 |
42 #include <fftw3.h> | 58 #include <fftw3.h> |
43 #include "xtract_globals_private.h" | 59 #include "xtract_globals_private.h" |
44 #include "xtract_macros_private.h" | 60 #include "xtract_macros_private.h" |
45 | 61 |
46 int xtract_spectrum(const float *data, const int N, const void *argv, float *result){ | 62 int xtract_spectrum(const float *data, const int N, const void *argv, float *result){ |
47 | 63 |
48 float *input, *rfft, q, temp, max; | 64 float *input, *rfft, q, temp, max, NxN; |
49 size_t bytes; | 65 size_t bytes; |
50 int n, | 66 int n, |
51 m, | 67 m, |
52 NxN, | |
53 M, | 68 M, |
54 vector, | 69 vector, |
55 withDC, | 70 withDC, |
56 argc, | 71 argc, |
57 normalise; | 72 normalise; |
87 | 102 |
88 case XTRACT_LOG_MAGNITUDE_SPECTRUM: | 103 case XTRACT_LOG_MAGNITUDE_SPECTRUM: |
89 for(n = 1; n < M; n++){ | 104 for(n = 1; n < M; n++){ |
90 if ((temp = XTRACT_SQ(rfft[n]) + | 105 if ((temp = XTRACT_SQ(rfft[n]) + |
91 XTRACT_SQ(rfft[N - n])) > XTRACT_LOG_LIMIT) | 106 XTRACT_SQ(rfft[N - n])) > XTRACT_LOG_LIMIT) |
92 temp = log(sqrt(temp) / N); | 107 temp = logf(sqrtf(temp) / (float)N); |
93 else | 108 else |
94 temp = XTRACT_LOG_LIMIT_DB; | 109 temp = XTRACT_LOG_LIMIT_DB; |
95 | 110 |
96 if(withDC){ | 111 if(withDC){ |
97 m = n; | 112 m = n; |
128 | 143 |
129 case XTRACT_LOG_POWER_SPECTRUM: | 144 case XTRACT_LOG_POWER_SPECTRUM: |
130 for(n = 1; n < M; n++){ | 145 for(n = 1; n < M; n++){ |
131 if ((temp = XTRACT_SQ(rfft[n]) + XTRACT_SQ(rfft[N - n])) > | 146 if ((temp = XTRACT_SQ(rfft[n]) + XTRACT_SQ(rfft[N - n])) > |
132 XTRACT_LOG_LIMIT) | 147 XTRACT_LOG_LIMIT) |
133 temp = log(temp / NxN); | 148 temp = logf(temp / NxN); |
134 else | 149 else |
135 temp = XTRACT_LOG_LIMIT_DB; | 150 temp = XTRACT_LOG_LIMIT_DB; |
136 | 151 |
137 if(withDC){ | 152 if(withDC){ |
138 m = n; | 153 m = n; |
159 else{ | 174 else{ |
160 m = n - 1; | 175 m = n - 1; |
161 result[M + m] = n * q; | 176 result[M + m] = n * q; |
162 } | 177 } |
163 | 178 |
164 result[m] = sqrt(XTRACT_SQ(rfft[n]) + | 179 result[m] = sqrtf(XTRACT_SQ(rfft[n]) + |
165 XTRACT_SQ(rfft[N - n])) / N; | 180 XTRACT_SQ(rfft[N - n])) / (float)N; |
166 max = result[m] > max ? result[m] : max; | 181 max = result[m] > max ? result[m] : max; |
167 } | 182 } |
168 break; | 183 break; |
169 } | 184 } |
170 | 185 |
252 for(filter = 0; filter < f->n_filters; filter++){ | 267 for(filter = 0; filter < f->n_filters; filter++){ |
253 result[filter] = 0.f; | 268 result[filter] = 0.f; |
254 for(n = 0; n < N; n++){ | 269 for(n = 0; n < N; n++){ |
255 result[filter] += data[n] * f->filters[filter][n]; | 270 result[filter] += data[n] * f->filters[filter][n]; |
256 } | 271 } |
257 result[filter] = log(result[filter] < XTRACT_LOG_LIMIT ? XTRACT_LOG_LIMIT : result[filter]); | 272 result[filter] = logf(result[filter] < XTRACT_LOG_LIMIT ? XTRACT_LOG_LIMIT : result[filter]); |
258 } | 273 } |
259 | 274 |
260 xtract_dct(result, f->n_filters, NULL, result); | 275 xtract_dct(result, f->n_filters, NULL, result); |
261 | 276 |
262 return XTRACT_SUCCESS; | 277 return XTRACT_SUCCESS; |
332 int n = N, i; | 347 int n = N, i; |
333 | 348 |
334 float md, temp; | 349 float md, temp; |
335 | 350 |
336 while(n--){ | 351 while(n--){ |
337 md = 0; | 352 md = 0.f; |
338 for(i = 0; i < N - n; i++){ | 353 for(i = 0; i < N - n; i++){ |
339 temp = data[i] - data[i + n]; | 354 temp = data[i] - data[i + n]; |
340 temp = (temp < 0 ? -temp : temp); | 355 temp = (temp < 0 ? -temp : temp); |
341 md += temp; | 356 md += temp; |
342 } | 357 } |
343 result[n] = md / N; | 358 result[n] = md / (float)N; |
344 } | 359 } |
345 | 360 |
346 return XTRACT_SUCCESS; | 361 return XTRACT_SUCCESS; |
347 } | 362 } |
348 | 363 |
351 int n = N, i; | 366 int n = N, i; |
352 | 367 |
353 float sd; | 368 float sd; |
354 | 369 |
355 while(n--){ | 370 while(n--){ |
356 sd = 0; | 371 sd = 0.f; |
357 for(i = 0; i < N - n; i++){ | 372 for(i = 0; i < N - n; i++){ |
358 /*sd = 1;*/ | 373 /*sd = 1;*/ |
359 sd += XTRACT_SQ(data[i] - data[i + n]); | 374 sd += XTRACT_SQ(data[i] - data[i + n]); |
360 } | 375 } |
361 result[n] = sd / N; | 376 result[n] = sd / (float)N; |
362 } | 377 } |
363 | 378 |
364 return XTRACT_SUCCESS; | 379 return XTRACT_SUCCESS; |
365 } | 380 } |
366 | 381 |
486 k = N; /* The length of *data */ | 501 k = N; /* The length of *data */ |
487 L = N - 1; /* The number of LPC coefficients */ | 502 L = N - 1; /* The number of LPC coefficients */ |
488 M = L * 2; /* The length of *result */ | 503 M = L * 2; /* The length of *result */ |
489 ref = result; | 504 ref = result; |
490 lpc = result+L; | 505 lpc = result+L; |
491 /* | 506 |
492 if(error == 0.0){ | 507 if(error == 0.0){ |
493 for(i = 0; i < M; i++) | 508 memset(result, 0, M * sizeof(float)); |
494 result[i] = 0.f; | |
495 return XTRACT_NO_RESULT; | 509 return XTRACT_NO_RESULT; |
496 } | 510 } |
497 */ | 511 |
498 memset(result, 0, M * sizeof(float)); | 512 memset(result, 0, M * sizeof(float)); |
499 | 513 |
500 for (i = 0; i < L; i++) { | 514 for (i = 0; i < L; i++) { |
501 | 515 |
502 /* Sum up this iteration's reflection coefficient. */ | 516 /* Sum up this iteration's reflection coefficient. */ |