Fast Fourier Transform Implementation in C#
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Fast Fourier Transform runs in O(n log n) time and O(n) space, in C# as in every other language.
- ✓System.Numerics.Complex, in System.Runtime.Numerics, is a readonly struct with Real, Imaginary, Magnitude, Phase and full operator support - but the BCL has no transform.
- ✓Complex is a value type, so index with a ref local (ref Complex c = ref span[k]) or assign the whole element back; a reviewer flags any foreach that mutates its loop variable, because it mutates a copy.
- ✓The C# trap: Two value-type traps that only bite here. Complex is a 16-byte readonly struct with get-only Real/Imaginary, so samples[k].Real = x is a compile error - you assign the whole element back, samples[k] = new Complex(r, i), or bind ref Complex c = ref samples[k] and write through that.
- ✓A Complex[] is one flat allocation rather than Java's n objects plus a pointer array, so there is nothing for the collector to trace - but the allocation itself still counts: from 85,000 bytes up it lands on the large object heap, which is not compacted, and at 16 bytes per element a Complex[] crosses that around 5,300 samples, so an 8K transform allocated per frame fragments the heap.
What Fast Fourier Transform actually is
Fast Fourier Transform in C#: what you actually reach for
C# implementation
using System;
using System.Numerics;
using MathNet.Numerics.IntegralTransforms;
public static class Spectrum
{
public static double[] Magnitudes(ReadOnlySpan<double> signal, int n)
{
var samples = new Complex[n]; // one flat allocation
for (int i = 0; i < signal.Length && i < n; i++)
samples[i] = new Complex(signal[i], 0.0);
Fourier.Forward(samples, FourierOptions.NoScaling); // in place, no 1/n
var mag = new double[n / 2];
for (int k = 0; k < mag.Length; k++)
{
ref Complex c = ref samples[k]; // ref: no 16-byte copy per read
mag[k] = Math.Sqrt(c.Real * c.Real + c.Imaginary * c.Imaginary);
}
return mag;
}
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe C#-specific trap
Performance in C#, beyond the O(n log n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which C# type or library should I use for Fast Fourier Transform?
System.Numerics.Complex, in System.Runtime.Numerics, is a readonly struct with Real, Imaginary, Magnitude, Phase and full operator support - but the BCL has no transform. Math.NET Numerics is the standard choice: MathNet.Numerics.IntegralTransforms.Fourier.Forward(Complex[], FourierOptions) and Inverse, with ForwardReal for double[] input. FourierOptions selects the convention - Default, Matlab, NoScaling, AsymmetricScaling - and picking it explicitly is what prevents a factor-of-n argument three months later.
What is the most common C# mistake when implementing Fast Fourier Transform?
Two value-type traps that only bite here. Complex is a 16-byte readonly struct with get-only Real/Imaginary, so samples[k].Real = x is a compile error - you assign the whole element back, samples[k] = new Complex(r, i), or bind ref Complex c = ref samples[k] and write through that. A List<Complex> is worse than an array, not equal to it: its indexer returns by value, so there is no writable reference to take at all (any attempted mutation is CS1612) - reach for CollectionsMarshal.AsSpan(list) or keep a Complex[]. And Span<Complex> is a ref struct: it cannot cross an await, live in an iterator, or be captured by a lambda, so wrapping the transform in an async pipeline forces Memory<Complex> at the boundary and a .Span hop inside the synchronous core.
How does Fast Fourier Transform actually perform in C#?
A Complex[] is one flat allocation rather than Java's n objects plus a pointer array, so there is nothing for the collector to trace - but the allocation itself still counts: from 85,000 bytes up it lands on the large object heap, which is not compacted, and at 16 bytes per element a Complex[] crosses that around 5,300 samples, so an 8K transform allocated per frame fragments the heap. ArrayPool fixes it. RyuJIT will not vectorise complex multiplication; separate real and imaginary double[] with Vector<double> is what actually gets SIMD. Time complexity is O(n log n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialStudying for this? Student discounts on developer tools are collected on Noizz, tool by tool.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.