当前位置:网站首页>C # realizes FFT forward and inverse transformation and frequency domain filtering

C # realizes FFT forward and inverse transformation and frequency domain filtering

2022-07-04 16:39:00 Full stack programmer webmaster

To carry out FFT The first operation is to construct complex classes , Reference resources

http://blog.csdn.net/iamoyjj/archive/2009/05/15/4190089.aspx

The following program is implemented on the basis of relying on the above complex classes FFT Forward and inverse transform algorithm and frequency domain filtering algorithm , In addition, if it is a real number FFT Words , To put FFT The resulting complex array is converted to a real array , In the following classes Cmp2Mdl The function of method is this . This FFT Algorithm is base -2FFT Algorithm , therefore , If the input sequence must be 2 Of n The power point is long .

The basic principle of frequency domain filtering is :

1、 The input sequence is FFT

2、 The obtained spectrum is multiplied by a weight function ( filter , Transfer function of system )

3、 The results obtained are carried out IFFT

4、 If it is a real number operation, use Cmp2Mdl Method to real number

The code is as follows :

/// <summary>

/// Frequency analyzer

/// </summary>

public static class FreqAnalyzer

{

/// <summary>

/// Find the complex number complex Module of array modul Array

/// </summary>

/// <param name=”input”> A complex array </param>

/// <returns> Module group </returns>

public static double[] Cmp2Mdl(Complex[] input)

{

/// The length of the input array determines the length of the output array

double[] output = new double[input.Length];

/// Modulo all input complex numbers

for (int i = 0; i < input.Length; i++)

{

if (input[i].Real > 0)

{

output[i] = -input[i].ToModul();

}

else

{

output[i] = input[i].ToModul();

}

}

/// Return modulo array

return output;

}

/// <summary>

/// Fourier transform or inverse transform , Recursive implementation of multi-level butterfly operation

/// As the output of the inverse transformation, it needs to be divided by the length of the sequence

/// ! Be careful : The sequence length of the input class must be 2^n

/// </summary>

/// <param name=”input”> Input real sequence </param>

/// <param name=”invert”>false= Positive transformation ,true= Reverse transformation </param>

/// <returns> Fourier transform or inverse transform sequence </returns>

public static Complex[] FFT(double[] input, bool invert)

{

/// The length of the output sequence is determined by the input sequence

Complex[] output = new Complex[input.Length];

/// Convert the input real number to complex number

for (int i = 0; i < input.Length; i++)

{

output[i] = new Complex(input[i]);

}

/// return FFT or IFFT The sequence after

return output = FFT(output, invert);

}

/// <summary>

/// Fourier transform or inverse transform , Recursive implementation of multi-level butterfly operation

/// As the output of the inverse transformation, it needs to be divided by the length of the sequence

/// ! Be careful : The sequence length of the input class must be 2^n

/// </summary>

/// <param name=”input”> Complex input sequence </param>

/// <param name=”invert”>false= Positive transformation ,true= Reverse transformation </param>

/// <returns> Fourier transform or inverse transform sequence </returns>

private static Complex[] FFT(Complex[] input, bool invert)

{

/// The input sequence has only one element , Output this element and return

if (input.Length == 1)

{

return new Complex[] { input[0] };

}

/// Enter the length of the sequence

int length = input.Length;

/// Enter half the length of the sequence

int half = length / 2;

/// The length of the input sequence determines the length of the output sequence

Complex[] output = new Complex[length];

/// The cardinality of the positive transformation rotation factor

double fac = -2.0 * Math.PI / length;

/// The cardinality of the inverse transformation rotation factor is the opposite number of the positive transformation

if (invert)

{

fac = -fac;

}

/// Points with even subscripts in the sequence

Complex[] evens = new Complex[half];

for (int i = 0; i < half; i++)

{

evens[i] = input[2 * i];

}

/// Find even points FFT or IFFT Result , Recursive implementation of multi-level butterfly operation

Complex[] evenResult = FFT(evens, invert);

/// Points with odd subscripts in the sequence

Complex[] odds = new Complex[half];

for (int i = 0; i < half; i++)

{

odds[i] = input[2 * i + 1];

}

/// Find even points FFT or IFFT Result , Recursive implementation of multi-level butterfly operation

Complex[] oddResult = FFT(odds, invert);

for (int k = 0; k < half; k++)

{

/// Rotation factor

double fack = fac * k;

/// Butterfly operation

Complex oddPart = oddResult[k] * new Complex(Math.Cos(fack), Math.Sin(fack));

output[k] = evenResult[k] + oddPart;

output[k + half] = evenResult[k] – oddPart;

}

/// return FFT or IFFT Result

return output;

}

/// <summary>

/// Frequency domain filter

/// </summary>

/// <param name=”data”> Data to be filtered </param>

/// <param name=”Nc”> Cut off wave number of filter </param>

/// <param name=”Hd”> The weight function of the filter </param>

/// <returns> Filtered data </returns>

private static double[] FreqFilter(double[] data, int Nc, Complex[] Hd)

{

/// Maximum wave number == Data length

int N = data.Length;

/// Enter data for FFT

Complex[] fData = FreqAnalyzer.FFT(data, false);

/// spectrum shaping

for (int i = 0; i < N; i++)

{

fData[i] = Hd[i] * fData[i];

}

/// Data calculation after filtering IFFT

///! Not divided by data length

fData = FreqAnalyzer.FFT(fData, true);

/// Complex number into module

data = FreqAnalyzer.Cmp2Mdl(fData);

/// Divide by the data length

for (int i = 0; i < N; i++)

{

data[i] = -data[i] / N;

}

/// Return filtered data

return data;

}

}

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/110913.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041452570883.html