当前位置:网站首页>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
边栏推荐
- Review of Weibo hot search in 2021 and analysis of hot search in the beginning of the year
- Rearrange array
- Sql实现Split
- [Chongqing Guangdong education] National Open University spring 2019 1248 public sector human resource management reference questions
- Research Report on market supply and demand and strategy of China's four sided flat bag industry
- . Net applications consider x64 generation
- 基于check-point实现图数据构建任务
- Redis' optimistic lock and pessimistic lock for solving transaction conflicts
- 嵌入式软件架构设计-函数调用
- 《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下
猜你喜欢
DC-2靶场搭建及渗透实战详细过程(DC靶场系列)
error: ‘connect‘ was not declared in this scope connect(timer, SIGNAL(timeout()), this, SLOT(up
Working group and domain analysis of Intranet
嵌入式软件架构设计-函数调用
一图看懂ThreadLocal
Using celery in projects
Redis' optimistic lock and pessimistic lock for solving transaction conflicts
Communication mode based on stm32f1 single chip microcomputer
Opencv learning -- geometric transformation of image processing
What should ABAP do when it calls a third-party API and encounters garbled code?
随机推荐
Sql实现Split
What should ABAP do when it calls a third-party API and encounters garbled code?
Actual combat | use composite material 3 in application
Will the memory of ParticleSystem be affected by maxparticles
A trap used by combinelatest and a debouncetime based solution
Variable cannot have type 'void'
Market trend report, technical innovation and market forecast of tetrabromophthalate (pht4 diol) in China
Principle and general steps of SQL injection
时钟轮在 RPC 中的应用
The four most common errors when using pytorch
Understand asp Net core - Authentication Based on jwtbearer
Filtered off site request to
Understand asp Net core - Authentication Based on jwtbearer
Research Report on market supply and demand and strategy of tetramethylpyrazine industry in China
Common knowledge of unity Editor Extension
c# 实现定义一套中间SQL可以跨库执行的SQL语句
Unity prefab day04
Cut! 39 year old Ali P9, saved 150million
JS to realize the countdown function
AutoCAD - set color