/* * This method destroys both matrix */ void stopMatrix() { destroyMatrix(&matrix1); destroyMatrix(&matrix2); }
int main(int argc, char **argv) { if(argc != 2) { printf("Correct usage: ./wavePropagationSimulator bm\nWhere bm = a (for not generating a new benchmark) or b (generates benchmark)"); exit(4); } int intNTotalNodes = N_HNODES*N_VNODES; // Initiate the CSR Matrix which stands as the coefficients matrix to the linear system CSR_Matrix CSRM_UKP1; CSRM_UKP1 = initCSRMatrix(intNTotalNodes); loadUKP1CSR(CSRM_UKP1,0,waveSpeed); if(argv[1][0] == 'b') { // Generating the Grid. This function uses the constants set in constants.h to do it generateGridToFile("mesh.m"); // These functions generate series of wavefields, they should be uncommented on further tests //generateWaveBenchmark("wave1.m",wave1); generateWaveBenchmarkTimeVariant("wave2","wave2",wave2); } // Trying to establish time variance for u // // Now u must be a vector of extended 2d-vectors, since every "row" in u corresponds to a time step // An extended vector is a matrix shaped to store every node with a special mapping scheme, in which // they're stored line by line. // Initiating uVectorTimeVariant structMatrix *uVectorTimeVariant[2]; for(int i=0; i < 2; i++) { uVectorTimeVariant[i] = (structMatrix*) calloc(N_TIME_STEPS,sizeof(structMatrix)); for(int j = 0; j < N_TIME_STEPS; j++) uVectorTimeVariant[i][j] = initMatrix(intNTotalNodes,1); } // u_0 is a prescript value, since we know how the wave behaves when it starts propagating // Its initial values shall be stored in the testbench folder, for whatever wave used // In this initial test, we will use the "wave2", for its the one that already has time variance loadExtVectorOnInitialTime(uVectorTimeVariant,"../testbench/wave2/wave2_0.m"); //printMatrix(uVectorTimeVariant[0][0]); //printMatrix(uVectorTimeVariant[0][1]); // Calculating u_1 // u_1 is calculated via Taylor series, using both u_0, v_0 (u_0 derivative on time) - both of these are known // in advance - and the space derivatives on X and Y axis (this is easily extensible to a 3rd dimension), which // corresponds to the laplacian of u_0 and are calculated via Finite Differences. Once again, we use "wave2" as //a case test. // The Taylor series expression to u_1 is: // u_1 = u_0 + delta_time * v_0 + (delta_time^2/2) * (d^2 u/dx^2 + d^2 u/dy^2) * (waveSpeed(t=0)^2 * delta_time^2) / 2 // u_0 is already done in uVectorTimeVariant[0] // v_0 is easily calculated: structMatrix *v_0_m,v_0[2],laplacian[2]; v_0_m = loadExtVectorTimeDerivative(wave2TimeDerivative); laplacian[0] = initMatrix(intNTotalNodes,1); laplacian[1] = initMatrix(intNTotalNodes,1); float *boundaryVector; boundaryVector = calloc((2*(N_HNODES + N_VNODES -2)),sizeof(float)); for(int i=0; i < 2; i++) { //matrixSumDestined(uVectorTimeVariant[i][1],uVectorTimeVariant[i][0]); v_0[i] = matrixToExtVector(v_0_m[i]); destroyMatrix(v_0_m[i]); matrixTimesScalar(v_0[i],DELTA_TIME); laplacianViaFD(laplacian[i],uVectorTimeVariant[i][0]); correctLaplacian(laplacian[i],0); // Now summing up the factors matrixSumDestined(uVectorTimeVariant[i][1],uVectorTimeVariant[i][0]); matrixSumDestined(uVectorTimeVariant[i][1],v_0[i]); matrixSumDestined(uVectorTimeVariant[i][1],laplacian[i]); // at the end, apply the boundary conditions on u_1 generateBoundaryVectorFromFunction(boundaryVector,DELTA_TIME,i,wave2Returnable); applyBoundaryConditions(uVectorTimeVariant[i][1],boundaryVector); } //todo assemble the linear system //int k=1; structMatrix vectorB,UKL1; structMatrix tmp_Multiplier; UKL1 = initMatrix(intNTotalNodes,intNTotalNodes); tmp_Multiplier = initMatrix(intNTotalNodes,1); //tmp_Multiplier[Y_AXIS] = initMatrix(intNTotalNodes,1); vectorB = initMatrix(intNTotalNodes,1); exportWaveFieldToFile(extVectorToMatrix(uVectorTimeVariant[X_AXIS][0]),extVectorToMatrix(uVectorTimeVariant[Y_AXIS][0]),0,"wave2","wave2"); exportWaveFieldToFile(extVectorToMatrix(uVectorTimeVariant[X_AXIS][1]),extVectorToMatrix(uVectorTimeVariant[Y_AXIS][1]),1,"wave2","wave2"); for(int k=1; k < N_TIME_STEPS-1; k++) { loadUKL1(UKL1,(k-1)*DELTA_TIME,waveSpeed); for(int axis = 0; axis < 2; axis++) { //if(axis==1) printMatrix(UKL1); printf("Axis: %d %d\n",axis,Y_AXIS); copyMatrix(vectorB,uVectorTimeVariant[axis][k]); applyWaveSpeedTimeStepIntoMatrix(vectorB,DELTA_TIME,waveSpeed); matrixMultiplicationDestined(tmp_Multiplier,UKL1,uVectorTimeVariant[axis][k-1]); matrixSumDestined(vectorB,tmp_Multiplier); generateBoundaryVectorFromFunction(boundaryVector,(k+1)*DELTA_TIME,axis,wave2Returnable); applyBoundaryConditions(vectorB,boundaryVector); // todo SOR method in CSR matrix CSR_SOR(uVectorTimeVariant[axis][k+1],CSRM_UKP1,vectorB,1); resetMatrix(tmp_Multiplier); } //printMatrix(extVectorToMatrix(uVectorTimeVariant[Y_AXIS][k+1])); exportWaveFieldToFile(extVectorToMatrix(uVectorTimeVariant[X_AXIS][k+1]),extVectorToMatrix(uVectorTimeVariant[Y_AXIS][k+1]),k+1,"wave2","wave2"); } //printMatrix(extVectorToMatrix(tmp_Multiplier)); printf("\n"); //printCSRMatrix(CSRM_UKP1); //printMatrix(vectorB); /* // Initiating mesh structMatrix mesh_hmatrix,mesh_vmatrix; structMatrix tmp[2]; tmp[X_AXIS] = extVectorToMatrix(uVectorTimeVariant[X_AXIS][1]); tmp[Y_AXIS] = extVectorToMatrix(uVectorTimeVariant[Y_AXIS][1]); mesh_hmatrix = initMatrix(N_HNODES,N_VNODES); mesh_vmatrix = initMatrix(N_HNODES,N_VNODES); loadMesh(mesh_hmatrix,mesh_vmatrix); //for(int i=0; i< N_TIME_STEPS; i++) { printMatrixToFile(tmp[X_AXIS],"matrix.m","u"); printMatrixToFile(tmp[Y_AXIS],"matrix.m","v"); printMatrixToFile(mesh_hmatrix,"matrix.m","x"); printMatrixToFile(mesh_vmatrix,"matrix.m","y"); // At the end, print the "quiver" function FILE *destinyFile = fopen("matrix.m","a"); fprintf(destinyFile,"quiver(x,y,u,v)\n"); fclose(destinyFile); // } */ // Memory Freeing destroyCSRMatrix(CSRM_UKP1); destroyMatrix(vectorB); destroyMatrix(UKL1); destroyMatrix(tmp_Multiplier); for (int i=0; i < 2; i++) { destroyMatrix(laplacian[i]); destroyMatrix(v_0[i]); for(int j = 0; j < N_TIME_STEPS; j++) destroyMatrix(uVectorTimeVariant[i][j]); free(uVectorTimeVariant[i]); } free(v_0_m); free(boundaryVector); }
AUD_Int32s perf_dtw_mfcc_vad() { char inputPath[256] = { 0, }; char keywordPath[256] = { 0, }; AUD_Double threshold, minThreshold, maxThreshold, stepThreshold; AUD_Bool isRecognized = AUD_FALSE; AUD_Int32s data; AUD_Error error = AUD_ERROR_NONE; setbuf( stdin, NULL ); AUDLOG( "pls give template wav stream's path:\n" ); keywordPath[0] = '\0'; data = scanf( "%s", keywordPath ); AUDLOG( "template path is: %s\n", keywordPath ); setbuf( stdin, NULL ); AUDLOG( "pls give test wav stream's path:\n" ); inputPath[0] = '\0'; data = scanf( "%s", inputPath ); AUDLOG( "test stream path is: %s\n", inputPath ); setbuf( stdin, NULL ); AUDLOG( "pls give lowest test threshold:\n" ); data = scanf( "%lf", &minThreshold ); AUDLOG( "lowest threshold is: %.2f\n", minThreshold ); setbuf( stdin, NULL ); AUDLOG( "pls give max test threshold:\n" ); data = scanf( "%lf", &maxThreshold ); AUDLOG( "max threshold is: %.2f\n", maxThreshold ); setbuf( stdin, NULL ); AUDLOG( "pls give test threshold step:\n" ); data = scanf( "%lf", &stepThreshold ); AUDLOG( "threshold step is: %.2f\n", stepThreshold ); AUDLOG( "\n\n" ); // file & directory operation, linux dependent entry *pEntry = NULL; dir *pDir = NULL; entry *pKeywordEntry = NULL; dir *pKeywordDir = NULL; AUD_Int32s i, j; AUD_Feature keywordFeature; AUD_Int32s keywordSampleNum = 0; AUD_Int32s keywordWinNum = 0; AUD_Int8s keywordFile[256] = { 0, }; AUD_Int8s keywordName[256] = { 0, }; AUD_Int32s keywordID = 0; AUD_DTWSession dtwSession; AUD_Double score = 0.; AUD_Int32s sampleNum; AUD_Int32s keywordBufLen = 0; AUD_Int16s *pKeywordBuf = NULL; AUD_Int32s bufLen = 0; AUD_Int16s *pBuf = NULL; AUD_Int32s ret; // init performance benchmark void *hBenchmark = NULL; char logName[256] = { 0, }; snprintf( logName, 256, "dtw-mfcc-vad-%d-%d-%d-%d", WOV_DTW_TYPE, WOV_DISTANCE_METRIC, WOV_DTWTRANSITION_STYLE, WOV_DTWSCORING_METHOD ); ret = benchmark_init( &hBenchmark, (AUD_Int8s*)logName, keywords, sizeof(keywords) / sizeof(keywords[0]) ); AUD_ASSERT( ret == 0 ); for ( threshold = minThreshold; threshold < maxThreshold + stepThreshold; threshold += stepThreshold ) { int frameStride = 0; ret = benchmark_newthreshold( hBenchmark, threshold ); AUD_ASSERT( ret == 0 ); AUDLOG( "***********************************************\n" ); AUDLOG( "keyword training start...\n" ); pKeywordDir = openDir( (const char*)keywordPath ); keywordID = 0; while ( ( pKeywordEntry = scanDir( pKeywordDir ) ) ) { // train keyword AUD_Summary fileSummary; snprintf( (char*)keywordFile, 256, "%s/%s", (const char*)keywordPath, pKeywordEntry->name ); ret = parseWavFromFile( keywordFile, &fileSummary ); if ( ret < 0 ) { continue; } AUD_ASSERT( fileSummary.channelNum == CHANNEL_NUM && fileSummary.bytesPerSample == BYTES_PER_SAMPLE && fileSummary.sampleRate == SAMPLE_RATE ); keywordBufLen = fileSummary.dataChunkBytes + 100; pKeywordBuf = (AUD_Int16s*)calloc( keywordBufLen, 1 ); AUD_ASSERT( pKeywordBuf ); keywordSampleNum = readWavFromFile( keywordFile, pKeywordBuf, keywordBufLen ); if ( keywordSampleNum < 0 ) { continue; } // front end processing // pre-emphasis sig_preemphasis( pKeywordBuf, pKeywordBuf, keywordSampleNum ); for ( j = 0; j * FRAME_STRIDE + FRAME_LEN <= keywordSampleNum; j++ ) { ; } frameStride = FRAME_STRIDE; AUDLOG( "pattern[%d: %s] valid frame number[%d]\n", keywordID, keywordFile, j ); keywordWinNum = j; keywordFeature.featureMatrix.rows = keywordWinNum - MFCC_DELAY; keywordFeature.featureMatrix.cols = MFCC_FEATDIM; keywordFeature.featureMatrix.dataType = AUD_DATATYPE_INT32S; ret = createMatrix( &(keywordFeature.featureMatrix) ); AUD_ASSERT( ret == 0 ); keywordFeature.featureNorm.len = keywordWinNum - MFCC_DELAY; keywordFeature.featureNorm.dataType = AUD_DATATYPE_INT64S; ret = createVector( &(keywordFeature.featureNorm) ); AUD_ASSERT( ret == 0 ); void *hTemplateMfccHandle = NULL; // init mfcc handle error = mfcc16s32s_init( &hTemplateMfccHandle, FRAME_LEN, WINDOW_TYPE, MFCC_ORDER, frameStride, SAMPLE_RATE, COMPRESS_TYPE ); AUD_ASSERT( error == AUD_ERROR_NONE ); // calc MFCC feature error = mfcc16s32s_calc( hTemplateMfccHandle, pKeywordBuf, keywordSampleNum, &keywordFeature ); AUD_ASSERT( error == AUD_ERROR_NONE ); // deinit mfcc handle error = mfcc16s32s_deinit( &hTemplateMfccHandle ); AUD_ASSERT( error == AUD_ERROR_NONE ); strncpy( (char*)keywordName, pKeywordEntry->name, 255 ); ret = benchmark_newtemplate( hBenchmark, keywordName ); AUD_ASSERT( ret == 0 ); keywordID++; keywordBufLen = 0; free( pKeywordBuf ); pKeywordBuf = NULL; AUDLOG( "keyword training finish...\n" ); // recognize pEntry = NULL; pDir = NULL; pDir = openDir( (const char*)inputPath ); while ( ( pEntry = scanDir( pDir ) ) ) { AUD_Int8s inputStream[256] = { 0, }; void *hMfccHandle = NULL; AUD_Summary fileSummary; snprintf( (char*)inputStream, 256, "%s/%s", (const char*)inputPath, pEntry->name ); ret = parseWavFromFile( inputStream, &fileSummary ); if ( ret < 0 ) { continue; } AUD_ASSERT( fileSummary.channelNum == CHANNEL_NUM && fileSummary.bytesPerSample == BYTES_PER_SAMPLE && fileSummary.sampleRate == SAMPLE_RATE ); AUDLOG( "input stream: %s\n", inputStream ); bufLen = fileSummary.dataChunkBytes; pBuf = (AUD_Int16s*)malloc( bufLen + 100 ); AUD_ASSERT( pBuf ); sampleNum = readWavFromFile( inputStream, pBuf, bufLen ); AUD_ASSERT( sampleNum > 0 ); // VAD AUD_Int16s *pValidBuf = (AUD_Int16s*)calloc( bufLen, 1 ); AUD_ASSERT( pValidBuf ); AUD_Int16s *pSpeechFlag = (AUD_Int16s*)calloc( ( sampleNum / VAD_FRAME_LEN ) + 1, sizeof( AUD_Int16s ) ); AUD_ASSERT( pSpeechFlag ); void *hVadHandle = NULL; ret = sig_vadinit( &hVadHandle, pValidBuf, pSpeechFlag, bufLen, -1, -1 ); AUD_ASSERT( ret == 0 ); AUD_Int32s start = -1; for ( i = 0; i * VAD_FRAME_LEN + FRAME_LEN <= sampleNum; i++ ) { ret = sig_vadupdate( hVadHandle, pBuf + i * VAD_FRAME_LEN, &start ); if ( ret == 0 ) { continue; } else { break; } } if ( ret == 0 ) { continue; } AUD_Int32s validSampleNum = ret; sig_vaddeinit( &hVadHandle ); #if 1 char vadFile[256] = { 0, }; snprintf( vadFile, 255, "./vaded/%s", pEntry->name ); ret = writeWavToFile( (AUD_Int8s*)vadFile, pValidBuf + start, validSampleNum ); AUD_ASSERT( ret == 0 ); #endif // de-noise AUD_Int16s *pCleanBuf = (AUD_Int16s*)calloc( (validSampleNum + start) * BYTES_PER_SAMPLE, 1 ); AUD_ASSERT( pCleanBuf ); AUD_Int32s cleanLen = denoise_mmse( pValidBuf, pCleanBuf, (validSampleNum + start) ); #if 1 char cleanFile[256] = { 0, }; snprintf( cleanFile, 255, "./clean/%s", pEntry->name ); ret = writeWavToFile( (AUD_Int8s*)cleanFile, pCleanBuf, cleanLen ); AUD_ASSERT( ret == 0 ); #endif // front end processing // pre-emphasis sig_preemphasis( pCleanBuf, pCleanBuf, cleanLen ); for ( j = 0; j * FRAME_STRIDE + FRAME_LEN <= cleanLen; j++ ) { ; } frameStride = FRAME_LEN; bufLen = 0; free( pBuf ); pBuf = NULL; free( pValidBuf ); pValidBuf = NULL; free( pSpeechFlag ); pSpeechFlag = NULL; AUD_Feature inFeature; inFeature.featureMatrix.rows = j - MFCC_DELAY; inFeature.featureMatrix.cols = MFCC_FEATDIM; inFeature.featureMatrix.dataType = AUD_DATATYPE_INT32S; ret = createMatrix( &(inFeature.featureMatrix) ); AUD_ASSERT( ret == 0 ); inFeature.featureNorm.len = j - MFCC_DELAY; inFeature.featureNorm.dataType = AUD_DATATYPE_INT64S; ret = createVector( &(inFeature.featureNorm) ); AUD_ASSERT( ret == 0 ); // init mfcc handle error = mfcc16s32s_init( &hMfccHandle, FRAME_LEN, WINDOW_TYPE, MFCC_ORDER, frameStride, SAMPLE_RATE, COMPRESS_TYPE ); AUD_ASSERT( error == AUD_ERROR_NONE ); error = mfcc16s32s_calc( hMfccHandle, pCleanBuf, cleanLen, &inFeature ); AUD_ASSERT( error == AUD_ERROR_NONE ); // init dtw match dtwSession.dtwType = WOV_DTW_TYPE; dtwSession.distType = WOV_DISTANCE_METRIC; dtwSession.transitionType = WOV_DTWTRANSITION_STYLE; error = dtw_initsession( &dtwSession, &keywordFeature, inFeature.featureMatrix.rows ); AUD_ASSERT( error == AUD_ERROR_NONE ); error = dtw_updatefrmcost( &dtwSession, &inFeature ); AUD_ASSERT( error == AUD_ERROR_NONE ); error = dtw_match( &dtwSession, WOV_DTWSCORING_METHOD, &score, NULL ); AUD_ASSERT( error == AUD_ERROR_NONE ); error = dtw_deinitsession( &dtwSession ); AUD_ASSERT( error == AUD_ERROR_NONE ); // deinit mfcc handle error = mfcc16s32s_deinit( &hMfccHandle ); AUD_ASSERT( error == AUD_ERROR_NONE ); ret = destroyMatrix( &(inFeature.featureMatrix) ); AUD_ASSERT( ret == 0 ); ret = destroyVector( &(inFeature.featureNorm) ); AUD_ASSERT( ret == 0 ); AUDLOG( "score: %.2f\n", score ); isRecognized = AUD_FALSE; if ( score <= threshold ) { isRecognized = AUD_TRUE; } // insert log entry ret = benchmark_additem( hBenchmark, (AUD_Int8s*)pEntry->name, score, isRecognized ); AUD_ASSERT( ret == 0 ); cleanLen = 0; free( pCleanBuf ); pCleanBuf = NULL; } closeDir( pDir ); pDir = NULL; ret = benchmark_finalizetemplate( hBenchmark ); AUD_ASSERT( ret == 0 ); ret = destroyMatrix( &(keywordFeature.featureMatrix) ); AUD_ASSERT( ret == 0 ); ret = destroyVector( &(keywordFeature.featureNorm) ); AUD_ASSERT( ret == 0 ); } closeDir( pKeywordDir ); pKeywordDir = NULL; ret = benchmark_finalizethreshold( hBenchmark ); AUD_ASSERT( ret == 0 ); } ret = benchmark_free( &hBenchmark ); AUD_ASSERT( ret == 0 ); AUDLOG( "DTW-MFCC VAD Benchmark finished\n" ); return 0; }
int main(int argc, char **argv) { int threadsLimit = 0; int c; while ((c = getopt (argc, argv, "n:t")) != -1) { switch (c) { case 'n': threadsLimit = atoi(optarg); break; case 't': benchmark = true; break; default: ; } } if (threadsLimit) { omp_set_dynamic(0); omp_set_num_threads(threadsLimit); } clock_t start; start = clock(); bool *locks = (bool *)malloc((SIZEX + 2) * sizeof(bool)); for (int i = 0; i < SIZEX + 2; i++) locks[i] = false; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_size(MPI_COMM_WORLD, &size); initRandom(0, rank); Entity **matrix_a = createMatrix(SIZEX + 2, SIZEY + 2); Entity **matrix_b = createMatrix(SIZEX + 2, SIZEY + 2); initMatrix(matrix_a, SIZEX, SIZEY); MPI_Type_contiguous(sizeof(Entity), MPI_BYTE, &cell_t); MPI_Type_commit(&cell_t); MPI_Type_vector(SIZEX + 2, 1, 1, cell_t, &row_t); MPI_Type_commit(&row_t); MPI_Type_contiguous(sizeof(Counter), MPI_BYTE, &counter_t); MPI_Type_commit(&counter_t); Entity * northBuffer = (Entity *) malloc((SIZEX + 2) * sizeof(Entity)); Entity * southBuffer = (Entity *) malloc((SIZEX + 2) * sizeof(Entity)); if (!benchmark) { // update local counter and sync updateCounter(matrix_a); syncCounter(); printHeader(rank); printCSV(0, rank); } for (int n = 0; n < STEPS; n++) { // set adjacent borders if (rank == NORTH) { MPI_Recv(northBuffer, 1, row_t, SOUTH, TAG, MPI_COMM_WORLD, &status); setBorder(northBuffer, matrix_a, SIZEY+1); setBuffer(matrix_a, northBuffer, SIZEY); MPI_Send(northBuffer, 1, row_t, SOUTH, TAG, MPI_COMM_WORLD); } if (rank == SOUTH) { setBuffer(matrix_a, southBuffer, 1); MPI_Send(southBuffer, 1, row_t, NORTH, TAG, MPI_COMM_WORLD); MPI_Recv(southBuffer, 1, row_t, NORTH, TAG, MPI_COMM_WORLD, &status); setBorder(southBuffer, matrix_a, 0); } #pragma omp parallel for default(none) shared(matrix_a, matrix_b, n, locks) schedule(static, SIZEX/omp_get_max_threads()) for (int i = 1; i <= SIZEX; i++) { lock(i, locks); #pragma omp parallel for for (int j = 1; j <= SIZEY; j++) process(matrix_a, matrix_b, i, j); unlock(i, locks); } // merge adjacent border if (rank == NORTH) { MPI_Recv(northBuffer, 1, row_t, SOUTH, TAG, MPI_COMM_WORLD, &status); mergeGhost(northBuffer, matrix_b, SIZEY); setGhost(matrix_b, northBuffer, SIZEY+1); MPI_Send(northBuffer, 1, row_t, SOUTH, TAG, MPI_COMM_WORLD); } if (rank == SOUTH) { setGhost(matrix_b, southBuffer, 0); MPI_Send(southBuffer, 1, row_t, NORTH, TAG, MPI_COMM_WORLD); MPI_Recv(southBuffer, 1, row_t, NORTH, TAG, MPI_COMM_WORLD, &status); mergeGhost(southBuffer, matrix_b, 1); } // clear original adjacent border in matrix_a for (int i = 0; i < SIZEX + 2; i++) clearEntity(&matrix_a[i][rank == NORTH ? SIZEY + 1 : 0]); //some times it can not move back, then stay in the border transferInBorder(matrix_a, matrix_b); moveBackInBorder(matrix_b); // swap matrixes Entity **matrix_t = matrix_a; matrix_a = matrix_b; matrix_b = matrix_t; if (!benchmark) { updateCounter(matrix_a); syncCounter(); printCSV(n+1, rank); } } if (benchmark) printf("Thread: %d, Time: %f sec\n", omp_get_max_threads(), (double)(clock() - start) / CLOCKS_PER_SEC); destroyMatrix(matrix_a); destroyMatrix(matrix_b); free(northBuffer); free(southBuffer); MPI_Finalize(); return 0; }
int main(int argc, char *argv[]) { //double *a = malloc(dim*dim*sizeof(double)); //double a[9] = {1,0,0,0,1,0,0,0,1}; //double a[9] = {6,5,4,5,1,4,5,4,3}; //double a[16] = {1,2,3,4,5,6,3,8,9,3,4,6,3,7,1,9}; //double a[25] = {1,2,3,4,5,5,6,3,8,9,9,3,4,6,7,3,7,1,9,1,1,2,3,4,5}; //double a[9] = {1,2,3,4,5,6,3,8,9}; //double a[9] = {7.8102,4.4813,2.5607,0,-2.4327,3.0729,0,4,3}; //double a[9] = {5.4,4,7.7,3.5,-.7,2.8,-3.2,5.1,.8}; //double a[16] = {1,9,8,7,9,2,6,5,8,6,3,1,7,5,1,4}; //choose dimension of matrix here! int n = 10; //create a test matrix A using rand() double *a = malloc(n*n*sizeof(double)); double *atemp = a; int i; for(i = 0; i < n*n; i++, atemp++) { *atemp = ((double)rand()/(double)RAND_MAX); } //allocate space for output matrices B and U double *b = malloc(n*n*sizeof(double)); double *u = malloc(n*n*sizeof(double)); //run the main process for Hessenberg decomposition upperhes(a,n,u,b); //create matrix structures from input and output for testing Matrix aMat = createMatrix(a,n); Matrix bMat = createMatrix(b,n); Matrix uMat = createMatrix(u,n); Matrix uTran = createMatrix(u,n); transposeMatrix(uTran); //result2 = UAU^T Matrix result = initMatrix(n); Matrix result2 = initMatrix(n); matMul(uMat,aMat,result); matMul(result,uTran,result2); puts("A = "); printMatrix(aMat); puts("B = "); printMatrix(bMat); puts("UAU^T"); printMatrix(result2); //free malloc'ed memory before exiting free(a); free(b); free(u); destroyMatrix(aMat); destroyMatrix(bMat); destroyMatrix(uMat); destroyMatrix(uTran); destroyMatrix(result); destroyMatrix(result2); return 0; }
/** * Multiply two matrix and store the result into the result matrix given by parameter */ void multiplyCoolMatrix(Matrix matrixA, Matrix matrixB, Matrix* matrixResult) { int matrixSize = getSize(matrixA); createSquareMatrix(matrixResult, matrixSize); if (matrixSize > 2) { int subMatrixSize = matrixSize / 2; // Auxiliar variables Matrix auxMatrixA1 = NULL, auxMatrixB1 = NULL, auxMatrixA2 = NULL, auxMatrixB2 = NULL; Matrix auxContainer1 = NULL, auxContainer2 = NULL, solutionContainer = NULL; createSquareMatrix(&solutionContainer, subMatrixSize); createSquareMatrix(&auxMatrixA1, subMatrixSize); createSquareMatrix(&auxMatrixB1, subMatrixSize); createSquareMatrix(&auxMatrixA2, subMatrixSize); createSquareMatrix(&auxMatrixB2, subMatrixSize); createSquareMatrix(&auxContainer1, subMatrixSize); createSquareMatrix(&auxContainer2, subMatrixSize); int operation1Index = 0; for(short quadrant = 0; quadrant < 4; quadrant++) { operation1Index += 4; copyQuadrant(matrixA, &auxMatrixA1, operations[operation1Index -4]); copyQuadrant(matrixB, &auxMatrixB1, operations[operation1Index -3]); multiplyCoolMatrix(auxMatrixA1, auxMatrixB1, &auxContainer1); copyQuadrant(matrixA, &auxMatrixA2, operations[operation1Index -2]); copyQuadrant(matrixB, &auxMatrixB2, operations[operation1Index -1]); multiplyCoolMatrix(auxMatrixA2, auxMatrixB2, &auxContainer2); sumMatrix(auxContainer1, auxContainer2, &solutionContainer); assignCuadrant(solutionContainer, matrixResult, quadrant); } destroyMatrix(&auxMatrixA1); destroyMatrix(&auxMatrixB1); destroyMatrix(&auxMatrixA2); destroyMatrix(&auxMatrixB2); destroyMatrix(&auxContainer1); destroyMatrix(&auxContainer2); destroyMatrix(&solutionContainer); } else { int value1, value2, container = 0; int matrixSize = getSize(matrixA); for (int i = 0; i < matrixSize; i++) { for (int j = 0; j < matrixSize; j++) { for (int k= 0; k < matrixSize; k++) { getValue(matrixA, j, k, &value1); getValue(matrixB, k, i, &value2); container += value1 * value2; } assingValue(matrixResult, j, i, container); container = 0; } } } }
int main() { int i; double **K, **M, **Q, **T, **aux, **InverseM; double **A, **Q2, **R; double **L, **U; double *eigenValues, **eigenVectors; int aux2; h = (double) (b - a) / n; alfa = (double) a2 / a1; beta = (double) b2 / b1; if (ua == 0 && ub == 0) { tam = n - 1; aux2 = 1; } else { tam = n + 1; aux2 = 0; } createVector(&x, tam); x[0] = a + (h * aux2); for (i = 1; i < tam; i++) x[i] = x[i - 1] + h; createMatrix(&K, tam, tam); createMatrix(&M, tam, tam); createMatrix(&InverseM, tam, tam); createMatrix(&Q, tam, tam); createMatrix(&T, tam, tam); createMatrix(&aux, tam, tam); createMatrix(&A, tam, tam); createMatrix(&Q2, tam, tam); createMatrix(&R, tam, tam); createMatrix(&L, tam, tam); createMatrix(&U, tam, tam); createVector(&eigenValues, tam); createMatrix(&eigenVectors, tam, tam); funcT = functionT; funcT2 = functionT2; funcQ = functionQ; funcM = functionM; triDiagonalMatrix(tam, funcT, funcT2, funcT, T); diagonalMatrix(tam, funcQ, Q); diagonalMatrix(tam, funcM, M); //inverseMatrix(tam, M, InverseM); escaleByMatrix(tam, tam, 1.0/pow(h, 2), T, aux); addMatrix(tam, tam, aux, Q, K); copyMatrix(tam,tam,K,A); //multiplicationMatrix(tam, tam, InverseM, K, A); /*matrixLU(tam, A, L, U); printMatrix(tam, tam, A); printf("\n"); printMatrix(tam, tam, L); printf("\n"); L[0][0] = 0; L[0][1] = 0; L[1][0] = 0; L[1][1] = 0; printf("%d\n", isCeroDiagonalInf(L,tam)); printMatrix(tam, tam, U);*/ factorizeUL(tam,A,eigenValues,eigenVectors); printf("Eigenvalues:\n"); printVector(tam, eigenValues); printf("\nEigenvectors:\n"); printMatrix(tam,tam, eigenVectors); destroyMatrix(&K, tam, tam); destroyMatrix(&M, tam, tam); destroyMatrix(&InverseM, tam, tam); destroyMatrix(&Q, tam, tam); destroyMatrix(&T, tam, tam); destroyMatrix(&aux, tam, tam); destroyMatrix(&A, tam, tam); destroyMatrix(&Q2, tam, tam); destroyMatrix(&R, tam, tam); destroyMatrix(&L, tam, tam); destroyMatrix(&U, tam, tam); destroyVector(&x, tam); destroyVector(&eigenValues, tam); destroyMatrix(&eigenVectors, tam, tam); return 0; }
AUD_Int32s perf_dtw_mfcc() { char inputPath[256] = { 0, }; char keywordPath[256] = { 0, }; AUD_Double threshold, minThreshold, maxThreshold, stepThreshold; AUD_Bool isRecognized = AUD_FALSE; AUD_Int32s data; AUD_Error error = AUD_ERROR_NONE; setbuf( stdin, NULL ); AUDLOG( "pls give keyword wav stream's path:\n" ); keywordPath[0] = '\0'; data = scanf( "%s", keywordPath ); AUDLOG( "keyword path is: %s\n", keywordPath ); setbuf( stdin, NULL ); AUDLOG( "pls give test wav stream's path:\n" ); inputPath[0] = '\0'; data = scanf( "%s", inputPath ); AUDLOG( "test stream path is: %s\n", inputPath ); setbuf( stdin, NULL ); AUDLOG( "pls give min test threshold:\n" ); data = scanf( "%lf", &minThreshold ); AUDLOG( "min threshold is: %.2f\n", minThreshold ); setbuf( stdin, NULL ); AUDLOG( "pls give max test threshold:\n" ); data = scanf( "%lf", &maxThreshold ); AUDLOG( "max threshold is: %.2f\n", maxThreshold ); setbuf( stdin, NULL ); AUDLOG( "pls give test threshold step:\n" ); data = scanf( "%lf", &stepThreshold ); AUDLOG( "threshold step is: %.2f\n", stepThreshold ); AUDLOG( "\n\n" ); // file & directory operation, linux dependent entry *pEntry = NULL; dir *pDir = NULL; entry *pKeywordEntry = NULL; dir *pKeywordDir = NULL; AUD_Feature keywordFeature; AUD_Int32s keywordSampleNum = 0; AUD_Int32s keywordWinNum = 0; AUD_Int8s keywordFile[256] = { 0, }; AUD_Int8s keywordName[256] = { 0, }; AUD_Int32s keywordID = 0; AUD_DTWSession dtwSession; AUD_Double score = 0.; AUD_Int32s sampleNum; AUD_Int32s keywordBufLen = 0; AUD_Int16s *pKeywordBuf = NULL; AUD_Int32s bufLen = 0; AUD_Int16s *pBuf = NULL; AUD_Int32s frameStride = FRAME_STRIDE; AUD_Int32s j; AUD_Int32s ret; // init performance benchmark void *hBenchmark = NULL; char logName[256] = { 0, }; snprintf( logName, 256, "dtw-mfcc-%d-%d-%d-%d", WOV_DTW_TYPE, WOV_DISTANCE_METRIC, WOV_DTWTRANSITION_STYLE, WOV_DTWSCORING_METHOD ); ret = benchmark_init( &hBenchmark, (AUD_Int8s*)logName, keywords, sizeof(keywords) / sizeof(keywords[0]) ); AUD_ASSERT( ret == 0 ); for ( threshold = minThreshold; threshold < maxThreshold + stepThreshold; threshold += stepThreshold ) { ret = benchmark_newthreshold( hBenchmark, threshold ); AUD_ASSERT( ret == 0 ); AUDLOG( "***********************************************\n" ); AUDLOG( "keyword training start...\n" ); pKeywordDir = openDir( (const char*)keywordPath ); keywordID = 0; while ( ( pKeywordEntry = scanDir( pKeywordDir ) ) ) { // train keyword keywordBufLen = SAMPLE_RATE * BYTES_PER_SAMPLE * 10; pKeywordBuf = (AUD_Int16s*)calloc( keywordBufLen, 1 ); AUD_ASSERT( pKeywordBuf ); snprintf( (char*)keywordFile, 256, "%s/%s", (const char*)keywordPath, pKeywordEntry->name ); keywordSampleNum = readWavFromFile( keywordFile, pKeywordBuf, keywordBufLen ); if ( keywordSampleNum < 0 ) { continue; } // front end processing // pre-emphasis sig_preemphasis( pKeywordBuf, pKeywordBuf, keywordSampleNum ); for ( j = 0; j * frameStride + FRAME_LEN <= keywordSampleNum; j++ ) { ; } AUDLOG( "pattern[%d: %s] valid frame number[%d]\n", keywordID, keywordFile, j ); keywordWinNum = j; keywordFeature.featureMatrix.rows = keywordWinNum - MFCC_DELAY; keywordFeature.featureMatrix.cols = MFCC_FEATDIM; keywordFeature.featureMatrix.dataType = AUD_DATATYPE_INT32S; ret = createMatrix( &(keywordFeature.featureMatrix) ); AUD_ASSERT( ret == 0 ); keywordFeature.featureNorm.len = keywordWinNum - MFCC_DELAY; keywordFeature.featureNorm.dataType = AUD_DATATYPE_INT64S; ret = createVector( &(keywordFeature.featureNorm) ); AUD_ASSERT( ret == 0 ); void *hKeywordMfccHandle = NULL; // init mfcc handle error = mfcc16s32s_init( &hKeywordMfccHandle, FRAME_LEN, WINDOW_TYPE, MFCC_ORDER, frameStride, SAMPLE_RATE, COMPRESS_TYPE ); AUD_ASSERT( error == AUD_ERROR_NONE ); // calc MFCC feature error = mfcc16s32s_calc( hKeywordMfccHandle, pKeywordBuf, keywordSampleNum, &keywordFeature ); AUD_ASSERT( error == AUD_ERROR_NONE ); // deinit mfcc handle error = mfcc16s32s_deinit( &hKeywordMfccHandle ); AUD_ASSERT( error == AUD_ERROR_NONE ); strncpy( (char*)keywordName, pKeywordEntry->name, 255 ); ret = benchmark_newtemplate( hBenchmark, keywordName ); AUD_ASSERT( ret == 0 ); keywordID++; keywordBufLen = 0; free( pKeywordBuf ); pKeywordBuf = NULL; AUDLOG( "keyword training finish...\n" ); // recognize pEntry = NULL; pDir = NULL; pDir = openDir( (const char*)inputPath ); #if PERF_PROFILE AUD_Tick t; #endif while ( ( pEntry = scanDir( pDir ) ) ) { AUD_Int8s inputStream[256] = { 0, }; void *hMfccHandle = NULL; AUD_Summary fileSummary; snprintf( (char*)inputStream, 256, "%s/%s", (const char*)inputPath, pEntry->name ); ret = parseWavFromFile( inputStream, &fileSummary ); if ( ret < 0 ) { continue; } AUD_ASSERT( fileSummary.channelNum == CHANNEL_NUM && fileSummary.bytesPerSample == BYTES_PER_SAMPLE && fileSummary.sampleRate == SAMPLE_RATE ); AUDLOG( "input stream: %s\n", inputStream ); bufLen = fileSummary.dataChunkBytes; pBuf = (AUD_Int16s*)calloc( bufLen + 100, 1 ); AUD_ASSERT( pBuf ); #if PERF_PROFILE t = -time_gettick(); #endif sampleNum = readWavFromFile( inputStream, pBuf, bufLen ); AUD_ASSERT( sampleNum > 0 ); #if PERF_PROFILE t += time_gettick(); AUDLOG( "PERF: read wav files takes %.2f ms\n", t / 1000. ); #endif // front end processing // pre-emphasis #if PERF_PROFILE t = -time_gettick(); #endif sig_preemphasis( pBuf, pBuf, sampleNum ); #if PERF_PROFILE t += time_gettick(); AUDLOG( "PERF: pre-emphasis takes %.2f ms\n", t / 1000. ); #endif for ( j = 0; j * frameStride + FRAME_LEN <= sampleNum; j++ ) { ; } #if PERF_PROFILE t = -time_gettick(); #endif AUD_Feature inFeature; inFeature.featureMatrix.rows = j - MFCC_DELAY; inFeature.featureMatrix.cols = MFCC_FEATDIM; inFeature.featureMatrix.dataType = AUD_DATATYPE_INT32S; ret = createMatrix( &(inFeature.featureMatrix) ); AUD_ASSERT( ret == 0 ); #if PERF_PROFILE t += time_gettick(); AUDLOG( "PERF: create feature matrix takes %.2f ms\n", t / 1000. ); #endif #if PERF_PROFILE t = -time_gettick(); #endif inFeature.featureNorm.len = j - MFCC_DELAY; inFeature.featureNorm.dataType = AUD_DATATYPE_INT64S; ret = createVector( &(inFeature.featureNorm) ); AUD_ASSERT( ret == 0 ); #if PERF_PROFILE t += time_gettick(); AUDLOG( "PERF: create feature norm takes %.2f ms\n", t / 1000. ); #endif // init mfcc handle #if PERF_PROFILE t = -time_gettick(); #endif error = mfcc16s32s_init( &hMfccHandle, FRAME_LEN, WINDOW_TYPE, MFCC_ORDER, frameStride, SAMPLE_RATE, COMPRESS_TYPE ); AUD_ASSERT( error == AUD_ERROR_NONE ); #if PERF_PROFILE t += time_gettick(); AUDLOG( "PERF: mfcc init takes %.2f ms\n", t / 1000. ); #endif #if PERF_PROFILE t = -time_gettick(); #endif error = mfcc16s32s_calc( hMfccHandle, pBuf, sampleNum, &inFeature ); AUD_ASSERT( error == AUD_ERROR_NONE ); #if PERF_PROFILE t += time_gettick(); AUDLOG( "PERF: mfcc calc takes %.2f ms\n", t / 1000. ); #endif // init dtw match dtwSession.dtwType = WOV_DTW_TYPE; dtwSession.distType = WOV_DISTANCE_METRIC; dtwSession.transitionType = WOV_DTWTRANSITION_STYLE; error = dtw_initsession( &dtwSession, &keywordFeature, inFeature.featureMatrix.rows ); AUD_ASSERT( error == AUD_ERROR_NONE ); #if PERF_PROFILE t = -time_gettick(); #endif error = dtw_updatefrmcost( &dtwSession, &inFeature ); AUD_ASSERT( error == AUD_ERROR_NONE ); #if PERF_PROFILE t += time_gettick(); AUDLOG( "PERF: dtw update frame cost takes %.2f ms\n", t / 1000. ); #endif #if PERF_PROFILE t = -time_gettick(); #endif error = dtw_match( &dtwSession, WOV_DTWSCORING_METHOD, &score, NULL ); AUD_ASSERT( error == AUD_ERROR_NONE ); #if PERF_PROFILE t += time_gettick(); AUDLOG( "PERF: dtw match takes %.2f ms\n", t / 1000. ); #endif error = dtw_deinitsession( &dtwSession ); AUD_ASSERT( error == AUD_ERROR_NONE ); // deinit mfcc handle error = mfcc16s32s_deinit( &hMfccHandle ); AUD_ASSERT( error == AUD_ERROR_NONE ); ret = destroyMatrix( &(inFeature.featureMatrix) ); AUD_ASSERT( ret == 0 ); ret = destroyVector( &(inFeature.featureNorm) ); AUD_ASSERT( ret == 0 ); isRecognized = AUD_FALSE; if ( score <= threshold ) { isRecognized = AUD_TRUE; } AUDLOG( "score: %.2f\n", score ); // insert log entry ret = benchmark_additem( hBenchmark, (AUD_Int8s*)pEntry->name, score, isRecognized ); AUD_ASSERT( ret == 0 ); bufLen = 0; free( pBuf ); pBuf = NULL; } closeDir( pDir ); pDir = NULL; ret = benchmark_finalizetemplate( hBenchmark ); AUD_ASSERT( ret == 0 ); ret = destroyMatrix( &(keywordFeature.featureMatrix) ); AUD_ASSERT( ret == 0 ); ret = destroyVector( &(keywordFeature.featureNorm) ); AUD_ASSERT( ret == 0 ); } closeDir( pKeywordDir ); pKeywordDir = NULL; ret = benchmark_finalizethreshold( hBenchmark ); AUD_ASSERT( ret == 0 ); } ret = benchmark_free( &hBenchmark ); AUD_ASSERT( ret == 0 ); AUDLOG( "DTW-MFCC Benchmark finished\n" ); return 0; }
SmithWatermanDP::~SmithWatermanDP() { destroyMatrix(); deleteBtMatrix(); }
AUD_Int32s wov_adapt_gmm_si() { AUD_Error error = AUD_ERROR_NONE; AUD_Int32s ret = 0; AUD_Int8s wavPath[256] = { 0, }; AUD_Int32s data; setbuf( stdout, NULL ); setbuf( stdin, NULL ); AUDLOG( "pls give adapt wav stream's folder path:\n" ); wavPath[0] = '\0'; data = scanf( "%s", wavPath ); AUDLOG( "adapt wav stream's folder path is: %s\n", wavPath ); // step 1: read UBM model from file void *hUbm = NULL; FILE *fpUbm = fopen( WOV_UBM_GMMMODEL_FILE, "rb" ); if ( fpUbm == NULL ) { AUDLOG( "cannot open ubm model file: [%s]\n", WOV_UBM_GMMMODEL_FILE ); return AUD_ERROR_IOFAILED; } error = gmm_import( &hUbm, fpUbm ); AUD_ASSERT( error == AUD_ERROR_NONE ); fclose( fpUbm ); fpUbm = NULL; // AUDLOG( "ubm GMM as:\n" ); // gmm_show( hUbm ); AUD_Int32s i = 0, j = 0; entry *pEntry = NULL; dir *pDir = NULL; AUD_Int32s totalWinNum = 0; pDir = openDir( (const char*)wavPath ); if ( pDir == NULL ) { AUDLOG( "cannot open folder: %s\n", wavPath ); return -1; } while ( ( pEntry = scanDir( pDir ) ) ) { AUD_Int8s keywordFile[256] = { 0, }; AUD_Summary fileSummary; AUD_Int32s sampleNum = 0; snprintf( (char*)keywordFile, 256, "%s/%s", wavPath, pEntry->name ); // AUDLOG( "%s\n", keywordFile ); ret = parseWavFromFile( keywordFile, &fileSummary ); if ( ret < 0 ) { continue; } AUD_ASSERT( fileSummary.channelNum == CHANNEL_NUM && fileSummary.bytesPerSample == BYTES_PER_SAMPLE && fileSummary.sampleRate == SAMPLE_RATE ); // request memeory for template sampleNum = fileSummary.dataChunkBytes / fileSummary.bytesPerSample; for ( j = 0; j * FRAME_STRIDE + FRAME_LEN <= sampleNum; j++ ) { ; } j = j - MFCC_DELAY; totalWinNum += j; } closeDir( pDir ); pDir = NULL; AUD_Matrix featureMatrix; featureMatrix.rows = totalWinNum; featureMatrix.cols = MFCC_FEATDIM; featureMatrix.dataType = AUD_DATATYPE_INT32S; ret = createMatrix( &featureMatrix ); AUD_ASSERT( ret == 0 ); AUD_Int32s currentRow = 0; pDir = openDir( (const char*)wavPath ); while ( ( pEntry = scanDir( pDir ) ) ) { AUD_Int8s keywordFile[256] = { 0, }; AUD_Summary fileSummary; AUD_Int32s sampleNum = 0; void *hMfccHandle = NULL; snprintf( (char*)keywordFile, 256, "%s/%s", wavPath, pEntry->name ); // AUDLOG( "%s\n", keywordFile ); ret = parseWavFromFile( keywordFile, &fileSummary ); if ( ret < 0 ) { continue; } AUD_ASSERT( fileSummary.channelNum == CHANNEL_NUM && fileSummary.bytesPerSample == BYTES_PER_SAMPLE && fileSummary.sampleRate == SAMPLE_RATE ); AUD_Int32s bufLen = fileSummary.dataChunkBytes; AUD_Int16s *pBuf = (AUD_Int16s*)calloc( bufLen, 1 ); AUD_ASSERT( pBuf ); sampleNum = readWavFromFile( (AUD_Int8s*)keywordFile, pBuf, bufLen ); AUD_ASSERT( sampleNum > 0 ); // pre-processing // pre-emphasis sig_preemphasis( pBuf, pBuf, sampleNum ); // calc framing number for ( j = 0; j * FRAME_STRIDE + FRAME_LEN <= sampleNum; j++ ) { ; } // XXX: select salient frames AUD_Feature feature; feature.featureMatrix.rows = j - MFCC_DELAY; feature.featureMatrix.cols = MFCC_FEATDIM; feature.featureMatrix.dataType = AUD_DATATYPE_INT32S; feature.featureMatrix.pInt32s = featureMatrix.pInt32s + currentRow * feature.featureMatrix.cols; feature.featureNorm.len = j - MFCC_DELAY; feature.featureNorm.dataType = AUD_DATATYPE_INT64S; ret = createVector( &(feature.featureNorm) ); AUD_ASSERT( ret == 0 ); error = mfcc16s32s_init( &hMfccHandle, FRAME_LEN, WINDOW_TYPE, MFCC_ORDER, FRAME_STRIDE, SAMPLE_RATE, COMPRESS_TYPE ); AUD_ASSERT( error == AUD_ERROR_NONE ); error = mfcc16s32s_calc( hMfccHandle, pBuf, sampleNum, &feature ); AUD_ASSERT( error == AUD_ERROR_NONE ); error = mfcc16s32s_deinit( &hMfccHandle ); AUD_ASSERT( error == AUD_ERROR_NONE ); free( pBuf ); pBuf = NULL; bufLen = 0; ret = destroyVector( &(feature.featureNorm) ); AUD_ASSERT( ret == 0 ); currentRow += feature.featureMatrix.rows; } closeDir( pDir ); pDir = NULL; AUD_Matrix llrMatrix; llrMatrix.rows = totalWinNum; llrMatrix.cols = gmm_getmixnum( hUbm ); llrMatrix.dataType = AUD_DATATYPE_DOUBLE; ret = createMatrix( &llrMatrix ); AUD_ASSERT( ret == 0 ); AUD_Double llr = 0.; for ( j = 0; j < featureMatrix.rows; j++ ) { AUD_Vector componentLLR; componentLLR.len = llrMatrix.cols; componentLLR.dataType = AUD_DATATYPE_DOUBLE; componentLLR.pDouble = llrMatrix.pDouble + j * llrMatrix.cols; llr = gmm_llr( hUbm, &(featureMatrix), j, &componentLLR ); } AUD_Vector sumLlr; sumLlr.len = llrMatrix.cols; sumLlr.dataType = AUD_DATATYPE_DOUBLE; ret = createVector( &sumLlr ); AUD_ASSERT( ret == 0 ); AUD_Double *pSumLlr = sumLlr.pDouble; for ( j = 0; j < llrMatrix.cols; j++ ) { pSumLlr[j] = llrMatrix.pDouble[j]; } for ( i = 1; i < llrMatrix.rows; i++ ) { for ( j = 0; j < llrMatrix.cols; j++ ) { pSumLlr[j] = logadd( pSumLlr[j], *(llrMatrix.pDouble + i * llrMatrix.cols + j) ); } } #if 0 AUD_Vector bestIndex; bestIndex.len = TOP_N; bestIndex.dataType = AUD_DATATYPE_INT32S; ret = createVector( &bestIndex ); AUD_ASSERT( ret == 0 ); // get top TOP_N component ret = sortVector( &sumLlr, &bestIndex ); AUD_ASSERT( ret == 0 ); #else llr = pSumLlr[0]; for ( j = 1; j < sumLlr.len; j++ ) { llr = logadd( llr, pSumLlr[j] ); } // AUDLOG( "llr: %.f\n", llr ); AUD_Vector sortIndex; sortIndex.len = sumLlr.len; sortIndex.dataType = AUD_DATATYPE_INT32S; ret = createVector( &sortIndex ); AUD_ASSERT( ret == 0 ); ret = sortVector( &sumLlr, &sortIndex ); AUD_ASSERT( ret == 0 ); int num = 0; double val = 0.; for ( i = 0; i < sortIndex.len; i++ ) { // ln( 0.001 ) ~= -7. val = pSumLlr[sortIndex.pInt32s[i]] - llr + 7.; // AUDLOG( "%f, \n", val ); if ( val < 0 ) { break; } num++; } // AUDLOG( "\n" ); AUD_ASSERT( num > 0 ); AUDLOG( "computed component num: %d\n", num ); num = AUD_MAX( num, TOP_N ); AUDLOG( "normalized component num: %d\n", num ); AUD_Vector bestIndex; bestIndex.len = num; bestIndex.dataType = AUD_DATATYPE_INT32S; bestIndex.pInt32s = sortIndex.pInt32s; #endif int slash = '/'; char *ptr = strrchr( (char*)wavPath, slash ); ptr++; // select imposter GMM void *hImposterGmm = NULL; AUD_Int8s imposterGmmName[256] = { 0, }; snprintf( (char*)imposterGmmName, 256, "%s-imposter", ptr ); error = gmm_select( &hImposterGmm, hUbm, &bestIndex, 0 | GMM_INVERTSELECT_MASK, imposterGmmName ); AUD_ASSERT( error == AUD_ERROR_NONE ); gmm_show( hImposterGmm ); // export gmm char imposterFile[256] = { 0 }; snprintf( imposterFile, 256, "%s/%s-imposter.gmm", WOV_IMPOSTER_GMMMODEL_DIR, ptr ); AUDLOG( "Export imposter GMM Model to: %s\n", imposterFile ); FILE *fpImposterGmm = fopen( imposterFile, "wb" ); AUD_ASSERT( fpImposterGmm ); error = gmm_export( hImposterGmm, fpImposterGmm ); AUD_ASSERT( error == AUD_ERROR_NONE ); AUDLOG( "Export imposter GMM Model File Done\n" ); fclose( fpImposterGmm ); fpImposterGmm = NULL; error = gmm_free( &hImposterGmm ); AUD_ASSERT( error == AUD_ERROR_NONE ); // select keyword GMM void *hAdaptedGmm = NULL; AUD_Int8s adaptedGmmName[256] = { 0, }; snprintf( (char*)adaptedGmmName, 256, "%s", ptr ); // AUDLOG( "%s\n", adaptedGmmName ); error = gmm_select( &hAdaptedGmm, hUbm, &bestIndex, 0, adaptedGmmName ); AUD_ASSERT( error == AUD_ERROR_NONE ); ret = destroyVector( &sumLlr ); AUD_ASSERT( ret == 0 ); ret = destroyMatrix( &llrMatrix ); AUD_ASSERT( ret == 0 ); #if 0 ret = destroyVector( &bestIndex ); AUD_ASSERT( ret == 0 ); #else ret = destroyVector( &sortIndex ); AUD_ASSERT( ret == 0 ); #endif #if 1 // adapt GMM error = gmm_adapt( hAdaptedGmm, &featureMatrix ); AUD_ASSERT( error == AUD_ERROR_NONE ); #endif gmm_show( hAdaptedGmm ); // export gmm char modelFile[256] = { 0 }; snprintf( modelFile, 256, "%s/%s.gmm", WOV_KEYWORD_GMMMODEL_DIR, ptr ); AUDLOG( "Export GMM Model to: %s\n", modelFile ); FILE *fpGmm = fopen( modelFile, "wb" ); AUD_ASSERT( fpGmm ); error = gmm_export( hAdaptedGmm, fpGmm ); AUD_ASSERT( error == AUD_ERROR_NONE ); AUDLOG( "Export GMM Model File Done\n" ); fclose( fpGmm ); fpGmm = NULL; ret = destroyMatrix( &featureMatrix ); AUD_ASSERT( ret == 0 ); error = gmm_free( &hAdaptedGmm ); AUD_ASSERT( error == AUD_ERROR_NONE ); error = gmm_free( &hUbm ); AUD_ASSERT( error == AUD_ERROR_NONE ); AUDLOG( "keyword model adapt2 done\n" ); return 0; }
int main(int argc, char *argv[]) { //choose dimension here int dim = 20; //allocate memory for A, U and B double *a = malloc(dim*dim*sizeof(double)); double *u = malloc(dim*dim*sizeof(double)); double *b = malloc(dim*dim*sizeof(double)); //create a test matrix A using rand() double *atemp = a; int i; for(i = 0; i < dim*dim; i++, atemp++) { *atemp = ((double)rand()/(double)RAND_MAX); } //invert matrix by double GS procedure inv_double_gs(a,dim,u,b); //print out matrices A, U and B (A inverse) double *a2 = a; double *u2 = u; double *b2 = b; for(i = 0; i < dim*dim; i++, a2++) { if(!(i % dim)) puts(""); printf("%f ",*a2); } puts(""); for(i = 0; i < dim*dim; i++, u2++) { if(!(i % dim)) puts(""); printf("%f ",*u2); } puts(""); for(i = 0; i < dim*dim; i++, b2++) { if(!(i % dim)) puts(""); printf("%f ",*b2); } puts(""); // check if A*B = I Matrix amat = createMatrix(a,dim); Matrix bmat = createMatrix(b,dim); Matrix cmat = initMatrix(dim); matMul(amat,bmat,cmat); puts(""); printMatrix(cmat); destroyMatrix(amat); destroyMatrix(bmat); destroyMatrix(cmat); //free all memory free(a); free(u); free(b); return 0; }
/** * Calcuate the next life cycle for all the cells with the board projected onto a torus. */ void calculateLifeTorus(LifeBoard *LifeBoard) { if (LifeBoard == NULL) { return; } int boardSize = LifeBoard->boardSize; boolean **newBoard = (boolean **)malloc(sizeof(boolean *) * boardSize); for (int i = 0; i < boardSize; i++) { newBoard[i] = (boolean *)malloc(sizeof(boolean) * boardSize); } for (int x = 0; x < boardSize; x++) { for (int y = 0; y < boardSize; y++) { int count = 0; /* We need to map boardSize + 1 to 0 and -1 to boardSize */ int maxBoardSize = boardSize - 1; int xPlusOne = (x + 1) > maxBoardSize ? 0 : (x + 1); int xMinusOne = (x - 1) < 0 ? maxBoardSize : (x - 1); int yPlusOne = (y + 1) > maxBoardSize ? 0 : (y + 1); int yMinusOne = (y - 1) < 0 ? maxBoardSize : (y - 1); if (LifeBoard->matrix[xMinusOne][yPlusOne]) { count++; } if (LifeBoard->matrix[x][yPlusOne]) { count++; } if (LifeBoard->matrix[xPlusOne][yPlusOne]) { count++; } if (LifeBoard->matrix[xMinusOne][y]) { count++; } if (LifeBoard->matrix[xPlusOne][y]) { count++; } if (LifeBoard->matrix[xMinusOne][yMinusOne]) { count++; } if (LifeBoard->matrix[x][yMinusOne]) { count++; } if (LifeBoard->matrix[xPlusOne][yMinusOne]) { count++; } /* The rules for the game of life are : Any live cell with fewer than two neighbors dies of loneliness. Any live cell with more than three neighbors dies of crowding. Any dead cell with exactly three neighbors comes to life. Any live cell with two or three neighbors lives, unchanged, to the next generation. */ if (LifeBoard->matrix[x][y]) { if (count < 2) { newBoard[x][y] = false; } else if (count > 3) { newBoard[x][y] = false; } else { newBoard[x][y] = LifeBoard->matrix[x][y]; } } else { if (count == 3) { newBoard[x][y] = true; } else { newBoard[x][y] = false; } } } } destroyMatrix(LifeBoard->matrix, LifeBoard->boardSize); LifeBoard->matrix = newBoard; }
AUD_Int32s train_keyword_hmm( const AUD_Int8s *pKeywordFile, AUD_Int8s *pHmmName ) { AUD_Error error = AUD_ERROR_NONE; // step 1: read garbage model from file void *hGarbageGmm = NULL; FILE *fpGarbage = fopen( (char*)WOV_UBM_GMMHMMMODEL_FILE, "rb" ); if ( fpGarbage == NULL ) { AUDLOG( "cannot open gmm model file: [%s]\n", WOV_UBM_GMMHMMMODEL_FILE ); return AUD_ERROR_IOFAILED; } error = gmm_import( &hGarbageGmm, fpGarbage ); AUD_ASSERT( error == AUD_ERROR_NONE ); fclose( fpGarbage ); fpGarbage = NULL; // AUDLOG( "garbage GMM as:\n" ); // gmm_show( hGarbageGmm ); // step 2: read template stream & extract MFCC feature vector AUD_Int32s sampleNum = 0; AUD_Int32s bufLen = SAMPLE_RATE * BYTES_PER_SAMPLE * 10; AUD_Int16s *pBuf = (AUD_Int16s*)calloc( bufLen, 1 ); AUD_ASSERT( pBuf ); AUD_Int32s ret; // read stream from file sampleNum = readWavFromFile( (AUD_Int8s*)pKeywordFile, pBuf, bufLen ); AUD_ASSERT( sampleNum > 0 ); AUD_Int32s i = 0, j = 0, k = 0, m = 0; // front end processing // pre-emphasis sig_preemphasis( pBuf, pBuf, sampleNum ); // calc frame number for ( j = 0; j * FRAME_STRIDE + FRAME_LEN <= sampleNum; j++ ) { ; } AUD_Feature feature; feature.featureMatrix.rows = j - MFCC_DELAY; feature.featureMatrix.cols = MFCC_FEATDIM; feature.featureMatrix.dataType = AUD_DATATYPE_INT32S; ret = createMatrix( &(feature.featureMatrix) ); AUD_ASSERT( ret == 0 ); feature.featureNorm.len = j - MFCC_DELAY; feature.featureNorm.dataType = AUD_DATATYPE_INT64S; ret = createVector( &(feature.featureNorm) ); AUD_ASSERT( ret == 0 ); // init mfcc handle void *hMfccHandle = NULL; error = mfcc16s32s_init( &hMfccHandle, FRAME_LEN, WINDOW_TYPE, MFCC_ORDER, FRAME_STRIDE, SAMPLE_RATE, COMPRESS_TYPE ); AUD_ASSERT( error == AUD_ERROR_NONE ); // calc MFCC feature error = mfcc16s32s_calc( hMfccHandle, pBuf, sampleNum, &feature ); AUD_ASSERT( error == AUD_ERROR_NONE ); free( pBuf ); pBuf = NULL; // step 3: for each feature vector, get the bestN most likelihood component indices from GMM AUD_Vector componentLLR; componentLLR.len = gmm_getmixnum( hGarbageGmm ); componentLLR.dataType = AUD_DATATYPE_DOUBLE; ret = createVector( &componentLLR ); AUD_ASSERT( ret == 0 ); AUD_Matrix indexTable; indexTable.rows = feature.featureMatrix.rows ; indexTable.cols = WOV_KEYWORD_GMMMODEL_ORDER; indexTable.dataType = AUD_DATATYPE_INT32S; ret = createMatrix( &indexTable ); AUD_ASSERT( ret == 0 ); AUD_Matrix llrTable; llrTable.rows = feature.featureMatrix.rows; llrTable.cols = WOV_KEYWORD_GMMMODEL_ORDER; llrTable.dataType = AUD_DATATYPE_DOUBLE; ret = createMatrix( &llrTable ); AUD_ASSERT( ret == 0 ); AUD_Double totalLLR; for ( i = 0; i < feature.featureMatrix.rows; i++ ) { totalLLR = gmm_llr( hGarbageGmm, &(feature.featureMatrix), i, &componentLLR ); #if 0 showVector( &componentLLR ); #endif // sort the bestN likelihood AUD_Int32s *pIndex = indexTable.pInt32s + i * indexTable.cols; AUD_Double *pLLR = llrTable.pDouble + i * llrTable.cols; for ( j = 0; j < WOV_KEYWORD_GMMMODEL_ORDER; j++ ) { pIndex[j] = -1; pLLR[j] = 0.; } for ( j = 0; j < componentLLR.len; j++ ) { for ( k = 0; k < WOV_KEYWORD_GMMMODEL_ORDER; k++ ) { if ( pIndex[k] == -1 ) { pIndex[k] = j; pLLR[k] = componentLLR.pDouble[j]; break; } else if ( componentLLR.pDouble[j] > pLLR[k] ) { for ( m = WOV_KEYWORD_GMMMODEL_ORDER - 1; m > k ; m-- ) { pIndex[m] = pIndex[m - 1]; pLLR[m] = pLLR[m - 1]; } pIndex[k] = j; pLLR[k] = componentLLR.pDouble[j]; break; } } } } #if 0 AUDLOG( "index table( %s, %s, %d ):\n", __FILE__, __FUNCTION__, __LINE__ ); showMatrix( &indexTable ); AUDLOG( "llr table( %s, %s, %d ):\n", __FILE__, __FUNCTION__, __LINE__ ); showMatrix( &llrTable ); #endif ret = destroyVector( &componentLLR ); AUD_ASSERT( ret == 0 ); // step 4: cluster GMM AUD_Int32s *pClusterLabel = (AUD_Int32s*)calloc( sizeof(AUD_Int32s) * feature.featureMatrix.rows, 1 ); AUD_ASSERT( pClusterLabel ); error = gmm_cluster( hGarbageGmm, &indexTable, WOV_GMM_CLUSTER_THRESHOLD, pClusterLabel ); AUD_ASSERT( error == AUD_ERROR_NONE ); AUD_Int32s stateNum = pClusterLabel[feature.featureMatrix.rows - 1]; AUD_ASSERT( stateNum >= 5 ); // step 5: select and build state GMM void **phKeywordGmms = (void**)calloc( sizeof(void*) * stateNum, 1 ); AUD_ASSERT( phKeywordGmms ); AUD_Vector indexVector; indexVector.len = WOV_KEYWORD_GMMMODEL_ORDER; indexVector.dataType = AUD_DATATYPE_INT32S; ret = createVector( &indexVector ); AUD_ASSERT( ret == 0 ); AUD_Vector llrVector; llrVector.len = WOV_KEYWORD_GMMMODEL_ORDER; llrVector.dataType = AUD_DATATYPE_DOUBLE; ret = createVector( &llrVector ); AUD_ASSERT( ret == 0 ); int start = 0, end = 0; for ( i = 0; i < stateNum; i++ ) { for ( j = 0; j < indexVector.len; j++ ) { indexVector.pInt32s[j] = -1; llrVector.pInt32s[j] = 1.; } for ( j = start; j < feature.featureMatrix.rows; j++ ) { if ( pClusterLabel[j] != i ) { break; } } end = j; for ( k = start * llrTable.cols; k < end * llrTable.cols; k++ ) { for ( m = 0; m < indexVector.len; m++ ) { if ( llrTable.pDouble[k] == llrVector.pDouble[m] && indexTable.pInt32s[k] == indexVector.pInt32s[m] ) { break; } else if ( indexVector.pInt32s[m] == -1 || llrTable.pDouble[k] > llrVector.pDouble[m] ) { for ( int n = indexVector.len - 1; n > m ; n-- ) { indexVector.pInt32s[n] = indexVector.pInt32s[n - 1]; llrVector.pDouble[n] = llrVector.pDouble[n - 1]; } indexVector.pInt32s[m] = indexTable.pInt32s[k]; llrVector.pDouble[m] = llrTable.pDouble[k]; break; } } } // AUDLOG( "Final GMM indices for state[%d]:\n", i ); // showVector( &indexVector ); AUD_Int8s gmmName[256] = { 0, }; sprintf( (char*)gmmName, "state%d", i ); error = gmm_select( &phKeywordGmms[i], hGarbageGmm, &indexVector, 0, gmmName ); AUD_ASSERT( error == AUD_ERROR_NONE ); start = end; } ret = destroyMatrix( &indexTable ); AUD_ASSERT( ret == 0 ); ret = destroyMatrix( &llrTable ); AUD_ASSERT( ret == 0 ); ret = destroyVector( &indexVector ); AUD_ASSERT( ret == 0 ); ret = destroyVector( &llrVector ); AUD_ASSERT( ret == 0 ); free( pClusterLabel ); pClusterLabel = NULL; // step 6: generate keyword model by Baum-Welch algorithm AUD_Vector pi; pi.len = stateNum; pi.dataType = AUD_DATATYPE_DOUBLE; ret = createVector( &pi ); AUD_ASSERT( ret == 0 ); pi.pDouble[0] = 1.0f; void *hKeywordHmm = NULL; error = gmmhmm_init( &hKeywordHmm, stateNum, &pi, phKeywordGmms ); AUD_ASSERT( error == AUD_ERROR_NONE ); error = gmmhmm_learn( hKeywordHmm, &feature, 1, 0.001 ); AUD_ASSERT( error == AUD_ERROR_NONE ); // step 8: write model to file error = gmmhmm_export( hKeywordHmm, pHmmName ); AUD_ASSERT( error == AUD_ERROR_NONE ); // gmmhmm_show( hKeywordHmm ); // clean field error = mfcc16s32s_deinit( &hMfccHandle ); AUD_ASSERT( error == AUD_ERROR_NONE ); error = gmm_free( &hGarbageGmm ); AUD_ASSERT( error == AUD_ERROR_NONE ); ret = destroyMatrix( &(feature.featureMatrix) ); AUD_ASSERT( ret == 0 ); ret = destroyVector( &(feature.featureNorm) ); AUD_ASSERT( ret == 0 ); ret = destroyVector( &pi ); AUD_ASSERT( ret == 0 ); for ( i = 0; i < stateNum; i++ ) { error = gmm_free( &phKeywordGmms[i] ); AUD_ASSERT( error == AUD_ERROR_NONE ); } free( phKeywordGmms ); phKeywordGmms = NULL; error = gmmhmm_free( &hKeywordHmm ); AUD_ASSERT( error == AUD_ERROR_NONE ); return 0; }
AUD_Int32s denoise_aud( AUD_Int16s *pInBuf, AUD_Int16s *pOutBuf, AUD_Int32s inLen ) { Fft_16s *hFft = NULL; Ifft_16s *hIfft = NULL; AUD_Window16s *hWin = NULL; AUD_Int32s frameSize = 512; AUD_Int32s frameStride = 256; AUD_Int32s frameOverlap = 256; AUD_Int32s nFFT = frameSize; AUD_Int32s nSpecLen = nFFT / 2 + 1; AUD_Int32s nNoiseFrame = 6; // (AUD_Int32s)( ( 0.25 * SAMPLE_RATE - frameSize ) / frameStride + 1 ); AUD_Int32s i, j, k, m, n, ret; AUD_Int32s cleanLen = 0; // pre-emphasis // sig_preemphasis( pInBuf, pInBuf, inLen ); // init hamming module win16s_init( &hWin, AUD_WIN_HAMM, frameSize, 14 ); AUD_ASSERT( hWin ); // init fft handle fft_init( &hFft, nFFT, 15 ); AUD_ASSERT( hFft ); // init ifft handle ifft_init( &hIfft, nFFT, 15 ); AUD_ASSERT( hIfft ); AUD_Int16s *pFrame = (AUD_Int16s*)calloc( frameSize * sizeof(AUD_Int16s), 1 ); AUD_ASSERT( pFrame ); // FFT AUD_Int32s *pFFTMag = (AUD_Int32s*)calloc( nFFT * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pFFTMag ); AUD_Int32s *pFFTRe = (AUD_Int32s*)calloc( nFFT * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pFFTRe ); AUD_Int32s *pFFTIm = (AUD_Int32s*)calloc( nFFT * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pFFTIm ); AUD_Int32s *pFFTCleanRe = (AUD_Int32s*)calloc( nFFT * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pFFTCleanRe ); AUD_Int32s *pFFTCleanIm = (AUD_Int32s*)calloc( nFFT * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pFFTCleanIm ); // noise spectrum AUD_Double *pNoiseEn = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pNoiseEn ); AUD_Double *pNoiseB = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pNoiseB ); AUD_Double *pXPrev = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pXPrev ); AUD_Double *pAb = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pAb ); AUD_Double *pH = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pH ); AUD_Double *pGammak = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pGammak ); AUD_Double *pKsi = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pKsi ); AUD_Double *pLogSigmak = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pLogSigmak ); AUD_Double *pAlpha = (AUD_Double*)calloc( nSpecLen * sizeof(AUD_Double), 1 ); AUD_ASSERT( pAlpha ); AUD_Int32s *pLinToBark = (AUD_Int32s*)calloc( nSpecLen * sizeof(AUD_Int32s), 1 ); AUD_ASSERT( pLinToBark ); AUD_Int16s *pxOld = (AUD_Int16s*)calloc( frameOverlap * sizeof(AUD_Int16s), 1 ); AUD_ASSERT( pxOld ); AUD_Int16s *pxClean = (AUD_Int16s*)calloc( nFFT * sizeof(AUD_Int16s), 1 ); AUD_ASSERT( pxClean ); /* AUD_Int32s critBandEnds[22] = { 0, 100, 200, 300, 400, 510, 630, 770, 920, 1080, 1270, 1480, 1720, 2000, 2320, 2700, 3150, 3700, 4400, 5300, 6400, 7700 }; */ AUD_Int32s critFFTEnds[CRITICAL_BAND_NUM + 1] = { 0, 4, 7, 10, 13, 17, 21, 25, 30, 35, 41, 48, 56, 64, 75, 87, 101, 119, 141, 170, 205, 247, 257 }; // generate linear->bark transform mapping k = 0; for ( i = 0; i < CRITICAL_BAND_NUM; i++ ) { while ( k >= critFFTEnds[i] && k < critFFTEnds[i + 1] ) { pLinToBark[k] = i; k++; } } AUD_Double absThr[CRITICAL_BAND_NUM] = { 38, 31, 22, 18.5, 15.5, 13, 11, 9.5, 8.75, 7.25, 4.75, 2.75, 1.5, 0.5, 0, 0, 0, 0, 2, 7, 12, 15.5 }; AUD_Double dbOffset[CRITICAL_BAND_NUM]; AUD_Double sumn[CRITICAL_BAND_NUM]; AUD_Double spread[CRITICAL_BAND_NUM]; for ( i = 0; i < CRITICAL_BAND_NUM; i++ ) { absThr[i] = pow( 10., absThr[i] / 10. ) / nFFT / ( 65535. * 65535. ); dbOffset[i] = 10. + i; sumn[i] = 0.474 + i; spread[i] = pow( 10., ( 15.81 + 7.5 * sumn[i] - 17.5 * sqrt( 1. + sumn[i] * sumn[i] ) ) / 10. ); } AUD_Double dcGain[CRITICAL_BAND_NUM]; for ( i = 0; i < CRITICAL_BAND_NUM; i++ ) { dcGain[i] = 0.; for ( j = 0; j < CRITICAL_BAND_NUM; j++ ) { dcGain[i] += spread[MABS( i - j )]; } } AUD_Matrix exPatMatrix; exPatMatrix.rows = CRITICAL_BAND_NUM; exPatMatrix.cols = nSpecLen; exPatMatrix.dataType = AUD_DATATYPE_DOUBLE; ret = createMatrix( &exPatMatrix ); AUD_ASSERT( ret == 0 ); // excitation pattern AUD_Int32s index = 0; for ( i = 0; i < exPatMatrix.rows; i++ ) { AUD_Double *pExpatRow = exPatMatrix.pDouble + i * exPatMatrix.cols; for ( j = 0; j < exPatMatrix.cols; j++ ) { index = MABS( i - pLinToBark[j] ); pExpatRow[j] = spread[index]; } } AUD_Int32s frameNum = (inLen - frameSize) / frameStride + 1; AUD_ASSERT( frameNum > nNoiseFrame ); // compute noise mean for ( i = 0; i < nNoiseFrame; i++ ) { win16s_calc( hWin, pInBuf + i * frameSize, pFrame ); fft_mag( hFft, pFrame, frameSize, pFFTMag ); for ( j = 0; j < nSpecLen; j++ ) { pNoiseEn[j] += pFFTMag[j] / 32768. * pFFTMag[j] / 32768.; } } for ( j = 0; j < nSpecLen; j++ ) { pNoiseEn[j] /= nNoiseFrame; } // get cirtical band mean filtered noise power AUD_Int32s k1 = 0, k2 = 0; for ( i = 0; i < CRITICAL_BAND_NUM; i++ ) { k1 = k2; AUD_Double segSum = 0.; while ( k2 >= critFFTEnds[i] && k2 < critFFTEnds[i + 1] ) { segSum += pNoiseEn[k2]; k2++; } segSum /= ( k2 - k1 ); for ( m = k1; m < k2; m++ ) { pNoiseB[m] = segSum; } } #if 0 AUDLOG( "noise band spectrum:\n" ); for ( j = 0; j < nSpecLen; j++ ) { AUDLOG( "%.2f, ", pNoiseB[j] ); } AUDLOG( "\n" ); #endif AUD_Matrix frameMatrix; frameMatrix.rows = nSpecLen; frameMatrix.cols = 1; frameMatrix.dataType = AUD_DATATYPE_DOUBLE; ret = createMatrix( &frameMatrix ); AUD_ASSERT( ret == 0 ); AUD_Double *pFrameEn = frameMatrix.pDouble; AUD_Matrix xMatrix; xMatrix.rows = nSpecLen; xMatrix.cols = 1; xMatrix.dataType = AUD_DATATYPE_DOUBLE; ret = createMatrix( &xMatrix ); AUD_ASSERT( ret == 0 ); AUD_Double *pX = xMatrix.pDouble; AUD_Matrix cMatrix; cMatrix.rows = CRITICAL_BAND_NUM; cMatrix.cols = 1; cMatrix.dataType = AUD_DATATYPE_DOUBLE; ret = createMatrix( &cMatrix ); AUD_ASSERT( ret == 0 ); AUD_Double *pC = cMatrix.pDouble; AUD_Matrix tMatrix; tMatrix.rows = 1; tMatrix.cols = CRITICAL_BAND_NUM; tMatrix.dataType = AUD_DATATYPE_DOUBLE; ret = createMatrix( &tMatrix ); AUD_ASSERT( ret == 0 ); AUD_Double *pT = tMatrix.pDouble; AUD_Matrix tkMatrix; tkMatrix.rows = 1; tkMatrix.cols = nSpecLen; tkMatrix.dataType = AUD_DATATYPE_DOUBLE; ret = createMatrix( &tkMatrix ); AUD_ASSERT( ret == 0 ); AUD_Double *pTk = tkMatrix.pDouble; AUD_Double dB0[CRITICAL_BAND_NUM]; AUD_Double epsilon = pow( 2, -52 ); #define ESTIMATE_MASKTHRESH( sigMatrix, tkMatrix )\ do {\ AUD_Double *pSig = sigMatrix.pDouble; \ for ( m = 0; m < exPatMatrix.rows; m++ ) \ { \ AUD_Double suma = 0.; \ AUD_Double *pExpatRow = exPatMatrix.pDouble + m * exPatMatrix.cols; \ for ( n = 0; n < exPatMatrix.cols; n++ ) \ { \ suma += pExpatRow[n] * pSig[n]; \ } \ pC[m] = suma; \ } \ AUD_Double product = 1.; \ AUD_Double sum = 0.; \ for ( m = 0; m < sigMatrix.rows; m++ ) \ { \ product *= pSig[m]; \ sum += pSig[m]; \ } \ AUD_Double power = 1. / sigMatrix.rows;\ AUD_Double sfmDB = 10. * log10( pow( product, power ) / sum / sigMatrix.rows + epsilon ); \ AUD_Double alpha = AUD_MIN( 1., sfmDB / (-60.) ); \ for ( m = 0; m < tMatrix.cols; m++ ) \ { \ dB0[m] = dbOffset[m] * alpha + 5.5; \ pT[m] = pC[m] / pow( 10., dB0[m] / 10. ) / dcGain[m]; \ pT[m] = AUD_MAX( pT[m], absThr[m] ); \ } \ for ( m = 0; m < tkMatrix.cols; m++ ) \ { \ pTk[m] = pT[pLinToBark[m]]; \ } \ } while ( 0 ) AUD_Double aa = 0.98; AUD_Double mu = 0.98; AUD_Double eta = 0.15; AUD_Double vadDecision; k = 0; // start processing for ( i = 0; i < frameNum; i++ ) { win16s_calc( hWin, pInBuf + i * frameStride, pFrame ); fft_calc( hFft, pFrame, frameSize, pFFTRe, pFFTIm ); // compute SNR vadDecision = 0.; for ( j = 0; j < nSpecLen; j++ ) { pFrameEn[j] = pFFTRe[j] / 32768. * pFFTRe[j] / 32768. + pFFTIm[j] / 32768. * pFFTIm[j] / 32768.; pGammak[j] = AUD_MIN( pFrameEn[j] / pNoiseEn[j], 40. ); if ( i > 0 ) { pKsi[j] = aa * pXPrev[j] / pNoiseEn[j] + ( 1 - aa ) * AUD_MAX( pGammak[j] - 1., 0. ); } else { pKsi[j] = aa + ( 1. - aa ) * AUD_MAX( pGammak[j] - 1., 0. ); } pLogSigmak[j] = pGammak[j] * pKsi[j] / ( 1. + pKsi[j] ) - log( 1. + pKsi[j] ); vadDecision += ( j > 0 ? 2 : 1 ) * pLogSigmak[j]; } vadDecision /= nFFT; #if 0 AUDLOG( "X prev:\n" ); for ( j = 0; j < nSpecLen; j++ ) { AUDLOG( "%.2f, ", pXPrev[j] ); } AUDLOG( "\n" ); #endif #if 0 AUDLOG( "gamma k:\n" ); for ( j = 0; j < nSpecLen; j++ ) { AUDLOG( "%.2f, ", pGammak[j] ); } AUDLOG( "\n" ); #endif #if 0 AUDLOG( "ksi:\n" ); for ( j = 0; j < nSpecLen; j++ ) { AUDLOG( "%.2f, ", pKsi[j] ); } AUDLOG( "\n" ); #endif #if 0 AUDLOG( "log sigma k:\n" ); for ( j = 0; j < nSpecLen; j++ ) { AUDLOG( "%.2f, ", pLogSigmak[j] ); } AUDLOG( "\n" ); #endif // AUDLOG( "vadDecision: %.2f\n", vadDecision ); // re-estimate noise if ( vadDecision < eta ) { for ( j = 0; j < nSpecLen; j++ ) { pNoiseEn[j] = mu * pNoiseEn[j] + ( 1. - mu ) * pFrameEn[j]; } // re-estimate crital band based noise AUD_Int32s k1 = 0, k2 = 0; for ( int band = 0; band < CRITICAL_BAND_NUM; band++ ) { k1 = k2; AUD_Double segSum = 0.; while ( k2 >= critFFTEnds[band] && k2 < critFFTEnds[band + 1] ) { segSum += pNoiseEn[k2]; k2++; } segSum /= ( k2 - k1 ); for ( m = k1; m < k2; m++ ) { pNoiseB[m] = segSum; } } #if 0 AUDLOG( "noise band spectrum:\n" ); for ( j = 0; j < nSpecLen; j++ ) { AUDLOG( "%.2f, ", pNoiseB[j] ); } AUDLOG( "\n" ); #endif } for ( j = 0; j < nSpecLen; j++ ) { pX[j] = AUD_MAX( pFrameEn[j] - pNoiseEn[j], 0.001 ); pXPrev[j] = pFrameEn[j]; } ESTIMATE_MASKTHRESH( xMatrix, tkMatrix ); for ( int iter = 0; iter < 2; iter++ ) { for ( j = 0; j < nSpecLen; j++ ) { pAb[j] = pNoiseB[j] + pNoiseB[j] * pNoiseB[j] / pTk[j]; pFrameEn[j] = pFrameEn[j] * pFrameEn[j] / ( pFrameEn[j] + pAb[j] ); ESTIMATE_MASKTHRESH( frameMatrix, tkMatrix ); #if 0 showMatrix( &tMatrix ); #endif } } #if 0 AUDLOG( "tk:\n" ); for ( j = 0; j < nSpecLen; j++ ) { AUDLOG( "%.2f, ", pTk[j] ); } AUDLOG( "\n" ); #endif pAlpha[0] = ( pNoiseB[0] + pTk[0] ) * ( pNoiseB[0] / pTk[0] ); pH[0] = pFrameEn[0] / ( pFrameEn[0] + pAlpha[0] ); pXPrev[0] *= pH[0] * pH[0]; pFFTCleanRe[0] = 0; pFFTCleanIm[0] = 0; for ( j = 1; j < nSpecLen; j++ ) { pAlpha[j] = ( pNoiseB[j] + pTk[j] ) * ( pNoiseB[j] / pTk[j] ); pH[j] = pFrameEn[j] / ( pFrameEn[j] + pAlpha[j] ); pFFTCleanRe[j] = pFFTCleanRe[nFFT - j] = (AUD_Int32s)round( pH[j] * pFFTRe[j] ); pFFTCleanIm[j] = (AUD_Int32s)round( pH[j] * pFFTIm[j] ); pFFTCleanIm[nFFT - j] = -pFFTCleanIm[j]; pXPrev[j] *= pH[j] * pH[j]; } #if 0 AUDLOG( "denoise transfer function:\n" ); for ( j = 0; j < nSpecLen; j++ ) { AUDLOG( "%.2f, ", pH[j] ); } AUDLOG( "\n" ); #endif #if 0 AUDLOG( "clean FFT with phase:\n" ); for ( j = 0; j < nFFT; j++ ) { AUDLOG( "%d + j%d, ", pFFTCleanRe[j], pFFTCleanIm[j] ); } AUDLOG( "\n" ); #endif ifft_real( hIfft, pFFTCleanRe, pFFTCleanIm, nFFT, pxClean ); #if 0 AUDLOG( "clean speech:\n" ); for ( j = 0; j < nFFT; j++ ) { AUDLOG( "%d, ", pxClean[j] ); } AUDLOG( "\n" ); #endif for ( j = 0; j < frameStride; j++ ) { if ( j < frameOverlap ) { pOutBuf[k + j] = pxOld[j] + pxClean[j]; pxOld[j] = pxClean[frameStride + j]; } else { pOutBuf[k + j] = pxClean[j]; } } k += frameStride; cleanLen += frameStride; } // de-emphasis // sig_deemphasis( pOutBuf, pOutBuf, cleanLen ); win16s_free( &hWin ); fft_free( &hFft ); ifft_free( &hIfft ); free( pFrame ); free( pNoiseEn ); free( pNoiseB ); free( pFFTMag ); free( pFFTRe ); free( pFFTIm ); free( pXPrev ); free( pAb ); free( pH ); free( pFFTCleanRe ); free( pFFTCleanIm ); free( pxOld ); free( pxClean ); free( pGammak ); free( pKsi ); free( pLogSigmak ); free( pAlpha ); free( pLinToBark ); ret = createMatrix( &xMatrix ); AUD_ASSERT( ret == 0 ); ret = destroyMatrix( &exPatMatrix ); AUD_ASSERT( ret == 0 ); ret = destroyMatrix( &frameMatrix ); AUD_ASSERT( ret == 0 ); ret = destroyMatrix( &cMatrix ); AUD_ASSERT( ret == 0 ); ret = destroyMatrix( &tMatrix ); AUD_ASSERT( ret == 0 ); ret = destroyMatrix( &tkMatrix ); AUD_ASSERT( ret == 0 ); return cleanLen; }
void generateWaveBenchmarkTimeVariant(char *destinyFileName,char *folderName,void (*waveFunction)(structMatrix,structMatrix,structMatrix*,structMatrix*)) { /* * Initiating the mesh */ structMatrix mesh_hmatrix,mesh_vmatrix; structMatrix u_matrix_timeV[N_TIME_STEPS],v_matrix_timeV[N_TIME_STEPS]; // Initiating mesh mesh_hmatrix = initMatrix(N_HNODES,N_VNODES); mesh_vmatrix = initMatrix(N_HNODES,N_VNODES); loadMesh(mesh_hmatrix,mesh_vmatrix); char path[N_TIME_STEPS][MAX_FILENAME_LENGTH]; generateFileNames(path,destinyFileName,folderName); for(int i=0; i< N_TIME_STEPS; i++) { //printf("path[%d]: %s\n",i,path[i]); // Initializing u and v matrixes u_matrix_timeV[i] = initMatrix(N_HNODES,N_VNODES); v_matrix_timeV[i] = initMatrix(N_HNODES,N_VNODES); } // Calculations are handled via callback // This function must calculate for every time-step! waveFunction(mesh_hmatrix,mesh_vmatrix,u_matrix_timeV,v_matrix_timeV); // Printing matrixes to files for(int i=0; i< N_TIME_STEPS; i++) { char indexu[4],indexv[4],quiverexp[30]; sprintf(indexu,"u%d",i); sprintf(indexv,"v%d",i); sprintf(quiverexp,"quiver(x,y,%s,%s)\n",indexu,indexv); printMatrixToFile(u_matrix_timeV[i],path[i],indexu); printMatrixToFile(v_matrix_timeV[i],path[i],indexv); printMatrixToFile(mesh_hmatrix,path[i],"x"); printMatrixToFile(mesh_vmatrix,path[i],"y"); // At the end, print the "quiver" function FILE *destinyFile = fopen(path[i],"a"); fprintf(destinyFile,"%s",quiverexp); fclose(destinyFile); } // Memory freeing for(int i=0; i< N_TIME_STEPS; i++) { destroyMatrix(u_matrix_timeV[i]); destroyMatrix(v_matrix_timeV[i]); } destroyMatrix(mesh_hmatrix); destroyMatrix(mesh_vmatrix); // Generate the plotter script char plotterName[MAX_FILENAME_LENGTH]; FILE *plotter; strcpy(plotterName,"../testbench/"); strcat(plotterName,folderName); strcat(plotterName,"/"); strcat(plotterName,destinyFileName); strcat(plotterName,"_plotter.m"); plotter = fopen(plotterName,"w"); for(int i=0; i< N_TIME_STEPS; i++ ) { fprintf(plotter,"%s_%d \nprint -djpg %s_%d.jpg\n",destinyFileName,i,destinyFileName,i); } fclose(plotter); }