matlab中fft2函数,C#实现matlab的几个函数(fft2,ifft2,psf2otf,diff)

使用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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值