FftSharp Documentation
repository·main·Indexed 18 days ago
https://github.com/swharden/fftsharpA dependency-free .NET Standard library for Fast Fourier Transform (FFT) tools, designed for signal processing tasks including spectral analysis, filtering, and windowing. It provides routines for Complex and Real Discrete Fourier Transforms, as well as Discrete Cosine and Sine Transforms. The library includes support for window functions (Hanning, Hamming, Blackman, Tukey), frequency filtering (LowPass, HighPass, BandPass, BandStop), and power spectral density calculations.
What's inside FftSharp
- The OOURA FFT package is a general-purpose library for calculating Discrete Fourier, Cosine, and Sine Transforms of 1-dimensional sequences with a length of $2^N$. It is available in both C and Fortran implementations and offers several versions optimized for different hardware architectures and performance requirements.
Explore external FFT/IFFT implementations
mainThis repository provides a curated list of various FFT (Fast Fourier Transform) and IFFT (Inverse Fast Fourier Transform) implementations across different programming languages. Developers can use these as references, study their optimizations, or integrate them into their own projects depending on their licensing and performance requirements.How ddst (Discrete Sine Transform) works
mainThe
ddstroutine implements a discrete sine transform by appending a butterfly operation tordft.In a backward transform, it processes an input array
a[]to create an intermediate arrayr[], callsrdftof length $n$, and then maps the results $A[k]$ to the final sine coefficients $S[k]$ using the following relationship:- $S[2*k] = ext{Re}(A[k] * (1+i))$
- $S[2*k-1] = - ext{Im}(A[k] * (1+i))$
How windowing works and why to use it
mainWindowing involves multiplying a waveform by a bell-shaped curve before performing an FFT. This process improves the frequency resolution and signal-to-noise ratio at lower frequencies by reducing spectral leakage.
In FftSharp, you can use window functions in two ways:
window.ApplyInPlace(signal): Modifies the original array.window.Apply(signal): Returns a new array containing the windowed signal.
Common window functions include
Hanning,Hamming,Blackman, andTukey.double[] signal = FftSharp.SampleData.SampleAudio1(); var window = new FftSharp.Windows.Hanning(); double[] windowed = window.Apply(signal);How dfct and dfst work
mainThese routines are used to split transforms into half-length components:
dfct: Splits a Cosine Transform intodfctandddctof half length. It usesddctrecursively. To maintain in-place operation, data infft*g_h.*versions are sorted in bit-reversal order.dfst: Splits a Sine Transform intodfstandddstof half length. It usesddstrecursively. To maintain in-place operation, data infft*g_h.*versions are sorted in bit-reversal order.
How rdft (Real Discrete Fourier Transform) works
mainThe
rdftroutine implements a real transform by appending a butterfly operation to acdftcall.In a forward transform, it takes an array
a[]and produces an arrayx[]wherex[j] = a[2*j] + i*a[2*j+1]. It then callscdftof length $n/2$. The final results $A[k]$ are derived from thecdftoutput $X[k]$ using complex conjugates and specific weighting factors to account for the real-valued input symmetry.How ddct (Discrete Cosine Transform) works
mainThe
ddctroutine implements a discrete cosine transform by appending a butterfly operation tordft.In a backward transform, it processes an input array
a[]to create an intermediate arrayr[](using real and imaginary parts of the input), callsrdftof length $n$, and then maps the results $A[k]$ to the final cosine coefficients $C[k]$ using the following relationship:- $C[2*k] = ext{Re}(A[k] * (1-i))$
- $C[2*k-1] = - ext{Im}(A[k] * (1-i))$
Choose the appropriate OOURA FFT implementation
mainThe package provides several file variations depending on your performance and memory needs:
fft4g*.*: Optimized for most machines (uses radix 4,2).fft8g*.*: Fast on UltraSPARC architectures (uses radix 8,4,2).fftsg*.*: Optimized for machines with multi-level caches (L1, L2, etc.) using a Split-Radix algorithm.fft*g_h.c(Simple Versions): These versions use no work area (no extra memory allocation), making them suitable for memory-constrained environments, though they may be slower than the fast versions.- Fast Versions (
fft*g.*): These use work areas to achieve higher performance.
Calculate FFT Resolution
mainTo determine the frequency resolution of your FFT, use the following formula based on the number of samples and the sampling period:
Resolution = 1.0 / (sampleLength * samplePeriod)var Resolution = 1.0 / (sampleLength * samplePeriod)Choose the appropriate FFT implementation
mainFftSharp provides several versions of FFT (Fast Fourier/Cosine/Sine Transform) optimized for different hardware and performance requirements. All implementations calculate transforms for 1-dimensional sequences of length $2^N$.
Fast Versions (Use work areas for speed)
fft4g*.*: Optimized for most general-purpose machines (uses radix 4,2).fft8g*.*: Optimized for UltraSPARC architectures (uses radix 8,4,2).fftsg*.*: Optimized for machines with multi-level caches (L1, L2, etc.) using a Split-Radix recursive algorithm.
Simple Versions (No work area required)
fft*g_h.c: These versions use no additional work area but are generally slower than the fast versions. They are available in C for radix 4,2, radix 8,4,2, and Split-Radix variants.
How cdft (Complex Discrete Fourier Transform) works
mainThe implementation method for
cdftdepends on the package version used:fft4g*.*andfft8g*.*: Uses an in-place, radix $2^M$, Sande-Tukey (decimation in frequency) method. The butterfly loop index is in bit-reverse order to maintain continuous memory access.fftsg*.*: Uses an in-place, Split-Radix, recursive fast algorithm.
How the FFT routines work
mainThe implementation details vary by routine and chosen package:
cdft(Complex DFT)fft4g*.*,fft8g*.*: Uses an in-place, radix $2^M$, Sande-Tukey (decimation in frequency) method. The butterfly loop index is in bit-reverse order to maintain continuous memory access.fftsg*.*: Uses an in-place, Split-Radix, recursive fast algorithm.
rdft(Real DFT)Appends a butterfly operation to
cdft. For a forward transform of length $n$, it constructs an array $x[j] = a[2j] + i imes a[2j+1]$ and callscdftof length $n/2$.ddct(Discrete Cosine Transform)Appends a butterfly operation to
rdft. In backward transform, it constructs an array $r[]$ based on the input $a[]$ and callsrdftof length $n$.ddst(Discrete Sine Transform)Appends a butterfly operation to
rdft. In backward transform, it constructs an array $r[]$ based on the input $a[]$ and callsrdftof length $n$.dfctanddfst(Symmetric/Anti-symmetric Transforms)These routines split the transform into
ddct/ddstof half length. To maintain in-place operation, data in the simple versions (fft*g_h.*) are sorted in bit-reversal order.