1+ using System ;
2+
3+ namespace FftSharp ;
4+
5+ // TODO: support spans
6+
7+ /// <summary>
8+ /// Fast Fourier Transform (FFT) operations using System.Numerics.Complex data types.
9+ /// </summary>
10+ public static class FFT
11+ {
12+ /// <summary>
13+ /// Apply the Fast Fourier Transform (FFT) to the Complex array in place.
14+ /// Length of the array must be a power of 2.
15+ /// </summary>
16+ public static void Forward ( System . Numerics . Complex [ ] samples )
17+ {
18+ if ( ! Transform . IsPowerOfTwo ( samples . Length ) )
19+ throw new ArgumentException ( $ "{ nameof ( samples ) } length must be a power of 2") ;
20+
21+ FftOperations . FFT_WithoutChecks ( samples ) ;
22+ }
23+
24+ /// <summary>
25+ /// Create a Complex array from the given data, apply the FFT, and return the result.
26+ /// Length of the array must be a power of 2.
27+ /// </summary>
28+ public static System . Numerics . Complex [ ] Forward ( double [ ] real )
29+ {
30+ if ( ! Transform . IsPowerOfTwo ( real . Length ) )
31+ throw new ArgumentException ( $ "{ nameof ( real ) } length must be a power of 2") ;
32+
33+ System . Numerics . Complex [ ] samples = real . ToComplexArray ( ) ;
34+ FftOperations . FFT_WithoutChecks ( samples ) ;
35+ return samples ;
36+ }
37+
38+ /// <summary>
39+ /// Apply the inverse Fast Fourier Transform (iFFT) to the Complex array in place.
40+ /// Length of the array must be a power of 2.
41+ /// </summary>
42+ public static void Inverse ( System . Numerics . Complex [ ] samples )
43+ {
44+ if ( ! Transform . IsPowerOfTwo ( samples . Length ) )
45+ throw new ArgumentException ( $ "{ nameof ( samples ) } length must be a power of 2") ;
46+
47+ FftOperations . IFFT_WithoutChecks ( samples ) ;
48+ }
49+
50+ /// <summary>
51+ /// Return frequencies for each index of a FFT.
52+ /// </summary>
53+ public static double [ ] FrequencyScale ( int length , double sampleRate , bool positiveOnly = true )
54+ {
55+ return Transform . FFTfreq ( sampleRate : sampleRate , pointCount : length , oneSided : positiveOnly ) ;
56+ }
57+ }
0 commit comments