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

📄 matrix.h

📁 图像处理的压缩算法
💻 H
📖 第 1 页 / 共 2 页
字号:
/*------------------------------------------------------------------------------*
 * File Name: matrix.h															*
 * Creation: TCZ 07/22/2001														*
 * Purpose: Origin C internal utility classes									*
 * Copyright (c) OriginLab Corp.2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007	*
 * All Rights Reserved															*
 * 																				*
 * Modification Log:															*
 * ER 01/29/03 QA70_3802	ADD_VECTOR_FFT_IFFT									*
 *------------------------------------------------------------------------------*/
#ifndef _MATRIX_H
#define _MATRIX_H

/*
//////////////////////////////////////////////////////////////////////////////////////
This header file will contain some matrix related global functions implemented in originC level.
We already have some functions implemented as member functions of the matrix class internally. 
To complement those functions, we add some necessary but not as basic as the member functions here.
*/

//declare error here
#define FFT_ERROR_COULUMN_ROW_NUMBER_NEGATIVE (-1)
#define FFT_ERROR_SOURCE_MATRIX_EMPTY (-2)
#define FFT_ERROR_GETSUBMATRIX_FUNCTION_ERROR	(-3)
#define FFT_ERROR_GETREAL_FUNCTION_FAIL	(-4)
/** >Analysis
			This function performs a 2-dimensional Fast Fourier Transform.
	Remarks:
			Data matrix should be of type double. If user have the data	matrix in integer form, 
			user should first transform it to double using the member function of 
			matrixbase "CastToDouble".
	Example:
		This example computes the 2 dimension fast fourier transform of the matrix mRe.
	
    #include <matrix.h>
	void test_FFT2()
	{
		matrix<double> mRe = {{-1.486782,-0.530723,0.139563,-1.018762},
	                       {-0.831440 ,0.938971,-0.760729,1.620196},
	                       {- 0.7794,-0.5500 ,0.6640, -0.4333},
	                       {-2.0366,1.9275,0.3313,1.1098}};
	   
		matrix <complex> mResult;
	
		int ii,jj,Err;
	
		//test function FFT2 for arbitrary source matrix
		Err = FFT2(mRe, mResult);
	
		if ( 0 != Err ) 
		{
        	ASSERT(false);
        	printf("two-dimension FFT2 fails\n");
		}
		else
		{
			for(ii = 0; ii < mResult.GetNumRows();  ii++)
			{
				for(jj = 0; jj < mResult.GetNumCols(); jj++)
			    	printf("(%f, %fi)  ",mResult[ii][jj].m_re,mResult[ii][jj].m_im);
				printf("\n");
			}
		}
	}
	
	The output is as following:
		(-0.424101, 0.000000i)  (-1.377089, -0.126954i)  (-1.955942, 0.000000i)  (-1.377089, 0.126954i)  
		(-0.449501, 0.091251i)  (0.328995, -0.725482i)  (-0.166409, -0.147816i)  (-0.420468, -0.423113i)  
		(-1.573601, 0.000000i)  (-0.157784, -0.058716i)  (2.491026, 0.000000i)  (-0.157784, 0.058716i)  
		(-0.449501, -0.091251i)  (-0.420468, 0.423113i)  (-0.166409, 0.147816i)  (0.328995, 0.725482i)  
	
	Parameters:
			matSource: The data source matrix
			matFFT2Result: The matrix containing the 2 dimension FFT results
			nRowProcess:
			nColProcess: The number of rows or columns user provided. If they are -1, there is no padding
	 					or truncating. If they are more than the sizes of the matrix, the function will pad zero to meet the size
						If less than, it will truncate the data matrix.
	Return:
			If succeed, it will return 0;	Otherwise it returns error indications.
			Error:
			-1: Provided column number or row number. Must be -1 or positive integer
			-2: The data source matrix is empty 
			-3: When calling member function, it fails.
			-4: When calling GetReal or GetImage member function of matrix class, it failed.
			Nag Error:
			 11: The column or the row size of matrix is less then 1
 			 73: System failed to allocate memory
  
*/
int FFT2(matrix& matSource, matrix<complex>& matFFT2Result, int nRowProcess  = -1, int nColProcess = -1);

