libcm is a C development framework with an emphasis on audio signal processing applications.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

cmProcTest.c 39KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmFloatTypes.h"
  4. #include "cmComplexTypes.h"
  5. #include "cmRpt.h"
  6. #include "cmErr.h"
  7. #include "cmCtx.h"
  8. #include "cmMem.h"
  9. #include "cmMallocDebug.h"
  10. #include "cmLinkedHeap.h"
  11. #include "cmSymTbl.h"
  12. #include "cmAudioFile.h"
  13. #include "cmMidi.h"
  14. #include "cmFile.h"
  15. #include "cmFileSys.h"
  16. #include "cmProcObj.h"
  17. #include "cmProcTemplate.h"
  18. #include "cmProc.h"
  19. #include "cmProc2.h"
  20. #include "cmMath.h"
  21. #include "cmVectOps.h"
  22. #include "cmKeyboard.h"
  23. #include "cmGnuPlot.h"
  24. #include "cmStack.h"
  25. #include "cmRbm.h"
  26. #include <time.h> // time()
  27. void cmTestPrint( cmRpt_t* rpt, const char* fmt, ... )
  28. {
  29. va_list vl;
  30. va_start(vl,fmt);
  31. if(rpt != NULL )
  32. cmRptVPrintf(rpt,fmt,vl);
  33. else
  34. vprintf(fmt,vl);
  35. va_end(vl);
  36. }
  37. void cmStandardizeTest(cmCtx_t* ctx )
  38. {
  39. cmRpt_t* rpt = ctx->err.rpt;
  40. unsigned rn = 3;
  41. unsigned cn = 2;
  42. double m[] = { 4, 5, 6, 7, 8, 9 };
  43. double uV[rn];
  44. double sdV[rn];
  45. cmVOD_PrintL("m", rpt, rn, cn, m );
  46. cmVOD_StandardizeRows(m, rn, cn, uV, sdV );
  47. cmVOD_PrintL("uV", rpt, 1, rn, uV);
  48. cmVOD_PrintL("sdV", rpt, 1, rn, sdV );
  49. cmVOD_PrintL("m", rpt, rn, cn, m );
  50. }
  51. void cmBinMtxFileTest(cmCtx_t* ctx )
  52. {
  53. const char* dataFn = "/home/kevin/temp/cmRbmData0.mtx";
  54. unsigned pointsN = 6; // count of data points in mtx (columns)
  55. unsigned dimN = 4; // dim of binary data matrix (rows)
  56. double probV[] = {.2,.5,.8,.6}; // probabilities of generating a 1.0
  57. assert(sizeof(probV)/sizeof(probV[0]) == dimN);
  58. // alloc the data matrix
  59. double* data0M = cmMemAllocZ( double, dimN*pointsN );
  60. unsigned i,j;
  61. // generate a stochastic binary data matrix according to prob's in probV[]
  62. for(i=0; i<pointsN; ++i)
  63. for(j=0; j<dimN; ++j)
  64. data0M[ i*dimN + j ] = rand() < (probV[j] * RAND_MAX);
  65. // write the binary matrix file
  66. cmBinMtxFileWrite(dataFn, dimN, pointsN, NULL, data0M, NULL, ctx->err.rpt );
  67. // print the original data
  68. cmVOD_PrintL("data0M",&ctx->rpt,dimN,pointsN,data0M);
  69. // read the binary matrix file
  70. unsigned rn,cn,en;
  71. cmRC_t rc0 = cmBinMtxFileSize(ctx,dataFn,&rn,&cn,&en);
  72. double* data1M = cmMemAllocZ(double,rn*cn);
  73. unsigned* colCntV = cmMemAllocZ(unsigned,rn);
  74. cmRC_t rc1 = cmBinMtxFileRead(ctx,dataFn,rn,cn,en,data1M,colCntV);
  75. if( rc0 != cmOkRC || rc1 != cmOkRC )
  76. cmRptPrintf(&ctx->rpt,"Error reading binary matrix file:%s",cmStringNullGuard(dataFn));
  77. // print the binary matrix file
  78. cmVOU_PrintL("colCntV",&ctx->rpt,1,rn,colCntV);
  79. cmVOD_PrintL("data1M", &ctx->rpt,rn,cn,data1M);
  80. cmMemFree(colCntV);
  81. cmMemFree(data0M);
  82. cmMemFree(data1M);
  83. }
  84. void cmFileGetLineTest(cmCtx_t* ctx )
  85. {
  86. const cmChar_t* fn = "/home/kevin/temp/labels.txt";
  87. cmFileH_t f = cmFileNullHandle;
  88. if( cmFileOpen(&f,fn,kReadFileFl,&ctx->rpt) == kOkFileRC )
  89. {
  90. unsigned bufByteCnt = 0;
  91. cmChar_t* buf = NULL;
  92. bool fl = true;
  93. while(fl)
  94. {
  95. switch( cmFileGetLine(f,buf,&bufByteCnt) )
  96. {
  97. case kOkFileRC:
  98. cmRptPrintf(&ctx->rpt,"%i %i %s\n",bufByteCnt,strlen(buf),buf);
  99. break;
  100. case kBufTooSmallFileRC:
  101. buf = cmMemResizeZ(cmChar_t,buf,bufByteCnt);
  102. break;
  103. default:
  104. fl = false;
  105. break;
  106. }
  107. }
  108. cmMemFree(buf);
  109. cmFileClose(&f);
  110. }
  111. }
  112. // Test the cmPvAnl API.
  113. // See cmPvAnlProcTest.m for the equivalent octave code.
  114. void cmPvAnlTest(cmCtx* c )
  115. {
  116. const char* ifn = "/home/kevin/temp/onset0.wav";
  117. const char* ofn = "/home/kevin/temp/test0.aif";
  118. unsigned afChIdx = 0;
  119. unsigned afChCnt = 1;
  120. unsigned afBegSmpIdx = 1000000;
  121. unsigned afEndSmpIdx = 1010000;
  122. unsigned wndSmpCnt = 4096;
  123. unsigned hopSmpCnt = 1024;
  124. unsigned procSmpCnt = hopSmpCnt;
  125. cmAudioFileRd* afRd = cmAudioFileRdAlloc(c,NULL,procSmpCnt,ifn, afChIdx, afBegSmpIdx, afEndSmpIdx );
  126. assert(afRd != NULL );
  127. cmAudioFileWr* afWr = cmAudioFileWrAlloc(c,NULL,procSmpCnt,ofn, afRd->info.srate,afChCnt,afRd->info.bits );
  128. assert(afWr != NULL );
  129. cmPvAnl* pvAnl = cmPvAnlAlloc(c,NULL,procSmpCnt,afRd->info.srate,wndSmpCnt,hopSmpCnt, kNoCalcHzPvaFl );
  130. assert(pvAnl != NULL);
  131. while( cmAudioFileRdRead(afRd) != cmEofRC )
  132. {
  133. while( cmPvAnlExec(pvAnl,afRd->outV, afRd->outN ) )
  134. {
  135. printf(" : %f %i\n",cmVOR_Sum(pvAnl->magV,pvAnl->binCnt),pvAnl->binCnt);
  136. }
  137. cmAudioFileWrExec(afWr,0,afRd->outV,afRd->outN);
  138. }
  139. cmPvAnlFree(&pvAnl);
  140. cmAudioFileWrFree(&afWr);
  141. cmAudioFileRdFree(&afRd);
  142. }
  143. void cmAudioFileProcTest(cmCtx_t* ctx)
  144. {
  145. //const cmChar_t* aifFn = "/home/kevin/media/audio/sourcetone/00-11-060-I-Shapeshifter-TranquilVapor.aiff";
  146. //const cmChar_t* wavFn = "/home/kevin/temp/mas/onset_conv/Piano 3_01.aif";
  147. const cmChar_t* wavFn = "/home/kevin/temp/mas/onsets/Piano 3_01.wav";
  148. //const cmChar_t* wavFn = "/home/kevin/temp/onsetsConv0.aif";
  149. //const cmChar_t* wavFn = "/home/kevin/media/audio/20110723-Kriesberg/Audio Files/Piano 3_01.aif";
  150. const cmChar_t* fn = wavFn;
  151. cmAudioFileInfo_t afInfo;
  152. cmRC_t cmRC;
  153. cmAudioFileH_t afH = cmAudioFileNewOpen( fn, &afInfo, &cmRC, &ctx->rpt );
  154. if( cmRC != kOkAfRC )
  155. printf("Unable to open the audio file:%s\n",fn);
  156. else
  157. {
  158. //cmAudioFileReport( afH, &ctx->rpt, 9785046, 100);
  159. //cmAudioFileReport( afH, &ctx->rpt, 15134420, 100); // onset_conv/Piano 3_01.aif
  160. //cmAudioFileReport( afH, &ctx->rpt, 12862654, 100); // Audio Files/Piano 3_01.wav
  161. cmAudioFileReport( afH, &ctx->rpt, 96092658, 100); // onsets/Piano 3_01.wav
  162. cmAudioFileDelete(&afH);
  163. }
  164. }
  165. // This code test the af
  166. void cmAudioFileReadWriteTest(cmCtx_t* ctx )
  167. {
  168. const cmChar_t* inDir = "/home/kevin/src/cm/src/data/audio_file_format_test";
  169. const cmChar_t* outDir = "/home/kevin/temp/af1/out";
  170. unsigned dirCnt = 0;
  171. unsigned i = 0;
  172. if( cmPlotInitialize(NULL) != cmOkRC )
  173. return;
  174. if( cmFsIsDir(outDir) == false )
  175. cmFsMkDir(outDir);
  176. // get the files in the input directory
  177. cmFileSysDirEntry_t* dep = cmFsDirEntries( inDir, kFileFsFl | kFullPathFsFl, &dirCnt );
  178. for(i=0; dep != NULL && i<dirCnt; ++i)
  179. {
  180. cmAudioFileH_t afH;
  181. cmAudioFileInfo_t afInfo;
  182. cmRC_t cmRC;
  183. // open the ith file in the input directory
  184. if( cmAudioFileIsValid( afH = cmAudioFileNewOpen( dep[i].name, &afInfo, &cmRC, &ctx->rpt)) == false)
  185. {
  186. cmRptPrintf(&ctx->rpt,"Audio file open error occurred on %s\n",dep[i].name);
  187. continue;
  188. }
  189. cmFileSysPathPart_t* pp;
  190. unsigned smpCnt = afInfo.frameCnt * afInfo.chCnt; // count of samples to read and write
  191. cmSample_t* buf = cmMemAlloc( cmSample_t, smpCnt ); // allocate the sample buffer
  192. cmSample_t* bp[ afInfo.chCnt];
  193. unsigned actualFrmCnt;
  194. unsigned chIdx = 0;
  195. unsigned j;
  196. // initialize the audio channel buffer
  197. for(j=0; j<afInfo.chCnt; ++j)
  198. bp[j] = buf + (j*afInfo.frameCnt);
  199. // parse the input file name
  200. if((pp = cmFsPathParts(dep[i].name)) == NULL )
  201. cmRptPrintf(&ctx->rpt,"Unable to locate the file parts for '%s'.",dep[i].name);
  202. else
  203. {
  204. // use the input file name to form the output file name
  205. const cmChar_t* outFn = cmFsMakeFn(outDir,pp->fnStr,"bin",NULL);
  206. const cmChar_t* audOutFn = cmFsMakeFn(outDir,pp->fnStr,"aif",NULL);
  207. cmAudioFileH_t aofH = cmAudioFileNewCreate(audOutFn, afInfo.srate, afInfo.bits, afInfo.chCnt, &cmRC, &ctx->rpt);
  208. // read the entire audio file into the sample buffer
  209. if( cmAudioFileReadSample(afH,afInfo.frameCnt,chIdx,afInfo.chCnt,bp, &actualFrmCnt ) != kOkAfRC )
  210. cmRptPrintf(&ctx->rpt,"Audio file read error occurred on %s\n",dep[i].name);
  211. // write the audio file out as a binary matrix file
  212. if( cmBinMtxFileWrite(outFn, actualFrmCnt, afInfo.chCnt, buf, NULL, NULL, &ctx->rpt ) != cmOkRC )
  213. cmRptPrintf(&ctx->rpt,"Binary matrix write failed on '%s'\n", outFn );
  214. // write the audio output file
  215. if( cmAudioFileIsValid(aofH) )
  216. if( cmAudioFileWriteSample(aofH,afInfo.frameCnt,afInfo.chCnt, bp ) != cmOkRC)
  217. cmRptPrintf(&ctx->rpt,"Audio output file write failed on '%s'.", audOutFn);
  218. // plot the audio file signals
  219. cmPlotSetup(pp->fnStr,1,1);
  220. for(j=0; j<afInfo.chCnt; ++j)
  221. cmPlotLineS(NULL,NULL,bp[j],NULL,afInfo.frameCnt,NULL,kSolidPlotLineId);
  222. cmPlotDraw();
  223. //cmKeyPress(NULL);
  224. // close the output audio file
  225. if( cmAudioFileIsValid(aofH) )
  226. cmAudioFileDelete(&aofH);
  227. cmFsFreeFn(audOutFn); // release the output audio file name
  228. cmFsFreeFn(outFn); // release the output file name
  229. cmFsFreePathParts(pp); // release the parse recd
  230. }
  231. cmAudioFileDelete(&afH); // release the audio file
  232. cmMemPtrFree(&buf); // release the audio buffer
  233. }
  234. cmFsDirFreeEntries(dep);
  235. cmPlotFinalize();
  236. }
  237. void cmZeroCrossTest( cmRpt_t* rpt )
  238. {
  239. double srate = 32;
  240. unsigned vn = srate * 2;
  241. cmSample_t v6[ vn ];
  242. unsigned impPhs = 0;
  243. double hz = 2;
  244. cmSample_t d = 0;
  245. impPhs = cmVOS_SynthSine( v6, vn, impPhs, srate, hz );
  246. cmVOS_MultVS( v6, vn, -1 );
  247. unsigned zc = cmVOS_ZeroCrossCount( v6, vn, &d);
  248. cmTestPrint(rpt,"zero cross: %i \n",zc);
  249. cmVOS_Print( rpt, 1, vn, v6 );
  250. }
  251. void cmMelTest(cmRpt_t* rpt)
  252. {
  253. double srate = 44100;
  254. unsigned binCnt = 513;
  255. unsigned bandCnt = 36;
  256. cmSample_t t[ binCnt * bandCnt ];
  257. cmCtx* c = cmCtxAlloc(NULL,rpt,cmLHeapNullHandle,cmSymTblNullHandle);
  258. cmMatrixBuf* m = cmMatrixBufAlloc( c, NULL, binCnt, bandCnt );
  259. cmVOS_MelMask( m->bufPtr, bandCnt, binCnt, srate, kShiftMelFl );
  260. cmVOS_Transpose( t, m->bufPtr, bandCnt, binCnt );
  261. cmPlotSetup("Test Proc Impl",1,1);
  262. unsigned i;
  263. double sum = 0;
  264. for(i=0; i<bandCnt; ++i )
  265. {
  266. sum += cmVOS_Sum( t + (i*binCnt), binCnt );
  267. cmPlotLineS( NULL, NULL, t + (i*binCnt), NULL, 35, NULL, kSolidPlotLineId );
  268. }
  269. printf("sum:%f\n",sum);
  270. cmPlotDraw();
  271. cmKeyPress(NULL);
  272. cmCtxFree(&c);
  273. cmMatrixBufFree( &m);
  274. }
  275. void cmDctTest()
  276. {
  277. unsigned coeffCnt = 20;
  278. unsigned filtCnt = 36;
  279. cmSample_t m[ coeffCnt * filtCnt ];
  280. cmSample_t t[ coeffCnt * filtCnt ];
  281. cmVOS_DctMatrix( m, coeffCnt, filtCnt );
  282. cmVOS_Transpose( t, m, coeffCnt, filtCnt );
  283. cmPlotSetup("Test",1,1);
  284. unsigned i;
  285. for(i=0; i<coeffCnt; ++i )
  286. {
  287. cmPlotLineS( NULL, NULL, t+(i*filtCnt), NULL, filtCnt, NULL, kSolidPlotLineId );
  288. }
  289. cmPlotDraw();
  290. cmKeyPress(NULL);
  291. }
  292. void cmMtxMultTest(cmRpt_t* rpt)
  293. {
  294. unsigned mrn = 3;
  295. unsigned mcn = 2;
  296. unsigned vn = mcn;
  297. unsigned on = mrn;
  298. double m[] = { 1, 2, 3, 4, 5, 6 };
  299. double v[] = { 1, 2 };
  300. double o[ on ];
  301. cmVOD_MultVMV( o, mrn, m, mcn, v );
  302. cmVOD_PrintL( "o:\n", rpt, 1, on, o );
  303. cmVOD_MultVVM( o, on, v, vn, m );
  304. cmVOD_PrintL( "o:\n", rpt, 1, on, o );
  305. cmReal_t A[] = { 8, 3, 4, 1, 5, 9, 6, 7, 2 }; // magic(3)
  306. cmReal_t B[] = { 1, 2, 3, 4, 5, 6};
  307. unsigned arn = 3;
  308. unsigned cmn = 3;
  309. unsigned brn = cmn;
  310. unsigned bcn = 2;
  311. cmReal_t D[arn*bcn];
  312. cmVOR_MultMMM( D, arn, bcn, A, B, brn );
  313. cmVOR_PrintL("D:\n",rpt,arn,bcn,D);
  314. // D = B*At
  315. cmVOR_MultMMMt( D, bcn, arn, B, A, cmn );
  316. cmVOR_PrintL("Dt:\n",rpt,bcn,arn,D);
  317. // D += B*At
  318. cmVOR_MultMMM1( D, bcn, arn, 1.0, B, A, cmn, 1.0, kTransposeM1Fl );
  319. cmVOR_PrintL("Dt:\n",rpt,bcn,arn,D);
  320. // D = B*At - with explicit physical row counts for each matrix
  321. cmVOR_MultMMM2( D, bcn, arn, 1.0, B, A, cmn, 0.0, kTransposeM1Fl, bcn, bcn, cmn );
  322. cmVOR_PrintL("Dt:\n",rpt,bcn,arn,D);
  323. // D = 3*B*At - with explicit physical row counts for each matrix
  324. cmVOR_MultMMM2( D, bcn, arn, 3.0, B, A, cmn, 0.0, kTransposeM1Fl, bcn, bcn, cmn );
  325. cmVOR_PrintL("Dt:\n",rpt,bcn,arn,D);
  326. /// dpb[dn] = mp[mrn,dn] * vp[mrn]
  327. double v1[] = {1, 2, 3};
  328. cmVOD_MultVMtV( o, mcn, m, mrn, v1 );
  329. cmVOD_PrintL( "o:\n", rpt, 1, on, o );
  330. cmTestPrint(rpt,"multsum: %f\n", cmVOR_MultSumVV( A, B, 6 ));
  331. }
  332. void cmShiftRotateTest(cmRpt_t* rpt)
  333. {
  334. unsigned vn = 5;
  335. double v[] = { 0, 1, 2, 3, 4 };
  336. cmVOD_Print( rpt, 1, vn, v );
  337. cmVOD_Rotate( v, vn, 2 );
  338. cmVOD_Print( rpt, 1, vn, v );
  339. cmVOD_Rotate( v, vn, -3 );
  340. cmVOD_Print( rpt, 1, vn, v );
  341. cmVOD_Shift( v, vn, 2, 9 );
  342. cmVOD_Print( rpt, 1, vn, v );
  343. cmVOD_Shift( v, vn, -2, 9 );
  344. cmVOD_Print( rpt, 1, vn, v );
  345. unsigned pn = 6;
  346. double p[] = { 0, 1, 0, 1, 0, 1 };
  347. unsigned pi[5];
  348. unsigned pm = cmVOD_PeakIndexes( pi, 5, p, pn, 1.1 );
  349. printf("%i : ",pm);
  350. cmVOU_Print( rpt, 1, pm, pi );
  351. cmReal_t m0[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  352. cmReal_t m1[9];
  353. cmVOD_PrintL("m0:\n",rpt,3,3,m0);
  354. cmVOR_RotateM( m1, 3,3, m0, 3, -2 );
  355. cmVOD_PrintL("m1:\n",rpt,3,3,m1);
  356. }
  357. void cmRandomTest(cmRpt_t* rpt )
  358. {
  359. cmPlotSetup("Random",1,1);
  360. unsigned i;
  361. unsigned yn = 1000;
  362. unsigned y[yn];
  363. cmReal_t w[] = { .2, .3, .5};
  364. // seed the random number generator with a clock with seconds resolution
  365. srand((unsigned)time(NULL));
  366. unsigned wn = sizeof(w)/sizeof(w[0]);
  367. unsigned h[wn];
  368. cmVOR_WeightedRandInt(y,yn,w,wn);
  369. cmVOU_Hist(h,wn,y,yn);
  370. cmVOU_Print(rpt,1,wn,h);
  371. for(i=0; i<wn; ++i)
  372. printf("%f ",(float)h[i]/yn);
  373. printf("\n");
  374. }
  375. void cmTestGaussWin(cmRpt_t* rpt)
  376. {
  377. unsigned vn = 10;
  378. double v[ vn ];
  379. unsigned n = 4;
  380. cmVOD_GaussWin(v,n,5.0/3.0);
  381. cmVOD_Print(rpt,1,n,v);
  382. }
  383. // meanV[K] and varV[K] identify the center and variance of K clusters
  384. // kV[xcn] identifes the cluster associated with each data point
  385. // Each column of xM[] contains a single 2D data point (xrn is therefore always 2)
  386. void cmPlotGauss2D( cmRpt_t* rpt, const cmReal_t* meanV, const cmReal_t* varV, const cmReal_t* xM, unsigned xD, unsigned xN, const char* colorStr )
  387. {
  388. cmReal_t txM[ xD*xN ];
  389. cmVOR_Transpose(txM,xM,xD,xN);
  390. cmPlotLineR( NULL, txM, txM + xN, NULL, xN, colorStr, kCirclePlotPtId );
  391. unsigned cn = 100;
  392. cmReal_t cV[ cn*2 ];
  393. cmVOR_CircleCoords( cV, cn, meanV[0], meanV[1], varV[0], varV[1] );
  394. cmPlotLineR( NULL, cV, cV+cn, NULL, cn, colorStr, kSolidPlotLineId );
  395. cmPlotLineR( NULL, meanV, meanV+1, NULL, 1, colorStr, kAsteriskPlotPtId );
  396. printf("%s\n",colorStr);
  397. cmVOR_Print(rpt,xN,xD,txM);
  398. }
  399. cmReal_t _cmProcTestKmeansDist( void* userPtr, const cmReal_t* v0, const cmReal_t* v1, unsigned vn )
  400. { return cmVOR_EuclidDistance(v0,v1,vn); }
  401. void cmGaussTest(cmRpt_t* rpt )
  402. {
  403. cmReal_t minV = -1;
  404. cmReal_t incr = .1;
  405. unsigned xn = ((fabs(minV)-minV)/incr)+2;
  406. cmReal_t x0[xn],y0[xn];
  407. cmVOR_Seq(x0,xn,minV,incr);
  408. //cmVOR_Print(rpt,1,xn,x0);
  409. cmVOR_GaussPDF( y0, xn, x0, 0, 1 );
  410. cmPlotSetup("Gauss",1,1);
  411. //cmPlotLineR( NULL, NULL, y0, NULL, xn, NULL, kSolidPlotLineId );
  412. unsigned x1N = 10;
  413. unsigned x1D = 2;
  414. cmReal_t m1V[] = { 0, 0 };
  415. cmReal_t v1V[] = { .5,.5 };
  416. cmReal_t x1[ x1D * x1N ];
  417. cmVOR_RandomGaussM( x1, x1D, x1N, m1V, v1V );
  418. //cmPlotGauss2D(rpt,m1V,v1V,x1,x1D,x1N,"magenta");
  419. unsigned x2N = 5; // data points per cluster
  420. unsigned x2D = 2; // data dimensions
  421. unsigned x2K = 3; // count of clusters
  422. cmReal_t m2M[] = { 0, 0, 1, 1, 2, 2 }; // cluster means
  423. cmReal_t v2M[] = { .5,.5, .5, .5, .5, .5 }; // cluster variances
  424. cmReal_t x2[ x2D * x2N * x2K ]; // data matrix
  425. char x2c[][6] = { "red","green","blue" };
  426. cmVOR_RandomGaussMM( x2, x2D, x2N*x2K, m2M, v2M, x2K );
  427. cmVOR_Print(rpt,x2D,x2N*x2K,x2);
  428. unsigned k = 0;
  429. //for(k=0; k<x2K; ++k)
  430. // cmPlotGauss2D( rpt, m2M + (k*x2D), v2M + (k*x2D), x2 + (k*x2D*x2N), x2D, x2N, x2c[k] );
  431. unsigned classIdxV[ x2N*x2K ]; // kmeans generated point assignment vector
  432. cmReal_t centroidM[ x2D*x2K ]; // kmeans generated centroids
  433. // classify each point using kmeans
  434. unsigned iterCnt = cmVOR_Kmeans(classIdxV, centroidM, x2K, x2, x2D, x2N*x2K, NULL, 0, false, _cmProcTestKmeansDist, NULL );
  435. cmTestPrint(rpt,"kmeans iterations:%i\n",iterCnt);
  436. // plot kmeans clusters
  437. for(k=0; k<x2K; ++k)
  438. {
  439. cmReal_t pM[ x2D * x2N ];
  440. cmReal_t dV[ x2D ];
  441. cmReal_t vV[ x2D ];
  442. unsigned i,j=0;
  443. cmVOR_Fill(vV,x2D,0);
  444. // for each data point in cluster k
  445. for(i=0; i<x2N*x2K; ++i)
  446. if( classIdxV[i] == k )
  447. {
  448. // store the data point for later plotting
  449. cmVOR_Copy( pM+(j++*x2D), x2D, x2 + (i*x2D) );
  450. // calculate the variance
  451. cmVOR_SubVVV( dV, x2D, centroidM + (k*x2D), x2 + (i*x2D ));
  452. cmVOR_MultVV( dV, x2D, dV);
  453. cmVOR_AddVV( vV, x2D, dV );
  454. }
  455. // normalize the variance
  456. if( j - 1 > 0 )
  457. cmVOR_DivVS( vV, x2D, j-1 );
  458. // plot data points in cluster k
  459. cmPlotGauss2D( rpt, centroidM + (k*x2D), vV, pM, x2D, j, x2c[k] );
  460. }
  461. cmPlotDraw();
  462. cmKeyPress(NULL);
  463. }
  464. void cmFPExceptTest()
  465. {
  466. double n = DBL_EPSILON;
  467. double d = DBL_MAX;
  468. //double r0 = sqrt(-1.0);
  469. double r0 = n/d;
  470. printf("%e\n",r0);
  471. }
  472. void cmLaTest(cmRpt_t* rpt)
  473. {
  474. unsigned a0n = 3;
  475. unsigned a1n = 4;
  476. cmReal_t A0[] = { 1,4,7,2,5,8,3,6,9 };
  477. cmReal_t A1[] = { 1,0,0,0, 0,-2,0,0, 0,0,3,0, 0,0,0,4 };
  478. cmReal_t t[ a1n*a1n ];
  479. cmReal_t det = cmVOR_DetM(A0,a0n);
  480. printf("det:%e\n",det);
  481. cmVOR_InvM(A0,a0n);
  482. cmVOR_PrintE(rpt,a0n,a0n,A0);
  483. det = cmVOR_DetDiagM(A1,a1n);
  484. printf("det:%e\n",det);
  485. cmVOR_InvDiagM(A1,a1n);
  486. cmVOR_PrintE(rpt,a1n,a1n,A1);
  487. cmReal_t A[] = { 8, 3, 4, 1, 5, 9, 6, 7, 2 }; // magic(3)
  488. cmReal_t B[] = { 1, 2, 3, 4, 5, 6};
  489. //cmVOR_SolveLS(A,3,B,2);
  490. cmVOR_Print(rpt,3,2,B);
  491. cmReal_t D[2*3];
  492. cmVOR_MultMMM( D, 2, 3, B, A, 3 );
  493. cmVOR_Print(rpt,2,3,D);
  494. cmVOR_RandSymPosDef( A1, a1n, t );
  495. cmVOR_PrintL("A1:\n",rpt,a1n,a1n,A1);
  496. cmVOR_Chol(A1,a1n);
  497. cmVOR_PrintL("A1:\n",rpt,a1n,a1n,A1);
  498. cmVOR_RandSymPosDef( A1, a1n, t );
  499. cmVOR_PrintL("A1:\n",rpt,a1n,a1n,A1);
  500. cmVOR_CholZ(A1,a1n);
  501. cmVOR_PrintL("A1:\n",rpt,a1n,a1n,A1);
  502. }
  503. typedef struct
  504. {
  505. const cmReal_t* xM; // matrix base
  506. unsigned D; // row cnt
  507. unsigned N; // col cnt
  508. } cmTestReadFuncData_t;
  509. const cmReal_t* cmTestReadFunc( void* userPtr, unsigned colIdx )
  510. {
  511. cmTestReadFuncData_t* p = (cmTestReadFuncData_t*)userPtr;
  512. assert( userPtr != NULL && colIdx < p->N );
  513. return p->xM + (colIdx*p->D);
  514. }
  515. void cmMvnProbTest(cmRpt_t* rpt)
  516. {
  517. unsigned D = 2;
  518. unsigned N = 3;
  519. cmReal_t uV[] = { 0, 0 };
  520. cmReal_t sM[] = { 1, 0, 0, 1 };
  521. cmReal_t xM[] = { 1, 2, 3, 4, 5, 6 };
  522. cmReal_t yV[ N ];
  523. cmTestReadFuncData_t r;
  524. cmVOR_MultVarGaussPDF( yV, xM, uV, sM, D, N, false );
  525. //cmVOR_PrintE(rpt,1,N,yV);
  526. cmVOR_RandSymPosDef(sM, D, NULL );
  527. //cmVOR_PrintL("sM:\n",rpt,D,D,sM);
  528. cmVOR_RandomGaussNonDiagM( xM, D, N, uV, sM, NULL );
  529. //cmVOR_PrintL("yM:\n",rpt,D,N,xM);
  530. cmReal_t S[] ={ 0.67462, 0.49828, 0.49828, 0.36804 };
  531. cmVOR_MultVS(S,N*N,10);
  532. cmReal_t mu[] = {0, 0};
  533. cmReal_t logDet = cmVOR_LogDetM(S,D);
  534. cmReal_t x[] = {-.1, -.1, 0, 0, .1, .1};
  535. bool diagFl = false;
  536. cmVOR_InvM(S,D);
  537. r.xM = x;
  538. r.D = D;
  539. r.N = N;
  540. cmVOR_MultVarGaussPDF3( yV, cmTestReadFunc, &r, mu, S, logDet, D, N, diagFl );
  541. cmVOR_PrintL("pr:\n",rpt,1,N,yV);
  542. cmVOR_MultVarGaussPDF2( yV, x, mu, S, logDet, D, N, diagFl );
  543. cmVOR_PrintL("pr:\n",rpt,1,N,yV);
  544. }
  545. void cmCovarTest(cmRpt_t* rpt)
  546. {
  547. unsigned D = 2;
  548. unsigned N = 1000;
  549. cmReal_t xM[D*N];
  550. cmReal_t uV[D];
  551. cmReal_t sM[D*D];
  552. cmReal_t scM[D*D];
  553. srand((unsigned)time(NULL));
  554. if(1)
  555. {
  556. cmVOR_RandSymPosDef(sM,D,NULL);
  557. cmVOR_Random(uV,D,0,1);
  558. }
  559. if(0)
  560. {
  561. sM[0] = .1;
  562. sM[1] = 0;
  563. sM[2] = 0;
  564. sM[3] = .1;
  565. }
  566. if(0)
  567. {
  568. uV[0] = 0;
  569. uV[1] = 0;
  570. sM[0] = .48533;
  571. sM[1] = .27140;
  572. sM[2] = .27140;
  573. sM[3] = .15191;
  574. }
  575. cmVOR_RandomGaussNonDiagM(xM,D,N,uV,sM,NULL);
  576. cmVOR_GaussCovariance(scM,D,xM,N,NULL,NULL,0);
  577. //cmVOR_PrintL("xM:\n", rpt, D,N,xM);
  578. cmVOR_PrintL("uV: ", rpt, 1, D, uV);
  579. cmVOR_PrintL("sM:\n",rpt, D, D, sM);
  580. cmVOR_PrintL("covar:\n",rpt, D,D,scM);
  581. }
  582. const cmReal_t* cmCovarSrcFunc( void* p, unsigned idx )
  583. { return ((const cmReal_t*)p) + 3*idx; }
  584. void cmCovarTest2(cmRpt_t* rpt )
  585. {
  586. const int D = 3;
  587. const int N = 10;
  588. // each data point is in a column of xM[]
  589. cmReal_t xM[] = {0.18621, 0.39466, 0.29122, 0.49663, 0.58397, 0.98434, 0.26542, 0.88850, 0.10009, 0.18815, 0.42153, 0.30218, 0.56357, 0.55696, 0.50647, 0.64502, 0.78920, 0.70395, 0.88892, 0.26669, 0.27277, 0.74299, 0.32620, 0.89648, 0.99930, 0.78351, 0.35355, 0.86343, 0.87964, 0.21095};
  590. cmReal_t sM[ D * D ];
  591. cmVOR_GaussCovariance2(sM,D,cmCovarSrcFunc,N,xM,NULL,NULL,0);
  592. cmVOR_PrintL("1 covar: ",rpt, D,D,sM);
  593. cmVOR_GaussCovariance(sM,D,xM,N,NULL,NULL,0);
  594. cmVOR_PrintL("2 covar: ",rpt, D,D,sM);
  595. /*
  596. m = [
  597. 0.18621 0.39466 0.29122;
  598. 0.49663 0.58397 0.98434;
  599. 0.26542 0.88850 0.10009;
  600. 0.18815 0.42153 0.30218;
  601. 0.56357 0.55696 0.50647;
  602. 0.64502 0.78920 0.70395;
  603. 0.88892 0.26669 0.27277;
  604. 0.74299 0.32620 0.89648;
  605. 0.99930 0.78351 0.35355;
  606. 0.86343 0.87964 0.21095;
  607. ]
  608. octave> cov(m)
  609. ans =
  610. 0.0885575 0.0092695 0.0123219
  611. 0.0092695 0.0546553 -0.0168115
  612. 0.0123219 -0.0168115 0.0909342
  613. */
  614. }
  615. void cmMahalanobisTest(cmRpt_t* rpt )
  616. {
  617. const int D = 3;
  618. const int N = 10;
  619. // each data point is in a column of xM[]
  620. cmReal_t xM[] = {0.18621, 0.39466, 0.29122, 0.49663, 0.58397, 0.98434, 0.26542, 0.88850, 0.10009, 0.18815, 0.42153, 0.30218, 0.56357, 0.55696, 0.50647, 0.64502, 0.78920, 0.70395, 0.88892, 0.26669, 0.27277, 0.74299, 0.32620, 0.89648, 0.99930, 0.78351, 0.35355, 0.86343, 0.87964, 0.21095};
  621. cmReal_t uV[D];
  622. //cmReal_t xV[] = {0.633826, 0.349463, 0.053582 };
  623. cmReal_t xV[] = {0.72477, 0.98973, 0.11622};
  624. cmReal_t sM[ D * D ];
  625. // find the mean of the data set (mean across the columns)
  626. cmVOR_Mean2( uV, cmCovarSrcFunc, D, N, xM );
  627. cmVOR_PrintL("mean: ",rpt, 1, D, uV );
  628. cmVOR_GaussCovariance(sM,D,xM,N,uV,NULL,0);
  629. cmVOR_PrintL("covar: ",rpt, D,D,sM);
  630. cmReal_t* r = cmVOR_InvM(sM,D);
  631. cmVOR_PrintL("inv covar: ",rpt, D,D,sM);
  632. cmReal_t d = cmVOR_MahalanobisDistance( xV, D, uV, sM );
  633. cmRptPrintf(rpt,"Mahalanobis dist:%f %p\n",d,r);
  634. /*
  635. octave>m =
  636. 0.18621 0.39466 0.29122
  637. 0.49663 0.58397 0.98434
  638. 0.26542 0.88850 0.10009
  639. 0.18815 0.42153 0.30218
  640. 0.56357 0.55696 0.50647
  641. 0.64502 0.78920 0.70395
  642. 0.88892 0.26669 0.27277
  643. 0.74299 0.32620 0.89648
  644. 0.99930 0.78351 0.35355
  645. 0.86343 0.87964 0.21095
  646. octave> u = mean(m)
  647. u = 0.58396 0.58908 0.46220
  648. octave> y
  649. y = 0.633826 0.349463 0.053582
  650. octave> sqrt((y-u)*inv(cov(m))*(y-u)')
  651. ans = 2.0322
  652. */
  653. }
  654. void cmRandIntSeqTest(cmRpt_t* rpt)
  655. {
  656. unsigned vn = 10;
  657. unsigned v[vn];
  658. unsigned i = 0;
  659. for(i=0; i<10; ++i)
  660. {
  661. cmVOU_RandomSeq(v,vn);
  662. cmVOU_PrintL("v: ", rpt, 1, vn, v );
  663. }
  664. }
  665. //------------------------------------------------------------------------------------------------------------
  666. void cmSynthTest()
  667. {
  668. unsigned vn = 128;
  669. unsigned blkN = 2;
  670. cmSample_t v0[ vn*blkN ];
  671. cmSample_t v1[ vn*blkN ];
  672. cmSample_t v2[ vn*blkN ];
  673. cmSample_t v3[ vn*blkN ];
  674. cmSample_t v4[ vn*blkN ];
  675. cmSample_t v5[ vn*blkN ];
  676. cmSample_t v6[ vn*blkN ];
  677. cmSample_t v7[ vn*blkN ];
  678. double srate = vn;
  679. double hz = 1;
  680. unsigned sinPhs = 0,cosPhs = 0, sqrPhs=0, sawPhs=0, triPhs=0, pulPhs=0, impPhs=0, phsPhs=0;
  681. unsigned otCnt = 7;
  682. unsigned i;
  683. for(i=0; i<blkN; ++i)
  684. {
  685. sinPhs = cmVOS_SynthSine( v0+(i*vn), ((i+1)*vn), sinPhs, srate, hz );
  686. cosPhs = cmVOS_SynthCosine( v1+(i*vn), ((i+1)*vn), cosPhs, srate, hz );
  687. sqrPhs = cmVOS_SynthSquare( v2+(i*vn), ((i+1)*vn), sqrPhs, srate, hz, otCnt );
  688. sawPhs = cmVOS_SynthSawtooth( v3+(i*vn), ((i+1)*vn), sawPhs, srate, hz, otCnt );
  689. triPhs = cmVOS_SynthTriangle( v4+(i*vn), ((i+1)*vn), triPhs, srate, hz, otCnt );
  690. pulPhs = cmVOS_SynthPulseCos( v5+(i*vn), ((i+1)*vn), pulPhs, srate, hz, otCnt );
  691. impPhs = cmVOS_SynthImpulse( v6+(i*vn), ((i+1)*vn), impPhs, srate, hz );
  692. phsPhs = cmVOS_SynthPhasor( v7+(i*vn), ((i+1)*vn), phsPhs, srate, hz );
  693. }
  694. cmPlotSetup("Test Proc Impl",2,1);
  695. cmPlotLineS( "cos", NULL, v1, NULL, vn*blkN, NULL, kSolidPlotLineId );
  696. cmPlotLineS( "sqr", NULL, v2, NULL, vn*blkN, NULL, kSolidPlotLineId );
  697. cmPlotLineS( "imp", NULL, v6, NULL, vn*blkN, NULL, kSolidPlotLineId );
  698. cmPlotLineS( "tri", NULL, v4, NULL, vn*blkN, NULL, kSolidPlotLineId );
  699. cmPlotSelectSubPlot( 1, 0 );
  700. cmPlotLineS( "sin", NULL, v0, NULL, vn*blkN, NULL, kSolidPlotLineId );
  701. cmPlotLineS( "saw", NULL, v3, NULL, vn*blkN, NULL, kSolidPlotLineId );
  702. cmPlotLineS( "pul", NULL, v5, NULL, vn*blkN, NULL, kSolidPlotLineId );
  703. cmPlotLineS( "phs", NULL, v7, NULL, vn*blkN, NULL, kSolidPlotLineId );
  704. cmPlotDraw();
  705. //cmPlotPrint(false);
  706. }
  707. void cmMeanVarTest(cmRpt_t* rpt )
  708. {
  709. enum { cnt = 7, dim=2 };
  710. cmReal_t v[cnt*dim] = {0, 1, 3, 4, 6, 7, 9, 10, 12, 13, 15, 16, 18, 19 };
  711. cmReal_t mean;
  712. cmTestPrint(rpt, "sum:%f\n", cmVOR_SumN(v,cnt, dim));
  713. cmTestPrint(rpt, "mean:%f\n", mean = cmVOR_MeanN(v,cnt,dim));
  714. cmTestPrint(rpt, "var:%f\n", cmVOR_VarianceN(v,cnt,dim,NULL));
  715. cmTestPrint(rpt, "var:%f\n", cmVOR_VarianceN(v,cnt,dim,&mean));
  716. unsigned rn = 3;
  717. unsigned cn = 5;
  718. cmReal_t mM[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14};
  719. cmReal_t aV[cn];
  720. cmReal_t vV[cn];
  721. cmVOR_MeanM(aV,mM,rn,cn,0);
  722. cmVOR_PrintL("mean cols: ", rpt,1, cn, aV );
  723. cmVOR_VarianceM(vV,mM,rn,cn,aV,0);
  724. cmVOR_PrintL("var cols: ", rpt,1, cn, vV );
  725. cmVOR_MeanM(aV,mM,rn,cn,1);
  726. cmVOR_PrintL("mean rows: ", rpt,1, rn, aV );
  727. cmVOR_VarianceM(vV,mM,rn,cn,aV,1);
  728. cmVOR_PrintL("var rows: ", rpt,1, rn, vV );
  729. cmVOR_VarianceM(vV,mM,rn,cn,NULL,0);
  730. cmVOR_PrintL("var cols: ", rpt,1, cn, vV );
  731. cmVOR_VarianceM(vV,mM,rn,cn,NULL,1);
  732. cmVOR_PrintL("var rows: ", rpt,1, rn, vV );
  733. cmReal_t mV[] = { 1,2,2,2 };
  734. cmTestPrint(rpt,"Mode:%f\n",cmVOR_Mode(mM,rn*cn));
  735. cmTestPrint(rpt,"Mode:%f\n",cmVOR_Mode(mV,5));
  736. }
  737. void cmMedianFilterTest( cmRpt_t* rpt )
  738. {
  739. enum { xn=5 };
  740. unsigned wn;
  741. cmReal_t x[xn] = { 0, 1, 2, 3, 4 };
  742. cmReal_t y[xn];
  743. for( wn=1; wn<=7; ++wn)
  744. {
  745. cmVOR_MedianFilt( x, xn, wn, y, 1 );
  746. cmTestPrint(NULL,"%i : ",wn);
  747. cmVOR_Print(rpt,1,xn,y);
  748. }
  749. }
  750. void cmConstQTest1( cmConstQ* p, cmRpt_t* rpt )
  751. {
  752. const char* fn = "/home/kevin/src/ac/m2i.txt";
  753. cmCtx* c = cmCtxAlloc(NULL,rpt, cmLHeapNullHandle,cmSymTblNullHandle);
  754. cmMatrixBuf* m0p = cmMatrixBufAllocFile( c, NULL, fn );
  755. printf("target mtx:%i x %i\n", m0p->rn, m0p->cn);
  756. printf("sparse mtx:%i x %i\n", p->wndSmpCnt, p->constQBinCnt);
  757. unsigned ri,ci;
  758. for(ri=0; ri<m0p->rn; ++ri)
  759. {
  760. double sum0 = 0;
  761. double sum1 = 0;
  762. double dsum = 0;
  763. double md = 0;
  764. unsigned mi = -1;
  765. for(ci=0; ci<m0p->cn; ++ci)
  766. {
  767. double v0 = cmMatrixBufColPtr( m0p, ci)[ri];
  768. double v1 = cimag( p->skM[ (ci*p->wndSmpCnt)+ri ] );
  769. double d = fabs(v1-v0);
  770. sum0 += v0;
  771. sum1 += v1;
  772. dsum += d;
  773. if( d > md )
  774. {
  775. mi = ci;
  776. md = d;
  777. }
  778. }
  779. printf("%3i (%4i % 9e) % 9e%% s0:% 9e s1:% 9e\n",ri,mi,md,dsum/sum0,sum0,sum1);
  780. }
  781. cmMatrixBufFree(&m0p);
  782. cmCtxFree(&c);
  783. }
  784. void cmSRCTest( cmRpt_t* rpt )
  785. {
  786. const char* ifn = "/home/kevin/src/ac/test0.aif";
  787. //ifn = "/home/kevin/src/st/fv/audio/00-11-060-I-Shapeshifter-TranquilVapor.aiff";
  788. const char* ofn = "/home/kevin/src/ac/temp1.aif";
  789. unsigned upFact = 1;
  790. unsigned dnFact = 8;
  791. unsigned procSmpCnt = 64;
  792. unsigned iChIdx = 0;
  793. unsigned dnProcSmpCnt = procSmpCnt / dnFact;
  794. unsigned oChCnt = 1;
  795. unsigned oBitsPerSmp = 16;
  796. unsigned oChIdx = 0;
  797. cmCtx* c = cmCtxAlloc(NULL,rpt,cmLHeapNullHandle,cmSymTblNullHandle);
  798. cmAudioFileRd* arp = cmAudioFileRdAlloc( c, NULL, procSmpCnt, ifn, iChIdx, 0, cmInvalidIdx );
  799. if( arp != NULL )
  800. {
  801. cmSRC* srp = cmSRCAlloc( c, NULL, arp->info.srate,procSmpCnt, upFact, dnFact );
  802. cmSRC* urp = cmSRCAlloc( c, NULL, arp->info.srate/dnFact,dnProcSmpCnt, dnFact, upFact );
  803. cmAudioFileWr* awp = cmAudioFileWrAlloc( c, NULL, procSmpCnt, ofn, arp->info.srate, oChCnt, oBitsPerSmp);
  804. //cmSRCShow(c,srp);
  805. printf("frame cnt:%i\n",arp->info.frameCnt);
  806. unsigned i = 0, j=0, cnt=10000;
  807. while( cmAudioFileRdRead( arp ) == cmOkRC )
  808. {
  809. if( arp->eofFl || arp->outN != procSmpCnt )
  810. break;
  811. cmSRCExec(srp,arp->outV, arp->outN);
  812. cmSRCExec(urp,srp->outV, srp->outN);
  813. cmAudioFileWrExec( awp, oChIdx, urp->outV, urp->outN );
  814. i+=arp->outN;
  815. if( i > procSmpCnt * cnt )
  816. {
  817. j += i;
  818. i = 0;
  819. printf("%i ",j);
  820. fflush(stdout);
  821. }
  822. }
  823. printf("%i done\n",j + i);
  824. fflush(stdout);
  825. cmAudioFileWrFree(&awp);
  826. cmSRCFree(&urp);
  827. cmSRCFree(&srp);
  828. }
  829. cmAudioFileRdFree(&arp);
  830. cmCtxFree(&c);
  831. }
  832. void cmBeatHistTest( cmRpt_t* rpt )
  833. {
  834. unsigned i;
  835. cmCtx c;
  836. cmCtxInit(&c, rpt,cmLHeapNullHandle,cmSymTblNullHandle);
  837. cmPlotSetup("Beat Histogram Test",1,1);
  838. enum { wndN = 13 };
  839. unsigned frmCnt = 512; // df[] element count
  840. double srate = 1/.0116; // df[] sample rate
  841. double bpm = 120; // beats per minute
  842. //unsigned spb = floor(60*srate/bpm); // samples per beat
  843. cmSample_t wndV[ wndN ];
  844. cmSample_t df[ frmCnt ];
  845. cmSample_t is[ frmCnt ];
  846. // create a df signal by convolving a impulse train with a hanning window
  847. cmVOS_HannMatlab(wndV,wndN);
  848. cmVOS_SynthImpulse( is, frmCnt, 0, srate, bpm/60 );
  849. cmConvolveSignal( &c, wndV, wndN, is, frmCnt, df, frmCnt );
  850. //cmPlotLineS( "df", NULL, df, NULL, frmCnt, NULL, kSolidPlotLineId );
  851. //cmVOS_Print(rpt, 1, 25, df );
  852. cmBeatHist* p = cmBeatHistAlloc(&c,NULL,frmCnt);
  853. //cmVOR_Print(rpt, p->frmCnt, p->maxLagCnt, p->m );
  854. for(i=0; i<frmCnt; ++i)
  855. cmBeatHistExec(p,df[i]);
  856. cmBeatHistCalc(p);
  857. cmPlotDraw();
  858. cmBeatHistFree(&p);
  859. }
  860. void cmOlaProcTest( cmRpt_t* rpt )
  861. {
  862. cmCtx c;
  863. cmOla ola;
  864. unsigned wndSmpCnt = 32;
  865. unsigned hopSmpCnt = 8;
  866. unsigned procSmpCnt = 4;
  867. unsigned wndCnt = 8;
  868. unsigned i = 0;
  869. cmSample_t wndV[] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1 };
  870. assert( sizeof(wndV)/sizeof(wndV[0]) == wndSmpCnt );
  871. cmCtxAlloc(&c, rpt,cmLHeapNullHandle,cmSymTblNullHandle);
  872. cmOlaAlloc(&c,&ola,wndSmpCnt,hopSmpCnt, procSmpCnt, kUnityWndId );
  873. for(i=0; i<wndCnt; ++i)
  874. {
  875. cmOlaExecS(&ola,wndV,wndSmpCnt);
  876. int j;
  877. for(j=0; j<hopSmpCnt/procSmpCnt; ++j)
  878. {
  879. cmVOS_Print( rpt, 1, ola.procSmpCnt, ola.outPtr );
  880. cmOlaExecOut(&ola);
  881. }
  882. }
  883. cmObjFreeStatic( cmOlaFree, cmOla, ola );
  884. cmObjFreeStatic( cmCtxFree, cmCtx, c );
  885. }
  886. void cmTestBarkFiltMask(cmRpt_t* rpt)
  887. {
  888. // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
  889. //cmReal_t bl[]= { 0,100,200,300,400,510,630,770, 920,1080,1270,1480,1720,2000,2320,2700,3150,3700,4400,5300,6400,7700, 9500,12000};
  890. //cmReal_t bc[]= { 50,150,250,350,450,570,700,840,1000,1170,1370,1600,1850,2150,2500,2900,3400,4000,4800,5800,7000,8500,10500,13500};
  891. //cmReal_t bh[]= {100,200,300,400,510,630,770,920,1080,1270,1480,1720,2000,2320,2700,3150,3700,4400,5300,6400,7700,9500,12000,15500};
  892. // -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 (23+1)
  893. cmReal_t b[]= {0, 50,150,250,350,450,570,700,840,1000,1170,1370,1600,1850,2150,2500,2900,3400,4000,4800,5800,7000,8500,10500,13500, 15500 };
  894. unsigned bandCnt = 24;
  895. unsigned binCnt = 64;
  896. unsigned binHz = 100.0;
  897. cmReal_t maskMtx[ bandCnt * binCnt ];
  898. cmReal_t stSpread = 0;
  899. cmVOR_TriangleMask(maskMtx, bandCnt, binCnt, b+1, binHz, stSpread, b+0, b+2 );
  900. cmVOR_Print( rpt, bandCnt, binCnt, maskMtx);
  901. }
  902. void cmTestFftTemplate(cmRpt_t* rpt)
  903. {
  904. double srate = 16;
  905. double hz = 1;
  906. unsigned n = 16;
  907. unsigned otCnt = 3;
  908. cmCtx* c = cmCtxAlloc( NULL, rpt,cmLHeapNullHandle,cmSymTblNullHandle);
  909. // take the fft of a vector of cmSample_t values
  910. cmFftSR* p0;
  911. cmIFftRS* p2;
  912. cmSample_t vs[n];
  913. cmVOS_SynthSquare( vs, n, 0, srate, hz, otCnt );
  914. cmVOS_PrintL( "\nsig:\n",rpt, 1, n, vs);
  915. p0 = cmFftAllocSR(c,NULL, vs, n, kToPolarFftFl );
  916. p2 = cmIFftAllocRS(c,NULL,p0->binCnt);
  917. cmFftExecSR( p0, NULL, 0 );
  918. cmVOR_DivVS( p0->magV, p0->binCnt, p0->wndSmpCnt );
  919. cmVOR_PrintL( "\nmag:\n",rpt, 1, p0->binCnt, p0->magV );
  920. cmIFftExecPolarRS( p2, p0->magV, p0->phsV );
  921. cmVOS_PrintL( "\nifft:\n",rpt,1, p2->binCnt, p2->outV );
  922. cmIFftFreeRS(&p2);
  923. cmFftFreeSR(&p0);
  924. // take the fft of a vector of cmReal_t values
  925. cmFftRR* p1;
  926. cmIFftRR* p3;
  927. cmReal_t vr[n];
  928. cmVOR_SynthSquare( vr, n, 0, srate, hz, otCnt );
  929. cmVOR_PrintL("\nsig\n", rpt, 1, n, vr);
  930. p1 = cmFftAllocRR(c,NULL, vr, n, kToPolarFftFl );
  931. p3 = cmIFftAllocRR(c,NULL, p1->binCnt);
  932. cmFftExecRR( p1, NULL, 0 );
  933. cmVOR_DivVS( p1->magV, p1->binCnt, p1->wndSmpCnt );
  934. cmVOR_PrintL( "\nmag:\n",rpt, 1, p1->binCnt, p1->magV );
  935. cmIFftExecPolarRR( p3, p1->magV, p1->phsV );
  936. cmVOR_PrintL( "\nifft:\n",rpt,1, p3->binCnt, p3->outV );
  937. cmIFftFreeRR(&p3);
  938. cmFftFreeRR(&p1);
  939. cmCtxFree(&c);
  940. }
  941. cmReal_t cmSelDistFunc( void* userPtr, const cmReal_t* v0, const cmReal_t* v1, unsigned vn )
  942. { return cmVOR_EuclidDistance(v0,v1,vn); }
  943. void cmSelectCols(cmRpt_t* rpt)
  944. {
  945. unsigned mcn = 10;
  946. unsigned mrn = 3;
  947. unsigned selN = 3;
  948. cmReal_t m0[ mrn * mcn ];
  949. cmReal_t m1[ mrn * selN ];
  950. unsigned selIdxV[ selN ];
  951. cmVOR_Random(m0,mrn*mcn,0.0,1.0);
  952. cmVOR_PrintL("\norg\n",rpt, mrn, mcn, m0 );
  953. cmVOR_SelectRandom(m1,selIdxV,selN,m0,mrn,mcn);
  954. cmVOR_PrintL("\nrandom\n",rpt, mrn, selN, m1 );
  955. cmVOU_PrintL("\nindexes\n",rpt, 1, selN, selIdxV );
  956. cmVOR_SelectMaxDist(m1, selIdxV, selN, m0, mrn, mcn, cmSelDistFunc, NULL );
  957. cmVOR_PrintL("\nselect max dist\n",rpt, mrn, selN, m1 );
  958. cmVOU_PrintL("\nindexes\n",rpt, 1, selN, selIdxV );
  959. cmVOR_SelectMaxAvgDist(m1, selIdxV, selN, m0, mrn, mcn, cmSelDistFunc, NULL );
  960. cmVOR_PrintL("\nselect max avg dist\n",rpt, mrn, selN, m1 );
  961. cmVOU_PrintL("\nindexes\n",rpt, 1, selN, selIdxV );
  962. }
  963. void cmNMF_Test( cmRpt_t* rpt )
  964. {
  965. //const char* plotDev = "wxwidgets";
  966. int r = 2;
  967. int n = 100;
  968. int m = 1000;
  969. int vn = 25;
  970. unsigned maxIterCnt = 2000;
  971. unsigned convergeCnt = 40;
  972. cmReal_t* V = cmMemAllocZ( cmReal_t, n*m );
  973. cmReal_t* W = cmMemAllocZ( cmReal_t, n*r );
  974. cmReal_t* H = cmMemAllocZ( cmReal_t, r*m );
  975. cmReal_t* d = cmMemAllocZ( cmReal_t, vn );
  976. cmReal_t* t = cmMemAllocZ( cmReal_t, vn );
  977. unsigned i;
  978. cmCtx* ctx = cmCtxAlloc(NULL,rpt,cmLHeapNullHandle,cmSymTblNullHandle);
  979. cmNmf_t* nmf = cmNmfAlloc(ctx, NULL, n, m, r, maxIterCnt, convergeCnt );
  980. cmVOR_Hann(d,vn);
  981. // prevent zeros in V[]
  982. cmVOR_Random(V,n*m,0.0,0.1);
  983. for(i=0; i<m; ++i)
  984. {
  985. cmVOR_MultVVS( t, vn, d, (cmReal_t)i/m );
  986. cmVOR_AddVV( V + (i*n), vn, t );
  987. }
  988. for(i=0; i<m; ++i)
  989. {
  990. cmVOR_MultVVS( t, vn, d, 1.0 - ((cmReal_t)i/m) );
  991. cmVOR_AddVV( V + (i*n) + n - (vn+1), vn, t );
  992. }
  993. cmNmfExec( nmf, V, m );
  994. //cmPlviewPlotImage(plotDev, vRptFunc, NULL, V, n, m, 0, m, 0, n );
  995. //cmPlviewPlotColY(plotDev, vRptFunc, NULL, nmf->W, n, r, 0, n, 0.0, 1.0 );
  996. //cmPlviewPlotRowY(plotDev, vRptFunc, NULL, nmf->H, r, m, 0, m, 0.0, 1.0 );
  997. cmNmfFree(&nmf);
  998. cmCtxFree(&ctx);
  999. cmMemPtrFree(&V);
  1000. cmMemPtrFree(&W);
  1001. cmMemPtrFree(&H);
  1002. cmMemPtrFree(&d);
  1003. cmMemPtrFree(&t);
  1004. }
  1005. void cmLinearMapTest( cmRpt_t* rpt )
  1006. {
  1007. int sN = 4;
  1008. int dN = 15;
  1009. cmReal_t sV[sN];
  1010. cmVOR_Seq( sV, sN, 0, 1 );
  1011. cmReal_t dV[dN];
  1012. cmVOR_LinearMap(dV, dN, sV, sN );
  1013. cmVOR_Print(rpt, 1, dN, dV );
  1014. int s2N = 15;
  1015. int d2N = 4;
  1016. cmReal_t s2V[sN];
  1017. cmReal_t d2V[dN];
  1018. cmVOR_Seq( s2V, s2N, 0, 1 );
  1019. cmVOR_LinearMap(d2V, d2N, s2V, s2N );
  1020. cmVOR_Print(rpt, 1, d2N, d2V );
  1021. }
  1022. void cmMemErrCallback( void* userDataPtr, const char* fmt, va_list vl )
  1023. { vprintf(fmt,vl);}
  1024. // Test a cmProc
  1025. void cmProcTestProc(cmCtx_t* ctx )
  1026. {
  1027. unsigned baseSymId = 1000;
  1028. unsigned dfltBlockByteCnt = 1024;
  1029. cmLHeapH_t lhH = cmLHeapCreate(dfltBlockByteCnt,ctx);
  1030. cmSymTblH_t stH = cmSymTblCreate(cmSymTblNullHandle,baseSymId,ctx);
  1031. cmCtx* c = cmCtxAlloc(NULL,&ctx->rpt,lhH,stH);
  1032. assert( cmLHeapIsValid(lhH));
  1033. assert( cmSymTblIsValid(stH));
  1034. //cmShiftBufTest(c);
  1035. cmPvAnlTest(c);
  1036. cmLHeapDestroy(&lhH);
  1037. cmSymTblDestroy(&stH);
  1038. cmCtxFree(&c);
  1039. }
  1040. void cmPuTest(cmCtx_t* ctx);
  1041. void cmAudLabelFileTest(cmCtx_t* ctx);
  1042. void cmProcTestNoInit(cmCtx_t* ctx)
  1043. {
  1044. //cmSelectCols(rpt);
  1045. //cmNMF_Test(rpt);
  1046. //cmGmmTest( rpt );
  1047. //cmMvnProbTest(rpt);
  1048. //cmShiftBufTest(rpt,NULL);
  1049. //cmLinearMapTest(&ctx->rpt);
  1050. //cmCovarTest2(&ctx->rpt);
  1051. //cmMahalanobisTest(&ctx->rpt);
  1052. //cmProcTestProc(ctx); // test a cmProcObj
  1053. //cmAudioFileProcTest(ctx);
  1054. //cmAudioFileReadWriteTest(ctx);
  1055. //cmPuTest(ctx);
  1056. //cmFileGetLineTest(ctx);
  1057. //cmAudLabelFileTest(ctx);
  1058. //cmStackTest(ctx);
  1059. //cmBinMtxFileTest(ctx );
  1060. //cmMtxMultTest(ctx->err.rpt);
  1061. //cmStandardizeTest(ctx);
  1062. cmRbmBinaryTest(ctx);
  1063. }
  1064. void cmProcTestGnuPlot( cmCtx_t* ctx )
  1065. {
  1066. cmPlotInitialize(NULL);
  1067. cmProcTestNoInit(ctx);
  1068. cmTestPrint(&ctx->rpt,"%s\n","press any key");
  1069. cmKeyPress(NULL);
  1070. cmPlotFinalize();
  1071. }
  1072. void cmProcTest(cmCtx_t* ctx)
  1073. {
  1074. cmKbRecd kb;
  1075. cmMdInitialize( ctx->guardByteCnt, ctx->alignByteCnt, ctx->mmFlags, &ctx->rpt );
  1076. cmPlotInitialize(NULL);
  1077. // cmAudioFileTest();
  1078. // this should be added to cmCtxInit()
  1079. //cmSetupFloatPointExceptHandler(NULL);
  1080. //cmDelayTest();
  1081. //cmMelTest();
  1082. //cmDctTest();
  1083. //cmMtxMultTest(rpt);
  1084. //cmSonesTest();
  1085. //cmProcImplTestSynth();
  1086. //cmZeroCrossTest();
  1087. //cmFIRTest();
  1088. //cmFftTest();
  1089. //cmFftTestComplex();
  1090. //cmAudioFileWrTest();
  1091. //cmSRCTest();
  1092. //cmProcImplTest1();
  1093. //cmFuncFilterTest();
  1094. //cmRandomTest(rpt);
  1095. //cmTestGaussWin();
  1096. //cmTestMtxBuf();
  1097. //cmDhmmTest();
  1098. //cmGaussTest(rpt);
  1099. //cmLaTest(rpt);
  1100. //cmFPExceptTest();
  1101. //cmMvnProbTest(rpt);
  1102. //cmShiftRotateTest(rpt);
  1103. //cmMeanVarTest(rpt);
  1104. //cmStatsProcTest(rpt);
  1105. //cmMedianFilterTest(rpt);
  1106. //cmFilterTest(rpt);
  1107. //cmBeatHistTest(rpt);
  1108. //cmIFftTest(rpt);
  1109. //cmConvolveTest(rpt);
  1110. //cmBeatHistTest(rpt);
  1111. //cmWndFuncTest(rpt);a
  1112. //cmGmmTest( rpt );
  1113. //cmChmmTest( rpt );
  1114. //cmCovarTest(rpt);
  1115. //cmRandIntSeqTest(rpt);
  1116. //cmChordTest(rpt);
  1117. //cmConstQTest1()
  1118. //void cmSRCTest( rpt );
  1119. //cmFuncFilterTest();
  1120. //cmOlaProcTest(rpt);
  1121. //cmTestBarkFiltMask(rpt);
  1122. //cmTestFftTemplate(rpt);
  1123. cmProcTestNoInit(ctx);
  1124. cmTestPrint(&ctx->rpt,"%s\n","press any key");
  1125. cmKeyPress(&kb);
  1126. cmPlotFinalize();
  1127. cmMdReport( kIgnoreNormalMmFl );
  1128. cmMdFinalize();
  1129. }