comparison src/OnsetDetectionFunction.cpp @ 115:54c657d621dd

Code style updates
author Adam Stark <adamstark.uk@gmail.com>
date Fri, 18 Aug 2023 20:00:10 +0200
parents 6aea5918992d
children
comparison
equal deleted inserted replaced
114:d6d9df2db3e1 115:54c657d621dd
21 21
22 #include <math.h> 22 #include <math.h>
23 #include "OnsetDetectionFunction.h" 23 #include "OnsetDetectionFunction.h"
24 24
25 //======================================================================= 25 //=======================================================================
26 OnsetDetectionFunction::OnsetDetectionFunction (int hopSize_,int frameSize_) 26 OnsetDetectionFunction::OnsetDetectionFunction (int hopSize_, int frameSize_)
27 : onsetDetectionFunctionType (ComplexSpectralDifferenceHWR), windowType (HanningWindow) 27 : onsetDetectionFunctionType (ComplexSpectralDifferenceHWR), windowType (HanningWindow)
28 { 28 {
29 // indicate that we have not initialised yet 29 // indicate that we have not initialised yet
30 initialised = false; 30 initialised = false;
31 31
35 // initialise with arguments to constructor 35 // initialise with arguments to constructor
36 initialise (hopSize_, frameSize_, ComplexSpectralDifferenceHWR, HanningWindow); 36 initialise (hopSize_, frameSize_, ComplexSpectralDifferenceHWR, HanningWindow);
37 } 37 }
38 38
39 //======================================================================= 39 //=======================================================================
40 OnsetDetectionFunction::OnsetDetectionFunction(int hopSize_,int frameSize_,int onsetDetectionFunctionType_,int windowType_) 40 OnsetDetectionFunction::OnsetDetectionFunction (int hopSize_, int frameSize_, int onsetDetectionFunctionType_, int windowType_)
41 : onsetDetectionFunctionType (ComplexSpectralDifferenceHWR), windowType (HanningWindow) 41 : onsetDetectionFunctionType (ComplexSpectralDifferenceHWR), windowType (HanningWindow)
42 { 42 {
43 // indicate that we have not initialised yet 43 // indicate that we have not initialised yet
44 initialised = false; 44 initialised = false;
45 45
67 // pass the new frame and hop size to the main initialisation function 67 // pass the new frame and hop size to the main initialisation function
68 initialise (hopSize_, frameSize_, onsetDetectionFunctionType, windowType); 68 initialise (hopSize_, frameSize_, onsetDetectionFunctionType, windowType);
69 } 69 }
70 70
71 //======================================================================= 71 //=======================================================================
72 void OnsetDetectionFunction::initialise(int hopSize_,int frameSize_,int onsetDetectionFunctionType_,int windowType_) 72 void OnsetDetectionFunction::initialise (int hopSize_, int frameSize_, int onsetDetectionFunctionType_, int windowType_)
73 { 73 {
74 hopSize = hopSize_; // set hopsize 74 hopSize = hopSize_; // set hopsize
75 frameSize = frameSize_; // set framesize 75 frameSize = frameSize_; // set framesize
76 76
77 onsetDetectionFunctionType = onsetDetectionFunctionType_; // set detection function type 77 onsetDetectionFunctionType = onsetDetectionFunctionType_; // set detection function type
138 #endif 138 #endif
139 139
140 #ifdef USE_KISS_FFT 140 #ifdef USE_KISS_FFT
141 complexOut.resize (frameSize); 141 complexOut.resize (frameSize);
142 142
143 for (int i = 0; i < frameSize;i++) 143 for (int i = 0; i < frameSize; i++)
144 { 144 {
145 complexOut[i].resize(2); 145 complexOut[i].resize(2);
146 } 146 }
147 147
148 fftIn = new kiss_fft_cpx[frameSize]; 148 fftIn = new kiss_fft_cpx[frameSize];
264 264
265 265
266 //======================================================================= 266 //=======================================================================
267 void OnsetDetectionFunction::performFFT() 267 void OnsetDetectionFunction::performFFT()
268 { 268 {
269 int fsize2 = (frameSize/2); 269 int fsize2 = (frameSize / 2);
270 270
271 #ifdef USE_FFTW 271 #ifdef USE_FFTW
272 // window frame and copy to complex array, swapping the first and second half of the signal 272 // window frame and copy to complex array, swapping the first and second half of the signal
273 for (int i = 0;i < fsize2;i++) 273 for (int i = 0; i < fsize2; i++)
274 { 274 {
275 complexIn[i][0] = frame[i + fsize2] * window[i + fsize2]; 275 complexIn[i][0] = frame[i + fsize2] * window[i + fsize2];
276 complexIn[i][1] = 0.0; 276 complexIn[i][1] = 0.0;
277 complexIn[i+fsize2][0] = frame[i] * window[i]; 277 complexIn[i+fsize2][0] = frame[i] * window[i];
278 complexIn[i+fsize2][1] = 0.0; 278 complexIn[i+fsize2][1] = 0.0;
313 double sum; 313 double sum;
314 314
315 sum = 0; // initialise sum 315 sum = 0; // initialise sum
316 316
317 // sum the squares of the samples 317 // sum the squares of the samples
318 for (int i = 0;i < frameSize;i++) 318 for (int i = 0; i < frameSize; i++)
319 { 319 {
320 sum = sum + (frame[i] * frame[i]); 320 sum = sum + (frame[i] * frame[i]);
321 } 321 }
322 322
323 return sum; // return sum 323 return sum; // return sum
358 double sum; 358 double sum;
359 359
360 // perform the FFT 360 // perform the FFT
361 performFFT(); 361 performFFT();
362 362
363 // compute first (N/2)+1 mag values 363 // compute first (N / 2) + 1 mag values
364 for (int i = 0;i < (frameSize/2)+1;i++) 364 for (int i = 0; i < (frameSize / 2) + 1; i++)
365 { 365 {
366 magSpec[i] = sqrt (pow (complexOut[i][0], 2) + pow (complexOut[i][1], 2)); 366 magSpec[i] = sqrt (pow (complexOut[i][0], 2) + pow (complexOut[i][1], 2));
367 } 367 }
368 // mag spec symmetric above (N/2)+1 so copy previous values 368 // mag spec symmetric above (N / 2) + 1 so copy previous values
369 for (int i = (frameSize/2)+1; i < frameSize; i++) 369 for (int i = (frameSize / 2) + 1; i < frameSize; i++)
370 { 370 {
371 magSpec[i] = magSpec[frameSize-i]; 371 magSpec[i] = magSpec[frameSize - i];
372 } 372 }
373 373
374 sum = 0; // initialise sum to zero 374 sum = 0; // initialise sum to zero
375 375
376 for (int i = 0; i < frameSize; i++) 376 for (int i = 0; i < frameSize; i++)
379 diff = magSpec[i] - prevMagSpec[i]; 379 diff = magSpec[i] - prevMagSpec[i];
380 380
381 // ensure all difference values are positive 381 // ensure all difference values are positive
382 if (diff < 0) 382 if (diff < 0)
383 { 383 {
384 diff = diff*-1; 384 diff = diff * -1;
385 } 385 }
386 386
387 // add difference to sum 387 // add difference to sum
388 sum = sum + diff; 388 sum = sum + diff;
389 389
401 double sum; 401 double sum;
402 402
403 // perform the FFT 403 // perform the FFT
404 performFFT(); 404 performFFT();
405 405
406 // compute first (N/2)+1 mag values 406 // compute first (N / 2) + 1 mag values
407 for (int i = 0;i < (frameSize/2) + 1; i++) 407 for (int i = 0; i < (frameSize / 2) + 1; i++)
408 { 408 {
409 magSpec[i] = sqrt (pow (complexOut[i][0],2) + pow (complexOut[i][1],2)); 409 magSpec[i] = sqrt (pow (complexOut[i][0],2) + pow (complexOut[i][1],2));
410 } 410 }
411 // mag spec symmetric above (N/2)+1 so copy previous values 411 // mag spec symmetric above (N / 2) + 1 so copy previous values
412 for (int i = (frameSize/2)+1;i < frameSize;i++) 412 for (int i = (frameSize / 2) + 1; i < frameSize; i++)
413 { 413 {
414 magSpec[i] = magSpec[frameSize-i]; 414 magSpec[i] = magSpec[frameSize - i];
415 } 415 }
416 416
417 sum = 0; // initialise sum to zero 417 sum = 0; // initialise sum to zero
418 418
419 for (int i = 0;i < frameSize;i++) 419 for (int i = 0; i < frameSize; i++)
420 { 420 {
421 // calculate difference 421 // calculate difference
422 diff = magSpec[i] - prevMagSpec[i]; 422 diff = magSpec[i] - prevMagSpec[i];
423 423
424 // only add up positive differences 424 // only add up positive differences
446 performFFT(); 446 performFFT();
447 447
448 sum = 0; // initialise sum to zero 448 sum = 0; // initialise sum to zero
449 449
450 // compute phase values from fft output and sum deviations 450 // compute phase values from fft output and sum deviations
451 for (int i = 0;i < frameSize;i++) 451 for (int i = 0; i < frameSize; i++)
452 { 452 {
453 // calculate phase value 453 // calculate phase value
454 phase[i] = atan2 (complexOut[i][1], complexOut[i][0]); 454 phase[i] = atan2 (complexOut[i][1], complexOut[i][0]);
455 455
456 // calculate magnitude value 456 // calculate magnitude value
458 458
459 459
460 // if bin is not just a low energy bin then examine phase deviation 460 // if bin is not just a low energy bin then examine phase deviation
461 if (magSpec[i] > 0.1) 461 if (magSpec[i] > 0.1)
462 { 462 {
463 dev = phase[i] - (2*prevPhase[i]) + prevPhase2[i]; // phase deviation 463 dev = phase[i] - (2 * prevPhase[i]) + prevPhase2[i]; // phase deviation
464 pdev = princarg (dev); // wrap into [-pi,pi] range 464 pdev = princarg (dev); // wrap into [-pi,pi] range
465 465
466 // make all values positive 466 // make all values positive
467 if (pdev < 0) 467 if (pdev < 0)
468 { 468 {
469 pdev = pdev*-1; 469 pdev = pdev * -1;
470 } 470 }
471 471
472 // add to sum 472 // add to sum
473 sum = sum + pdev; 473 sum = sum + pdev;
474 } 474 }
492 performFFT(); 492 performFFT();
493 493
494 sum = 0; // initialise sum to zero 494 sum = 0; // initialise sum to zero
495 495
496 // compute phase values from fft output and sum deviations 496 // compute phase values from fft output and sum deviations
497 for (int i = 0;i < frameSize;i++) 497 for (int i = 0; i < frameSize; i++)
498 { 498 {
499 // calculate phase value 499 // calculate phase value
500 phase[i] = atan2 (complexOut[i][1], complexOut[i][0]); 500 phase[i] = atan2 (complexOut[i][1], complexOut[i][0]);
501 501
502 // calculate magnitude value 502 // calculate magnitude value
532 performFFT(); 532 performFFT();
533 533
534 sum = 0; // initialise sum to zero 534 sum = 0; // initialise sum to zero
535 535
536 // compute phase values from fft output and sum deviations 536 // compute phase values from fft output and sum deviations
537 for (int i = 0;i < frameSize;i++) 537 for (int i = 0; i < frameSize; i++)
538 { 538 {
539 // calculate phase value 539 // calculate phase value
540 phase[i] = atan2 (complexOut[i][1], complexOut[i][0]); 540 phase[i] = atan2 (complexOut[i][1], complexOut[i][0]);
541 541
542 // calculate magnitude value 542 // calculate magnitude value
583 { 583 {
584 // calculate magnitude value 584 // calculate magnitude value
585 magSpec[i] = sqrt (pow (complexOut[i][0],2) + pow (complexOut[i][1],2)); 585 magSpec[i] = sqrt (pow (complexOut[i][0],2) + pow (complexOut[i][1],2));
586 586
587 587
588 sum = sum + (magSpec[i] * ((double) (i+1))); 588 sum = sum + (magSpec[i] * ((double) (i + 1)));
589 589
590 // store values for next calculation 590 // store values for next calculation
591 prevMagSpec[i] = magSpec[i]; 591 prevMagSpec[i] = magSpec[i];
592 } 592 }
593 593
604 performFFT(); 604 performFFT();
605 605
606 sum = 0; // initialise sum to zero 606 sum = 0; // initialise sum to zero
607 607
608 // compute phase values from fft output and sum deviations 608 // compute phase values from fft output and sum deviations
609 for (int i = 0;i < frameSize;i++) 609 for (int i = 0; i < frameSize; i++)
610 { 610 {
611 // calculate magnitude value 611 // calculate magnitude value
612 magSpec[i] = sqrt (pow (complexOut[i][0],2) + pow (complexOut[i][1],2)); 612 magSpec[i] = sqrt (pow (complexOut[i][0],2) + pow (complexOut[i][1],2));
613 613
614 // calculate difference 614 // calculate difference
617 if (mag_diff < 0) 617 if (mag_diff < 0)
618 { 618 {
619 mag_diff = -mag_diff; 619 mag_diff = -mag_diff;
620 } 620 }
621 621
622 sum = sum + (mag_diff * ((double) (i+1))); 622 sum = sum + (mag_diff * ((double) (i + 1)));
623 623
624 // store values for next calculation 624 // store values for next calculation
625 prevMagSpec[i] = magSpec[i]; 625 prevMagSpec[i] = magSpec[i];
626 } 626 }
627 627
638 performFFT(); 638 performFFT();
639 639
640 sum = 0; // initialise sum to zero 640 sum = 0; // initialise sum to zero
641 641
642 // compute phase values from fft output and sum deviations 642 // compute phase values from fft output and sum deviations
643 for (int i = 0;i < frameSize;i++) 643 for (int i = 0; i < frameSize; i++)
644 { 644 {
645 // calculate magnitude value 645 // calculate magnitude value
646 magSpec[i] = sqrt (pow (complexOut[i][0],2) + pow (complexOut[i][1],2)); 646 magSpec[i] = sqrt (pow (complexOut[i][0],2) + pow (complexOut[i][1],2));
647 647
648 // calculate difference 648 // calculate difference
649 mag_diff = magSpec[i] - prevMagSpec[i]; 649 mag_diff = magSpec[i] - prevMagSpec[i];
650 650
651 if (mag_diff > 0) 651 if (mag_diff > 0)
652 { 652 {
653 sum = sum + (mag_diff * ((double) (i+1))); 653 sum = sum + (mag_diff * ((double) (i + 1)));
654 } 654 }
655 655
656 // store values for next calculation 656 // store values for next calculation
657 prevMagSpec[i] = magSpec[i]; 657 prevMagSpec[i] = magSpec[i];
658 } 658 }
668 //======================================================================= 668 //=======================================================================
669 void OnsetDetectionFunction::calculateHanningWindow() 669 void OnsetDetectionFunction::calculateHanningWindow()
670 { 670 {
671 double N; // variable to store framesize minus 1 671 double N; // variable to store framesize minus 1
672 672
673 N = (double) (frameSize-1); // framesize minus 1 673 N = (double) (frameSize - 1); // framesize minus 1
674 674
675 // Hanning window calculation 675 // Hanning window calculation
676 for (int n = 0; n < frameSize; n++) 676 for (int n = 0; n < frameSize; n++)
677 { 677 {
678 window[n] = 0.5 * (1 - cos (2 * pi * (n / N))); 678 window[n] = 0.5 * (1 - cos (2 * pi * (n / N)));
683 void OnsetDetectionFunction::calclulateHammingWindow() 683 void OnsetDetectionFunction::calclulateHammingWindow()
684 { 684 {
685 double N; // variable to store framesize minus 1 685 double N; // variable to store framesize minus 1
686 double n_val; // double version of index 'n' 686 double n_val; // double version of index 'n'
687 687
688 N = (double) (frameSize-1); // framesize minus 1 688 N = (double) (frameSize - 1); // framesize minus 1
689 n_val = 0; 689 n_val = 0;
690 690
691 // Hamming window calculation 691 // Hamming window calculation
692 for (int n = 0;n < frameSize;n++) 692 for (int n = 0; n < frameSize; n++)
693 { 693 {
694 window[n] = 0.54 - (0.46 * cos (2 * pi * (n_val/N))); 694 window[n] = 0.54 - (0.46 * cos (2 * pi * (n_val / N)));
695 n_val = n_val+1; 695 n_val = n_val+1;
696 } 696 }
697 } 697 }
698 698
699 //======================================================================= 699 //=======================================================================
700 void OnsetDetectionFunction::calculateBlackmanWindow() 700 void OnsetDetectionFunction::calculateBlackmanWindow()
701 { 701 {
702 double N; // variable to store framesize minus 1 702 double N; // variable to store framesize minus 1
703 double n_val; // double version of index 'n' 703 double n_val; // double version of index 'n'
704 704
705 N = (double) (frameSize-1); // framesize minus 1 705 N = (double) (frameSize - 1); // framesize minus 1
706 n_val = 0; 706 n_val = 0;
707 707
708 // Blackman window calculation 708 // Blackman window calculation
709 for (int n = 0;n < frameSize;n++) 709 for (int n = 0; n < frameSize; n++)
710 { 710 {
711 window[n] = 0.42 - (0.5*cos(2*pi*(n_val/N))) + (0.08*cos(4*pi*(n_val/N))); 711 window[n] = 0.42 - (0.5 * cos (2 * pi * (n_val / N))) + (0.08 * cos (4 * pi * (n_val / N)));
712 n_val = n_val+1; 712 n_val = n_val + 1;
713 } 713 }
714 } 714 }
715 715
716 //======================================================================= 716 //=======================================================================
717 void OnsetDetectionFunction::calculateTukeyWindow() 717 void OnsetDetectionFunction::calculateTukeyWindow()
720 double n_val; // double version of index 'n' 720 double n_val; // double version of index 'n'
721 double alpha; // alpha [default value = 0.5]; 721 double alpha; // alpha [default value = 0.5];
722 722
723 alpha = 0.5; 723 alpha = 0.5;
724 724
725 N = (double) (frameSize-1); // framesize minus 1 725 N = (double) (frameSize - 1); // framesize minus 1
726 726
727 // Tukey window calculation 727 // Tukey window calculation
728 728
729 n_val = (double) (-1*((frameSize/2)))+1; 729 n_val = (double) (-1 * ((frameSize / 2))) + 1;
730 730
731 for (int n = 0;n < frameSize;n++) // left taper 731 for (int n = 0; n < frameSize; n++) // left taper
732 { 732 {
733 if ((n_val >= 0) && (n_val <= (alpha*(N/2)))) 733 if ((n_val >= 0) && (n_val <= (alpha * (N / 2))))
734 { 734 {
735 window[n] = 1.0; 735 window[n] = 1.0;
736 } 736 }
737 else if ((n_val <= 0) && (n_val >= (-1*alpha*(N/2)))) 737 else if ((n_val <= 0) && (n_val >= (-1 * alpha * (N / 2))))
738 { 738 {
739 window[n] = 1.0; 739 window[n] = 1.0;
740 } 740 }
741 else 741 else
742 { 742 {
743 window[n] = 0.5*(1+cos(pi*(((2*n_val)/(alpha*N))-1))); 743 window[n] = 0.5 * (1 + cos (pi * (((2 * n_val) / (alpha * N)) - 1)));
744 } 744 }
745 745
746 n_val = n_val+1; 746 n_val = n_val + 1;
747 } 747 }
748 748
749 } 749 }
750 750
751 //======================================================================= 751 //=======================================================================
752 void OnsetDetectionFunction::calculateRectangularWindow() 752 void OnsetDetectionFunction::calculateRectangularWindow()
753 { 753 {
754 // Rectangular window calculation 754 // Rectangular window calculation
755 for (int n = 0;n < frameSize;n++) 755 for (int n = 0; n < frameSize; n++)
756 { 756 {
757 window[n] = 1.0; 757 window[n] = 1.0;
758 } 758 }
759 } 759 }
760 760