📄 example.cs
字号:
using System;
using System.Runtime.InteropServices;
using UtilityMatlabCompilerVer4 ;
namespace MatlabCSharpExample
{
class Example
{
//constructor
public Example()
{
fftlibInitialize();
}
public void CleanUp()
{
fftlibTerminate();
}
/* declare dll functions */
[ DllImport( "fftlib.dll ", CallingConvention = CallingConvention.Cdecl)]
public static extern void fftlibInitialize();
[ DllImport( "fftlib.dll ", CallingConvention = CallingConvention.Cdecl)]
public static extern void fftlibTerminate();
[ DllImport( "fftlib.dll ", CallingConvention = CallingConvention.Cdecl)]
public static extern void mlfMyfft(int nargout, ref IntPtr Y, IntPtr X);
[ DllImport( "fftlib.dll ", CallingConvention = CallingConvention.Cdecl)]
public static extern void mlfMyifft(int nargout, ref IntPtr Y, IntPtr X);
[ DllImport( "fftlib.dll ", CallingConvention = CallingConvention.Cdecl)]
public static extern void mlfMyfft2(int nargout, ref IntPtr Y, IntPtr X);
[ DllImport( "fftlib.dll ", CallingConvention = CallingConvention.Cdecl)]
public static extern void mlfMyifft2(int nargout, ref IntPtr Y, IntPtr X);
/* end dll functions */
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("Fast Fourier Transform.") ;
Example obj = new Example() ;
obj.FastFourierTrans1D() ;
obj.InverseFastFourierTrans1D() ;
obj.FastFourierTrans2D() ;
obj.InverseFastFourierTrans2D() ;
obj.CleanUp() ;
}
/* **************************** */
public void FastFourierTrans1D()
{
double[] db_X = { 6, 3, 7, -9, 0, 3, -2, 1 } ;
int vectorSize = 8 ;
/* declare mxArray variables */
IntPtr mx_X = (IntPtr) null ;
IntPtr mx_Y = (IntPtr) null ;
/* convert Cs double to mxArray */
mx_X = MatlabCSharp.double2mxArray_vectorColumnReal (db_X) ;
/* call an implemental function */
mlfMyfft(1, ref mx_Y, mx_X);
/* convert back to Cs double */
double[] db_YReal = new double [vectorSize] ;
double[] db_YImag = new double [vectorSize] ;
MatlabCSharp.mxArray2double_vectorComplex(mx_Y, ref db_YReal, ref db_YImag) ;
/* print out */
Console.WriteLine("Fast Fourier Transform of X : ") ;
int i ;
for (i=0; i<vectorSize; i++)
{
Console.Write( db_YReal[i].ToString() + " + " ) ;
Console.Write( db_YImag[i].ToString() + "i" + "\n" ) ;
}
Console.WriteLine("\n") ;
}
/* **************************** */
public void InverseFastFourierTrans1D()
{
double[] db_YReal = { 9.00, 13.0711, 1.00, -1.0711, 13.00, -1.0711 , 1.00 , 13.0711 } ;
double[] db_YImag = { 0 , -1.9289, -14.00, 16.0711, 0 , -16.0711, 14.00, 1.9289 } ;
int vectorSize = 8 ;
/* declare mxArray variables */
IntPtr mx_X = (IntPtr) null ;
IntPtr mx_Y = (IntPtr) null ;
/* convert Cs double to mxArray */
mx_Y = MatlabCSharp.double2mxArray_vectorColumnComplex(db_YReal, db_YImag) ;
/* call an implemental function */
mlfMyifft (1, ref mx_X, mx_Y);
/* convert back to Cs double */
double[] db_XReal = new double [vectorSize] ;
double[] db_XImag = new double [vectorSize] ;
MatlabCSharp.mxArray2double_vectorComplex(mx_X, ref db_XReal, ref db_XImag) ;
/* print out */
Console.WriteLine("Inverse Fast Fourier Transform of Y : " ) ;
int i ;
for (i=0; i<vectorSize; i++)
{
Console.Write( db_XReal[i].ToString() + " + " ) ;
Console.Write( db_XImag[i].ToString() + "i" + "\n" ) ;
}
Console.WriteLine("\n") ;
}
public void FastFourierTrans2D()
{
double[,] X = { {4 , 3.2, 6.8, 9.1 } ,
{-4 , 1.2, 4.3, 5.4 } ,
{2.2, -6.7, 8 , 12.2 } } ;
int row = 3 ;
int col = 4 ;
int i, j ;
/* declare mxArray variables */
IntPtr mx_X = (IntPtr) null ;
IntPtr mx_Y = (IntPtr) null ;
/* convert Cs double to mxArray */
mx_X = MatlabCSharp.double2mxArray_matrixReal(X) ;
/* call an implemental function */
mlfMyfft2(1, ref mx_Y, mx_X);
/* convert back to Cs double */
double [,] db_YReal = new double [row, col] ;
double [,] db_YImag = new double [row, col] ;
MatlabCSharp.mxArray2double_matrixComplex(mx_Y, ref db_YReal, ref db_YImag) ;
/* print out */
Console.WriteLine("2-D Fast Fourier Transform of X : ") ;
for (i=0; i<row; i++)
{
for (j=0; j<col; j++ )
{
Console.Write( db_YReal.GetValue(i,j).ToString() + " + " );
Console.Write( db_YImag.GetValue(i,j).ToString() + "i" + "\t\t" ) ;
}
Console.WriteLine("\n") ;
}
Console.WriteLine("\n") ;
}
/* **************************** */
public void InverseFastFourierTrans2D()
{
double[,] YReal = { { 45.7000, -16.9000, -3.1000, -16.9000 } ,
{ 11.8000, -8.4806, -0.7000, 16.9806 } ,
{ 11.8000, 16.9806, -0.7000, -8.4806 } };
double[,] YImag = {{ 0, 29.0000, 0 , -29.0000 } ,
{ 7.6210, -3.4849, 9.5263, 7.8151 } ,
{-7.6210, -7.8151, -9.5263, 3.4849 } };
int row = 3 ;
int col = 4 ;
int i, j ;
/* declare mxArray variables */
IntPtr mx_X = (IntPtr) null ;
IntPtr mx_Y = (IntPtr) null ;
/* convert Cs double to mxArray */
mx_Y = MatlabCSharp.double2mxArray_matrixComplex(YReal, YImag) ;
/* call an implemental function */
mlfMyifft2 (1, ref mx_X, mx_Y);
/* convert back to Cs double */
double[,] db_XReal = new double [row, col] ;
double[,] db_XImag = new double [row, col] ;
MatlabCSharp.mxArray2double_matrixComplex(mx_X, ref db_XReal, ref db_XImag) ;
/* print out */
Console.WriteLine("Inverse 2-D Fast Fourier Transform of Y : " ) ;
for (i=0; i<row; i++)
{
for (j=0; j<col; j++ )
{
Console.Write( db_XReal.GetValue(i,j).ToString() + " + " ) ;
Console.Write( db_XImag.GetValue(i,j).ToString() + "i" + "\t\t" );
}
Console.WriteLine("\n") ;
}
Console.WriteLine("\n") ;
}
} // end class
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -