-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFFT.cs
More file actions
61 lines (59 loc) · 1.75 KB
/
Copy pathFFT.cs
File metadata and controls
61 lines (59 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;
namespace FFT
{
/// <summary>
/// Implementation of fast fourier transform
/// </summary>
public class FFT
{
/// <summary>
/// Converts signal to frequencies sequence
/// </summary>
public static Complex[] CalcForward(int[] input)
{
int N = input.Length;
Complex[] complexResult = new Complex[N];
int n2 = N / 2;
Complex j = new Complex(0, 1);
double pi2 = Math.PI * 2;
for (int k = 0; k < N; k++)
{
Complex jpi2k = -j * pi2 * k;
for (int n = 0; n < N; n++)
{
Complex s = (jpi2k * n) / N;
complexResult[k] += Complex.Exp(s) * input[n];
}
}
return complexResult;
}
/// <summary>
/// Converts frequencies sequence to signal
/// </summary>
public static Complex[] CalcBackward(Complex[] input)
{
int N = input.Length;
Complex[] complexResult = new Complex[N];
int n2 = N / 2;
Complex j = new Complex(0, 1);
double pi2 = Math.PI * 2;
for (int k = 0; k < N; k++)
{
Complex xk = new Complex(0, 0);
Complex jpi2k = j * pi2 * k;
for (int n = 0; n < N; n++)
{
Complex s = (jpi2k * n) / N;
xk += Complex.Exp(s) * input[n];
}
complexResult[k] = xk/N;
}
return complexResult;
}
}
}