/** >Analysis
			This function performs a 2-dimensional Fast Fourier Transform.
	Remarks:
			Data matrix should be of type complex. If user have the data matrix in integer form, 
			user should first transform it to complex. 
	Example:
		This example computes the 2 dimension fast fourier transform of the matrix mSource. 
		
	void test_FFT2()
	{
		matrix<double> mRe = {{-1.486782,-0.530723,0.139563,-1.018762},
	                       {-0.831440 ,0.938971,-0.760729,1.620196},
	                       {- 0.7794,-0.5500 ,0.6640, -0.4333},
	                       {-2.0366,1.9275,0.3313,1.1098}};
	
		matrix<double> mIm = {{2.482,3.5723,0.9553,-1.01842},
	                       {-1.8317 ,4.38271,-1.0729,1.226},
	                       {0.7724,-0.5540 ,1.540, -1.333},
	                       {2.0456,-1.935,2.6513,1.2708}};
	         
		matrix <complex> mSource, mResult;
	
		mSource.MakeComplex(mRe,mIm);
	
		int ii,jj,Err;
	
		//test function FFT2 for arbitrary source matrix
		Err = FFT2(mSource, mResult);
	
		if ( 0 != Err ) 
		{
        	ASSERT(false);
        	printf("two-dimension FFT2 fails\n");
		}
		else
		{
			for(ii = 0; ii < mResult.GetNumRows();  ii++)
			{
				for(jj = 0; jj < mResult.GetNumCols(); jj++)
			    	printf("(%f, %fi)  ",mResult[ii][jj].m_re,mResult[ii][jj].m_im);
				printf("\n");
			}
		}
	}
	
	The output is as following:
		(-0.424101, 3.288348i)  (-0.046932, -0.278304i)  (-1.955942, 0.482652i)  (-2.707247, -0.024397i)  
		(-0.781648, 1.482696i)  (1.243650, -1.742534i)  (-3.635011, -0.976811i)  (-1.411672, 1.741090i)  
		(-1.573601, -0.080057i)  (1.196919, 0.472184i)  (2.491026, 2.058758i)  (-1.512486, 0.589616i)  
		(-0.117354, 1.300194i)  (0.570738, 2.587315i)  (3.302194, -0.681179i)  (-0.585660, -0.291571i)  

	Parameters:
			matSource: The data source matrix
			matFFT2Result: The matrix containing the 2 dimension FFT results
			nRowProcess:
			nColProcess: The number of rows or columns user provided. If they are -1, there is no padding
	 					or truncating. If they are more than the sizes of the matrix, the function will pad zero to meet the size
						If less than, it will truncate the data matrix.
	Return:
			If succeed, it will return 0;	Otherwise it returns error indications.
			Error:
			-1: Provided column number or row number. Must be -1 or positive integer
			-2: The data source matrix is empty 
			-3: When calling member function, it fails.
			-4: When calling GetReal or GetImage member function of matrix class, it failed.
			Nag Error:
			 11: The column or the row size of matrix is less then 1
 			 73: System failed to allocate memory
  
*/
int FFT2(matrix<complex>& matSource, matrix<complex>& matFFT2Result, int nRowProcess  = -1, int nColProcess = -1);

