You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

682 lines
15KB

  1. /*
  2. Copyright (C) 2003-2014 Paul Brossier <piem@aubio.org>
  3. This file is part of aubio.
  4. aubio is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. aubio is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with aubio. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /* see in mathutils.h for doc */
  16. #include "aubio_priv.h"
  17. #include "fvec.h"
  18. #include "mathutils.h"
  19. #include "musicutils.h"
  20. /** Window types */
  21. typedef enum
  22. {
  23. aubio_win_ones,
  24. aubio_win_rectangle,
  25. aubio_win_hamming,
  26. aubio_win_hanning,
  27. aubio_win_hanningz,
  28. aubio_win_blackman,
  29. aubio_win_blackman_harris,
  30. aubio_win_gaussian,
  31. aubio_win_welch,
  32. aubio_win_parzen,
  33. aubio_win_default = aubio_win_hanningz,
  34. } aubio_window_type;
  35. fvec_t *
  36. new_aubio_window (char_t * window_type, uint_t length)
  37. {
  38. fvec_t * win = new_fvec (length);
  39. uint_t err;
  40. if (win == NULL) {
  41. return NULL;
  42. }
  43. err = fvec_set_window (win, window_type);
  44. if (err != 0) {
  45. del_fvec(win);
  46. return NULL;
  47. }
  48. return win;
  49. }
  50. uint_t fvec_set_window (fvec_t *win, char_t *window_type) {
  51. smpl_t * w = win->data;
  52. uint_t i, size = win->length;
  53. aubio_window_type wintype;
  54. if (window_type == NULL) {
  55. AUBIO_ERR ("window type can not be null.\n");
  56. return 1;
  57. } else if (strcmp (window_type, "ones") == 0)
  58. wintype = aubio_win_ones;
  59. else if (strcmp (window_type, "rectangle") == 0)
  60. wintype = aubio_win_rectangle;
  61. else if (strcmp (window_type, "hamming") == 0)
  62. wintype = aubio_win_hamming;
  63. else if (strcmp (window_type, "hanning") == 0)
  64. wintype = aubio_win_hanning;
  65. else if (strcmp (window_type, "hanningz") == 0)
  66. wintype = aubio_win_hanningz;
  67. else if (strcmp (window_type, "blackman") == 0)
  68. wintype = aubio_win_blackman;
  69. else if (strcmp (window_type, "blackman_harris") == 0)
  70. wintype = aubio_win_blackman_harris;
  71. else if (strcmp (window_type, "gaussian") == 0)
  72. wintype = aubio_win_gaussian;
  73. else if (strcmp (window_type, "welch") == 0)
  74. wintype = aubio_win_welch;
  75. else if (strcmp (window_type, "parzen") == 0)
  76. wintype = aubio_win_parzen;
  77. else if (strcmp (window_type, "default") == 0)
  78. wintype = aubio_win_default;
  79. else {
  80. AUBIO_ERR ("unknown window type `%s`.\n", window_type);
  81. return 1;
  82. }
  83. switch(wintype) {
  84. case aubio_win_ones:
  85. fvec_ones(win);
  86. break;
  87. case aubio_win_rectangle:
  88. fvec_set_all(win, .5);
  89. break;
  90. case aubio_win_hamming:
  91. for (i=0;i<size;i++)
  92. w[i] = 0.54 - 0.46 * COS(TWO_PI * i / (size));
  93. break;
  94. case aubio_win_hanning:
  95. for (i=0;i<size;i++)
  96. w[i] = 0.5 - (0.5 * COS(TWO_PI * i / (size)));
  97. break;
  98. case aubio_win_hanningz:
  99. for (i=0;i<size;i++)
  100. w[i] = 0.5 * (1.0 - COS(TWO_PI * i / (size)));
  101. break;
  102. case aubio_win_blackman:
  103. for (i=0;i<size;i++)
  104. w[i] = 0.42
  105. - 0.50 * COS( TWO_PI*i/(size-1.0))
  106. + 0.08 * COS(2.0*TWO_PI*i/(size-1.0));
  107. break;
  108. case aubio_win_blackman_harris:
  109. for (i=0;i<size;i++)
  110. w[i] = 0.35875
  111. - 0.48829 * COS( TWO_PI*i/(size-1.0))
  112. + 0.14128 * COS(2.0*TWO_PI*i/(size-1.0))
  113. - 0.01168 * COS(3.0*TWO_PI*i/(size-1.0));
  114. break;
  115. case aubio_win_gaussian:
  116. {
  117. lsmp_t a, b, c = 0.5;
  118. uint_t n;
  119. for (n = 0; n < size; n++)
  120. {
  121. a = (n-c*(size-1))/(SQR(c)*(size-1));
  122. b = -c*SQR(a);
  123. w[n] = EXP(b);
  124. }
  125. }
  126. break;
  127. case aubio_win_welch:
  128. for (i=0;i<size;i++)
  129. w[i] = 1.0 - SQR((2.*i-size)/(size+1.0));
  130. break;
  131. case aubio_win_parzen:
  132. for (i=0;i<size;i++)
  133. w[i] = 1.0 - ABS((2.f*i-size)/(size+1.0f));
  134. break;
  135. default:
  136. break;
  137. }
  138. return 0;
  139. }
  140. smpl_t
  141. aubio_unwrap2pi (smpl_t phase)
  142. {
  143. /* mod(phase+pi,-2pi)+pi */
  144. return phase + TWO_PI * (1. + FLOOR (-(phase + PI) / TWO_PI));
  145. }
  146. smpl_t
  147. fvec_mean (fvec_t * s)
  148. {
  149. smpl_t tmp = 0.0;
  150. #if defined(HAVE_INTEL_IPP)
  151. aubio_ippsMean(s->data, (int)s->length, &tmp);
  152. return tmp;
  153. #elif defined(HAVE_ACCELERATE)
  154. aubio_vDSP_meanv(s->data, 1, &tmp, s->length);
  155. return tmp;
  156. #else
  157. uint_t j;
  158. for (j = 0; j < s->length; j++) {
  159. tmp += s->data[j];
  160. }
  161. return tmp / (smpl_t)(s->length);
  162. #endif
  163. }
  164. smpl_t
  165. fvec_sum (fvec_t * s)
  166. {
  167. smpl_t tmp = 0.0;
  168. #if defined(HAVE_INTEL_IPP)
  169. aubio_ippsSum(s->data, (int)s->length, &tmp);
  170. #elif defined(HAVE_ACCELERATE)
  171. aubio_vDSP_sve(s->data, 1, &tmp, s->length);
  172. #else
  173. uint_t j;
  174. for (j = 0; j < s->length; j++) {
  175. tmp += s->data[j];
  176. }
  177. #endif
  178. return tmp;
  179. }
  180. smpl_t
  181. fvec_max (fvec_t * s)
  182. {
  183. #if defined(HAVE_INTEL_IPP)
  184. smpl_t tmp = 0.;
  185. aubio_ippsMax( s->data, (int)s->length, &tmp);
  186. #elif defined(HAVE_ACCELERATE)
  187. smpl_t tmp = 0.;
  188. aubio_vDSP_maxv( s->data, 1, &tmp, s->length );
  189. #else
  190. uint_t j;
  191. smpl_t tmp = s->data[0];
  192. for (j = 1; j < s->length; j++) {
  193. tmp = (tmp > s->data[j]) ? tmp : s->data[j];
  194. }
  195. #endif
  196. return tmp;
  197. }
  198. smpl_t
  199. fvec_min (fvec_t * s)
  200. {
  201. #if defined(HAVE_INTEL_IPP)
  202. smpl_t tmp = 0.;
  203. aubio_ippsMin(s->data, (int)s->length, &tmp);
  204. #elif defined(HAVE_ACCELERATE)
  205. smpl_t tmp = 0.;
  206. aubio_vDSP_minv(s->data, 1, &tmp, s->length);
  207. #else
  208. uint_t j;
  209. smpl_t tmp = s->data[0];
  210. for (j = 1; j < s->length; j++) {
  211. tmp = (tmp < s->data[j]) ? tmp : s->data[j];
  212. }
  213. #endif
  214. return tmp;
  215. }
  216. uint_t
  217. fvec_min_elem (fvec_t * s)
  218. {
  219. #ifndef HAVE_ACCELERATE
  220. uint_t j, pos = 0.;
  221. smpl_t tmp = s->data[0];
  222. for (j = 0; j < s->length; j++) {
  223. pos = (tmp < s->data[j]) ? pos : j;
  224. tmp = (tmp < s->data[j]) ? tmp : s->data[j];
  225. }
  226. #else
  227. smpl_t tmp = 0.;
  228. vDSP_Length pos = 0;
  229. aubio_vDSP_minvi(s->data, 1, &tmp, &pos, s->length);
  230. #endif
  231. return (uint_t)pos;
  232. }
  233. uint_t
  234. fvec_max_elem (fvec_t * s)
  235. {
  236. #ifndef HAVE_ACCELERATE
  237. uint_t j, pos = 0;
  238. smpl_t tmp = 0.0;
  239. for (j = 0; j < s->length; j++) {
  240. pos = (tmp > s->data[j]) ? pos : j;
  241. tmp = (tmp > s->data[j]) ? tmp : s->data[j];
  242. }
  243. #else
  244. smpl_t tmp = 0.;
  245. vDSP_Length pos = 0;
  246. aubio_vDSP_maxvi(s->data, 1, &tmp, &pos, s->length);
  247. #endif
  248. return (uint_t)pos;
  249. }
  250. void
  251. fvec_shift (fvec_t * s)
  252. {
  253. uint_t half = s->length / 2, start = half, j;
  254. // if length is odd, middle element is moved to the end
  255. if (2 * half < s->length) start ++;
  256. #ifndef HAVE_BLAS
  257. for (j = 0; j < half; j++) {
  258. ELEM_SWAP (s->data[j], s->data[j + start]);
  259. }
  260. #else
  261. aubio_cblas_swap(half, s->data, 1, s->data + start, 1);
  262. #endif
  263. if (start != half) {
  264. for (j = 0; j < half; j++) {
  265. ELEM_SWAP (s->data[j + start - 1], s->data[j + start]);
  266. }
  267. }
  268. }
  269. void
  270. fvec_ishift (fvec_t * s)
  271. {
  272. uint_t half = s->length / 2, start = half, j;
  273. // if length is odd, middle element is moved to the beginning
  274. if (2 * half < s->length) start ++;
  275. #ifndef HAVE_BLAS
  276. for (j = 0; j < half; j++) {
  277. ELEM_SWAP (s->data[j], s->data[j + start]);
  278. }
  279. #else
  280. aubio_cblas_swap(half, s->data, 1, s->data + start, 1);
  281. #endif
  282. if (start != half) {
  283. for (j = 0; j < half; j++) {
  284. ELEM_SWAP (s->data[half], s->data[j]);
  285. }
  286. }
  287. }
  288. void fvec_push(fvec_t *in, smpl_t new_elem) {
  289. uint_t i;
  290. for (i = 0; i < in->length - 1; i++) {
  291. in->data[i] = in->data[i + 1];
  292. }
  293. in->data[in->length - 1] = new_elem;
  294. }
  295. void fvec_clamp(fvec_t *in, smpl_t absmax) {
  296. uint_t i;
  297. for (i = 0; i < in->length; i++) {
  298. if (in->data[i] > 0 && in->data[i] > ABS(absmax)) {
  299. in->data[i] = absmax;
  300. } else if (in->data[i] < 0 && in->data[i] < -ABS(absmax)) {
  301. in->data[i] = -absmax;
  302. }
  303. }
  304. }
  305. smpl_t
  306. aubio_level_lin (const fvec_t * f)
  307. {
  308. smpl_t energy = 0.;
  309. #ifndef HAVE_BLAS
  310. uint_t j;
  311. for (j = 0; j < f->length; j++) {
  312. energy += SQR (f->data[j]);
  313. }
  314. #else
  315. energy = aubio_cblas_dot(f->length, f->data, 1, f->data, 1);
  316. #endif
  317. return energy / f->length;
  318. }
  319. smpl_t
  320. fvec_local_hfc (fvec_t * v)
  321. {
  322. smpl_t hfc = 0.;
  323. uint_t j;
  324. for (j = 0; j < v->length; j++) {
  325. hfc += (j + 1) * v->data[j];
  326. }
  327. return hfc;
  328. }
  329. void
  330. fvec_min_removal (fvec_t * v)
  331. {
  332. smpl_t v_min = fvec_min (v);
  333. fvec_add (v, - v_min );
  334. }
  335. smpl_t
  336. fvec_alpha_norm (fvec_t * o, smpl_t alpha)
  337. {
  338. uint_t j;
  339. smpl_t tmp = 0.;
  340. for (j = 0; j < o->length; j++) {
  341. tmp += POW (ABS (o->data[j]), alpha);
  342. }
  343. return POW (tmp / o->length, 1. / alpha);
  344. }
  345. void
  346. fvec_alpha_normalise (fvec_t * o, smpl_t alpha)
  347. {
  348. uint_t j;
  349. smpl_t norm = fvec_alpha_norm (o, alpha);
  350. for (j = 0; j < o->length; j++) {
  351. o->data[j] /= norm;
  352. }
  353. }
  354. void
  355. fvec_add (fvec_t * o, smpl_t val)
  356. {
  357. uint_t j;
  358. for (j = 0; j < o->length; j++) {
  359. o->data[j] += val;
  360. }
  361. }
  362. void
  363. fvec_mul (fvec_t *o, smpl_t val)
  364. {
  365. uint_t j;
  366. for (j = 0; j < o->length; j++) {
  367. o->data[j] *= val;
  368. }
  369. }
  370. void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp,
  371. uint_t post, uint_t pre) {
  372. uint_t length = vec->length, j;
  373. for (j=0;j<length;j++) {
  374. vec->data[j] -= fvec_moving_thres(vec, tmp, post, pre, j);
  375. }
  376. }
  377. smpl_t
  378. fvec_moving_thres (fvec_t * vec, fvec_t * tmpvec,
  379. uint_t post, uint_t pre, uint_t pos)
  380. {
  381. uint_t k;
  382. smpl_t *medar = (smpl_t *) tmpvec->data;
  383. uint_t win_length = post + pre + 1;
  384. uint_t length = vec->length;
  385. /* post part of the buffer does not exist */
  386. if (pos < post + 1) {
  387. for (k = 0; k < post + 1 - pos; k++)
  388. medar[k] = 0.; /* 0-padding at the beginning */
  389. for (k = post + 1 - pos; k < win_length; k++)
  390. medar[k] = vec->data[k + pos - post];
  391. /* the buffer is fully defined */
  392. } else if (pos + pre < length) {
  393. for (k = 0; k < win_length; k++)
  394. medar[k] = vec->data[k + pos - post];
  395. /* pre part of the buffer does not exist */
  396. } else {
  397. for (k = 0; k < length - pos + post; k++)
  398. medar[k] = vec->data[k + pos - post];
  399. for (k = length - pos + post; k < win_length; k++)
  400. medar[k] = 0.; /* 0-padding at the end */
  401. }
  402. return fvec_median (tmpvec);
  403. }
  404. smpl_t fvec_median (fvec_t * input) {
  405. uint_t n = input->length;
  406. smpl_t * arr = (smpl_t *) input->data;
  407. uint_t low, high ;
  408. uint_t median;
  409. uint_t middle, ll, hh;
  410. low = 0 ; high = n-1 ; median = (low + high) / 2;
  411. for (;;) {
  412. if (high <= low) /* One element only */
  413. return arr[median] ;
  414. if (high == low + 1) { /* Two elements only */
  415. if (arr[low] > arr[high])
  416. ELEM_SWAP(arr[low], arr[high]) ;
  417. return arr[median] ;
  418. }
  419. /* Find median of low, middle and high items; swap into position low */
  420. middle = (low + high) / 2;
  421. if (arr[middle] > arr[high]) ELEM_SWAP(arr[middle], arr[high]);
  422. if (arr[low] > arr[high]) ELEM_SWAP(arr[low], arr[high]);
  423. if (arr[middle] > arr[low]) ELEM_SWAP(arr[middle], arr[low]) ;
  424. /* Swap low item (now in position middle) into position (low+1) */
  425. ELEM_SWAP(arr[middle], arr[low+1]) ;
  426. /* Nibble from each end towards middle, swapping items when stuck */
  427. ll = low + 1;
  428. hh = high;
  429. for (;;) {
  430. do ll++; while (arr[low] > arr[ll]) ;
  431. do hh--; while (arr[hh] > arr[low]) ;
  432. if (hh < ll)
  433. break;
  434. ELEM_SWAP(arr[ll], arr[hh]) ;
  435. }
  436. /* Swap middle item (in position low) back into correct position */
  437. ELEM_SWAP(arr[low], arr[hh]) ;
  438. /* Re-set active partition */
  439. if (hh <= median)
  440. low = ll;
  441. if (hh >= median)
  442. high = hh - 1;
  443. }
  444. }
  445. smpl_t fvec_quadratic_peak_pos (const fvec_t * x, uint_t pos) {
  446. smpl_t s0, s1, s2; uint_t x0, x2;
  447. smpl_t half = .5, two = 2.;
  448. if (pos == 0 || pos == x->length - 1) return pos;
  449. x0 = (pos < 1) ? pos : pos - 1;
  450. x2 = (pos + 1 < x->length) ? pos + 1 : pos;
  451. if (x0 == pos) return (x->data[pos] <= x->data[x2]) ? pos : x2;
  452. if (x2 == pos) return (x->data[pos] <= x->data[x0]) ? pos : x0;
  453. s0 = x->data[x0];
  454. s1 = x->data[pos];
  455. s2 = x->data[x2];
  456. return pos + half * (s0 - s2 ) / (s0 - two * s1 + s2);
  457. }
  458. smpl_t fvec_quadratic_peak_mag (fvec_t *x, smpl_t pos) {
  459. smpl_t x0, x1, x2;
  460. uint_t index = (uint_t)(pos - .5) + 1;
  461. if (pos >= x->length || pos < 0.) return 0.;
  462. if ((smpl_t)index == pos) return x->data[index];
  463. x0 = x->data[index - 1];
  464. x1 = x->data[index];
  465. x2 = x->data[index + 1];
  466. return x1 - .25 * (x0 - x2) * (pos - index);
  467. }
  468. uint_t fvec_peakpick(const fvec_t * onset, uint_t pos) {
  469. uint_t tmp=0;
  470. tmp = (onset->data[pos] > onset->data[pos-1]
  471. && onset->data[pos] > onset->data[pos+1]
  472. && onset->data[pos] > 0.);
  473. return tmp;
  474. }
  475. smpl_t
  476. aubio_quadfrac (smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf)
  477. {
  478. smpl_t tmp =
  479. s0 + (pf / 2.) * (pf * (s0 - 2. * s1 + s2) - 3. * s0 + 4. * s1 - s2);
  480. return tmp;
  481. }
  482. smpl_t
  483. aubio_freqtomidi (smpl_t freq)
  484. {
  485. smpl_t midi;
  486. if (freq < 2. || freq > 100000.) return 0.; // avoid nans and infs
  487. /* log(freq/A-2)/log(2) */
  488. midi = freq / 6.875;
  489. midi = LOG (midi) / 0.6931471805599453;
  490. midi *= 12;
  491. midi -= 3;
  492. return midi;
  493. }
  494. smpl_t
  495. aubio_miditofreq (smpl_t midi)
  496. {
  497. smpl_t freq;
  498. if (midi > 140.) return 0.; // avoid infs
  499. freq = (midi + 3.) / 12.;
  500. freq = EXP (freq * 0.6931471805599453);
  501. freq *= 6.875;
  502. return freq;
  503. }
  504. smpl_t
  505. aubio_bintofreq (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
  506. {
  507. smpl_t freq = samplerate / fftsize;
  508. return freq * MAX(bin, 0);
  509. }
  510. smpl_t
  511. aubio_bintomidi (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
  512. {
  513. smpl_t midi = aubio_bintofreq (bin, samplerate, fftsize);
  514. return aubio_freqtomidi (midi);
  515. }
  516. smpl_t
  517. aubio_freqtobin (smpl_t freq, smpl_t samplerate, smpl_t fftsize)
  518. {
  519. smpl_t bin = fftsize / samplerate;
  520. return MAX(freq, 0) * bin;
  521. }
  522. smpl_t
  523. aubio_miditobin (smpl_t midi, smpl_t samplerate, smpl_t fftsize)
  524. {
  525. smpl_t freq = aubio_miditofreq (midi);
  526. return aubio_freqtobin (freq, samplerate, fftsize);
  527. }
  528. uint_t
  529. aubio_is_power_of_two (uint_t a)
  530. {
  531. if ((a & (a - 1)) == 0) {
  532. return 1;
  533. } else {
  534. return 0;
  535. }
  536. }
  537. uint_t
  538. aubio_next_power_of_two (uint_t a)
  539. {
  540. uint_t i = 1;
  541. while (i < a) i <<= 1;
  542. return i;
  543. }
  544. uint_t
  545. aubio_power_of_two_order (uint_t a)
  546. {
  547. int order = 0;
  548. int temp = aubio_next_power_of_two(a);
  549. while (temp >>= 1) {
  550. ++order;
  551. }
  552. return order;
  553. }
  554. smpl_t
  555. aubio_db_spl (const fvec_t * o)
  556. {
  557. return 10. * LOG10 (aubio_level_lin (o));
  558. }
  559. uint_t
  560. aubio_silence_detection (const fvec_t * o, smpl_t threshold)
  561. {
  562. return (aubio_db_spl (o) < threshold);
  563. }
  564. smpl_t
  565. aubio_level_detection (const fvec_t * o, smpl_t threshold)
  566. {
  567. smpl_t db_spl = aubio_db_spl (o);
  568. if (db_spl < threshold) {
  569. return 1.;
  570. } else {
  571. return db_spl;
  572. }
  573. }
  574. smpl_t
  575. aubio_zero_crossing_rate (fvec_t * input)
  576. {
  577. uint_t j;
  578. uint_t zcr = 0;
  579. for (j = 1; j < input->length; j++) {
  580. // previous was strictly negative
  581. if (input->data[j - 1] < 0.) {
  582. // current is positive or null
  583. if (input->data[j] >= 0.) {
  584. zcr += 1;
  585. }
  586. // previous was positive or null
  587. } else {
  588. // current is strictly negative
  589. if (input->data[j] < 0.) {
  590. zcr += 1;
  591. }
  592. }
  593. }
  594. return zcr / (smpl_t) input->length;
  595. }
  596. void
  597. aubio_autocorr (const fvec_t * input, fvec_t * output)
  598. {
  599. uint_t i, j, length = input->length;
  600. smpl_t *data, *acf;
  601. smpl_t tmp = 0;
  602. data = input->data;
  603. acf = output->data;
  604. for (i = 0; i < length; i++) {
  605. tmp = 0.;
  606. for (j = i; j < length; j++) {
  607. tmp += data[j - i] * data[j];
  608. }
  609. acf[i] = tmp / (smpl_t) (length - i);
  610. }
  611. }
  612. void
  613. aubio_cleanup (void)
  614. {
  615. #ifdef HAVE_FFTW3F
  616. fftwf_cleanup ();
  617. #else
  618. #ifdef HAVE_FFTW3
  619. fftw_cleanup ();
  620. #endif
  621. #endif
  622. }