void TimeWarp::createCombinedMatrix(DoubleMatrix myChromaMatrix, DoubleVector energyVector, DoubleMatrix* chromaEnergyMatrix){ chromaEnergyMatrix->clear(); int sizeRatio = energyVector.size() / myChromaMatrix.size();// printf("COMBINE: size of my chroma is %i\n", (int) myChromaMatrix.size());// energyVector.size() / myChromaMatrix.size(); printf("COMBINED: size ratio of energy to chroma is %i \n", sizeRatio); int chromaSize = myChromaMatrix.size(); // printf("index is %i\n", index); for (int i = 0;i < energyVector.size();i++){ DoubleVector d; int index = min(chromaSize-1, (int) floor(i/sizeRatio)); for (int y = 0;y < 12;y++){ d.push_back(myChromaMatrix[index][y]);// } d.push_back(energyVector[i]); (*chromaEnergyMatrix).push_back(d); } printf("COMBINED: size of chroma energy is %i\n", (int)(*chromaEnergyMatrix).size()); // int x = (int)(*chromaEnergyMatrix).size()/3; // printf("energy[%i] %f \n", x, energyVector[x]); /* for (int y = 0;y < 13;y++){ printf("chroma[%i][%i] %f \n", x, y, myChromaMatrix[x/sizeRatio][y]); printf("c[%i][%i] %f \n", x, y, (*chromaEnergyMatrix)[x][y]); } printf("\n"); */ }
/*! * \brief assertSizeEqual Asserts that the two matrices * given as parameters are of equal size. * Throws an invalid argument exception if that is not the case. * \param A First matrix. * \param B Second matrix. */ void assertSizeEqual(const DoubleMatrix &A, const DoubleMatrix &B) { if(A.size() != B.size()) { throw std::invalid_argument("Matrices are not of equal size!"); } }
void EXPECT_MATRIX_DOUBLE_EQ(DoubleMatrix A, DoubleMatrix B, string message = ""){ EXPECT_EQ(A.size(), B.size()) << "Number of lines is different. " << message; EXPECT_EQ(A[0].size(), B[0].size()) << "Number of columns is different. " << message; unsigned int N = A.size(); unsigned int M = A[0].size(); for (unsigned int i = 0; i < N; i++){ for (unsigned int j = 0; j < M; j++) EXPECT_FLOAT_EQ(A[i][j], B[i][j]) << message; } }
void StopRule::multiple (DoubleMatrix &mat1, DoubleVector &vec2, DoubleVector &proVec) { int row_, col_; proVec.resize(mat1.size()); for (row_ = 0; row_ < mat1.size (); row_ ++) { proVec[row_] = 0.0; for (col_ = 0; col_ < mat1[0].size(); col_ ++) proVec[row_] += mat1[row_][col_] * vec2[col_]; } }
MatlabEngine::DoubleMatrix MatlabEngine::GetMatrix( const string& inExp ) { DoubleMatrix result; mxArray* ans = GetMxArray( inExp ); if( ans ) { int numDims = mxGetNumberOfDimensions( ans ); const int* dims = mxGetDimensions( ans ); if( numDims != 2 ) bcierr << "Can only handle two dimensions" << endl; result.resize( dims[ 0 ], vector<double>( dims[ 1 ] ) ); double* value = mxGetPr( ans ); if( value ) { int indices[] = { 0, 0 }; for( size_t i = 0; i < result.size(); ++i ) { indices[ 0 ] = i; for( size_t j = 0; j < result[ i ].size(); ++j ) { indices[ 1 ] = j; result[ i ][ j ] = value[ mxCalcSingleSubscript( ans, 2, indices ) ]; } } } mxDestroyArray( ans ); } return result; }
void StopRule::multiple (DoubleMatrix &mat1, DoubleMatrix &mat2, DoubleMatrix &proMat) { int row_, col_; //proMat.setLimit (mat1.getNRow (), mat2.getNCol ()); proMat.resize(mat1.size()); int nrow_ = proMat.size(); int ncol_ = mat2[0].size(); for (row_ = 0; row_ < proMat.size(); row_++) proMat[row_].resize(ncol_); for (row_ = 0; row_ < nrow_; row_ ++) for (col_ = 0; col_ < ncol_; col_ ++) { proMat[row_][col_] = 0.0; for (int count_ = 0; count_ < mat1[0].size(); count_ ++) { proMat[row_][col_] += mat1[row_][count_] * mat2[count_][col_]; // std::cout << mat1[row_][count_] << " --> " << mat2[count_][col_] << endl; } } }
int TimeWarp::getMinimumIndexOfRowFromMatrix(int j, DoubleMatrix& matrix){ int minimumIndex = 0; double minimumValue = 0; int width = matrix.size(); if (j >= 0 && width > 0 && j < matrix[0].size()){//&& i < matrix.size() //int height = (*matrix)[i].size(); int i = 0; minimumValue = matrix[i][j]; while (i < width){ if (matrix[i][j] < minimumValue){ minimumIndex = i; minimumValue = matrix[i][j]; }//end if new value i++; } }else{ printf("ERROR FROM GETTING MINIMIM!!! - zero - out of bounds, received %i and size is %i\n", j, (int)matrix.size()); } // printf("minimum row index is %i\n", minimumIndex); return minimumIndex; }
int TimeWarp::findMinimumOfMatrixColumn(DoubleMatrix d, int column){ int minimumIndex = 0; double minimumValue = d[column][0]; for (int i = 0;i < d.size();i++){ if (d[column][i] < minimumValue){ minimumIndex = i; minimumValue = d[column][i]; } } return minimumIndex; }
void TimeWarp::calculateCausalChromaSimilarityMatrix(DoubleMatrix& firstChromaMatrix, DoubleMatrix& secondChromaMatrix, DoubleMatrix& simMatrix){ //calculates the chroma only similarity matrix //but we have already done some, so is extending it... int size = 0; if (simMatrix.size() > 0){ size = simMatrix[0].size(); } if (secondChromaMatrix.size() > size ){ for (int x = 0;x < firstChromaMatrix.size();x++){ if (x < simMatrix.size()){ //i.e. entries already exist for (int y = (int)simMatrix[x].size();y < secondChromaMatrix.size();y++){ double distance; if (useDotProduct) distance = getChromaSimilarity(x, y, &firstChromaMatrix, &secondChromaMatrix); else distance = getEuclideanDistance(x, y, &firstChromaMatrix, &secondChromaMatrix); printf("putting one back X %i Y %i dist %f \n", x, y, distance); simMatrix[x].push_back(distance); } } else { DoubleVector d; for (int y = 0;y < secondChromaMatrix.size();y++){ double distance; if (useDotProduct) distance = getChromaSimilarity(x, y, &firstChromaMatrix, &secondChromaMatrix); else distance = getEuclideanDistance(x, y, &firstChromaMatrix, &secondChromaMatrix); printf("new row X %i Y %i dist %f\n", x, y, distance); d.push_back( distance); } simMatrix.push_back(d); } } } if (size > 0) printf("Causial CHROMA ONLY SIM SIZE %i x %i; ", (int)simMatrix.size(), (int) simMatrix[0].size()); printf("First matrix SIZE %i , SECOND size %i\n", (int)firstChromaMatrix.size(), (int) secondChromaMatrix.size()); }
bool MatlabEngine::PutMatrix( const string& inExp, const DoubleMatrix& inValue ) { int sizeDim2 = inValue.empty() ? 0 : inValue[ 0 ].size(); mxArray* val = mxCreateNumericMatrix( inValue.size(), sizeDim2, mxDOUBLE_CLASS, mxREAL ); double* data = mxGetPr( val ); if( data ) { int indices[] = { 0, 0 }; for( size_t i = 0; i < inValue.size(); ++i ) { indices[ 0 ] = i; for( size_t j = 0; j < inValue[ i ].size(); ++j ) { indices[ 1 ] = j; data[ mxCalcSingleSubscript( val, 2, indices ) ] = inValue[ i ][ j ]; } } } bool success = PutMxArray( inExp, val ); mxDestroyArray( val ); return success; }
bool MatlabEngine::PutMatrix( const string& inExp, const DoubleMatrix& inValue ) { mwSize sizeDim2 = static_cast<mwSize>( inValue.empty() ? 0 : inValue[ 0 ].size() ); mxArray* val = mxCreateNumericMatrix_( static_cast<mwSize>( inValue.size() ), sizeDim2, mxDOUBLE_CLASS, mxREAL ); double* data = mxGetPr_( val ); if( data ) { mwIndex indices[] = { 0, 0 }; for( mwIndex i = 0; i < static_cast<mwIndex>( inValue.size() ); ++i ) { indices[ 0 ] = i; for( mwIndex j = 0; j < static_cast<mwIndex>( inValue[ i ].size() ); ++j ) { indices[ 1 ] = j; data[ mxCalcSingleSubscript_( val, 2, indices ) ] = inValue[ i ][ j ]; } } } bool success = PutMxArray( inExp, val ); mxDestroyArray_( val ); return success; }
void TimeWarp::calculateAlignmentMatrix(DoubleMatrix firstMatrix, DoubleMatrix secondMatrix, DoubleMatrix* alignmentMatrix){//, DoubleMatrix simMatrix printf("starting Alignment calculation\n"); //initialise alignment alignmentMatrix->clear(); DoubleVector d; d.push_back(getDistance(0,0)); (*alignmentMatrix).push_back(d); bool chromaCalculated = false; bool secondCalculated = false; while (!chromaCalculated || !secondCalculated) { if (!chromaCalculated) chromaCalculated = extendAlignmentAlong((int) firstMatrix.size(), alignmentMatrix); if (!secondCalculated) secondCalculated = extendAlignmentUp((int) secondMatrix.size(), alignmentMatrix); } printf("Alignment matrix calculated, size %i\n", (int) (*alignmentMatrix).size()); }
DoubleMatrix mult(DoubleMatrix& m2, ComplexMatrix& m1) { // Check dimensions unsigned int m1_nRows = m1.numRows(); unsigned int m2_nRows = m2.numRows(); unsigned int m1_nColumns = m1.numCols(); unsigned int m2_nColumns = m2.numCols(); if (m1.size() == 0) { return real(m1); } if (m2.size() == 0) { return m2; } DoubleMatrix result(m1_nRows, m2_nColumns); if (m1_nColumns == m2_nRows) { for (unsigned int row = 0; row < result.numRows(); row++) { for (unsigned int col = 0; col < m2_nColumns; col++) { double sum = 0.0; for (unsigned int k = 0; k < m1_nColumns; k++) { sum = sum + (real(m1[row][k]) * m2[k][col]); } result[row][col] = sum; } } return result; } if (m1_nRows == m2_nColumns) { return mult(m2, m1); } throw ("Incompatible matrix operands to multiply"); }
DoubleMatrix ParseMatrixFromText(const string& textMatrix) { DoubleMatrix mat; //Parse the matrix vector<string> rows = splitString(textMatrix, "\n"); for(int row = 0; row < rows.size(); row++) { vector<string> values = splitString(rows[row], " \t"); for(int col = 0; col < values.size(); col++) { if(!mat.size()) { mat.resize(rows.size(), values.size()); } mat(row, col) = toDouble(values[col]); } } return mat; }
void TimeWarp::calculateRestrictedCausalChromaSimilarityMatrix(DoubleMatrix& firstChromaMatrix, DoubleMatrix& secondChromaMatrix, DoubleMatrix& simMatrix, const int& Xstart, const int& Ystart, const int& Xlimit, const int& yLimit){ //calculates the chroma only similarity matrix - used to reduce computation later when doing the joint energy and chroma matrix DoubleVector d; int size = 0; if (simMatrix.size() > 0){ size = simMatrix[0].size(); } if (secondChromaMatrix.size() > size ){ // printf("sim matrix size %i and second matrix size %i Xstart %i\n", simMatrix.size(), secondChromaMatrix.size(), Xstart); for (int x = 0;x < Xstart;x++){ simMatrix[x].push_back(0); } int endX = min(Xlimit, (int)firstChromaMatrix.size()); for (int x = Xstart;x < endX;x++){ if (x < simMatrix.size()){ //i.e. entries already exist for (int y = (int)simMatrix[x].size();y < secondChromaMatrix.size();y++){ double distance; if (useDotProduct) distance = getChromaSimilarity(x, y, &firstChromaMatrix, &secondChromaMatrix); else distance = getEuclideanDistance(x, y, &firstChromaMatrix, &secondChromaMatrix); // printf("X %i Y %i dist %f\n", x, y, distance); simMatrix[x].push_back(distance); } } else { DoubleVector d; for (int y = 0;y < secondChromaMatrix.size();y++){ double distance; if (useDotProduct) distance = getChromaSimilarity(x, y, &firstChromaMatrix, &secondChromaMatrix); else distance = getEuclideanDistance(x, y, &firstChromaMatrix, &secondChromaMatrix); // printf("new row X %i Y %i dist %f\n", x, y, distance); d.clear(); d.push_back( distance); } simMatrix.push_back(d); } }//end for good section for (int x = endX;x < firstChromaMatrix.size(); x++){ if (x < simMatrix.size()){ simMatrix[x].push_back(0); }else{ d.clear(); d.push_back(0); simMatrix.push_back(d); } }//end for x to end } // if (size > 0) // printf("Causial CHROMA ONLY SIM SIZE %i x %i; ", (int)simMatrix.size(), (int) simMatrix[0].size()); //printf("First matrix SIZE %i , SECOND size %i\n", (int)firstChromaMatrix.size(), (int) secondChromaMatrix.size()); }