使用Nuget工具包MathNet
ifft2:
public static Matrix ifft2(Matrix matrix)
{
var Xsamples = matrix.ToRowArrays();//.ToRowMajorArray();
for (int i = 0; i < Xsamples.Length; i++)
{
Fourier.Inverse(Xsamples[i], option);
}
matrix = Matrix.Build.DenseOfRowArrays(Xsamples);
var Ysamples = matrix.ToColumnArrays();
for (int i = 0; i < Ysamples.Length; i++)
{
Fourier.Inverse(Ysamples[i], option);
}
matrix = Matrix.Build.DenseOfColumnArrays(Ysamples);
// optional
matrix.CoerceZero(1e-7);
return matrix;
}
diff:
public static Matrix diff(Matrix matrix, int grade, int type)
{
if (type == 1)//按行
{
var mat = Matrix.Build.Dense(matrix.RowCount - 1, matrix.ColumnCount);
for (int i = 0; i < matrix.RowCount; i++)
{
if (i == 0)
{
continue;
}
for (int j = 0; j < matrix.ColumnCount; j++)
{
mat[i - 1, j] = matrix[i, j] - matrix[i - 1, j];
}
}
return mat;
}
else if (type == 2)//按列
{
var mat = Matrix.Build.Dense(matrix.RowCount, matrix.ColumnCount - 1);
for (int j = 0; j < matrix.ColumnCount; j++)
{
if (j == 0)
{
continue;
}
for (int i = 0; i < matrix.RowCount; i++)
{
mat[i, j - 1] = matrix[i, j] - matrix[i, j - 1];
}
}
return mat;
}
else
{
return matrix;
}
///matrix = Matrix.Build.DenseOfColumnArrays(Cols);
}
psf2otf:
public static Matrix psf2otf(Matrix matrix, int row = 0, int col = 0)
{
if (row == 0)
{
row = matrix.RowCount;
}
if (col == 0)
{
col = matrix.ColumnCount;
}
var PadMatrix = Matrix.Build.Dense(row, col, 0);
for (int i = 0; i < matrix.RowCount; i++)
{
for (int j = 0; j < matrix.ColumnCount; j++)
{
PadMatrix[i, j] = matrix[i, j];
}
}
matrix = PadMatrix;
// Pad the PSF to outSize
// padSize = outSize - psfSize;
// psf = padarray(psf, padSize, 'post');
// Circularly shift otf so that the "center" of the PSF is at the
// (1, 1) element of the array.
// psf = circshift(psf, -floor(psfSize / 2));
int d_r = (int)Math.Floor((double)row / 2 - 1);
int d_c = (int)Math.Floor((double)col / 2 - 1);
var Rows = matrix.ToRowArrays();
Complex32[][] newRows = new Complex32[matrix.RowCount][];
for (int i = 0; i < matrix.RowCount; i++)
{
if (d_r + i < matrix.RowCount)
{
newRows[i] = Rows[d_r + i];
}
else
{
newRows[i] = Rows[d_r + i - matrix.RowCount];
}
}
matrix = Matrix.Build.DenseOfRowArrays(newRows);
//
var Cols = matrix.ToColumnArrays();
Complex32[][] newCols = new Complex32[matrix.ColumnCount][];
for (int i = 0; i < matrix.ColumnCount; i++)
{
if (d_c + i < matrix.ColumnCount)
{
newCols[i] = Cols[d_c + i];
}
else
{
newCols[i] = Cols[d_c + i - matrix.ColumnCount];
}
}
//var mid = Cols[0];
//Cols[0] = Cols[d_c];
//Cols[d_c] = mid;
matrix = Matrix.Build.DenseOfColumnArrays(newCols);
var Xsamples = matrix.ToRowArrays();//.ToRowMajorArray();
for (int i = 0; i < Xsamples.Length; i++)
{
MathNet.Numerics.IntegralTransforms.Fourier.Forward(Xsamples[i], option);
}
matrix = Matrix.Build.DenseOfRowArrays(Xsamples);
var Ysamples = matrix.ToColumnArrays();
for (int i = 0; i < Ysamples.Length; i++)
{
MathNet.Numerics.IntegralTransforms.Fourier.Forward(Ysamples[i], option);
}
matrix = Matrix.Build.DenseOfColumnArrays(Ysamples);
matrix.CoerceZero(1e-7);
return matrix;
// Foriour.
//MathNet.Numerics.IntegralTransforms.Fourier.Forward2D(matrix, FourierOptions.Matlab);
}
fft2
public static Matrix fft2(Matrix matrix)
{
var Xsamples = matrix.ToRowArrays();//.ToRowMajorArray();
for (int i = 0; i < Xsamples.Length; i++)
{
MathNet.Numerics.IntegralTransforms.Fourier.Forward(Xsamples[i], option);
}
matrix = Matrix.Build.DenseOfRowArrays(Xsamples);
var Ysamples = matrix.ToColumnArrays();
for (int i = 0; i < Ysamples.Length; i++)
{
MathNet.Numerics.IntegralTransforms.Fourier.Forward(Ysamples[i], option);
}
matrix = Matrix.Build.DenseOfColumnArrays(Ysamples);
matrix.CoerceZero(1e-7);
return matrix;
}
补充:其中option的定义为
public static FourierOptions option { get; set; } = FourierOptions.Matlab;