⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 matrix.c

📁 图像处理的压缩算法
💻 C
📖 第 1 页 / 共 2 页
字号:

	if(nRet = FFT(matSourceCopy, matFFTResult, nColProcess))
		return nRet;

	if(nRet = matFFTResult.Conjugate())
		return nRet;	
		
}	
	
static int seperate_complex_mat_to_real_and_imag_mat(matrix<complex>& matSource, matrix& matReal, matrix&  matImage, int nRowProcess, int nColProcess)
{
	matrix matTemp;
	BOOL bOK;
	bOK = matSource.GetReal(matTemp);
	if(!bOK)
		return FFT_ERROR_GETREAL_FUNCTION_FAIL;
	int nRet;
	if(nRet = set_matrix_with_padding_truncting(matTemp, matReal, nRowProcess, nColProcess))
		return nRet;
	bOK = matSource.GetImaginary(matTemp);
	if(!bOK)
		return FFT_ERROR_GETREAL_FUNCTION_FAIL;
	if(nRet = set_matrix_with_padding_truncting(matTemp, matImage, nRowProcess, nColProcess))
		return nRet;
}

// ER 01/29/03 QA70_3802	ADD_VECTOR_FFT_IFFT
// The following function performs 1-D FFT on a complex vector.
// It uses the matrix-based 1-D FFT function.

int FFT(vector<complex>& vecSource, vector<complex>& vecFFTResult)
{
	// Get size of source vector
	int iSize = vecSource.GetSize();
	if(iSize < 1) return -5;
	
	// Create a complex matrix of size 1xn and copy source vector into matrix
	matrix<complex> matSource;
	matSource.SetSize(1,iSize);
	matSource.SetByVector(vecSource);
	
	// Create complex matrix to hold result of FFT operation
	matrix<complex> matFFTResult;
	
	// Call the matrix-based FFT function to perform FFT on this one row
	int iRet = FFT(matSource, matFFTResult);
	if(iRet) return iRet;

	// Extract FFT result from result matrix, into a complex matrix, and return
	matFFTResult.GetAsVector(vecFFTResult);
	
	// return 0 on success
	return 0;
}

// ER 01/29/03 QA70_3802	ADD_VECTOR_FFT_IFFT
// The following function performs 1-D inverse FFT on a complex vector.
// It uses the matrix-based 1-D inverse FFT function.

int IFFT(vector<complex>& vecSource, vector<complex>& vecFFTResult)
{
	// Get size of source vector
	int iSize = vecSource.GetSize();
	if(iSize < 1) return -5;
	
	// Create a complex matrix of size 1xn and copy source vector into matrix
	matrix<complex> matSource;
	matSource.SetSize(1,iSize);
	matSource.SetByVector(vecSource);
	
	// Create complex matrix to hold result of IFFT operation
	matrix<complex> matFFTResult;
	
	// Call the matrix-based IFFT function to perform IFFT on this one row
	int iRet = IFFT(matSource, matFFTResult);
	if(iRet) return iRet;

	// Extract IFFT result from result matrix, into a complex matrix, and return
	matFFTResult.GetAsVector(vecFFTResult);
	
	// return 0 on success
	return 0;
}

/**
	int SVD(matrix<complex>& matSource, matrix& matS, matrix<complex>& matU, matrix<complex>& matV);
	int SVD(matrix<complex>& matSource, matrix<complex>& matS); 
	int SVD(matrix& matSource, matrix& matS);
	int SVD(matrix& matSource, matrix& matS, matrix& matU, matrix& matV);

			These four functions are used to do the singular value decomposition. matSource = matU* matS* matV'
	Example:
	Parameters:
			matSource: The  m * n source data matrix
			matS: The min(m, n) diaganol sigular matrix 
			matU: The left side  m * m unitary matrix
			matV: The right side n* n unitary matrix
	return:
			if succeed, it will return 0

*/
	
int SVD(matrix& matSource, matrix & matS, matrix& matU, matrix& matV)
{
	//fist check the size of the matrix
	int nNumCols = matSource.GetNumCols();
	int nNumRows = matSource.GetNumRows();
	//no need to check size, because nag will take care for it
	//set the result matrix size
	matU.SetSize(nNumRows, nNumRows);
	matV.SetSize(nNumCols, nNumCols);
	vector vecE;
	int nMinSize = nNumCols > nNumRows ? nNumRows : nNumCols;
	vecE.SetSize(nMinSize -1);
	vector vecS;
	vecS.SetSize(nMinSize);
	//call the nag function
	int nRet;
	int nNumIter, nfailIfor;
	//we make a copy of source data here becasue we do not want to overwirte the data
	matrix matSourceCopy(matSource);
	if(nRet = nag_real_svd(nNumRows, nNumCols, matSourceCopy, nNumCols, 0, NULL, 0, TRUE, matU, nNumRows, 
							vecS, TRUE, matV, nNumCols, &nNumIter, vecE, &nfailIfor))
		return nRet;
	//set back the U V matrix
	if(nNumRows >= nNumCols)
		matU = matSourceCopy;
	else
		matV = matSourceCopy;
	matV.Transpose(); // Nag return MatV'
	//set back matS
	nRet = matS.SetDiagonal(vecS, 0);
	
	//here we do not consider nfailinfo, becasue it is rarely occurs.
	return nRet;				
				
}
	
int SVD(matrix<complex>& matSource, matrix& matS, matrix<complex>& matU, matrix<complex>& matV)
{
	//fist check the size of the matrix
	int nNumCols = matSource.GetNumCols();
	int nNumRows = matSource.GetNumRows();
	//no need to check size, because nag will take care for it
	//set the result matrix size
	matU.SetSize(nNumRows, nNumRows);
	matV.SetSize(nNumCols, nNumCols);
	vector vecE;
	int nMinSize = nNumCols > nNumRows ? nNumRows : nNumCols;
	vecE.SetSize(nMinSize -1 );
	vector vecS;
	vecS.SetSize(nMinSize);
	//call the nag function
	int nRet;
	int nNumIter, nfailIfor;
	//we make a copy of source data here becasue we do not want to overwirte the data
	matrix<complex> matSourceCopy(matSource); 
	if(nRet = nag_complex_svd(nNumRows, nNumCols, matSourceCopy, nNumCols, 0, NULL, 0, TRUE, matU, nNumRows, 
							vecS, TRUE, matV, nNumCols, &nNumIter, vecE, &nfailIfor))
		return nRet;
	//set back the U V matrix
	if(nNumRows >= nNumCols)
		matU = matSourceCopy;
	else
		matV = matSourceCopy;
	matV.Transpose(); // Nag return MatV'
	//set back matS
	nRet = matS.SetDiagonal(vecS, 0);

	//here we do not consider nfailinfo, becasue it is rarely occurs.
	return nRet;				
				
}
	
int SVD(matrix<complex>& matSource, matrix& matS) 
{
	matrix<complex> matU, matV;
	int nRet = 0;
	nRet = SVD(matSource, matS, matU, matV);
	return nRet;
}


int SVD(matrix& matSource, matrix& matS)
{
	matrix matU, matV;
	int nRet = 0;
	nRet = SVD(matSource, matS, matU, matV);
	return nRet;
}


/**
	int Trace(matrix<short> & matSource, int & nSum);
	int Trace(matrix<int> & matSource, int & nSum);
	int Trace(matrix<double> & matSource, double & dSum);
	int Trace(matrix<complex> & matSource, complex & cSum);

			These four functions will give the trace of a matrix
	Remarks:
			we do not support every matrix type.
 	Example:
	Parameters:
			matSource: The source matrix
			nSum:
			dSum:
			cSum:
				These are the variables to store the trace.
	Return:
			if succeed, it will return 0

*/

#define CHECK_MATRIX_FOR_TRACE {\
	int nNumCols = matSource.GetNumCols(); \
	int nNumRows = matSource.GetNumRows(); \
	if(!nNumCols || !nNumRows)  \
		return FFT_ERROR_SOURCE_MATRIX_EMPTY; \
}


int Trace(matrix<short> &matSource, int& nSum)
{
	CHECK_MATRIX_FOR_TRACE;

	vector<short> vecDiag;
	int nRet;
	if(nRet = matSource.GetDiagonal(vecDiag))
		return nRet;
	if(nRet = vecDiag.Sum(nSum))
		return nRet;

}

int Trace(matrix<int> &matSource, int& nSum)
{
	CHECK_MATRIX_FOR_TRACE;
	
	vector<int> vecDiag;
	int nRet;
	if(nRet = matSource.GetDiagonal(vecDiag))
		return nRet;
	if(nRet = vecDiag.Sum(nSum))
		return nRet;

}
int Trace(matrix<double> &matSource, double& dSum)
{
	CHECK_MATRIX_FOR_TRACE;
	
	vector<double> vecDiag;
	int nRet;
	if(nRet = matSource.GetDiagonal(vecDiag))
		return nRet;
	if(nRet = vecDiag.Sum(dSum))
		return nRet;

}	

int Trace(matrix<complex> & matSource, complex& cSum)
{
	CHECK_MATRIX_FOR_TRACE;

	vector<complex> vecDiag;
	int nRet;
	if(nRet = matSource.GetDiagonal(vecDiag))
		return nRet;
	if(nRet = vecDiag.Sum(cSum))
		return nRet;

}

///TCZ 07/29/02 QA70-2497	ADD_GLOBAL_STATS_SUMMARY_FUNCTION
//#define MAT_ERR_WEIGHTS_DATA_SIZE_NOT_MATCH (-5) //defined in the matrix.h
/**
			This function will give the summary of the statistics of a source data contained in a matrix	
	Remarks:
			If user does not have the weight, then just supply a matix without any initialization for weight 
	Example:
	Parameters:
			matData: The Data Source
			MatrixStatsSummary: The Results
			matWeights: The weight if any
	Return:
			Return 0 if succeed.
			MAT_ERR_WEIGHTS_DATA_SIZE_NOT_MATCH: The weight matrix size does not match that of the source
	int MatrixBasicStats(matrix & mData, MatrixStats & sMatStatsSum, matrix * mpWeights = NULL);

*/
int MatrixBasicStats(matrix & mData, MatrixStats & sMatStatsSum, matrix * mpWeights )
{
	
	//first check the size of the weight and data size 
	int nNumCols = mData.GetNumCols();
	int nNumRows = mData.GetNumRows();
	if(mpWeights != NULL) 
	{
		int nNumColsWeights = mpWeights->GetNumCols();
		int nNumRowsWeights = mpWeights->GetNumRows();
		if(nNumCols != nNumColsWeights || nNumRows != nNumRowsWeights)
			return MAT_ERR_WEIGHTS_DATA_SIZE_NOT_MATCH;
	}
	int nNumPoints = nNumCols * nNumRows;
	int nValid;
	double dMean, dSD, dMax, dMin, dWSum;
	int nRet;
	if(mpWeights == NULL)
	{
		if( nRet = nag_summary_stats_1var(nNumPoints, mData, NULL, &nValid, &dMean, &dSD, NULL, NULL, &dMin, &dMax, &dWSum))
			return nRet;
	}
	else
	{
		if( nRet = nag_summary_stats_1var(nNumPoints, mData, *mpWeights, &nValid, &dMean, &dSD, NULL, NULL, &dMin, &dMax, &dWSum))
			return nRet;
	
	}	
	//call the second nag function to get the median and hinge
	double dRes[5];
	if( nRet = nag_5pt_summary_stats(nNumPoints, mData, dRes))
		return nRet;
	//set the structure value
	sMatStatsSum.nMissingValue = nNumPoints - nValid;
	sMatStatsSum.dMean = dMean;
	sMatStatsSum.dSD = dSD;
	sMatStatsSum.dMax = dMax;
	sMatStatsSum.dMin = dMin;
	sMatStatsSum.dMedian = dRes[2];
	sMatStatsSum.dLowHinge = dRes[1];
	sMatStatsSum.dUpHinge = dRes[3];
	sMatStatsSum.dWSum = dWSum;
	
	return nRet;	
}
//END-------------------------------ADD_GLOBAL_STATS_SUMMARY_FUNCTION

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -