forked from dzamkov/MD-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFFT.cs
123 lines (112 loc) · 4.32 KB
/
FFT.cs
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Collections.Generic;
namespace MD
{
/// <summary>
/// Contains functions for performing fourier transforms.
/// </summary>
public static class FFT
{
private static void _FFT<TSignal>(TSignal Input, bool Inverse, Complex[] Output, int InputOffset, int OutputOffset, int Samples, int Step)
where TSignal : IComplexSignal
{
if (Samples == 1)
{
Output[OutputOffset] = Input.Get(InputOffset);
}
else
{
int hsamps = (Samples / 2);
int dstep = Step * 2;
_FFT<TSignal>(Input, Inverse, Output, InputOffset, OutputOffset, hsamps, dstep);
_FFT<TSignal>(Input, Inverse, Output, InputOffset + Step, OutputOffset + hsamps, hsamps, dstep);
double c = (Inverse ? 2.0 : -2.0) * Math.PI;
for (int i = 0; i < hsamps; i++)
{
Complex t = Output[OutputOffset + i];
Complex e = new Complex(c * (double)i / (double)Samples).TimesI.Exp;
Complex es = e * Output[OutputOffset + hsamps + i];
Output[OutputOffset + i] = t + es;
Output[OutputOffset + hsamps + i] = t - es;
}
}
}
/// <summary>
/// Performs a fourier transform on the input data. Samples must be a power of two.
/// </summary>
public static void OnArray(bool Inverse, Complex[] Input, Complex[] Output, int InputOffset, int OutputOffset, int Samples)
{
OnSignal<ArrayComplexSignal>(new ArrayComplexSignal(Input), Inverse, Output, InputOffset, OutputOffset, Samples);
}
/// <summary>
/// Performs a fourier transform on the input data. Samples must be a power of two.
/// </summary>
public static void OnArray(bool Inverse, Complex[] Input, Complex[] Output, int Samples)
{
OnArray(Inverse, Input, Output, 0, 0, Samples);
}
/// <summary>
/// Performs a fourier transform on the input signal. Samples must be a power of two.
/// </summary>
public static void OnSignal<TSignal>(TSignal Signal, bool Inverse, Complex[] Output, int InputOffset, int OutputOffset, int Samples)
where TSignal : IComplexSignal
{
_FFT<TSignal>(Signal, Inverse, Output, InputOffset, OutputOffset, Samples, 1);
if (Inverse)
{
double d = 1.0 / (double)Samples;
for (int t = 0; t < Samples; t++)
{
Output[OutputOffset + t] *= d;
}
}
}
/// <summary>
/// Performs a fourier transform on the input signal. Samples must be a power of two.
/// </summary>
public static void OnSignal<TSignal>(TSignal Signal, bool Inverse, Complex[] Output, int Samples)
where TSignal : IComplexSignal
{
OnSignal<TSignal>(Signal, Inverse, Output, 0, 0, Samples);
}
/// <summary>
/// Gets the absolute frequency a sample in the frequency domain data represents.
/// </summary>
public static double GetFrequency(int Sample, int NumSamples, double SampleFrequency)
{
return (double)Sample * SampleFrequency / (double)NumSamples;
}
/// <summary>
/// Gets the sample in the frequency domain that has the specified frequency.
/// </summary>
public static double GetSample(double Frequency, int NumSamples, double SampleFrequency)
{
return (double)NumSamples * Frequency / SampleFrequency;
}
}
/// <summary>
/// A signal of complex numbers.
/// </summary>
public interface IComplexSignal
{
/// <summary>
/// Gets the sample at the specified index.
/// </summary>
Complex Get(int Index);
}
/// <summary>
/// A complex signal from an array.
/// </summary>
public struct ArrayComplexSignal : IComplexSignal
{
public ArrayComplexSignal(Complex[] Source)
{
this._Source = Source;
}
public Complex Get(int Index)
{
return this._Source[Index];
}
private Complex[] _Source;
}
}