Mercurial > hg > libxtract
comparison tests/xttest_scalar.cpp @ 269:446f6d3dc809
Add sawtooth tests
author | Jamie Bullock <jamie@jamiebullock.com> |
---|---|
date | Tue, 11 Nov 2014 17:30:17 +0000 |
parents | b371ffcecb74 |
children |
comparison
equal
deleted
inserted
replaced
268:b371ffcecb74 | 269:446f6d3dc809 |
---|---|
530 } | 530 } |
531 } | 531 } |
532 } | 532 } |
533 } | 533 } |
534 } | 534 } |
535 | |
536 SCENARIO( "F0 is correctly detected for a sawtooth wave", "[xtract_f0]" ) | |
537 { | |
538 uint16_t expected = 0; | |
539 uint16_t actual = 0; | |
540 | |
541 GIVEN( "a 512 sample block with a sample rate of 44100" ) | |
542 { | |
543 uint32_t blocksize = 512; | |
544 double samplerate = 44100; | |
545 double result = -1.0; | |
546 double amplitude = 1.0; | |
547 double table[blocksize]; | |
548 | |
549 WHEN( "the frequency is 86.1328125 Hz" ) // period of exactly 512 samples: 1 cycles in the block | |
550 { | |
551 double frequency = 86.1328125; | |
552 | |
553 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, amplitude); | |
554 int rv = xtract_f0(table, blocksize, &samplerate, &result); | |
555 | |
556 THEN( "frequency detection fails correctly (XTRACT_NO_RESULT is returned, result set to 0.0)" ) | |
557 { | |
558 REQUIRE(rv == XTRACT_NO_RESULT); | |
559 REQUIRE(result == 0.0); | |
560 } | |
561 } | |
562 | |
563 WHEN( "the frequency is 172.265625 Hz" ) // period of exactly 256 samples: 2 cycles in the block | |
564 { | |
565 double frequency = 172.265625; | |
566 | |
567 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, amplitude); | |
568 int rv = xtract_f0(table, blocksize, &samplerate, &result); | |
569 | |
570 THEN( "the detected F0 is accurate to the nearest quarter-tone" ) | |
571 { | |
572 actual = xttest_ftom(result); | |
573 expected = xttest_ftom(frequency); | |
574 uint16_t min = expected - 50; | |
575 uint16_t max = expected + 50; | |
576 CAPTURE( actual ); | |
577 CAPTURE( expected ); | |
578 REQUIRE( actual > min ); | |
579 REQUIRE( actual < max ); | |
580 } | |
581 } | |
582 | |
583 | |
584 WHEN( "the frequency is 344.53125 Hz" ) // period of exactly 128 samples: 4 cycles in the block | |
585 { | |
586 double frequency = 344.53125; | |
587 | |
588 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, amplitude); | |
589 xtract_f0(table, blocksize, &samplerate, &result); | |
590 | |
591 THEN( "the detected F0 is accurate to the nearest quarter-tone" ) | |
592 { | |
593 actual = xttest_ftom(result); | |
594 expected = xttest_ftom(frequency); | |
595 uint16_t min = expected - 50; | |
596 uint16_t max = expected + 50; | |
597 CAPTURE( actual ); | |
598 CAPTURE( expected ); | |
599 REQUIRE( actual > min ); | |
600 REQUIRE( actual < max ); | |
601 } | |
602 | |
603 | |
604 WHEN( "the amplitude is 0.01" ) // Only test a different amplitude for one case | |
605 { | |
606 double amplitude = 0.01; | |
607 | |
608 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, amplitude); | |
609 xtract_f0(table, blocksize, &samplerate, &result); | |
610 | |
611 THEN( "the detected F0 is accurate to the nearest quarter-tone" ) | |
612 { | |
613 actual = xttest_ftom(result); | |
614 expected = xttest_ftom(frequency); | |
615 uint16_t min = expected - 50; | |
616 uint16_t max = expected + 50; | |
617 CAPTURE( actual ); | |
618 CAPTURE( expected ); | |
619 REQUIRE( actual > min ); | |
620 REQUIRE( actual < max ); | |
621 } | |
622 } | |
623 } | |
624 } | |
625 | |
626 GIVEN( "a 1024 sample block with a sample rate of 44100" ) | |
627 { | |
628 uint32_t blocksize = 1024; | |
629 double samplerate = 44100; | |
630 double result = -1.0; | |
631 double amplitude = 1.0; | |
632 double table[blocksize]; | |
633 | |
634 WHEN( "the frequency is 86.1328125 Hz" ) // period of exactly 512 samples: 2 cycles in the block | |
635 { | |
636 double frequency = 86.1328125; | |
637 | |
638 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, amplitude); | |
639 int rv = xtract_f0(table, blocksize, &samplerate, &result); | |
640 | |
641 THEN( "the detected F0 is accurate to the nearest quarter-tone" ) | |
642 { | |
643 actual = xttest_ftom(result); | |
644 expected = xttest_ftom(frequency); | |
645 uint16_t min = expected - 50; | |
646 uint16_t max = expected + 50; | |
647 CAPTURE( actual ); | |
648 CAPTURE( expected ); | |
649 REQUIRE( actual > min ); | |
650 REQUIRE( actual < max ); | |
651 } | |
652 } | |
653 | |
654 WHEN( "the frequency is 140 Hz" ) // period of 315 samples: 3.25 cycles in the block | |
655 { | |
656 double frequency = 140; | |
657 | |
658 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, amplitude); | |
659 xtract_f0(table, blocksize, &samplerate, &result); | |
660 | |
661 THEN( "the detected F0 is accurate to the nearest MIDI cent" ) | |
662 { | |
663 actual = xttest_ftom(result); | |
664 expected = xttest_ftom(frequency); | |
665 CAPTURE( actual ); | |
666 CAPTURE( expected ); | |
667 REQUIRE(actual == expected); | |
668 } | |
669 } | |
670 | |
671 WHEN( "the frequency is 155 Hz" ) // period of 284.52 samples: 3.6 cycles in the block | |
672 { | |
673 double frequency = 155; | |
674 | |
675 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, amplitude); | |
676 xtract_f0(table, blocksize, &samplerate, &result); | |
677 | |
678 THEN( "the detected F0 is quantized to the nearest whole number of samples" ) | |
679 { | |
680 actual = xttest_ftom(result); | |
681 expected = xttest_ftom(155.28169014); // period of 284 samples | |
682 CAPTURE( result ); | |
683 CAPTURE( expected ); | |
684 REQUIRE(actual == expected); | |
685 } | |
686 } | |
687 | |
688 | |
689 WHEN( "the frequency is 172.265625 Hz" ) // period of exactly 256 samples: 4 cycles in the block | |
690 { | |
691 double frequency = 172.265625; | |
692 | |
693 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, amplitude); | |
694 xtract_f0(table, blocksize, &samplerate, &result); | |
695 | |
696 THEN( "the detected F0 is accurate to the nearest quarter-tone" ) | |
697 { | |
698 actual = xttest_ftom(result); | |
699 expected = xttest_ftom(frequency); | |
700 uint16_t min = expected - 50; | |
701 uint16_t max = expected + 50; | |
702 CAPTURE( actual ); | |
703 CAPTURE( expected ); | |
704 REQUIRE( actual > min ); | |
705 REQUIRE( actual < max ); | |
706 } | |
707 } | |
708 | |
709 WHEN( "the frequency is 344.53125 Hz" ) // period of exactly 128 samples: 8 cycles in the block | |
710 { | |
711 double frequency = 344.53125; | |
712 double noise[blocksize]; | |
713 expected = xttest_ftom(frequency); | |
714 CAPTURE( expected ); | |
715 | |
716 WHEN( "the amplitude is 1.0" ) | |
717 { | |
718 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, amplitude); | |
719 xtract_f0(table, blocksize, &samplerate, &result); | |
720 | |
721 THEN( "the detected F0 is accurate to the nearest quarter-tone" ) | |
722 { | |
723 actual = xttest_ftom(result); | |
724 expected = xttest_ftom(frequency); | |
725 uint16_t min = expected - 50; | |
726 uint16_t max = expected + 50; | |
727 CAPTURE( actual ); | |
728 CAPTURE( expected ); | |
729 REQUIRE( actual > min ); | |
730 REQUIRE( actual < max ); | |
731 } | |
732 } | |
733 | |
734 WHEN( "the amplitude is 0.01" ) // Only test a different amplitude for one case | |
735 { | |
736 amplitude = 0.01; | |
737 | |
738 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, amplitude); | |
739 xtract_f0(table, blocksize, &samplerate, &result); | |
740 | |
741 THEN( "the detected F0 is accurate to the nearest quarter-tone" ) | |
742 { | |
743 actual = xttest_ftom(result); | |
744 expected = xttest_ftom(frequency); | |
745 uint16_t min = expected - 50; | |
746 uint16_t max = expected + 50; | |
747 CAPTURE( actual ); | |
748 CAPTURE( expected ); | |
749 REQUIRE( actual > min ); | |
750 REQUIRE( actual < max ); | |
751 } | |
752 } | |
753 | |
754 WHEN( "white noise is added at 10%" ) // Only test noise for one case | |
755 { | |
756 amplitude = 0.1; | |
757 | |
758 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, 1.0 - amplitude); | |
759 xttest_gen_noise(noise, blocksize, amplitude); | |
760 xttest_add(table, noise, blocksize); | |
761 xtract_f0(table, blocksize, &samplerate, &result); | |
762 | |
763 THEN( "the detected F0 is accurate to the nearest quarter-tone" ) | |
764 { | |
765 actual = xttest_ftom(result); | |
766 expected = xttest_ftom(frequency); | |
767 uint16_t min = expected - 50; | |
768 uint16_t max = expected + 50; | |
769 CAPTURE( actual ); | |
770 CAPTURE( expected ); | |
771 REQUIRE( actual > min ); | |
772 REQUIRE( actual < max ); | |
773 } | |
774 } | |
775 | |
776 WHEN( "white noise is added at 20%" ) | |
777 { | |
778 amplitude = 0.2; | |
779 | |
780 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, 1.0 - amplitude); | |
781 xttest_gen_noise(noise, blocksize, amplitude); | |
782 xttest_add(table, noise, blocksize); | |
783 xtract_f0(table, blocksize, &samplerate, &result); | |
784 | |
785 THEN( "the detected F0 is accurate to the nearest quarter-tone" ) | |
786 { | |
787 actual = xttest_ftom(result); | |
788 expected = xttest_ftom(frequency); | |
789 uint16_t min = expected - 50; | |
790 uint16_t max = expected + 50; | |
791 CAPTURE( actual ); | |
792 CAPTURE( expected ); | |
793 REQUIRE( actual > min ); | |
794 REQUIRE( actual < max ); | |
795 } | |
796 } | |
797 | |
798 WHEN( "white noise is added at 25%" ) | |
799 { | |
800 amplitude = 0.25; | |
801 | |
802 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, 1.0 - amplitude); | |
803 xttest_gen_noise(noise, blocksize, amplitude); | |
804 xttest_add(table, noise, blocksize); | |
805 xtract_f0(table, blocksize, &samplerate, &result); | |
806 | |
807 THEN( "the detected F0 is accurate to the nearest quarter-tone" ) | |
808 { | |
809 actual = xttest_ftom(result); | |
810 expected = xttest_ftom(frequency); | |
811 uint16_t min = expected - 50; | |
812 uint16_t max = expected + 50; | |
813 CAPTURE( actual ); | |
814 CAPTURE( expected ); | |
815 REQUIRE( actual > min ); | |
816 REQUIRE( actual < max ); | |
817 } | |
818 } | |
819 | |
820 WHEN( "white noise is added at 30%" ) | |
821 { | |
822 amplitude = 0.25; | |
823 | |
824 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, 1.0 - amplitude); | |
825 xttest_gen_noise(noise, blocksize, amplitude); | |
826 xttest_add(table, noise, blocksize); | |
827 xtract_f0(table, blocksize, &samplerate, &result); | |
828 | |
829 THEN( "the detected F0 is accurate to the nearest quarter-tone" ) | |
830 { | |
831 actual = xttest_ftom(result); | |
832 uint16_t min = expected - 50; | |
833 uint16_t max = expected + 50; | |
834 CAPTURE( actual ); | |
835 REQUIRE( actual > min ); | |
836 REQUIRE( actual < max ); | |
837 } | |
838 } | |
839 | |
840 WHEN( "white noise is added at 35%" ) | |
841 { | |
842 amplitude = 0.35; | |
843 | |
844 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, 1.0 - amplitude); | |
845 xttest_gen_noise(noise, blocksize, amplitude); | |
846 xttest_add(table, noise, blocksize); | |
847 xtract_f0(table, blocksize, &samplerate, &result); | |
848 | |
849 THEN( "the detected F0 is accurate to the nearest quarter-tone" ) | |
850 { | |
851 actual = xttest_ftom(result); | |
852 expected = xttest_ftom(frequency); | |
853 uint16_t min = expected - 50; | |
854 uint16_t max = expected + 50; | |
855 CAPTURE( actual ); | |
856 CAPTURE( expected ); | |
857 REQUIRE( actual > min ); | |
858 REQUIRE( actual < max ); | |
859 } | |
860 } | |
861 } | |
862 } | |
863 | |
864 GIVEN( "a 1024 sample block with a sample rate of 11025" ) | |
865 { | |
866 uint32_t blocksize = 1024; | |
867 double samplerate = 11025; | |
868 double result = -1.0; | |
869 double table[blocksize]; | |
870 | |
871 WHEN( "the frequency is 86.1328125 Hz" ) // period of exactly 512 samples: 2 cycles in the block | |
872 { | |
873 double frequency = 86.1328125; | |
874 double amplitude = 1.0; | |
875 | |
876 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, amplitude); | |
877 int rv = xtract_f0(table, blocksize, &samplerate, &result); | |
878 | |
879 THEN( "the detected F0 is accurate to the nearest quarter-tone" ) | |
880 { | |
881 actual = xttest_ftom(result); | |
882 expected = xttest_ftom(frequency); | |
883 uint16_t min = expected - 50; | |
884 uint16_t max = expected + 50; | |
885 CAPTURE( actual ); | |
886 CAPTURE( expected ); | |
887 REQUIRE( actual > min ); | |
888 REQUIRE( actual < max ); | |
889 } | |
890 } | |
891 | |
892 WHEN( "the frequency is 172.265625 Hz" ) // period of exactly 256 samples: 4 cycles in the block | |
893 { | |
894 double frequency = 172.265625; | |
895 double amplitude = 1.0; | |
896 | |
897 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, amplitude); | |
898 xtract_f0(table, blocksize, &samplerate, &result); | |
899 | |
900 THEN( "the detected F0 is accurate to the nearest quarter-tone" ) | |
901 { | |
902 actual = xttest_ftom(result); | |
903 expected = xttest_ftom(frequency); | |
904 uint16_t min = expected - 50; | |
905 uint16_t max = expected + 50; | |
906 CAPTURE( actual ); | |
907 CAPTURE( expected ); | |
908 REQUIRE( actual > min ); | |
909 REQUIRE( actual < max ); | |
910 } | |
911 } | |
912 | |
913 WHEN( "the frequency is 344.53125 Hz" ) // period of exactly 128 samples: 8 cycles in the block | |
914 { | |
915 double frequency = 344.53125; | |
916 expected = xttest_ftom(frequency); | |
917 CAPTURE( expected ); | |
918 | |
919 WHEN( "the amplitude is 1.0" ) | |
920 { | |
921 double amplitude = 1.0; | |
922 | |
923 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, amplitude); | |
924 xtract_f0(table, blocksize, &samplerate, &result); | |
925 | |
926 THEN( "the detected F0 is accurate to the nearest quarter-tone" ) | |
927 { | |
928 actual = xttest_ftom(result); | |
929 expected = xttest_ftom(frequency); | |
930 uint16_t min = expected - 50; | |
931 uint16_t max = expected + 50; | |
932 CAPTURE( actual ); | |
933 CAPTURE( expected ); | |
934 REQUIRE( actual > min ); | |
935 REQUIRE( actual < max ); | |
936 } | |
937 } | |
938 | |
939 WHEN( "the amplitude is 0.01" ) // Only test a different amplitude for one case | |
940 { | |
941 double amplitude = 0.01; | |
942 | |
943 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, amplitude); | |
944 xtract_f0(table, blocksize, &samplerate, &result); | |
945 | |
946 THEN( "the detected F0 is accurate to the nearest quarter-tone" ) | |
947 { | |
948 actual = xttest_ftom(result); | |
949 expected = xttest_ftom(frequency); | |
950 uint16_t min = expected - 50; | |
951 uint16_t max = expected + 50; | |
952 CAPTURE( actual ); | |
953 CAPTURE( expected ); | |
954 REQUIRE( actual > min ); | |
955 REQUIRE( actual < max ); | |
956 } | |
957 } | |
958 | |
959 WHEN( "white noise is added at 20%" ) | |
960 { | |
961 double amplitude = 0.2; | |
962 double noise[blocksize]; | |
963 | |
964 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, 1.0 - amplitude); | |
965 xttest_gen_noise(noise, blocksize, amplitude); | |
966 xttest_add(table, noise, blocksize); | |
967 xtract_f0(table, blocksize, &samplerate, &result); | |
968 | |
969 THEN( "the detected F0 is accurate to the nearest quarter-tone" ) | |
970 { | |
971 actual = xttest_ftom(result); | |
972 uint16_t min = expected - 50; | |
973 uint16_t max = expected + 50; | |
974 CAPTURE( actual ); | |
975 REQUIRE( actual > min ); | |
976 REQUIRE( actual < max ); | |
977 } | |
978 } | |
979 | |
980 WHEN( "white noise is added at 40%" ) | |
981 { | |
982 double amplitude = 0.4; | |
983 double noise[blocksize]; | |
984 | |
985 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, 1.0 - amplitude); | |
986 xttest_gen_noise(noise, blocksize, amplitude); | |
987 xttest_add(table, noise, blocksize); | |
988 xtract_f0(table, blocksize, &samplerate, &result); | |
989 | |
990 THEN( "the detected F0 is inaccurate by more than one semitone" ) | |
991 { | |
992 actual = xttest_ftom(result); | |
993 uint16_t difference = abs(expected - actual); | |
994 CAPTURE( actual ); | |
995 REQUIRE( difference > 100 ); | |
996 } | |
997 } | |
998 } | |
999 } | |
1000 | |
1001 GIVEN( "a 2048 sample block with a sample rate of 44100" ) | |
1002 { | |
1003 uint32_t blocksize = 2048; | |
1004 double samplerate = 44100; | |
1005 double result = -1.0; | |
1006 double table[blocksize]; | |
1007 | |
1008 WHEN( "the frequency is 43.06640625 Hz" ) // period of exactly 256 samples: 2 cycles in the block | |
1009 { | |
1010 double frequency = 43.06640625; | |
1011 double amplitude = 1.0; | |
1012 | |
1013 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, amplitude); | |
1014 int rv = xtract_f0(table, blocksize, &samplerate, &result); | |
1015 | |
1016 THEN( "the detected F0 is accurate to the nearest quarter-tone" ) | |
1017 { | |
1018 actual = xttest_ftom(result); | |
1019 expected = xttest_ftom(frequency); | |
1020 uint16_t min = expected - 50; | |
1021 uint16_t max = expected + 50; | |
1022 CAPTURE( actual ); | |
1023 CAPTURE( expected ); | |
1024 REQUIRE( actual > min ); | |
1025 REQUIRE( actual < max ); | |
1026 } | |
1027 } | |
1028 | |
1029 WHEN( "the frequency is 86.1328125 Hz" ) // period of exactly 512 samples: 4 cycles in the block | |
1030 { | |
1031 double frequency = 86.1328125; | |
1032 double amplitude = 1.0; | |
1033 | |
1034 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, amplitude); | |
1035 int rv = xtract_f0(table, blocksize, &samplerate, &result); | |
1036 | |
1037 THEN( "the detected F0 is accurate to the nearest MIDI cent" ) | |
1038 { | |
1039 actual = xttest_ftom(result); | |
1040 expected = xttest_ftom(frequency); | |
1041 CAPTURE( actual ); | |
1042 CAPTURE( expected ); | |
1043 REQUIRE(actual == expected); | |
1044 } | |
1045 } | |
1046 | |
1047 WHEN( "the frequency is 172.265625 Hz" ) // period of exactly 256 samples: 8 cycles in the block | |
1048 { | |
1049 double frequency = 172.265625; | |
1050 double amplitude = 1.0; | |
1051 | |
1052 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, amplitude); | |
1053 xtract_f0(table, blocksize, &samplerate, &result); | |
1054 | |
1055 THEN( "the detected F0 is accurate to the nearest MIDI cent" ) | |
1056 { | |
1057 actual = xttest_ftom(result); | |
1058 expected = xttest_ftom(frequency); | |
1059 CAPTURE( actual ); | |
1060 CAPTURE( expected ); | |
1061 REQUIRE(actual == expected); | |
1062 } | |
1063 } | |
1064 | |
1065 WHEN( "the frequency is 344.53125 Hz" ) // period of exactly 128 samples: 16 cycles in the block | |
1066 { | |
1067 double frequency = 344.53125; | |
1068 | |
1069 WHEN( "the amplitude is 1.0" ) | |
1070 { | |
1071 double amplitude = 1.0; | |
1072 | |
1073 xttest_gen_sawtooth(table, blocksize, samplerate, frequency, amplitude); | |
1074 xtract_f0(table, blocksize, &samplerate, &result); | |
1075 | |
1076 THEN( "the detected F0 is accurate to the nearest MIDI cent" ) | |
1077 { | |
1078 actual = xttest_ftom(result); | |
1079 expected = xttest_ftom(frequency); | |
1080 CAPTURE( actual ); | |
1081 CAPTURE( expected ); | |
1082 REQUIRE(actual == expected); | |
1083 } | |
1084 } | |
1085 } | |
1086 } | |
1087 } | |
1088 |