Vixen
02b45cc4
csharp
public sealed class RealFft

A transform for signals that are real, which is all of them here.

No guide page documents this yet — the page shows what the code says about itself.

Remarks

Half the work for the identical answer. Audio is real-valued, so a complex transform of N samples is handed N zeroes it multiplies by anyway, and gives back N bins of which the upper half is the conjugate mirror of the lower. This packs the N real samples into an N/2-point complex transform — even samples as the real part, odd as the imaginary — and untangles the result afterwards. Half the butterflies and half the memory.

The output is N/2 + 1 bins and not N. That is not a truncation; it is everything there is. Bin 0 is DC and bin N/2 is Nyquist, both of which are real for a real input, and every bin above N/2 is determined by the one below it. A caller that wants the mirror can conjugate.

DC and Nyquist share a slot, which is the one trap here. Both are real, so the usual packing puts Nyquist in imaginary[0] where DC's imaginary part would be — it is always zero and the space would otherwise be wasted. This class does not do that: it gives N/2 + 1 honest bins, because the packing saves one float and costs everybody who reads the output an explanation. Anything that needs the dense form can still write it.

Taken now because there is finally something to spend it on. It was deferred for a long time, and rightly: it is a pure optimisation for an identical result, and it doubles the index arithmetic — which is exactly where a transform goes quietly wrong, producing a spectrum that is subtly incorrect rather than obviously broken. The tests therefore check it against the complex transform bin for bin rather than against hand-worked expectations.

Fields and properties (2)

  • public int Size

    How many real samples go in.

  • public int Bins

    How many bins come out: Size / 2 + 1, DC through Nyquist.

Methods (4)

  • public RealFft(int size)

    A transform of a size.

  • public void Forward(ReadOnlySpan<float> samples, Span<float> real, Span<float> imaginary)

    Transforms real samples into a spectrum.

  • public void Inverse(ReadOnlySpan<float> real, ReadOnlySpan<float> imaginary, Span<float> samples)

    Turns a spectrum back into real samples.

  • public void Magnitudes(ReadOnlySpan<float> real, ReadOnlySpan<float> imaginary, Span<float> magnitudes)

    The magnitude of every bin, which is what a spectrum is usually wanted for.

Used by (4)

  • OversamplingTestsVixen.Audio.Tests
  • PitchVocoderEffectVixen.Audio
  • PitchVocoderTestsVixen.Audio.Tests
  • RealFftTestsVixen.Audio.Tests