comparison src/scalar.c @ 198:951035634fb1

xtract_hps, made it more in the style of the library and octave error correction bit
author Sean Enderby <sean.enderby@gmail.com>
date Mon, 17 Feb 2014 12:58:28 +0000
parents e764049c0a82
children f6fcf3bec020 fdbef1474be9
comparison
equal deleted inserted replaced
197:02b7b3f96713 198:951035634fb1
806 806
807 return XTRACT_SUCCESS; 807 return XTRACT_SUCCESS;
808 808
809 } 809 }
810 810
811 /*int xtract_hps(const double *data, const int N, const void *argv, double *result) 811 int xtract_hps(const double *data, const int N, const void *argv, double *result)
812 { 812 {
813 813 int n, M, i, peak_index, position1_lwr;
814 int n = N, M, m, l, peak_index, position1_lwr; 814 double tempProduct, peak, largest1_lwr, ratio1;
815 double *coeffs2, *coeffs3, *product, L, 815
816 largest1_lwr, peak, ratio1, sr; 816 n = N / 2;
817 817
818 sr = *(double*)argv; 818 M = ceil(n / 3.0);
819 if(sr == 0) 819
820 sr = 44100.0; 820 if (M <= 1)
821 821 {
822 coeffs2 = (double *)malloc(N * sizeof(double)); 822 /* Input data is too short. */
823 coeffs3 = (double *)malloc(N * sizeof(double)); 823 *result = 0;
824 product = (double *)malloc(N * sizeof(double)); 824 return XTRACT_NO_RESULT;
825 825 }
826 while(n--) coeffs2[n] = coeffs3[n] = 1; 826
827 827 tempProduct = peak = 0;
828 M = N >> 1; 828 for (i = 0; i < M; ++i)
829 L = N / 3.0; 829 {
830 830 tempProduct = data [i] * data [i * 2] * data [i * 3];
831 while(M--) 831
832 { 832 if (tempProduct > peak)
833 m = M << 1; 833 {
834 coeffs2[M] = (data[m] + data[m+1]) * 0.5; 834 peak = tempProduct;
835 835 peak_index = i;
836 if(M < L)
837 {
838 l = M * 3;
839 coeffs3[M] = (data[l] + data[l+1] + data[l+2]) / 3.0;
840 }
841 }
842
843 peak_index = peak = 0;
844
845 for(n = 1; n < N; n++)
846 {
847 product[n] = data[n] * coeffs2[n] * coeffs3[n];
848 if(product[n] > peak)
849 {
850 peak_index = n;
851 peak = product[n];
852 } 836 }
853 } 837 }
854 838
855 largest1_lwr = position1_lwr = 0; 839 largest1_lwr = position1_lwr = 0;
856 840
857 for(n = 0; n < N; n++) 841 for(i = 0; i < N; ++i)
858 { 842 {
859 if(data[n] > largest1_lwr && n != peak_index) 843 if(data[i] > largest1_lwr && i != peak_index)
860 { 844 {
861 largest1_lwr = data[n]; 845 largest1_lwr = data[i];
862 position1_lwr = n; 846 position1_lwr = i;
863 } 847 }
864 } 848 }
865 849
866 ratio1 = data[position1_lwr] / data[peak_index]; 850 ratio1 = data[position1_lwr] / data[peak_index];
867 851
868 if(position1_lwr > peak_index * 0.4 && position1_lwr < 852 if(position1_lwr > peak_index * 0.4 && position1_lwr <
869 peak_index * 0.6 && ratio1 > 0.1) 853 peak_index * 0.6 && ratio1 > 0.1)
870 peak_index = position1_lwr; 854 peak_index = position1_lwr;
871 855
872 *result = sr / (double)peak_index; 856 *result = data [n + peak_index];
873
874 free(coeffs2);
875 free(coeffs3);
876 free(product);
877
878 return XTRACT_SUCCESS;
879 }*/
880
881 int xtract_hps(const double *data, const int N, const void *argv, double *result)
882 {
883 int numBins, numBinsToUse, i, maxIndex;
884 double tempProduct, currentMax;
885
886 numBins = N / 2;
887
888 numBinsToUse = ceil(numBins / 3.0);
889
890 if (numBinsToUse <= 1)
891 {
892 /* Input data is too short. */
893 *result = 0;
894 return XTRACT_NO_RESULT;
895 }
896
897 tempProduct = currentMax = 0;
898 for (i = 0; i < numBinsToUse; ++i)
899 {
900 tempProduct = data [i] * data [i * 2] * data [i * 3];
901
902 if (tempProduct > currentMax)
903 {
904 currentMax = tempProduct;
905 maxIndex = i;
906 }
907 }
908
909 *result = data [numBins + maxIndex];
910 857
911 return XTRACT_SUCCESS; 858 return XTRACT_SUCCESS;
912 } 859 }
913 860
914 int xtract_f0(const double *data, const int N, const void *argv, double *result) 861 int xtract_f0(const double *data, const int N, const void *argv, double *result)