当前位置:网站首页>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
边栏推荐
- Will the memory of ParticleSystem be affected by maxparticles
- FIREBIRD使用经验总结
- Interface fonctionnelle, référence de méthode, Widget de tri de liste implémenté par lambda
- [book club issue 13] coding format of video files
- Accounting regulations and professional ethics [11]
- AutoCAD - set color
- Variable cannot have type 'void'
- 实战:fabric 用户证书吊销操作流程
- 《吐血整理》保姆级系列教程-玩转Fiddler抓包教程(2)-初识Fiddler让你理性认识一下
- Final consistency of MESI cache in CPU -- why does CPU need cache
猜你喜欢
一图看懂ThreadLocal
[North Asia data recovery] a database data recovery case where the disk on which the database is located is unrecognized due to the RAID disk failure of HP DL380 server
TypeError: list indices must be integers or slices, not str
Filtered off site request to
Common knowledge of unity Editor Extension
AI system content recommendation issue 24
多年锤炼,迈向Kata 3.0 !走进开箱即用的安全容器体验之旅| 龙蜥技术
Using celery in projects
[tutorial] yolov5_ DeepSort_ The whole process of pytoch target tracking and detection
Qt---error: ‘QObject‘ is an ambiguous base of ‘MyView‘
随机推荐
Opencv learning -- arithmetic operation of image of basic operation
Market trend report, technical innovation and market forecast of taillight components in China
[book club issue 13] coding format of video files
Function test - knowledge points and common interview questions
[flask] ORM one to many relationship
The content of the source code crawled by the crawler is inconsistent with that in the developer mode
Review of Weibo hot search in 2021 and analysis of hot search in the beginning of the year
Unity animation day05
Communication mode based on stm32f1 single chip microcomputer
. Net delay queue
ONgDB图数据库与Spark的集成
Rearrange array
Model fusion -- stacking principle and Implementation
Understand asp Net core - Authentication Based on jwtbearer
How to decrypt worksheet protection password in Excel file
Laravel simply realizes Alibaba cloud storage + Baidu AI Cloud image review
After the eruption of Tonga volcano, we analyzed the global volcanic distribution and found that the area with the most volcanoes is here!
std::shared_ ptr initialization: make_ shared&lt; Foo&gt; () vs shared_ ptr&lt; T&gt; (new Foo) [duplicate]
Audio and video technology development weekly | 252
Understand the rate control mode rate control mode CBR, VBR, CRF (x264, x265, VPX)