快速傅立叶变换(FFT)的C#代码
来源:互联网 发布:录像机怎么映射端口 编辑:程序博客网 时间:2024/05/18 01:53
这个代码是从《快速傅立叶变换(FFT)的C++实现与Matlab实验》这篇文章里的源代码转换而来,请注意查看原文。
在这里自己转换成了C#代码,并作了一些改动,主要是对N值的确定,原文的N值为常量1024,自己通过对输入的数组的长度来确定N值,N值的确定符合2的n次方,函数返回N值。通过作者提供的测试变量进行测试,能得到相应的结果。代码如下:
FFT代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
/// <summary>
/// 快速傅立叶变换(Fast Fourier Transform)。
/// </summary>
public class TWFFT
{
private TWFFT()
{
}
{
/// <summary>
/// 快速傅立叶变换(Fast Fourier Transform)。
/// </summary>
public class TWFFT
{
private TWFFT()
{
}
private static void bitrp(float[] xreal, float[] ximag, int n)
{
// 位反转置换 Bit-reversal Permutation
int i, j, a, b, p;
{
// 位反转置换 Bit-reversal Permutation
int i, j, a, b, p;
for (i = 1, p = 0; i < n; i *= 2)
{
p++;
}
for (i = 0; i < n; i++)
{
a = i;
b = 0;
for (j = 0; j < p; j++)
{
b = b * 2 + a % 2;
a = a / 2;
}
if (b > i)
{
float t = xreal[i];
xreal[i] = xreal[b];
xreal[b] = t;
{
p++;
}
for (i = 0; i < n; i++)
{
a = i;
b = 0;
for (j = 0; j < p; j++)
{
b = b * 2 + a % 2;
a = a / 2;
}
if (b > i)
{
float t = xreal[i];
xreal[i] = xreal[b];
xreal[b] = t;
t = ximag[i];
ximag[i] = ximag[b];
ximag[b] = t;
}
}
}
ximag[i] = ximag[b];
ximag[b] = t;
}
}
}
public static int FFT(float[] xreal, float[] ximag)
{
//n值为2的N次方
int n = 2;
while (n <= xreal.Length)
{
n *= 2;
}
n /= 2;
{
//n值为2的N次方
int n = 2;
while (n <= xreal.Length)
{
n *= 2;
}
n /= 2;
// 快速傅立叶变换,将复数 x 变换后仍保存在 x 中,xreal, ximag 分别是 x 的实部和虚部
float[] wreal = new float[n / 2];
float[] wimag = new float[n / 2];
float treal, timag, ureal, uimag, arg;
int m, k, j, t, index1, index2;
float[] wreal = new float[n / 2];
float[] wimag = new float[n / 2];
float treal, timag, ureal, uimag, arg;
int m, k, j, t, index1, index2;
bitrp(xreal, ximag, n);
// 计算 1 的前 n / 2 个 n 次方根的共轭复数 W'j = wreal [j] + i * wimag [j] , j = 0, 1, ... , n / 2 - 1
arg = (float)(-2 * Math.PI / n);
treal = (float)Math.Cos(arg);
timag = (float)Math.Sin(arg);
wreal[0] = 1.0f;
wimag[0] = 0.0f;
for (j = 1; j < n / 2; j++)
{
wreal[j] = wreal[j - 1] * treal - wimag[j - 1] * timag;
wimag[j] = wreal[j - 1] * timag + wimag[j - 1] * treal;
}
arg = (float)(-2 * Math.PI / n);
treal = (float)Math.Cos(arg);
timag = (float)Math.Sin(arg);
wreal[0] = 1.0f;
wimag[0] = 0.0f;
for (j = 1; j < n / 2; j++)
{
wreal[j] = wreal[j - 1] * treal - wimag[j - 1] * timag;
wimag[j] = wreal[j - 1] * timag + wimag[j - 1] * treal;
}
for (m = 2; m <= n; m *= 2)
{
for (k = 0; k < n; k += m)
{
for (j = 0; j < m / 2; j++)
{
index1 = k + j;
index2 = index1 + m / 2;
t = n * j / m; // 旋转因子 w 的实部在 wreal [] 中的下标为 t
treal = wreal[t] * xreal[index2] - wimag[t] * ximag[index2];
timag = wreal[t] * ximag[index2] + wimag[t] * xreal[index2];
ureal = xreal[index1];
uimag = ximag[index1];
xreal[index1] = ureal + treal;
ximag[index1] = uimag + timag;
xreal[index2] = ureal - treal;
ximag[index2] = uimag - timag;
}
}
}
{
for (k = 0; k < n; k += m)
{
for (j = 0; j < m / 2; j++)
{
index1 = k + j;
index2 = index1 + m / 2;
t = n * j / m; // 旋转因子 w 的实部在 wreal [] 中的下标为 t
treal = wreal[t] * xreal[index2] - wimag[t] * ximag[index2];
timag = wreal[t] * ximag[index2] + wimag[t] * xreal[index2];
ureal = xreal[index1];
uimag = ximag[index1];
xreal[index1] = ureal + treal;
ximag[index1] = uimag + timag;
xreal[index2] = ureal - treal;
ximag[index2] = uimag - timag;
}
}
}
return n;
}
}
public static int IFFT(float[] xreal, float[] ximag)
{
//n值为2的N次方
int n = 2;
while (n <= xreal.Length)
{
n *= 2;
}
n /= 2;
{
//n值为2的N次方
int n = 2;
while (n <= xreal.Length)
{
n *= 2;
}
n /= 2;
// 快速傅立叶逆变换
float[] wreal = new float[n / 2];
float[] wimag = new float[n / 2];
float treal, timag, ureal, uimag, arg;
int m, k, j, t, index1, index2;
float[] wreal = new float[n / 2];
float[] wimag = new float[n / 2];
float treal, timag, ureal, uimag, arg;
int m, k, j, t, index1, index2;
bitrp(xreal, ximag, n);
// 计算 1 的前 n / 2 个 n 次方根 Wj = wreal [j] + i * wimag [j] , j = 0, 1, ... , n / 2 - 1
arg = (float)(2 * Math.PI / n);
treal = (float)(Math.Cos(arg));
timag = (float)(Math.Sin(arg));
wreal[0] = 1.0f;
wimag[0] = 0.0f;
for (j = 1; j < n / 2; j++)
{
wreal[j] = wreal[j - 1] * treal - wimag[j - 1] * timag;
wimag[j] = wreal[j - 1] * timag + wimag[j - 1] * treal;
}
arg = (float)(2 * Math.PI / n);
treal = (float)(Math.Cos(arg));
timag = (float)(Math.Sin(arg));
wreal[0] = 1.0f;
wimag[0] = 0.0f;
for (j = 1; j < n / 2; j++)
{
wreal[j] = wreal[j - 1] * treal - wimag[j - 1] * timag;
wimag[j] = wreal[j - 1] * timag + wimag[j - 1] * treal;
}
for (m = 2; m <= n; m *= 2)
{
for (k = 0; k < n; k += m)
{
for (j = 0; j < m / 2; j++)
{
index1 = k + j;
index2 = index1 + m / 2;
t = n * j / m; // 旋转因子 w 的实部在 wreal [] 中的下标为 t
treal = wreal[t] * xreal[index2] - wimag[t] * ximag[index2];
timag = wreal[t] * ximag[index2] + wimag[t] * xreal[index2];
ureal = xreal[index1];
uimag = ximag[index1];
xreal[index1] = ureal + treal;
ximag[index1] = uimag + timag;
xreal[index2] = ureal - treal;
ximag[index2] = uimag - timag;
}
}
}
{
for (k = 0; k < n; k += m)
{
for (j = 0; j < m / 2; j++)
{
index1 = k + j;
index2 = index1 + m / 2;
t = n * j / m; // 旋转因子 w 的实部在 wreal [] 中的下标为 t
treal = wreal[t] * xreal[index2] - wimag[t] * ximag[index2];
timag = wreal[t] * ximag[index2] + wimag[t] * xreal[index2];
ureal = xreal[index1];
uimag = ximag[index1];
xreal[index1] = ureal + treal;
ximag[index1] = uimag + timag;
xreal[index2] = ureal - treal;
ximag[index2] = uimag - timag;
}
}
}
for (j = 0; j < n; j++)
{
xreal[j] /= n;
ximag[j] /= n;
}
{
xreal[j] /= n;
ximag[j] /= n;
}
return n;
}
}
}
}
}
}
这里只给出了函数FFT()的,函数IFFT()可以进行相应测试。测试代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
float[] a = {
0.5751f,0.4514f,0.0439f,0.0272f,0.3127f,0.0129f,0.3840f,0.6831f,
0.0928f,0.0353f,0.6124f ,0.6085f,0.0158f,0.0164f,0.1901f,0.5869f};
{
class Program
{
static void Main(string[] args)
{
float[] a = {
0.5751f,0.4514f,0.0439f,0.0272f,0.3127f,0.0129f,0.3840f,0.6831f,
0.0928f,0.0353f,0.6124f ,0.6085f,0.0158f,0.0164f,0.1901f,0.5869f};
float[] b = {
0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,
0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f};
0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,
0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f};
int n = TWFFT.FFT(a,b);
Console.WriteLine("FFT:");
Console.WriteLine("FFT的返回值N = {0}",n);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("i\ta\tb");
Console.WriteLine();
for (int i = 0; i < n; i++)
{
Console.WriteLine("{0}\t{1}\t{2}",i,a[i],b[i]);
}
for (int i = 0; i < n; i++)
{
Console.WriteLine("{0}\t{1}\t{2}",i,a[i],b[i]);
}
Console.ReadLine();
}
}
}
}
}
}
- 快速傅立叶变换(FFT)的C#代码
- 快速傅立叶变换(FFT)
- 快速傅立叶变换(FFT)
- 【FFT-快速傅立叶变换】
- 快速傅立叶变换FFT
- 快速傅立叶变换(FFT)的C++实现
- FFT快速傅立叶变换的工作原理
- FFT快速傅立叶变换的工作原理
- 快速傅立叶变换FFT的实现
- 快速傅立叶变换(FFT)的一种推导
- 快速傅立叶变换算法 FFT
- FFT快速傅立叶变换的工作原理与流程
- 神经网络中快速傅立叶变换(FFT)的梯度传递
- 快速傅立叶变换(FFT)C语言函数
- [UOJ34]多项式乘法(快速傅立叶变换FFT)
- [codevs3123]大整数乘法(快速傅立叶变换FFT)
- [BZOJ4503]两个串(快速傅立叶变换FFT)
- 快速傅立叶变换(FFT)的C++实现与Matlab实验
- UIScrollView的作用原理,实现scrollView中touch事件作用子视图
- java file删除目录下的文件及文件夹
- 华为中兴的美国:蛋糕大却硌牙
- SQL语句中where 1=1的基本用法
- Microsoft Sync Framework同步数据库(一)概述和方案
- 快速傅立叶变换(FFT)的C#代码
- D2RQ对HBase进行Semantic Data Mapping
- HADOOP本地可视化界面的设计及基本实现
- 嫣然一笑苦了情
- Secure CRT 中文乱码
- Android教父高焕堂:开源只是手段,开放才是目的
- 一个int类型到底占多少个字节
- 测试
- 欧盟高举反垄断大旗 Win8添加12款主流浏览器