/** >Analysis
			This function performs a 2-dimensional inverse Fast Fourier Transform.
	Remarks:
			Data matrix should be of type complex. If user have the data matrix in integer form, 
			user should first transform it to complex. 
	Example:
		This example computes the 2 dimension inverse fast fourier transform of the matrix mSource. 
		
	void test_IFFT2()
	{
		matrix<double> mRe = {{-1.486782,-0.530723,0.139563,-1.018762},
	                       {-0.831440 ,0.938971,-0.760729,1.620196},
	                       {- 0.7794,-0.5500 ,0.6640, -0.4333},
	                       {-2.0366,1.9275,0.3313,1.1098}};
	
		matrix<double> mIm = {{2.482,3.5723,0.9553,-1.01842},
	                       {-1.8317 ,4.38271,-1.0729,1.226},
	                       {0.7724,-0.5540 ,1.540, -1.333},
	                       {2.0456,-1.935,2.6513,1.2708}};
	         
		matrix <complex> mSource, mResult;
	
		mSource.MakeComplex(mRe,mIm);
	
		int ii,jj,Err;
	
		Err = IFFT2(mSource, mResult);
	
		if ( 0 != Err ) 
		{
			ASSERT(false);
			printf("two-dimension IFFT2 fails\n");
		}
		else
		{
			for(ii = 0; ii < mResult.GetNumRows();  ii++)
			{
				for(jj = 0; jj < mResult.GetNumCols(); jj++)
			    	printf("(%f, %fi)  ",mResult[ii][jj].m_re,mResult[ii][jj].m_im);
				printf("\n");
			}
		}
	}
	
	The output is as following:
	    (-0.424101, 3.288348i)  (-2.707247, -0.024397i)  (-1.955942, 0.482652i)  (-0.046932, -0.278304i)  
		(-0.117354, 1.300194i)  (-0.585660, -0.291571i)  (3.302194, -0.681179i)  (0.570738, 2.587315i)  
		(-1.573601, -0.080057i)  (-1.512486, 0.589616i)  (2.491026, 2.058758i)  (1.196919, 0.472184i)  
		(-0.781648, 1.482696i)  (-1.411672, 1.741090i)  (-3.635011, -0.976811i)  (1.243650, -1.742534i)  
	
	Parameters:
			matSource: The data source matrix
			matIFFT2Result: The matrix containing the 2 dimension inverse FFT results
			nRowProcess:
			nColProcess:The number of rows or columns user provided. if they are -1, there are no padding
	 					or truncating. if they are larger than the size of the matrix, the function will pad zero to meet
						the size. If less than, it will truncate the data matrix.
	Return:
			If succeed, it will return 0;	Otherwise it returns error indications.
			Error:
			-1: Provided column number or row number. Must be -1 or positive integer
			-2: The data source matrix is empty 
			-3: When calling member function, it fails.
			-4: When calling GetReal or GetImage member function of matrix class, it failed.
			Nag Error:
			 11: The column or the row size of matrix is less then 1
 			 73: System failed to allocate memory
*/
int IFFT2(matrix<complex>& matSource, matrix<complex>& matIFFT2Result, int nRowProcess  = -1, int nColProcess = -1);

/** >Analysis
			These two Functions will do the 1 dimension Fast Fourier Transform.
  	Remarks:
			Data matrix should be of type double. If user have the data	matrix in integer form, 
			user should first transform it to double using the member function of 
			matrixbase "CastToDouble".
	Example:
		This example computes the 1 dimension fast fourier transform on each row of the matrix mRe.
		
	void test_FFT()
	{
		matrix <double> mRe = {{1,2.5,3.5,2,1.0,2.3},{3.2,4.1,2.2,1.0,4,2}};
		int ii , jj;
	
		matrix <complex> mResult;
	
		//test 1d fft function for arbitrary source matrix.//
		int Err = FFT(mRe, mResult, -1);
	
		if ( 0 != Err ) 
		{
       		ASSERT(false);
       		printf("one-dimension FFT fails.\n");
		}
		
		else
		{
			for(ii = 0; ii < mResult.GetNumRows();  ii++)
			{
		    	for(jj = 0; jj < mResult.GetNumCols(); jj++)
					printf("(%f, %fi)  ",mResult[ii][jj].m_re,mResult[ii][jj].m_im);
				printf("\n");		    
			}
		}
	}
	
	The output is as following:
		(5.021454, 0.000000i)  (-0.347011, -0.954594i)  (-0.673610, 0.813173i)  (-0.530723, 0.000000i)  (-0.673610, -0.813173i)  (-0.347011, 0.954594i)  
        (6.736097, 0.000000i)  (0.877734, -0.106066i)  (-0.796084, -1.378858i)  (0.938971, 0.000000i)  (-0.796084, 1.378858i)  (0.877734, 0.106066i)  
	
    Parameters:
			matSource: The data source matrix
			matFFTResult: The matrix containing the 1 dimension FFT results
			nColProcess: The number of columns user provided. If it is -1, function will not do any padding
	 					or truncating. But if it is larger than the size of the  matrix, it will pad zero to meet the size
						if less than, it will truncate the data matrix.
	Return:
			If succeed, it will return 0;	Otherwise it returns error indications.
			Error:
			-1: Provided column number or row number. Must be -1 or positive integer
			-2: The data source matrix is empty 
			-3: When calling member function, it fails.
			-4: When calling GetReal or GetImage member function of matrix class, it failed.
			Nag Error:
			 11: The column or the row size of matrix is less then 1
 			 73: System failed to allocate memory

*/
int FFT(matrix& matSource, matrix<complex>& matFFTResult, int nColProcess = -1);

