- Timestamp:
- Dec 4, 2009, 1:41:59 AM (15 years ago)
- Branches:
- feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
- Children:
- fc61225
- Parents:
- d207300
- Location:
- src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/mathutils.c
rd207300 r8e5c051 46 46 new_aubio_window (char_t * window_type, uint_t size) 47 47 { 48 // create fvec of size x 1 channel 49 fvec_t * win = new_fvec( size, 1); 50 smpl_t * w = win->data[0]; 48 fvec_t * win = new_fvec (size); 49 smpl_t * w = win->data; 51 50 uint_t i; 52 51 aubio_window_type wintype; … … 134 133 fvec_mean (fvec_t * s) 135 134 { 136 uint_t i,j;135 uint_t j; 137 136 smpl_t tmp = 0.0; 138 for ( i = 0; i < s->channels; i++)139 for (j = 0; j < s->length; j++)140 tmp += s->data[i][j];137 for (j = 0; j < s->length; j++) { 138 tmp += s->data[j]; 139 } 141 140 return tmp / (smpl_t) (s->length); 142 141 } 143 142 144 143 smpl_t 145 fvec_ mean_channel (fvec_t * s, uint_t i)144 fvec_sum (fvec_t * s) 146 145 { 147 146 uint_t j; 148 147 smpl_t tmp = 0.0; 149 for (j = 0; j < s->length; j++) 150 tmp += s->data[i][j]; 151 return tmp / (smpl_t) (s->length); 152 } 153 154 smpl_t 155 fvec_sum (fvec_t * s) 156 { 157 uint_t i, j; 148 for (j = 0; j < s->length; j++) { 149 tmp += s->data[j]; 150 } 151 return tmp; 152 } 153 154 smpl_t 155 fvec_max (fvec_t * s) 156 { 157 uint_t j; 158 158 smpl_t tmp = 0.0; 159 for (i = 0; i < s->channels; i++) { 160 for (j = 0; j < s->length; j++) { 161 tmp += s->data[i][j]; 162 } 159 for (j = 0; j < s->length; j++) { 160 tmp = (tmp > s->data[j]) ? tmp : s->data[j]; 163 161 } 164 162 return tmp; … … 166 164 167 165 smpl_t 168 fvec_max (fvec_t * s)169 {170 uint_t i, j;171 smpl_t tmp = 0.0;172 for (i = 0; i < s->channels; i++) {173 for (j = 0; j < s->length; j++) {174 tmp = (tmp > s->data[i][j]) ? tmp : s->data[i][j];175 }176 }177 return tmp;178 }179 180 smpl_t181 166 fvec_min (fvec_t * s) 182 167 { 183 uint_t i, j; 184 smpl_t tmp = s->data[0][0]; 185 for (i = 0; i < s->channels; i++) { 186 for (j = 0; j < s->length; j++) { 187 tmp = (tmp < s->data[i][j]) ? tmp : s->data[i][j]; 188 } 168 uint_t j; 169 smpl_t tmp = s->data[0]; 170 for (j = 0; j < s->length; j++) { 171 tmp = (tmp < s->data[j]) ? tmp : s->data[j]; 189 172 } 190 173 return tmp; … … 194 177 fvec_min_elem (fvec_t * s) 195 178 { 196 uint_t i, j, pos = 0.; 197 smpl_t tmp = s->data[0][0]; 198 for (i = 0; i < s->channels; i++) { 199 for (j = 0; j < s->length; j++) { 200 pos = (tmp < s->data[i][j]) ? pos : j; 201 tmp = (tmp < s->data[i][j]) ? tmp : s->data[i][j]; 202 } 179 uint_t j, pos = 0.; 180 smpl_t tmp = s->data[0]; 181 for (j = 0; j < s->length; j++) { 182 pos = (tmp < s->data[j]) ? pos : j; 183 tmp = (tmp < s->data[j]) ? tmp : s->data[j]; 203 184 } 204 185 return pos; … … 208 189 fvec_max_elem (fvec_t * s) 209 190 { 210 uint_t i,j, pos = 0;191 uint_t j, pos = 0; 211 192 smpl_t tmp = 0.0; 212 for (i = 0; i < s->channels; i++) { 213 for (j = 0; j < s->length; j++) { 214 pos = (tmp > s->data[i][j]) ? pos : j; 215 tmp = (tmp > s->data[i][j]) ? tmp : s->data[i][j]; 216 } 193 for (j = 0; j < s->length; j++) { 194 pos = (tmp > s->data[j]) ? pos : j; 195 tmp = (tmp > s->data[j]) ? tmp : s->data[j]; 217 196 } 218 197 return pos; … … 222 201 fvec_shift (fvec_t * s) 223 202 { 224 uint_t i, j; 225 for (i = 0; i < s->channels; i++) { 226 for (j = 0; j < s->length / 2; j++) { 227 ELEM_SWAP (s->data[i][j], s->data[i][j + s->length / 2]); 228 } 203 uint_t j; 204 for (j = 0; j < s->length / 2; j++) { 205 ELEM_SWAP (s->data[j], s->data[j + s->length / 2]); 229 206 } 230 207 } … … 234 211 { 235 212 smpl_t energy = 0.; 236 uint_t i, j; 237 for (i = 0; i < f->channels; i++) { 238 for (j = 0; j < f->length; j++) { 239 energy += SQR (f->data[i][j]); 240 } 213 uint_t j; 214 for (j = 0; j < f->length; j++) { 215 energy += SQR (f->data[j]); 241 216 } 242 217 return energy; … … 247 222 { 248 223 smpl_t hfc = 0.; 249 uint_t i, j; 250 for (i = 0; i < v->channels; i++) { 251 for (j = 0; j < v->length; j++) { 252 hfc += (i + 1) * v->data[i][j]; 253 } 224 uint_t j; 225 for (j = 0; j < v->length; j++) { 226 hfc += (j + 1) * v->data[j]; 254 227 } 255 228 return hfc; … … 266 239 fvec_alpha_norm (fvec_t * o, smpl_t alpha) 267 240 { 268 uint_t i,j;241 uint_t j; 269 242 smpl_t tmp = 0.; 270 for (i = 0; i < o->channels; i++) { 271 for (j = 0; j < o->length; j++) { 272 tmp += POW (ABS (o->data[i][j]), alpha); 273 } 243 for (j = 0; j < o->length; j++) { 244 tmp += POW (ABS (o->data[j]), alpha); 274 245 } 275 246 return POW (tmp / o->length, 1. / alpha); … … 279 250 fvec_alpha_normalise (fvec_t * o, smpl_t alpha) 280 251 { 281 uint_t i,j;252 uint_t j; 282 253 smpl_t norm = fvec_alpha_norm (o, alpha); 283 for (i = 0; i < o->channels; i++) { 284 for (j = 0; j < o->length; j++) { 285 o->data[i][j] /= norm; 286 } 254 for (j = 0; j < o->length; j++) { 255 o->data[j] /= norm; 287 256 } 288 257 } … … 291 260 fvec_add (fvec_t * o, smpl_t val) 292 261 { 293 uint_t i, j; 294 for (i = 0; i < o->channels; i++) { 295 for (j = 0; j < o->length; j++) { 296 o->data[i][j] += val; 297 } 262 uint_t j; 263 for (j = 0; j < o->length; j++) { 264 o->data[j] += val; 298 265 } 299 266 } 300 267 301 268 void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp, 302 uint_t post, uint_t pre , uint_t channel) {303 uint_t length = vec->length, i=channel,j;269 uint_t post, uint_t pre) { 270 uint_t length = vec->length, j; 304 271 for (j=0;j<length;j++) { 305 vec->data[ i][j] -= fvec_moving_thres(vec, tmp, post, pre, j, i);272 vec->data[j] -= fvec_moving_thres(vec, tmp, post, pre, j); 306 273 } 307 274 } … … 309 276 smpl_t 310 277 fvec_moving_thres (fvec_t * vec, fvec_t * tmpvec, 311 uint_t post, uint_t pre, uint_t pos , uint_t channel)312 { 313 uint_t i = channel,k;314 smpl_t *medar = (smpl_t *) tmpvec->data [i];278 uint_t post, uint_t pre, uint_t pos) 279 { 280 uint_t k; 281 smpl_t *medar = (smpl_t *) tmpvec->data; 315 282 uint_t win_length = post + pre + 1; 316 283 uint_t length = vec->length; … … 320 287 medar[k] = 0.; /* 0-padding at the beginning */ 321 288 for (k = post + 1 - pos; k < win_length; k++) 322 medar[k] = vec->data[ 0][k + pos - post];289 medar[k] = vec->data[k + pos - post]; 323 290 /* the buffer is fully defined */ 324 291 } else if (pos + pre < length) { 325 292 for (k = 0; k < win_length; k++) 326 medar[k] = vec->data[ 0][k + pos - post];293 medar[k] = vec->data[k + pos - post]; 327 294 /* pre part of the buffer does not exist */ 328 295 } else { 329 296 for (k = 0; k < length - pos + post; k++) 330 medar[k] = vec->data[ 0][k + pos - post];297 medar[k] = vec->data[k + pos - post]; 331 298 for (k = length - pos + post; k < win_length; k++) 332 299 medar[k] = 0.; /* 0-padding at the end */ 333 300 } 334 return fvec_median _channel (tmpvec, i);335 } 336 337 smpl_t fvec_median _channel (fvec_t * input, uint_t channel) {301 return fvec_median (tmpvec); 302 } 303 304 smpl_t fvec_median (fvec_t * input) { 338 305 uint_t n = input->length; 339 smpl_t * arr = (smpl_t *) input->data [channel];306 smpl_t * arr = (smpl_t *) input->data; 340 307 uint_t low, high ; 341 308 uint_t median; … … 386 353 } 387 354 388 smpl_t fvec_quadint (fvec_t * x, uint_t pos , uint_t i) {355 smpl_t fvec_quadint (fvec_t * x, uint_t pos) { 389 356 smpl_t s0, s1, s2; 390 357 uint_t x0 = (pos < 1) ? pos : pos - 1; 391 358 uint_t x2 = (pos + 1 < x->length) ? pos + 1 : pos; 392 if (x0 == pos) return (x->data[ i][pos] <= x->data[i][x2]) ? pos : x2;393 if (x2 == pos) return (x->data[ i][pos] <= x->data[i][x0]) ? pos : x0;394 s0 = x->data[ i][x0];395 s1 = x->data[ i][pos];396 s2 = x->data[ i][x2];359 if (x0 == pos) return (x->data[pos] <= x->data[x2]) ? pos : x2; 360 if (x2 == pos) return (x->data[pos] <= x->data[x0]) ? pos : x0; 361 s0 = x->data[x0]; 362 s1 = x->data[pos]; 363 s2 = x->data[x2]; 397 364 return pos + 0.5 * (s2 - s0 ) / (s2 - 2.* s1 + s0); 398 365 } 399 366 400 367 uint_t fvec_peakpick(fvec_t * onset, uint_t pos) { 401 uint_t i=0, tmp=0; 402 /*for (i=0;i<onset->channels;i++)*/ 403 tmp = (onset->data[i][pos] > onset->data[i][pos-1] 404 && onset->data[i][pos] > onset->data[i][pos+1] 405 && onset->data[i][pos] > 0.); 368 uint_t tmp=0; 369 tmp = (onset->data[pos] > onset->data[pos-1] 370 && onset->data[pos] > onset->data[pos+1] 371 && onset->data[pos] > 0.); 406 372 return tmp; 407 373 } … … 512 478 aubio_zero_crossing_rate (fvec_t * input) 513 479 { 514 uint_t i = 0,j;480 uint_t j; 515 481 uint_t zcr = 0; 516 482 for (j = 1; j < input->length; j++) { 517 483 // previous was strictly negative 518 if (input->data[ i][j - 1] < 0.) {484 if (input->data[j - 1] < 0.) { 519 485 // current is positive or null 520 if (input->data[ i][j] >= 0.) {486 if (input->data[j] >= 0.) { 521 487 zcr += 1; 522 488 } … … 524 490 } else { 525 491 // current is strictly negative 526 if (input->data[ i][j] < 0.) {492 if (input->data[j] < 0.) { 527 493 zcr += 1; 528 494 } … … 535 501 aubio_autocorr (fvec_t * input, fvec_t * output) 536 502 { 537 uint_t i, j, k,length = input->length;503 uint_t i, j, length = input->length; 538 504 smpl_t *data, *acf; 539 505 smpl_t tmp = 0; 540 for (k = 0; k < input->channels; k++) { 541 data = input->data[k]; 542 acf = output->data[k]; 543 for (i = 0; i < length; i++) { 544 tmp = 0.; 545 for (j = i; j < length; j++) { 546 tmp += data[j - i] * data[j]; 547 } 548 acf[i] = tmp / (smpl_t) (length - i); 506 data = input->data; 507 acf = output->data; 508 for (i = 0; i < length; i++) { 509 tmp = 0.; 510 for (j = i; j < length; j++) { 511 tmp += data[j - i] * data[j]; 549 512 } 513 acf[i] = tmp / (smpl_t) (length - i); 550 514 } 551 515 } -
src/mathutils.h
rd207300 r8e5c051 41 41 */ 42 42 smpl_t fvec_mean (fvec_t * s); 43 44 /** compute the mean of a vector channel45 46 \param s vector to compute mean from47 \param i channel to compute mean from48 49 \return the mean of v50 51 */52 smpl_t fvec_mean_channel (fvec_t * s, uint_t i);53 43 54 44 /** find the max of a vector … … 205 195 */ 206 196 smpl_t fvec_moving_thres (fvec_t * v, fvec_t * tmp, uint_t post, uint_t pre, 207 uint_t pos , uint_t channel);197 uint_t pos); 208 198 209 199 /** apply adaptive threshold to a vector … … 218 208 219 209 */ 220 void fvec_adapt_thres (fvec_t * v, fvec_t * tmp, uint_t post, uint_t pre, 221 uint_t channel); 210 void fvec_adapt_thres (fvec_t * v, fvec_t * tmp, uint_t post, uint_t pre); 222 211 223 212 /** returns the median of a vector … … 232 221 233 222 \param v vector to get median from 234 \param channel channel to get median from235 223 236 224 \return the median of v 237 225 238 226 */ 239 smpl_t fvec_median _channel (fvec_t * v, uint_t channel);227 smpl_t fvec_median (fvec_t * v); 240 228 241 229 /** finds exact peak index by quadratic interpolation*/ 242 smpl_t fvec_quadint (fvec_t * x, uint_t pos , uint_t channel);230 smpl_t fvec_quadint (fvec_t * x, uint_t pos); 243 231 244 232 /** Quadratic interpolation using Lagrange polynomial.
Note: See TracChangeset
for help on using the changeset viewer.