annotate src/scalar.c @ 49:4e931b464278

Added xtract_sharpness()
author Jamie Bullock <jamie@postlude.co.uk>
date Thu, 21 Dec 2006 13:23:12 +0000
parents 8703c202a247
children 45c585bb7996
rev   line source
jamie@1 1 /* libxtract feature extraction library
jamie@1 2 *
jamie@1 3 * Copyright (C) 2006 Jamie Bullock
jamie@1 4 *
jamie@1 5 * This program is free software; you can redistribute it and/or modify
jamie@1 6 * it under the terms of the GNU General Public License as published by
jamie@1 7 * the Free Software Foundation; either version 2 of the License, or
jamie@1 8 * (at your option) any later version.
jamie@1 9 *
jamie@1 10 * This program is distributed in the hope that it will be useful,
jamie@1 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jamie@1 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
jamie@1 13 * GNU General Public License for more details.
jamie@1 14 *
jamie@1 15 * You should have received a copy of the GNU General Public License
jamie@1 16 * along with this program; if not, write to the Free Software
jamie@1 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
jamie@1 18 * USA.
jamie@1 19 */
jamie@1 20
jamie@1 21
jamie@1 22 /* xtract_scalar.c: defines functions that extract a feature as a single value from an input vector */
jamie@1 23
jamie@1 24 #include "xtract/libxtract.h"
jamie@1 25 #include "math.h"
jamie@5 26 #include <stdlib.h>
jamie@43 27 #include <string.h>
jamie@1 28
jamie@43 29 int xtract_mean(const float *data, const int N, const void *argv, float *result){
jamie@25 30
jamie@1 31 int n = N;
jamie@1 32
jamie@1 33 while(n--)
jamie@42 34 *result += data[n];
jamie@25 35
jamie@1 36 *result /= N;
jamie@38 37
jamie@38 38 return SUCCESS;
jamie@1 39 }
jamie@1 40
jamie@43 41 int xtract_variance(const float *data, const int N, const void *argv, float *result){
jamie@25 42
jamie@1 43 int n = N;
jamie@1 44
jamie@1 45 while(n--)
jamie@43 46 *result += pow(data[n] - *(float *)argv, 2);
jamie@25 47
jamie@43 48 *result = *result / (N - 1);
jamie@44 49
jamie@38 50 return SUCCESS;
jamie@1 51 }
jamie@1 52
jamie@43 53 int xtract_standard_deviation(const float *data, const int N, const void *argv, float *result){
jamie@25 54
jamie@1 55 *result = sqrt(*(float *)argv);
jamie@25 56
jamie@38 57 return SUCCESS;
jamie@1 58 }
jamie@1 59
jamie@43 60 int xtract_average_deviation(const float *data, const int N, const void *argv, float *result){
jamie@25 61
jamie@1 62 int n = N;
jamie@44 63
jamie@1 64 while(n--)
jamie@42 65 *result += fabs(data[n] - *(float *)argv);
jamie@25 66
jamie@1 67 *result /= N;
jamie@1 68
jamie@38 69 return SUCCESS;
jamie@1 70 }
jamie@1 71
jamie@43 72 int xtract_skewness(const float *data, const int N, const void *argv, float *result){
jamie@25 73
jamie@1 74 int n = N;
jamie@1 75
jamie@42 76 float temp;
jamie@25 77
jamie@42 78 while(n--){
jamie@42 79 temp = (data[n] - ((float *)argv)[0]) / ((float *)argv)[1];
jamie@42 80 *result += pow(temp, 3);
jamie@42 81 }
jamie@1 82
jamie@42 83 *result /= N;
jamie@44 84
jamie@38 85 return SUCCESS;
jamie@1 86 }
jamie@1 87
jamie@43 88 int xtract_kurtosis(const float *data, const int N, const void *argv, float *result){
jamie@25 89
jamie@1 90 int n = N;
jamie@1 91
jamie@42 92 float temp;
jamie@25 93
jamie@42 94 while(n--){
jamie@42 95 temp = (data[n] - ((float *)argv)[0]) / ((float *)argv)[1];
jamie@42 96 *result += pow(temp, 4);
jamie@42 97 }
jamie@25 98
jamie@42 99 *result /= N;
jamie@42 100 *result -= 3.0f;
jamie@44 101
jamie@38 102 return SUCCESS;
jamie@1 103 }
jamie@1 104
jamie@11 105
jamie@43 106 int xtract_centroid(const float *data, const int N, const void *argv, float *result){
jamie@25 107
jamie@37 108 int n = (N >> 1);
jamie@11 109
jamie@43 110 const float *freqs, *amps;
jamie@43 111 float FA = 0.f, A = 0.f;
jamie@11 112
jamie@25 113 freqs = data;
jamie@38 114 amps = data + n;
jamie@25 115
jamie@11 116 while(n--){
jamie@25 117 FA += freqs[n] * amps[n];
jamie@25 118 A += amps[n];
jamie@25 119 }
jamie@25 120
jamie@25 121 *result = FA / A;
jamie@11 122
jamie@38 123 return SUCCESS;
jamie@11 124 }
jamie@11 125
jamie@43 126 int xtract_irregularity_k(const float *data, const int N, const void *argv, float *result){
jamie@25 127
jamie@1 128 int n,
jamie@37 129 M = N - 1;
jamie@1 130
jamie@1 131 for(n = 1; n < M; n++)
jamie@42 132 *result += fabs(data[n] - (data[n-1] + data[n] + data[n+1]) / 3);
jamie@1 133
jamie@38 134 return SUCCESS;
jamie@1 135 }
jamie@1 136
jamie@43 137 int xtract_irregularity_j(const float *data, const int N, const void *argv, float *result){
jamie@25 138
jamie@1 139 int n = N;
jamie@1 140
jamie@37 141 float num = 0.f, den = 0.f;
jamie@1 142
jamie@1 143 while(n--){
jamie@42 144 num += pow(data[n] - data[n+1], 2);
jamie@42 145 den += pow(data[n], 2);
jamie@1 146 }
jamie@25 147
jamie@1 148 *result = num / den;
jamie@1 149
jamie@38 150 return SUCCESS;
jamie@1 151 }
jamie@1 152
jamie@43 153 int xtract_tristimulus_1(const float *data, const int N, const void *argv, float *result){
jamie@1 154
jamie@1 155 int n = N;
jamie@1 156
jamie@42 157 float den, p1, temp;
jamie@1 158
jamie@42 159 den = p1 = temp = 0.f;
jamie@1 160
jamie@42 161 for(n = 0; n < N; n++){
jamie@42 162 if((temp = data[n])){
jamie@42 163 den += temp;
jamie@42 164 if(!p1)
jamie@42 165 p1 = temp;
jamie@42 166 }
jamie@42 167 }
jamie@42 168
jamie@42 169 *result = p1 / den;
jamie@1 170
jamie@38 171 return SUCCESS;
jamie@1 172 }
jamie@1 173
jamie@43 174 int xtract_tristimulus_2(const float *data, const int N, const void *argv, float *result){
jamie@25 175
jamie@1 176 int n = N;
jamie@1 177
jamie@42 178 float den, p2, p3, p4, temp;
jamie@1 179
jamie@42 180 den = p2 = p3 = p4 = temp = 0.f;
jamie@1 181
jamie@42 182 for(n = 0; n < N; n++){
jamie@42 183 if((temp = data[n])){
jamie@42 184 den += temp;
jamie@42 185 if(!p2)
jamie@42 186 p2 = temp;
jamie@42 187 else if(!p3)
jamie@42 188 p3 = temp;
jamie@42 189 else if(!p4)
jamie@42 190 p4 = temp;
jamie@42 191 }
jamie@42 192 }
jamie@42 193
jamie@42 194 *result = (p2 + p3 + p4) / den;
jamie@25 195
jamie@38 196 return SUCCESS;
jamie@1 197 }
jamie@1 198
jamie@43 199 int xtract_tristimulus_3(const float *data, const int N, const void *argv, float *result){
jamie@25 200
jamie@42 201 int n = N, count = 0;
jamie@1 202
jamie@42 203 float den, num, temp;
jamie@1 204
jamie@42 205 den = num = temp = 0.f;
jamie@1 206
jamie@42 207 for(n = 0; n < N; n++){
jamie@42 208 if((temp = data[n])){
jamie@42 209 den += temp;
jamie@42 210 if(count >= 5)
jamie@42 211 num += temp;
jamie@42 212 count++;
jamie@42 213 }
jamie@42 214 }
jamie@25 215
jamie@1 216 *result = num / den;
jamie@25 217
jamie@38 218 return SUCCESS;
jamie@1 219 }
jamie@1 220
jamie@43 221 int xtract_smoothness(const float *data, const int N, const void *argv, float *result){
jamie@25 222
jamie@1 223 int n = N;
jamie@1 224
jamie@43 225 float *input;
jamie@43 226
jamie@43 227 input = (float *)malloc(N * sizeof(float));
jamie@43 228 input = memcpy(input, data, N * sizeof(float));
jamie@43 229
jamie@43 230 if (input[0] <= 0) input[0] = 1;
jamie@43 231 if (input[1] <= 0) input[1] = 1;
jamie@25 232
jamie@1 233 for(n = 2; n < N; n++){
jamie@43 234 if(input[n] <= 0) input[n] = 1;
jamie@43 235 *result += abs(20 * log(input[n-1]) - (20 * log(input[n-2]) +
jamie@43 236 20 * log(input[n-1]) + 20 * log(input[n])) / 3);
jamie@25 237 }
jamie@43 238
jamie@43 239 free(input);
jamie@44 240
jamie@38 241 return SUCCESS;
jamie@1 242 }
jamie@1 243
jamie@43 244 int xtract_spread(const float *data, const int N, const void *argv, float *result){
jamie@1 245
jamie@1 246 int n = N;
jamie@1 247
jamie@48 248 float num = 0.f, den = 0.f, temp;
jamie@1 249
jamie@1 250 while(n--){
jamie@48 251 temp = n - *(float *)argv;
jamie@48 252 num += SQ(temp) * data[n];
jamie@25 253 den += data[n];
jamie@1 254 }
jamie@1 255
jamie@1 256 *result = sqrt(num / den);
jamie@25 257
jamie@38 258 return SUCCESS;
jamie@1 259 }
jamie@1 260
jamie@43 261 int xtract_zcr(const float *data, const int N, const void *argv, float *result){
jamie@1 262
jamie@1 263 int n = N;
jamie@25 264
jamie@1 265 for(n = 1; n < N; n++)
jamie@25 266 if(data[n] * data[n-1] < 0) (*result)++;
jamie@25 267
jamie@1 268 *result /= N;
jamie@25 269
jamie@38 270 return SUCCESS;
jamie@1 271 }
jamie@1 272
jamie@43 273 int xtract_rolloff(const float *data, const int N, const void *argv, float *result){
jamie@1 274
jamie@1 275 int n = N;
jamie@42 276 float pivot, temp;
jamie@42 277
jamie@42 278 pivot = temp = 0.f;
jamie@25 279
jamie@1 280 while(n--) pivot += data[n];
jamie@25 281
jamie@42 282 pivot *= ((float *)argv)[0];
jamie@25 283
jamie@42 284 for(n = 0; temp < pivot; n++)
jamie@42 285 temp += data[n];
jamie@1 286
jamie@42 287 *result = (n / (float)N) * (((float *)argv)[1] * .5);
jamie@25 288
jamie@38 289 return SUCCESS;
jamie@1 290 }
jamie@1 291
jamie@43 292 int xtract_loudness(const float *data, const int N, const void *argv, float *result){
jamie@25 293
jamie@47 294 int n = N, rv;
jamie@25 295
jamie@47 296 if(n > BARK_BANDS)
jamie@47 297 rv = BAD_VECTOR_SIZE;
jamie@47 298 else
jamie@47 299 rv = SUCCESS;
jamie@1 300
jamie@1 301 while(n--)
jamie@25 302 *result += pow(data[n], 0.23);
jamie@38 303
jamie@47 304 return rv;
jamie@1 305 }
jamie@1 306
jamie@43 307 int xtract_flatness(const float *data, const int N, const void *argv, float *result){
jamie@1 308
jamie@42 309 int n;
jamie@1 310
jamie@44 311 double num, den, temp;
jamie@25 312
jamie@44 313 den = data[0];
jamie@44 314 num = (data[0] == 0.f ? 1.f : data[0]);
jamie@43 315
jamie@44 316 for(n = 1; n < N; n++){
jamie@44 317 if((temp = data[n]) != 0.f) {
jamie@44 318 num *= temp;
jamie@44 319 den += temp;
jamie@25 320 }
jamie@1 321 }
jamie@44 322
jamie@44 323 num = pow(num, 1.f / N);
jamie@1 324 den /= N;
jamie@25 325
jamie@45 326 if(num < VERY_SMALL_NUMBER)
jamie@45 327 num = VERY_SMALL_NUMBER;
jamie@44 328
jamie@45 329 if(den < VERY_SMALL_NUMBER)
jamie@45 330 den = VERY_SMALL_NUMBER;
jamie@44 331
jamie@42 332 *result = num / den;
jamie@25 333
jamie@38 334 return SUCCESS;
jamie@44 335
jamie@1 336 }
jamie@1 337
jamie@43 338 int xtract_tonality(const float *data, const int N, const void *argv, float *result){
jamie@25 339
jamie@1 340 float sfmdb, sfm;
jamie@25 341
jamie@1 342 sfm = *(float *)argv;
jamie@1 343
jamie@45 344 sfmdb = (sfm > 0 ? ((10 * log10(sfm)) / -60) : 0);
jamie@25 345
jamie@1 346 *result = MIN(sfmdb, 1);
jamie@25 347
jamie@38 348 return SUCCESS;
jamie@1 349 }
jamie@1 350
jamie@43 351 int xtract_crest(const float *data, const int N, const void *argv, float *result){
jamie@25 352
jamie@45 353 float max, mean;
jamie@45 354
jamie@45 355 max = mean = 0.f;
jamie@45 356
jamie@45 357 max = *(float *)argv;
jamie@45 358 mean = *((float *)argv+1);
jamie@45 359
jamie@45 360 *result = max / mean;
jamie@45 361
jamie@45 362 return SUCCESS;
jamie@25 363
jamie@1 364 }
jamie@1 365
jamie@43 366 int xtract_noisiness(const float *data, const int N, const void *argv, float *result){
jamie@25 367
jamie@45 368 float h, i, p; /*harmonics, inharmonics, partials */
jamie@45 369
jamie@45 370 i = p = h = 0.f;
jamie@45 371
jamie@45 372 h = *(float *)argv;
jamie@45 373 p = *((float *)argv+1);
jamie@45 374
jamie@45 375 i = p - h;
jamie@45 376
jamie@45 377 *result = i / p;
jamie@45 378
jamie@45 379 return SUCCESS;
jamie@25 380
jamie@1 381 }
jamie@2 382
jamie@43 383 int xtract_rms_amplitude(const float *data, const int N, const void *argv, float *result){
jamie@1 384
jamie@1 385 int n = N;
jamie@1 386
jamie@1 387 while(n--) *result += SQ(data[n]);
jamie@1 388
jamie@1 389 *result = sqrt(*result / N);
jamie@25 390
jamie@38 391 return SUCCESS;
jamie@1 392 }
jamie@1 393
jamie@43 394 int xtract_inharmonicity(const float *data, const int N, const void *argv, float *result){
jamie@1 395
jamie@41 396 int n = N >> 1;
jamie@43 397 float num = 0.f, den = 0.f, fund;
jamie@43 398 const float *freqs, *amps;
jamie@1 399
jamie@41 400 fund = *(float *)argv;
jamie@41 401 freqs = data;
jamie@41 402 amps = data + n;
jamie@25 403
jamie@1 404 while(n--){
jamie@41 405 num += abs(freqs[n] - n * fund) * SQ(amps[n]);
jamie@41 406 den += SQ(amps[n]);
jamie@1 407 }
jamie@1 408
jamie@41 409 *result = (2 * num) / (fund * den);
jamie@25 410
jamie@38 411 return SUCCESS;
jamie@1 412 }
jamie@1 413
jamie@1 414
jamie@43 415 int xtract_power(const float *data, const int N, const void *argv, float *result){
jamie@1 416
jamie@38 417 return FEATURE_NOT_IMPLEMENTED;
jamie@25 418
jamie@1 419 }
jamie@1 420
jamie@43 421 int xtract_odd_even_ratio(const float *data, const int N, const void *argv, float *result){
jamie@1 422
jamie@43 423 int M = (N >> 1), n;
jamie@1 424
jamie@43 425 float num = 0.f, den = 0.f, temp, f0;
jamie@1 426
jamie@43 427 f0 = *(float *)argv;
jamie@44 428
jamie@43 429 for(n = 0; n < M; n++){
jamie@43 430 if((temp = data[n])){
jamie@43 431 if(((int)(rintf(temp / f0)) % 2) != 0){
jamie@43 432 num += data[M + n];
jamie@43 433 }
jamie@43 434 else{
jamie@43 435 den += data[M + n];
jamie@43 436 }
jamie@43 437 }
jamie@1 438 }
jamie@1 439
jamie@1 440 *result = num / den;
jamie@25 441
jamie@38 442 return SUCCESS;
jamie@1 443 }
jamie@1 444
jamie@43 445 int xtract_sharpness(const float *data, const int N, const void *argv, float *result){
jamie@1 446
jamie@48 447 int n = N, rv;
jamie@48 448 float sl, g, temp; /* sl = specific loudness */
jamie@48 449
jamie@48 450 sl = g = temp = 0.f;
jamie@48 451
jamie@48 452 if(n > BARK_BANDS)
jamie@48 453 rv = BAD_VECTOR_SIZE;
jamie@48 454 else
jamie@48 455 rv = SUCCESS;
jamie@48 456
jamie@48 457
jamie@48 458 while(n--){
jamie@48 459 sl = pow(data[n], 0.23);
jamie@48 460 g = (n < 15 ? 1.f : 0.066 * exp(0.171 * n));
jamie@49 461 temp += n * g * sl;
jamie@48 462 }
jamie@48 463
jamie@48 464 *result = 0.11 * temp / N;
jamie@48 465
jamie@48 466 return rv;
jamie@25 467
jamie@1 468 }
jamie@1 469
jamie@43 470 int xtract_slope(const float *data, const int N, const void *argv, float *result){
jamie@1 471
jamie@48 472 const float *freqs, *amps;
jamie@48 473 float f, a,
jamie@48 474 F, A, FA, FSQ; /* sums of freqs, amps, freq * amps, freq squared */
jamie@48 475 int n, M;
jamie@48 476
jamie@48 477 F = A = FA = FSQ = 0.f;
jamie@48 478 n = M = N >> 1;
jamie@48 479
jamie@48 480 freqs = data;
jamie@48 481 amps = data + n;
jamie@48 482
jamie@48 483 while(n--){
jamie@48 484 f = freqs[n];
jamie@48 485 a = amps[n];
jamie@48 486 F += f;
jamie@48 487 A += a;
jamie@48 488 FA += f * a;
jamie@48 489 FSQ += f * f;
jamie@48 490 }
jamie@48 491
jamie@48 492 *result = (1.f / A) * (M * FA - F * A) / (M * FSQ - F * F);
jamie@48 493
jamie@48 494 return SUCCESS;
jamie@25 495
jamie@1 496 }
jamie@1 497
jamie@45 498 int xtract_lowest_value(const float *data, const int N, const void *argv, float *result){
jamie@25 499
jamie@45 500 int n = N;
jamie@45 501 float temp;
jamie@45 502
jamie@46 503 *result = data[--n];
jamie@45 504
jamie@45 505 while(n--){
jamie@45 506 if((temp = data[n]) > *(float *)argv)
jamie@45 507 *result = MIN(*result, data[n]);
jamie@45 508 }
jamie@45 509
jamie@45 510 return SUCCESS;
jamie@45 511 }
jamie@45 512
jamie@45 513 int xtract_highest_value(const float *data, const int N, const void *argv, float *result){
jamie@45 514
jamie@1 515 int n = N;
jamie@1 516
jamie@46 517 *result = data[--n];
jamie@44 518
jamie@45 519 while(n--)
jamie@45 520 *result = MAX(*result, data[n]);
jamie@44 521
jamie@38 522 return SUCCESS;
jamie@1 523 }
jamie@1 524
jamie@45 525
jamie@45 526 int xtract_sum(const float *data, const int N, const void *argv, float *result){
jamie@45 527
jamie@45 528 int n = N;
jamie@45 529
jamie@45 530 while(n--)
jamie@45 531 *result += *data++;
jamie@45 532
jamie@45 533 return SUCCESS;
jamie@45 534
jamie@45 535 }
jamie@45 536
jamie@43 537 int xtract_hps(const float *data, const int N, const void *argv, float *result){
jamie@1 538
jamie@1 539 int n = N, M, m, l, peak_index, position1_lwr;
jamie@1 540 float *coeffs2, *coeffs3, *product, L,
jamie@25 541 largest1_lwr, peak, ratio1, sr;
jamie@1 542
jamie@25 543 sr = *(float*)argv;
jamie@25 544
jamie@1 545 coeffs2 = (float *)malloc(N * sizeof(float));
jamie@1 546 coeffs3 = (float *)malloc(N * sizeof(float));
jamie@1 547 product = (float *)malloc(N * sizeof(float));
jamie@25 548
jamie@1 549 while(n--) coeffs2[n] = coeffs3[n] = 1;
jamie@1 550
jamie@1 551 M = N >> 1;
jamie@1 552 L = N / 3;
jamie@1 553
jamie@1 554 while(M--){
jamie@25 555 m = M << 1;
jamie@25 556 coeffs2[M] = (data[m] + data[m+1]) * 0.5f;
jamie@1 557
jamie@25 558 if(M < L){
jamie@25 559 l = M * 3;
jamie@25 560 coeffs3[M] = (data[l] + data[l+1] + data[l+2]) / 3;
jamie@25 561 }
jamie@1 562 }
jamie@25 563
jamie@1 564 peak_index = peak = 0;
jamie@25 565
jamie@1 566 for(n = 1; n < N; n++){
jamie@25 567 product[n] = data[n] * coeffs2[n] * coeffs3[n];
jamie@25 568 if(product[n] > peak){
jamie@25 569 peak_index = n;
jamie@25 570 peak = product[n];
jamie@25 571 }
jamie@1 572 }
jamie@1 573
jamie@1 574 largest1_lwr = position1_lwr = 0;
jamie@1 575
jamie@1 576 for(n = 0; n < N; n++){
jamie@25 577 if(data[n] > largest1_lwr && n != peak_index){
jamie@25 578 largest1_lwr = data[n];
jamie@25 579 position1_lwr = n;
jamie@25 580 }
jamie@1 581 }
jamie@1 582
jamie@1 583 ratio1 = data[position1_lwr] / data[peak_index];
jamie@1 584
jamie@1 585 if(position1_lwr > peak_index * 0.4 && position1_lwr <
jamie@25 586 peak_index * 0.6 && ratio1 > 0.1)
jamie@25 587 peak_index = position1_lwr;
jamie@1 588
jamie@22 589 *result = sr / (float)peak_index;
jamie@25 590
jamie@1 591 free(coeffs2);
jamie@1 592 free(coeffs3);
jamie@1 593 free(product);
jamie@25 594
jamie@38 595 return SUCCESS;
jamie@1 596 }
jamie@5 597
jamie@5 598
jamie@43 599 int xtract_f0(const float *data, const int N, const void *argv, float *result){
jamie@5 600
jamie@25 601 int M, sr, tau, n;
jamie@43 602 size_t bytes;
jamie@43 603 float f0, err_tau_1, err_tau_x, array_max,
jamie@43 604 threshold_peak, threshold_centre,
jamie@43 605 *input;
jamie@22 606
jamie@25 607 sr = *(float *)argv;
jamie@43 608
jamie@43 609 input = (float *)malloc(bytes = N * sizeof(float));
jamie@43 610 input = memcpy(input, data, bytes);
jamie@25 611 /* threshold_peak = *((float *)argv+1);
jamie@25 612 threshold_centre = *((float *)argv+2);
jamie@25 613 printf("peak: %.2f\tcentre: %.2f\n", threshold_peak, threshold_centre);*/
jamie@25 614 /* add temporary dynamic control over thresholds to test clipping effects */
jamie@22 615
jamie@25 616 /* FIX: tweak and make into macros */
jamie@25 617 threshold_peak = .8;
jamie@25 618 threshold_centre = .3;
jamie@25 619 M = N >> 1;
jamie@25 620 err_tau_1 = 0;
jamie@25 621 array_max = 0;
jamie@25 622
jamie@25 623 /* Find the array max */
jamie@25 624 for(n = 0; n < N; n++){
jamie@43 625 if (input[n] > array_max)
jamie@43 626 array_max = input[n];
jamie@12 627 }
jamie@25 628
jamie@25 629 threshold_peak *= array_max;
jamie@25 630
jamie@25 631 /* peak clip */
jamie@25 632 for(n = 0; n < N; n++){
jamie@43 633 if(input[n] > threshold_peak)
jamie@43 634 input[n] = threshold_peak;
jamie@43 635 else if(input[n] < -threshold_peak)
jamie@43 636 input[n] = -threshold_peak;
jamie@25 637 }
jamie@25 638
jamie@25 639 threshold_centre *= array_max;
jamie@25 640
jamie@25 641 /* Centre clip */
jamie@25 642 for(n = 0; n < N; n++){
jamie@43 643 if (input[n] < threshold_centre)
jamie@43 644 input[n] = 0;
jamie@25 645 else
jamie@43 646 input[n] -= threshold_centre;
jamie@25 647 }
jamie@25 648
jamie@25 649 /* Estimate fundamental freq */
jamie@25 650 for (n = 1; n < M; n++)
jamie@43 651 err_tau_1 = err_tau_1 + fabs(input[n] - input[n+1]);
jamie@25 652 /* FIX: this doesn't pose too much load if it returns 'early', but if it can't find f0, load can be significant for larger block sizes M^2 iterations! */
jamie@25 653 for (tau = 2; tau < M; tau++){
jamie@25 654 err_tau_x = 0;
jamie@25 655 for (n = 1; n < M; n++){
jamie@43 656 err_tau_x = err_tau_x + fabs(input[n] - input[n+tau]);
jamie@25 657 }
jamie@25 658 if (err_tau_x < err_tau_1) {
jamie@25 659 f0 = sr / (tau + (err_tau_x / err_tau_1));
jamie@25 660 *result = f0;
jamie@43 661 free(input);
jamie@25 662 return SUCCESS;
jamie@25 663 }
jamie@25 664 }
jamie@43 665 *result = -0;
jamie@43 666 free(input);
jamie@25 667 return NO_RESULT;
jamie@5 668 }
jamie@43 669
jamie@43 670 int xtract_failsafe_f0(const float *data, const int N, const void *argv, float *result){
jamie@44 671
jamie@43 672 float *magnitudes = NULL, argf[2], *peaks = NULL, return_code;
jamie@44 673
jamie@43 674 return_code = xtract_f0(data, N, argv, result);
jamie@44 675
jamie@43 676 if(return_code == NO_RESULT){
jamie@44 677
jamie@43 678 magnitudes = (float *)malloc(N * sizeof(float));
jamie@43 679 peaks = (float *)malloc(N * sizeof(float));
jamie@46 680 xtract_magnitude_spectrum(data, N, argv, magnitudes);
jamie@43 681 argf[0] = 10.f;
jamie@43 682 argf[1] = *(float *)argv;
jamie@43 683 xtract_peaks(magnitudes, N, argf, peaks);
jamie@43 684 argf[0] = 0.f;
jamie@45 685 xtract_lowest_value(peaks, N >> 1, argf, result);
jamie@44 686
jamie@43 687 free(magnitudes);
jamie@43 688 free(peaks);
jamie@43 689 }
jamie@43 690
jamie@43 691 return SUCCESS;
jamie@43 692
jamie@43 693 }
jamie@44 694