comparison procedures.cpp @ 11:977f541d6683

GPL and cosmetic changes
author Wen X <xue.wen@elec.qmul.ac.uk>
date Wed, 10 Aug 2011 12:33:35 +0100
parents f0d2c9b5d3a3
children
comparison
equal deleted inserted replaced
10:c6528c38b23c 11:977f541d6683
1 /*
2 Harmonic sinusoidal modelling and tools
3
4 C++ code package for harmonic sinusoidal modelling and relevant signal processing.
5 Centre for Digital Music, Queen Mary, University of London.
6 This file copyright 2011 Wen Xue.
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
12 */
1 //--------------------------------------------------------------------------- 13 //---------------------------------------------------------------------------
2 14
3 #include <math.h> 15 #include <math.h>
4 #include <string.h> 16 #include <string.h>
5 #include <stddef.h> 17 #include <stddef.h>
316 328
317 int Fr=(end-start-Wid)/Offst+1; 329 int Fr=(end-start-Wid)/Offst+1;
318 330
319 for (int i=0; i<Fr; i++) 331 for (int i=0; i<Fr; i++)
320 { 332 {
321 RFFTCW(&data[i*Offst+start], Window, 0, 0, log2(Wid), w, x); 333 RFFTCW(&data[i*Offst+start], Window, 0, 0, Log2(Wid), w, x);
322 334
323 if (Spec) 335 if (Spec)
324 { 336 {
325 for (int j=0; j<Wid/2; j++) 337 for (int j=0; j<Wid/2; j++)
326 Spec[i][j]=sqrt(x[j].x*x[j].x+x[j].y*x[j].y)*amp; 338 Spec[i][j]=sqrt(x[j].x*x[j].x+x[j].y*x[j].y)*amp;
656 saved to dest[], and the last size2+zero sampled are saved to post_buffer[]; if not, the middle size1 668 saved to dest[], and the last size2+zero sampled are saved to post_buffer[]; if not, the middle size1
657 samples are saved to dest[], while pre_buffer[] and post_buffer[] are not used. 669 samples are saved to dest[], while pre_buffer[] and post_buffer[] are not used.
658 */ 670 */
659 void FFTConv(double* dest, double* source1, int size1, double* source2, int size2, int zero, double* pre_buffer, double* post_buffer) 671 void FFTConv(double* dest, double* source1, int size1, double* source2, int size2, int zero, double* pre_buffer, double* post_buffer)
660 { 672 {
661 int order=log2(size2-1)+1+1; 673 int order=Log2(size2-1)+1+1;
662 int Wid=1<<order; 674 int Wid=1<<order;
663 int HWid=Wid/2; 675 int HWid=Wid/2;
664 int Fr=size1/HWid; 676 int Fr=size1/HWid;
665 int res=size1-HWid*Fr; 677 int res=size1-HWid*Fr;
666 bool trunc=false; 678 bool trunc=false;
797 No return value. 809 No return value.
798 */ 810 */
799 void FFTConv(double* dest, double* source1, int size1, double* source2, int HWid, double* pre_buffer) 811 void FFTConv(double* dest, double* source1, int size1, double* source2, int HWid, double* pre_buffer)
800 { 812 {
801 int Wid=HWid*2; 813 int Wid=HWid*2;
802 int order=log2(Wid); 814 int order=Log2(Wid);
803 int Fr=size1/HWid; 815 int Fr=size1/HWid;
804 int res=size1-HWid*Fr; 816 int res=size1-HWid*Fr;
805 817
806 AllocateFFTBuffer(Wid, fft, w, x1); 818 AllocateFFTBuffer(Wid, fft, w, x1);
807 cdouble *x2=new cdouble[Wid]; 819 cdouble *x2=new cdouble[Wid];
898 910
899 No return value. Identical dest and source1 allowed. 911 No return value. Identical dest and source1 allowed.
900 */ 912 */
901 void FFTConv(unsigned char* dest, unsigned char* source1, int bps, int size1, double* source2, int size2, int zero, unsigned char* pre_buffer, unsigned char* post_buffer) 913 void FFTConv(unsigned char* dest, unsigned char* source1, int bps, int size1, double* source2, int size2, int zero, unsigned char* pre_buffer, unsigned char* post_buffer)
902 { 914 {
903 int order=log2(size2-1)+1+1; 915 int order=Log2(size2-1)+1+1;
904 int Wid=1<<order; 916 int Wid=1<<order;
905 int HWid=Wid/2; 917 int HWid=Wid/2;
906 int Fr=size1/HWid; 918 int Fr=size1/HWid;
907 int res=size1-HWid*Fr; 919 int res=size1-HWid*Fr;
908 bool trunc=false; 920 bool trunc=false;
1036 1048
1037 No return value. Identical data and dataout allowed 1049 No return value. Identical data and dataout allowed
1038 */ 1050 */
1039 void FFTFilter(double* dataout, double* data, int Count, int Wid, int On, int Off) 1051 void FFTFilter(double* dataout, double* data, int Count, int Wid, int On, int Off)
1040 { 1052 {
1041 int Order=log2(Wid); 1053 int Order=Log2(Wid);
1042 int HWid=Wid/2; 1054 int HWid=Wid/2;
1043 int Fr=(Count-Wid)/HWid+1; 1055 int Fr=(Count-Wid)/HWid+1;
1044 AllocateFFTBuffer(Wid, ldata, w, x); 1056 AllocateFFTBuffer(Wid, ldata, w, x);
1045 1057
1046 double* win=new double[Wid]; 1058 double* win=new double[Wid];
1108 void FFTFilterOLA(double* dataout, double* data, int Count, int Wid, int On, int Off, double* pre_buffer) 1120 void FFTFilterOLA(double* dataout, double* data, int Count, int Wid, int On, int Off, double* pre_buffer)
1109 { 1121 {
1110 AllocateFFTBuffer(Wid, spec, w, x); 1122 AllocateFFTBuffer(Wid, spec, w, x);
1111 memset(x, 0, sizeof(cdouble)*Wid); 1123 memset(x, 0, sizeof(cdouble)*Wid);
1112 for (int i=On+1; i<Off; i++) x[i].x=x[Wid-i].x=1-2*(i%2); 1124 for (int i=On+1; i<Off; i++) x[i].x=x[Wid-i].x=1-2*(i%2);
1113 CIFFTR(x, log2(Wid), w, spec); 1125 CIFFTR(x, Log2(Wid), w, spec);
1114 FFTConv(dataout, data, Count, spec, Wid, -Wid, pre_buffer, NULL); 1126 FFTConv(dataout, data, Count, spec, Wid, -Wid, pre_buffer, NULL);
1115 FreeFFTBuffer(spec); 1127 FreeFFTBuffer(spec);
1116 }//FFTFilterOLA 1128 }//FFTFilterOLA
1117 //version for integer input and output, where BytesPerSample specifies the integer format. 1129 //version for integer input and output, where BytesPerSample specifies the integer format.
1118 void FFTFilterOLA(unsigned char* dataout, unsigned char* data, int BytesPerSample, int Count, int Wid, int On, int Off, unsigned char* pre_buffer) 1130 void FFTFilterOLA(unsigned char* dataout, unsigned char* data, int BytesPerSample, int Count, int Wid, int On, int Off, unsigned char* pre_buffer)
1119 { 1131 {
1120 AllocateFFTBuffer(Wid, spec, w, x); 1132 AllocateFFTBuffer(Wid, spec, w, x);
1121 memset(x, 0, sizeof(cdouble)*Wid); 1133 memset(x, 0, sizeof(cdouble)*Wid);
1122 for (int i=On+1; i<Off; i++) x[i].x=x[Wid-i].x=1-2*(i%2); 1134 for (int i=On+1; i<Off; i++) x[i].x=x[Wid-i].x=1-2*(i%2);
1123 CIFFTR(x, log2(Wid), w, spec); 1135 CIFFTR(x, Log2(Wid), w, spec);
1124 FFTConv(dataout, data, BytesPerSample, Count, spec, Wid, -Wid, pre_buffer, NULL); 1136 FFTConv(dataout, data, BytesPerSample, Count, spec, Wid, -Wid, pre_buffer, NULL);
1125 FreeFFTBuffer(spec); 1137 FreeFFTBuffer(spec);
1126 }//FFTFilterOLA 1138 }//FFTFilterOLA
1127 1139
1128 /** 1140 /**
1147 x[i].x=x[Wid-i].x=amp[i]*cos(ph[i]); 1159 x[i].x=x[Wid-i].x=amp[i]*cos(ph[i]);
1148 x[i].y=amp[i]*sin(ph[i]); 1160 x[i].y=amp[i]*sin(ph[i]);
1149 x[Wid-i].y=-x[i].y; 1161 x[Wid-i].y=-x[i].y;
1150 } 1162 }
1151 x[HWid].x=amp[HWid], x[HWid].y=0; 1163 x[HWid].x=amp[HWid], x[HWid].y=0;
1152 CIFFTR(x, log2(Wid), w, spec); 1164 CIFFTR(x, Log2(Wid), w, spec);
1153 FFTConv(dataout, data, Count, spec, Wid, -Wid, pre_buffer, NULL); 1165 FFTConv(dataout, data, Count, spec, Wid, -Wid, pre_buffer, NULL);
1154 FreeFFTBuffer(spec); 1166 FreeFFTBuffer(spec);
1155 }//FFTFilterOLA 1167 }//FFTFilterOLA
1156 1168
1157 /** 1169 /**
1165 1177
1166 No return value. 1178 No return value.
1167 */ 1179 */
1168 double FFTMask(double* dataout, double* data, int Count, int Wid, double DigiOn, double DigiOff, double maskcoef) 1180 double FFTMask(double* dataout, double* data, int Count, int Wid, double DigiOn, double DigiOff, double maskcoef)
1169 { 1181 {
1170 int Order=log2(Wid); 1182 int Order=Log2(Wid);
1171 int HWid=Wid/2; 1183 int HWid=Wid/2;
1172 int Fr=(Count-Wid)/HWid+1; 1184 int Fr=(Count-Wid)/HWid+1;
1173 int On=Wid*DigiOn, Off=Wid*DigiOff; 1185 int On=Wid*DigiOn, Off=Wid*DigiOff;
1174 AllocateFFTBuffer(Wid, ldata, w, x); 1186 AllocateFFTBuffer(Wid, ldata, w, x);
1175 1187
2035 */ 2047 */
2036 void MFCC(int FrameWidth, int NumBands, int Ceps_Order, double* Data, double* Bands, double* C, double* Amps, cdouble* W, cdouble* X) 2048 void MFCC(int FrameWidth, int NumBands, int Ceps_Order, double* Data, double* Bands, double* C, double* Amps, cdouble* W, cdouble* X)
2037 { 2049 {
2038 double tmp, b2s, b2c, b2e; 2050 double tmp, b2s, b2c, b2e;
2039 2051
2040 RFFTC(Data, 0, 0, log2(FrameWidth), W, X, 0); 2052 RFFTC(Data, 0, 0, Log2(FrameWidth), W, X, 0);
2041 for (int i=0; i<=FrameWidth/2; i++) Amps[i]=X[i].x*X[i].x+X[i].y*X[i].y; 2053 for (int i=0; i<=FrameWidth/2; i++) Amps[i]=X[i].x*X[i].x+X[i].y*X[i].y;
2042 2054
2043 for (int i=0; i<NumBands; i++) 2055 for (int i=0; i<NumBands; i++)
2044 { 2056 {
2045 tmp=0; 2057 tmp=0;
2304 double* Arg=new double[Count*Pad]; 2316 double* Arg=new double[Count*Pad];
2305 AllocateFFTBuffer(Count*Pad, Amp, w, x); 2317 AllocateFFTBuffer(Count*Pad, Amp, w, x);
2306 memset(Amp, 0, sizeof(double)*Count*Pad); 2318 memset(Amp, 0, sizeof(double)*Count*Pad);
2307 memcpy(&Amp[Count*(Pad-1)/2], data, sizeof(double)*Count); 2319 memcpy(&Amp[Count*(Pad-1)/2], data, sizeof(double)*Count);
2308 ApplyWindow(Amp, Amp, params->Amp, Count); 2320 ApplyWindow(Amp, Amp, params->Amp, Count);
2309 RFFTC(Amp, Amp, Arg, log2(Count*Pad), w, x, 0); 2321 RFFTC(Amp, Amp, Arg, Log2(Count*Pad), w, x, 0);
2310 2322
2311 SmoothPhase(Arg, Count*Pad/2+1); 2323 SmoothPhase(Arg, Count*Pad/2+1);
2312 double result=Arg[Count*Pad/2]-Arg[0]; 2324 double result=Arg[Count*Pad/2]-Arg[0];
2313 delete[] Arg; 2325 delete[] Arg;
2314 FreeFFTBuffer(Amp); 2326 FreeFFTBuffer(Amp);
2620 */ 2632 */
2621 void TFFilter(double* data, double* dataout, int Count, int Wid, TTFSpans* Spans, bool Pass, WindowType wt, double windp, int Sps, int TOffst) 2633 void TFFilter(double* data, double* dataout, int Count, int Wid, TTFSpans* Spans, bool Pass, WindowType wt, double windp, int Sps, int TOffst)
2622 { 2634 {
2623 int HWid=Wid/2; 2635 int HWid=Wid/2;
2624 int Fr=Count/HWid-1; 2636 int Fr=Count/HWid-1;
2625 int Order=log2(Wid); 2637 int Order=Log2(Wid);
2626 2638
2627 int** lspan=new int*[Fr]; 2639 int** lspan=new int*[Fr];
2628 double* lxspan=new double[Fr]; 2640 double* lxspan=new double[Fr];
2629 2641
2630 lspan[0]=new int[Fr*Wid]; 2642 lspan[0]=new int[Fr*Wid];
2740 //version on integer data, where BytesPerSample specified the integer format. 2752 //version on integer data, where BytesPerSample specified the integer format.
2741 void TFFilter(void* data, void* dataout, int BytesPerSample, int Count, int Wid, TTFSpans* Spans, bool Pass, WindowType wt, double windp, int Sps, int TOffst) 2753 void TFFilter(void* data, void* dataout, int BytesPerSample, int Count, int Wid, TTFSpans* Spans, bool Pass, WindowType wt, double windp, int Sps, int TOffst)
2742 { 2754 {
2743 int HWid=Wid/2; 2755 int HWid=Wid/2;
2744 int Fr=Count/HWid-1; 2756 int Fr=Count/HWid-1;
2745 int Order=log2(Wid); 2757 int Order=Log2(Wid);
2746 2758
2747 int** lspan=new int*[Fr]; 2759 int** lspan=new int*[Fr];
2748 double* lxspan=new double[Fr]; 2760 double* lxspan=new double[Fr];
2749 2761
2750 lspan[0]=new int[Fr*Wid]; 2762 lspan[0]=new int[Fr*Wid];