/** >Analysis
			This function performs 1-dimensional Fast Fourier Transform on each row of data in a matrix.
  	Remarks:
			Data matrix should be of type complex. If user have the data matrix in integer form, 
			user should first transform it to complex.
	Example:
		This example computes the 1 dimension fast fourier transform on each row of the matrix mSource.
		
	void FFT_test()
	{
		matrix <double> mRe = {{1,2.5,3.5,2,1.0,2.3},{3.2,4.1,2.2,1.0,4,2}};
		matrix <double> mIm = {{1,0.5,1.0,0.8,3.1,0.3},{2.2,3.2,3.0,1.5,2,2.1}};
		int ii , jj;
	
		matrix <complex> mSource, mResult;
		mSource.MakeComplex(mRe,mIm);
	   
		//test 1d fft function for complex source matrix.//
	
		int Err = FFT(mSource, mResult);
	
		if ( 0 != Err ) 
		{
       		ASSERT(false);
       		printf("one-dimension FFT fails\n");
		}
		else
		{
	    	for(ii = 0; ii < mResult.GetNumRows();  ii++)
			{
		    	for(jj = 0; jj < mResult.GetNumCols(); jj++)
			    	printf("(%f, %fi)  ",mResult[ii][jj].m_re,mResult[ii][jj].m_im);
		    	printf("\n");
		    
			}
		}
	}
	
	The output is as following:
		(5.021454, 2.735264i)  (-1.018762, -1.546554i)  (0.139563, 0.547811i)  (-0.530723, 1.428869i)  (-1.486782, -1.078534i)  (0.324740, 0.362634i)  
        (6.736097, 5.715476i)  (1.620196, 0.240945i)  (-0.760729, -1.970818i)  (0.938971, 0.163299i)  (-0.831440, 0.786898i)  (0.135272, 0.453077i)  

	
	Parameters:
			matSource: The data source matrix
			matFFTResult: The matrix containing the 1 dimension FFT results
			nColProcess: The number of columns user provided. If it is -1, function will not do any padding
	 					or truncating. But if it is larger than the size of the  matrix, it will pad zero to meet the size
						if less than, it will truncate the data matrix.
	Return:
			If succeed, it will return 0;	Otherwise it returns error indications.
			Error:
			-1: Provided column number or row number. Must be -1 or positive integer
			-2: The data source matrix is empty 
			-3: When calling member function, it fails.
			-4: When calling GetReal or GetImage member function of matrix class, it failed.
			Nag Error:
			 11: The column or the row size of matrix is less then 1
 			 73: System failed to allocate memory

*/
int FFT(matrix<complex>& matSource, matrix<complex>& matFFTResult, int nColProcess = -1);

/** >Analysis
			This function performs 1-dimensional inverse Fast Fourier Transform on each row of data in a matrix.
	Remarks:
			Data matrix should be of type complex. If user have the data matrix in integer form, 
			user should first transform it to complex.

	Example:
		int test_IFFT()
		{
			matrix matSource = {{1,2,3,4,5},{6,7,8,9,10}};
			
			int nRet;
			matrix<complex> matResult;
			nRet = FFT(matSource,matResult, -1);
			if(nRet)
				return nRet;
			int nNumCols = matResult.GetNumCols();
			int nNumRows = matResult.GetNumRows();
			int ii, jj;
			for( ii = 0; ii < nNumRows; ii++)
			{
				for(jj = 0; jj < nNumCols; jj++)
					printf("%f + %f i",matResult[ii][jj].m_re, matResult[ii][jj].m_im);
				printf("\n");
			}

			printf("now do the padding\n");
			nRet = FFT(matSource,matResult, 14);
			if(nRet)
				return nRet;
			//after The padding	

			nNumCols = matResult.GetNumCols();
			nNumRows = matResult.GetNumRows();
			
			for( ii = 0; ii < nNumRows; ii++)
			{

⌨️ 快捷键说明

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