Beispiel #1
0
void testSaveMatrix(void) {
    FILE *matrixFile = fopen("test/matrix_to_save","wb");
    if(matrixFile == NULL) {
        return;
    } 
    
    size_t columns = 3;
    size_t rows = 3;
    floattype **matrix = AllocMatrix(rows, columns);
    floattype **expected = AllocMatrix(rows, columns);
    
    CU_ASSERT(CODE_OK == saveMatrix(matrix, matrixFile, rows, columns));
    FreeMatrix(matrix, rows);
    fclose(matrixFile);
    
    Matrix *matrixLoaded;
    FILE *loadFile = fopen("test/matrix_to_save","rb");
    matrixLoaded = loadMatrix(loadFile);
    fclose(loadFile);
    
    CU_ASSERT(NULL != matrixLoaded);
    if(NULL == matrixLoaded) {
        FreeMatrix(expected, rows);
        return;
    }
    
    CU_ASSERT(3 == matrixLoaded->header.colcount);
    CU_ASSERT(3 == matrixLoaded->header.rowcount);
    
    assertMatricesAreSame(expected, matrixLoaded->matrix, &(matrixLoaded->header));
    
    FreeMatrix(matrixLoaded->matrix, matrixLoaded->header.rowcount);
    free(matrixLoaded);
    FreeMatrix(expected, rows);
}
void
CGaussianCPD::AllocDistribution(const float* pMean, const float* pCov,
				float normCoeff, const float* const* pWeights,
				const int* parentCombination )
{
    PNL_CHECK_IS_NULL_POINTER( pMean );
    PNL_CHECK_IS_NULL_POINTER( pCov );
    if( m_CorrespDistribFun->GetDistributionType() == dtGaussian )
    {
        SetCoefficient( normCoeff, 0 );
	    AllocMatrix( pMean, matMean );
	    AllocMatrix( pCov, matCovariance );
	    if( pWeights )
	    {
	        int numParents = m_Domain.size() - 1;
	        for( int i = 0; i < numParents; i++ )
	        {
		        if( pWeights[i] )
		        {
		            m_CorrespDistribFun->AllocMatrix( pWeights[i],
		                matWeights, i );
		        }
	        }
	    }
    }
    else
    {
	    PNL_CHECK_IS_NULL_POINTER( parentCombination );
        SetCoefficient( normCoeff, parentCombination );
	    AllocMatrix( pMean, matMean, -1, parentCombination );
	    AllocMatrix( pCov, matCovariance,  -1, parentCombination );
	    if( pWeights )
	    {
	        intVector contParentsIndex;
	        static_cast<CCondGaussianDistribFun*>(m_CorrespDistribFun)->
		    GetContinuousParentsIndices(&contParentsIndex);
	        int numParents = contParentsIndex.size();
	        for( int i = 0; i < numParents; i++ )
	        {
		        if( pWeights[i] )
		        {
		            m_CorrespDistribFun->AllocMatrix( pWeights[i],
		                matWeights, i, parentCombination );
		        }
	        }
	    }
    }
}
void CSoftMaxCPD::AllocDistribution(const float* pWeights, 
                                    const float* pOffsets, const int* parentCombination)
{
    PNL_CHECK_IS_NULL_POINTER(pWeights);
    PNL_CHECK_IS_NULL_POINTER(pOffsets);
    
////////////////////////////////////////////////
    const CNodeType *nt;
    nt = GetModelDomain()->GetVariableType( m_Domain[m_Domain.size()-1] );
    int SoftMaxSize = nt->GetNodeSize();

    if (SoftMaxSize == 2)
    {
      int matSize = 0;
      int i;
      for (i = 0; i < m_Domain.size(); i++)
      {
         nt = GetModelDomain()->GetVariableType( m_Domain[i] );
         if(!(nt->IsDiscrete()))
         {
            matSize ++;
         }
      }
      //matSize = matSize;

      for (i = 0; i < 2*matSize-1; i+=2)
      {
        if (pWeights[i] - pWeights[i+1] == 0)
          PNL_THROW(CNotImplemented, "sigmoid must have distinct weights");
      }
    }
////////////////////////////////////////////////

    if (m_CorrespDistribFun->GetDistributionType() == dtSoftMax)
    {
        AllocMatrix(pWeights, matWeights);
        static_cast<CSoftMaxDistribFun*>(m_CorrespDistribFun)->
            AllocOffsetVector(pOffsets);
    }
    else
    {
        PNL_CHECK_IS_NULL_POINTER(parentCombination);
        AllocMatrix(pWeights, matWeights, -1, parentCombination);
        static_cast<CCondSoftMaxDistribFun*>(m_CorrespDistribFun)->
            AllocOffsetVector(pOffsets, parentCombination);
    }
}
Beispiel #4
0
/* Create a new image with uninitialized pixel values.
*/
SiftImage CreateImage(int rows, int cols, int pool)
{
    SiftImage im;

    im = NEW(ImageSt, pool);
    im->rows = rows;
    im->cols = cols;
    im->pixels = AllocMatrix(rows, cols, pool);
    return im;
}
/* Create a new image with uninitialized pixel values.
*/
Image CreateImage(int rows, int cols)
{
  Image im;
  im = (Image) malloc(sizeof(struct ImageSt));
  im->rows = rows;
  im->cols = cols;
  im->pixels = AllocMatrix(rows, cols);
  im->next = NULL;
  return im;
}
Beispiel #6
0
// ###############################
// Method name : Mem
// Description : memory allocation
// ###############################
int DEC_FIXP_LDPC_BIN::Mem()
{
    int ErrorFlag = IN_ERROR;

    // -----------------
    // memory allocation
    // -----------------
    if ( AllocPointer(ptFixp,   "DEC_FIXP_LDPC_BIN::ptFixp") == IN_ERROR ) goto end;
    if ( AllocArray(ptPostInfo, "DEC_FIXP_LDPC_BIN::ptPostInfo", VarNodesNb) == IN_ERROR ) goto end;
    if ( AllocMatrix(ptChkMess, "DEC_FIXP_LDPC_BIN::ptChkMess",  ChkNodesNb, MaxChkDeg) == IN_ERROR ) goto end;
    if ( AllocMatrix(ptVarMess, "DEC_FIXP_LDPC_BIN::ptVarMess",  ChkNodesNb, MaxChkDeg) == IN_ERROR ) goto end;

    // -----------------------------
    // memory successfully allocated
    // -----------------------------
    ErrorFlag = NO_ERROR;

end:
    return ErrorFlag;

}   // end of 'Mem' method
Beispiel #7
0
/* Apply the method developed by Matthew Brown (see BMVC 02 paper) to
   fit a 3D quadratic function through the DOG function values around
   the location (s,r,c), i.e., (scale,row,col), at which a peak has
   been detected.  Return the interpolated peak position by setting
   the vector "offset", which gives offset from position (s,r,c).  The
   returned function value is the interpolated DOG magnitude at this peak.
*/
float FitQuadratic(float offset[3], Image *dogs, int s, int r, int c)
{
    float g[3], **dog0, **dog1, **dog2;
    static float **H = NULL;

    /* First time through, allocate space for Hessian matrix, H. */
    if (H == NULL)
      H = AllocMatrix(3, 3, PERM_POOL);

    /* Select the dog images at peak scale, dog1, as well as the scale
       below, dog0, and scale above, dog2.
    */
    dog0 = dogs[s-1]->pixels;
    dog1 = dogs[s]->pixels;
    dog2 = dogs[s+1]->pixels;

    /* Fill in the values of the gradient from pixel differences. */
    g[0] = (dog2[r][c] - dog0[r][c]) / 2.0;
    g[1] = (dog1[r+1][c] - dog1[r-1][c]) / 2.0;
    g[2] = (dog1[r][c+1] - dog1[r][c-1]) / 2.0;

    /* Fill in the values of the Hessian from pixel differences. */
    H[0][0] = dog0[r][c] - 2.0 * dog1[r][c] + dog2[r][c];
    H[1][1] = dog1[r-1][c] - 2.0 * dog1[r][c] + dog1[r+1][c];
    H[2][2] = dog1[r][c-1] - 2.0 * dog1[r][c] + dog1[r][c+1];
    H[0][1] = H[1][0] = ((dog2[r+1][c] - dog2[r-1][c]) -
			 (dog0[r+1][c] - dog0[r-1][c])) / 4.0;
    H[0][2] = H[2][0] = ((dog2[r][c+1] - dog2[r][c-1]) -
			 (dog0[r][c+1] - dog0[r][c-1])) / 4.0;
    H[1][2] = H[2][1] = ((dog1[r+1][c+1] - dog1[r+1][c-1]) -
			 (dog1[r-1][c+1] - dog1[r-1][c-1])) / 4.0;

    /* Solve the 3x3 linear sytem, Hx = -g.  Result gives peak offset.
       Note that SolveLinearSystem destroys contents of H.
    */
    offset[0] = - g[0];
    offset[1] = - g[1];
    offset[2] = - g[2];
    SolveLinearSystem(offset, H, 3); 

    /* Also return value of DOG at peak location using initial value plus
       0.5 times linear interpolation with gradient to peak position
       (this is correct for a quadratic approximation).  
    */
    return (dog1[r][c] + 0.5 * DotProd(offset, g, 3));
}
Beispiel #8
0
//信道自适应,特征域
bool GMMMapScore::LFACompensateFeat(float * &p_pfSrcDesFeatBuf,int p_nFrmNum,
									  GMMStatistic &p_Statistic,
									  float *p_pfProbBuf,float **p_ppfProbBufBuf,float *p_pfIPPBuf_VecSize4)
{
	if(p_nFrmNum<=0||p_pfSrcDesFeatBuf==NULL)
	{
		return false;
	}

	// 分配空间
	FMatrix			AMatrix;							// m_nRankNum*m_nRankNum
	FVector			BVector,xVector;					// m_nRankNum*1
	FVector			UxVector;

	if (!AllocMatrix(AMatrix,m_nRankNum,m_nRankNum))	
		return false;
	if (!AllocVector(xVector,m_nRankNum))
	{
		FreeMatrix(AMatrix);	
		return false;
	}
	if (!AllocVector(BVector,m_nRankNum))
	{
		FreeMatrix(AMatrix);			
		FreeVector(xVector);
		return false;
	}
	if (!AllocVector(UxVector,m_nVecSize))
	{
		FreeMatrix(AMatrix);			
		FreeVector(xVector);
		FreeVector(BVector);
		return false;
	}

	ZeroMatrix(AMatrix);
	ZeroVector(BVector);
	for (int nn=0;nn<m_nRankNum;nn++)	// 初始化为I矩阵
		AMatrix.pBuf[nn*m_nRankNum+nn] = m_D.pBuf[nn];
	ResetStatisticBuf(p_Statistic);
	printf("Prob before LFA : ");
	// 计算统计量
	if (!ComputeStatistic_LFA(p_pfSrcDesFeatBuf,p_nFrmNum,p_Statistic,p_pfProbBuf,p_ppfProbBufBuf,p_pfIPPBuf_VecSize4,AMatrix,BVector))
	{
		FreeMatrix(AMatrix);
		FreeVector(BVector);
		FreeVector(xVector);
		FreeVector(UxVector);
		return false;
	}
	
	// 计算信道因子
	if (!Compute_X(AMatrix,BVector,xVector))
	{
		FreeMatrix(AMatrix);
		FreeVector(BVector);
		FreeVector(xVector);
		FreeVector(UxVector);
		return false;
	}

	// 特征转换
	for(int m=0;m<m_nMixNum;m++)
	{
		ZeroVector(UxVector);
		for(int d=0;d<m_nVecSize;d++)
		{
			for(int r=0;r<m_nRankNum;r++)
				UxVector.pBuf[d] += m_V.pBlockBuf[m][d*m_nRankNum+r]*xVector.pBuf[r];
		}
		for(int t=0;t<p_nFrmNum;t++)
		{
			if (p_ppfProbBufBuf[m][t]<1.0e-5)	continue;
			
			for(int d=0;d<m_nVecSize;d++)
			{
				p_pfSrcDesFeatBuf[t*m_nVecSizeStore+d] -= p_ppfProbBufBuf[m][t]*UxVector.pBuf[d];
			}
		}
	}
	printf("Prob after LFA : ");
	//xxiao add for debug
//	ComputeStatistic_LFA(p_pfSrcDesFeatBuf,p_nFrmNum,p_Statistic,p_pfProbBuf,p_ppfProbBufBuf,p_pfIPPBuf_VecSize4,AMatrix,BVector);
	// 释放空间
	FreeMatrix(AMatrix);
	FreeVector(BVector);
	FreeVector(xVector);
	FreeVector(UxVector);

	return true;
}