Class FFT

Computes the FFT magnitude spectrum for each channel using FftFlat (MIT, Ooura).

JSON configuration

Use these block entries inside a pipeline configuration. Input names must match other blocks in that pipeline; device addresses and file paths must match your setup.

JSON configuration:

{
  "Spectrum": {
    "Type": "FFT",
    "Inputs": [ "SlidingWindow1" ],
    "Params": [ "Rectangular", "Magnitude", 1.0, -120, 200, 2000 ],
    "Path": "C:/Data/session1"
  }
}

Params:

IndexDescription
0WindowType (string, default "Rectangular") — Rectangular, Hann, Hamming, or Blackman.
1OutputMode (string, default "Magnitude") — Magnitude, MagnitudeNormalized, Power, or DB.
2DbRef (double, default 1.0) — dB reference level.
3DbFloor (double, default −120.0) — minimum dB value.
4SpectrogramDepth (int, default 200) — number of time slices in the spectrogram.
5SampleRate (double, default 0) — fixed override for the frequency axis, used only when the pipeline supplies no SignalRate. 0 = follow the live pipeline SignalRate (recommended).

How Type, Inputs and Params work · Block catalogue and parameter reference

Inherited Members
Namespace: MOSAIC.Models.SignalProcessing
Assembly: MOSAIC.dll
Syntax
public class FFT : BaseBlock, INotifyPropertyChanged, INotifyPropertyChanging, ISubscriber, IPublisher, IBaseBlockProps, IJsonExportable, IDisposable, IAsyncDisposable
Remarks

Input: A MathNet.Numerics.LinearAlgebra.Matrix<T> (rows = samples, columns = channels) from an upstream SlidingWindow block. Also accepts a single MathNet.Numerics.LinearAlgebra.Vector<T> which is treated as a single-channel column matrix.

Output: A MathNet.Numerics.LinearAlgebra.Matrix<T> with N/2+1 rows (frequency bins) and one column per channel, scaled according to Mode.

Output modes:

  • Magnitude — raw magnitude.
  • MagnitudeNormalized — single-sided amplitude: magnitude × 2/N (interior bins) and magnitude × 1/N at the unpaired DC and Nyquist bins.
  • Power — magnitude².
  • DB — 20·log₁₀(magnitude / DbRef), floored at DbFloor.

Window functions: Rectangular (default), Hann, Hamming, Blackman. The upstream SlidingWindow should typically apply its own window; this block defaults to Rectangular to avoid double-windowing.

Spectrogram: A SpectrogramMonitor is created in the constructor for real-time visualisation. SelectedChannel: −1 = average all channels, 0..N−1 = single channel. The spectrogram always displays in dB regardless of Mode.

Sample rate: The frequency axis follows the live SignalRate (the true sample rate, which survives windowing and tracks runtime pipeline-rate changes). Params[5] is an optional fixed override, used only when the pipeline does not propagate a SignalRate; the block never uses the window fire rate (sampleRate / stride).

CSV logging: When a Path is specified, Dumper automatically logs the published spectrum matrix via Publish(object).

Constructors

FFT(string, double, WindowType, OutputMode, double, double, int, double)

Initializes a new FFT block.

Declaration
public FFT(string name = "FFT", double desiredRate = 0, FFT.WindowType windowType = WindowType.Rectangular, FFT.OutputMode outputMode = OutputMode.Magnitude, double dbRef = 1, double dbFloor = -120, int spectrogramDepth = 200, double sampleRate = 0)
Parameters
Type Name Description
string name

Display name.

double desiredRate

Processing rate (typically 0 — inherited from pipeline).

FFT.WindowType windowType

Window function to apply before the FFT.

FFT.OutputMode outputMode

Spectrum scaling mode.

double dbRef

dB reference level (positive).

double dbFloor

Minimum dB value.

int spectrogramDepth

Number of time slices retained in the spectrogram.

double sampleRate

Original signal sample rate (0 = infer from pipeline).

Fields

_channelCount

Number of input channels.

Declaration
[ObservableProperty]
private int _channelCount
Field Value
Type Description
int

_channels

Declaration
private int _channels
Field Value
Type Description
int

_currentFftSize

Current FFT size in samples.

Declaration
[ObservableProperty]
private int _currentFftSize
Field Value
Type Description
int

_dbFloor

Declaration
private double _dbFloor
Field Value
Type Description
double

_dbRef

Declaration
private double _dbRef
Field Value
Type Description
double

_fftBuffers

Declaration
private Complex[][]? _fftBuffers
Field Value
Type Description
Complex[][]

_fftEngines

Declaration
private FastFourierTransform[]? _fftEngines
Field Value
Type Description
FastFourierTransform[]

_fftSize

Declaration
private int _fftSize
Field Value
Type Description
int

_frameCount

Declaration
private long _frameCount
Field Value
Type Description
long

_frequencyResolution

Frequency resolution in Hz (sample rate / FFT size).

Declaration
[ObservableProperty]
private double _frequencyResolution
Field Value
Type Description
double

_initialized

Declaration
private bool _initialized
Field Value
Type Description
bool

_invalidConfigLogged

Declaration
private bool _invalidConfigLogged
Field Value
Type Description
bool

_outputBinCount

Number of output frequency bins (N/2 + 1).

Declaration
[ObservableProperty]
private int _outputBinCount
Field Value
Type Description
int

_outputBins

Declaration
private int _outputBins
Field Value
Type Description
int

_outputMode

Declaration
private volatile FFT.OutputMode _outputMode
Field Value
Type Description
FFT.OutputMode

_sampleRate

Original signal sample rate in Hz. Overrides the inferred rate from the pipeline because the FFT block receives data at the window fire rate, not the original sample rate.

Declaration
private double _sampleRate
Field Value
Type Description
double

_selectedChannel

Which channel to show in the spectrogram. −1 = average all channels, 0..N−1 = single channel.

Declaration
[ObservableProperty]
private int _selectedChannel
Field Value
Type Description
int

_spectrumScratch

Declaration
private double[]? _spectrumScratch
Field Value
Type Description
double[]

_windowCoeffs

Declaration
private double[]? _windowCoeffs
Field Value
Type Description
double[]

_windowType

Declaration
private volatile FFT.WindowType _windowType
Field Value
Type Description
FFT.WindowType

Properties

ChannelCount

Number of input channels.

Declaration
[ExcludeFromCodeCoverage]
public int ChannelCount { get; set; }
Property Value
Type Description
int

CurrentFftSize

Current FFT size in samples.

Declaration
[ExcludeFromCodeCoverage]
public int CurrentFftSize { get; set; }
Property Value
Type Description
int

DbFloor

dB floor (minimum dB value displayed/returned). Read/written across threads.

Declaration
public double DbFloor { get; set; }
Property Value
Type Description
double

DbRef

dB reference level (must be positive). Read/written across threads, so use a volatile access.

Declaration
public double DbRef { get; set; }
Property Value
Type Description
double

DumpFilePrefix

Recordings from this block are named <block>_fft.csv.

Declaration
protected override string DumpFilePrefix { get; }
Property Value
Type Description
string
Overrides
BaseBlock.DumpFilePrefix

FftDescription

Human-readable description string for UI display.

Declaration
public string FftDescription { get; }
Property Value
Type Description
string

FrequencyAxis

Frequency values for each output bin (Hz). null until initialised.

Declaration
public double[]? FrequencyAxis { get; private set; }
Property Value
Type Description
double[]

FrequencyResolution

Frequency resolution in Hz (sample rate / FFT size).

Declaration
[ExcludeFromCodeCoverage]
public double FrequencyResolution { get; set; }
Property Value
Type Description
double

JsonTypeName

Block type identifier for JSON serialization. Override for a custom name.

Declaration
protected override string JsonTypeName { get; }
Property Value
Type Description
string
Overrides
BaseBlock.JsonTypeName

Mode

Output scaling mode. Can be changed at runtime.

Declaration
public FFT.OutputMode Mode { get; set; }
Property Value
Type Description
FFT.OutputMode

OutputBinCount

Number of output frequency bins (N/2 + 1).

Declaration
[ExcludeFromCodeCoverage]
public int OutputBinCount { get; set; }
Property Value
Type Description
int

SelectedChannel

Which channel to show in the spectrogram. −1 = average all channels, 0..N−1 = single channel.

Declaration
[ExcludeFromCodeCoverage]
public int SelectedChannel { get; set; }
Property Value
Type Description
int

Spectrogram

Spectrogram monitor for real-time frequency-domain visualisation. Created in the constructor, always non-null.

Declaration
public SpectrogramMonitor Spectrogram { get; }
Property Value
Type Description
SpectrogramMonitor

Window

Window function. Can be changed at runtime — recomputes coefficients.

Declaration
public FFT.WindowType Window { get; set; }
Property Value
Type Description
FFT.WindowType

Methods

ComputeWindow(WindowType, int)

Computes window function coefficients. A size ≤ 1 window degenerates to pass-through (all 1.0), which also avoids the divide-by-(size−1) that would otherwise produce NaN.

Declaration
private static double[] ComputeWindow(FFT.WindowType type, int size)
Parameters
Type Name Description
FFT.WindowType type

Window type.

int size

Window size in samples.

Returns
Type Description
double[]

Array of window coefficients.

ConfigureInput(IServiceProvider, JsonModel)

Creates an FFT block from a JSON pipeline definition.

Declaration
public static FFT ConfigureInput(IServiceProvider sp, JsonModel m)
Parameters
Type Name Description
IServiceProvider sp

Service provider for dependency injection.

JsonModel m

JSON model. See the class-level example for the Params layout.

Returns
Type Description
FFT

A configured FFT instance.

Dispose()

Disposes the spectrogram and base class resources (including CSV dumper).

Declaration
public override void Dispose()
Overrides
BaseBlock.Dispose()

EnsureInit(int, int)

Initialises FFT engines, buffers, and frequency axis for the given dimensions. Returns false (without throwing) for an invalid configuration — a non-power-of-2 size or non-positive dimensions — so a misconfigured pipeline degrades to "no output" instead of throwing once per frame.

Declaration
private bool EnsureInit(int fftSize, int channels)
Parameters
Type Name Description
int fftSize

Number of samples (must be a power of 2).

int channels

Number of input channels.

Returns
Type Description
bool

true if the block is ready to process; otherwise false.

ExtractSpectrum(Complex[], Matrix<double>, int, OutputMode, double, double)

Extracts the single-sided spectrum from one channel's FFT result into the output matrix, scaled per mode. The DC and Nyquist bins are unpaired and so are not doubled — this single-sided correction applies only to MagnitudeNormalized.

Declaration
private void ExtractSpectrum(Complex[] fftResult, Matrix<double> output, int channel, FFT.OutputMode mode, double dbRef, double dbFloor)
Parameters
Type Name Description
Complex[] fftResult
Matrix<double> output
int channel
FFT.OutputMode mode
double dbRef
double dbFloor

GetJsonParams()

Block-specific parameters for JSON. Override to provide custom parameters.

Declaration
protected override IReadOnlyList<object>? GetJsonParams()
Returns
Type Description
IReadOnlyList<object>

The values for Params, in the order this block's ConfigureInput reads them, or null when the block takes no parameters.

Overrides
BaseBlock.GetJsonParams()

MagnitudeToDb(double, double, double)

Converts magnitude to dB with floor clamping.

Declaration
private static double MagnitudeToDb(double magnitude, double dbRef, double dbFloor)
Parameters
Type Name Description
double magnitude
double dbRef
double dbFloor
Returns
Type Description
double

OnChannelCountChanged(int)

Executes the logic for when ChannelCount just changed.

Declaration
private void OnChannelCountChanged(int value)
Parameters
Type Name Description
int value

The new property value that was set.

Remarks

This method is invoked right after the value of ChannelCount is changed.

OnChannelCountChanged(int, int)

Executes the logic for when ChannelCount just changed.

Declaration
private void OnChannelCountChanged(int oldValue, int newValue)
Parameters
Type Name Description
int oldValue

The previous property value that was replaced.

int newValue

The new property value that was set.

Remarks

This method is invoked right after the value of ChannelCount is changed.

OnChannelCountChanging(int)

Executes the logic for when ChannelCount is changing.

Declaration
private void OnChannelCountChanging(int value)
Parameters
Type Name Description
int value

The new property value being set.

Remarks

This method is invoked right before the value of ChannelCount is changed.

OnChannelCountChanging(int, int)

Executes the logic for when ChannelCount is changing.

Declaration
private void OnChannelCountChanging(int oldValue, int newValue)
Parameters
Type Name Description
int oldValue

The previous property value that is being replaced.

int newValue

The new property value being set.

Remarks

This method is invoked right before the value of ChannelCount is changed.

OnCurrentFftSizeChanged(int)

Executes the logic for when CurrentFftSize just changed.

Declaration
private void OnCurrentFftSizeChanged(int value)
Parameters
Type Name Description
int value

The new property value that was set.

Remarks

This method is invoked right after the value of CurrentFftSize is changed.

OnCurrentFftSizeChanged(int, int)

Executes the logic for when CurrentFftSize just changed.

Declaration
private void OnCurrentFftSizeChanged(int oldValue, int newValue)
Parameters
Type Name Description
int oldValue

The previous property value that was replaced.

int newValue

The new property value that was set.

Remarks

This method is invoked right after the value of CurrentFftSize is changed.

OnCurrentFftSizeChanging(int)

Executes the logic for when CurrentFftSize is changing.

Declaration
private void OnCurrentFftSizeChanging(int value)
Parameters
Type Name Description
int value

The new property value being set.

Remarks

This method is invoked right before the value of CurrentFftSize is changed.

OnCurrentFftSizeChanging(int, int)

Executes the logic for when CurrentFftSize is changing.

Declaration
private void OnCurrentFftSizeChanging(int oldValue, int newValue)
Parameters
Type Name Description
int oldValue

The previous property value that is being replaced.

int newValue

The new property value being set.

Remarks

This method is invoked right before the value of CurrentFftSize is changed.

OnDesiredRateChanged(double, double)

Recalculates the frequency axis when the block (tick) rate changes.

Declaration
protected override void OnDesiredRateChanged(double oldRate, double newRate)
Parameters
Type Name Description
double oldRate
double newRate
Overrides
BaseBlock.OnDesiredRateChanged(double, double)

OnFrequencyResolutionChanged(double)

Executes the logic for when FrequencyResolution just changed.

Declaration
private void OnFrequencyResolutionChanged(double value)
Parameters
Type Name Description
double value

The new property value that was set.

Remarks

This method is invoked right after the value of FrequencyResolution is changed.

OnFrequencyResolutionChanged(double, double)

Executes the logic for when FrequencyResolution just changed.

Declaration
private void OnFrequencyResolutionChanged(double oldValue, double newValue)
Parameters
Type Name Description
double oldValue

The previous property value that was replaced.

double newValue

The new property value that was set.

Remarks

This method is invoked right after the value of FrequencyResolution is changed.

OnFrequencyResolutionChanging(double)

Executes the logic for when FrequencyResolution is changing.

Declaration
private void OnFrequencyResolutionChanging(double value)
Parameters
Type Name Description
double value

The new property value being set.

Remarks

This method is invoked right before the value of FrequencyResolution is changed.

OnFrequencyResolutionChanging(double, double)

Executes the logic for when FrequencyResolution is changing.

Declaration
private void OnFrequencyResolutionChanging(double oldValue, double newValue)
Parameters
Type Name Description
double oldValue

The previous property value that is being replaced.

double newValue

The new property value being set.

Remarks

This method is invoked right before the value of FrequencyResolution is changed.

OnOutputBinCountChanged(int)

Executes the logic for when OutputBinCount just changed.

Declaration
private void OnOutputBinCountChanged(int value)
Parameters
Type Name Description
int value

The new property value that was set.

Remarks

This method is invoked right after the value of OutputBinCount is changed.

OnOutputBinCountChanged(int, int)

Executes the logic for when OutputBinCount just changed.

Declaration
private void OnOutputBinCountChanged(int oldValue, int newValue)
Parameters
Type Name Description
int oldValue

The previous property value that was replaced.

int newValue

The new property value that was set.

Remarks

This method is invoked right after the value of OutputBinCount is changed.

OnOutputBinCountChanging(int)

Executes the logic for when OutputBinCount is changing.

Declaration
private void OnOutputBinCountChanging(int value)
Parameters
Type Name Description
int value

The new property value being set.

Remarks

This method is invoked right before the value of OutputBinCount is changed.

OnOutputBinCountChanging(int, int)

Executes the logic for when OutputBinCount is changing.

Declaration
private void OnOutputBinCountChanging(int oldValue, int newValue)
Parameters
Type Name Description
int oldValue

The previous property value that is being replaced.

int newValue

The new property value being set.

Remarks

This method is invoked right before the value of OutputBinCount is changed.

OnReceive(object, object)

Processes incoming data: accepts matrices (from SlidingWindow) or vectors (single-channel).

Declaration
protected override void OnReceive(object sender, object data)
Parameters
Type Name Description
object sender

The upstream block.

object data

A MathNet.Numerics.LinearAlgebra.Matrix<T> or MathNet.Numerics.LinearAlgebra.Vector<T>.

Overrides
BaseBlock.OnReceive(object, object)

OnSelectedChannelChanged(int)

Executes the logic for when SelectedChannel just changed.

Declaration
private void OnSelectedChannelChanged(int value)
Parameters
Type Name Description
int value

The new property value that was set.

Remarks

This method is invoked right after the value of SelectedChannel is changed.

OnSelectedChannelChanged(int, int)

Executes the logic for when SelectedChannel just changed.

Declaration
private void OnSelectedChannelChanged(int oldValue, int newValue)
Parameters
Type Name Description
int oldValue

The previous property value that was replaced.

int newValue

The new property value that was set.

Remarks

This method is invoked right after the value of SelectedChannel is changed.

OnSelectedChannelChanging(int)

Executes the logic for when SelectedChannel is changing.

Declaration
private void OnSelectedChannelChanging(int value)
Parameters
Type Name Description
int value

The new property value being set.

Remarks

This method is invoked right before the value of SelectedChannel is changed.

OnSelectedChannelChanging(int, int)

Executes the logic for when SelectedChannel is changing.

Declaration
private void OnSelectedChannelChanging(int oldValue, int newValue)
Parameters
Type Name Description
int oldValue

The previous property value that is being replaced.

int newValue

The new property value being set.

Remarks

This method is invoked right before the value of SelectedChannel is changed.

OnSignalRateChanged(double)

Recalculates the frequency axis when the true signal sample rate changes.

Declaration
protected override void OnSignalRateChanged(double newRate)
Parameters
Type Name Description
double newRate
Overrides
BaseBlock.OnSignalRateChanged(double)

ProcessWindow(Matrix<double>)

Computes the FFT for each channel, updates the spectrogram, and publishes the result.

Declaration
private void ProcessWindow(Matrix<double> window)
Parameters
Type Name Description
Matrix<double> window

Input matrix (rows = samples, columns = channels).

PushToSpectrogram(Matrix<double>, int, OutputMode, double, double)

Pushes the current spectrum frame to the spectrogram (always in dB for display).

Declaration
private void PushToSpectrogram(Matrix<double> output, int cols, FFT.OutputMode mode, double dbRef, double dbFloor)
Parameters
Type Name Description
Matrix<double> output
int cols
FFT.OutputMode mode
double dbRef
double dbFloor

ToDbForDisplay(double, OutputMode, bool, double, double)

Converts a mode-scaled output value back to dB for spectrogram display. edge marks the DC/Nyquist bins, whose normalized scaling differs.

Declaration
private double ToDbForDisplay(double value, FFT.OutputMode mode, bool edge, double dbRef, double dbFloor)
Parameters
Type Name Description
double value
FFT.OutputMode mode
bool edge
double dbRef
double dbFloor
Returns
Type Description
double

UpdateFrequencyAxis()

Rebuilds FrequencyAxis and updates spectrogram frequency bounds. Priority: the live SignalRate (the true sample rate, which survives windowing and tracks runtime pipeline-rate changes) → the explicit _sampleRate override (Params[5], used only when the pipeline supplies no SignalRate) → DesiredRate. InputRate is deliberately not used: for a windowed input it is the window fire rate (sampleRate / stride), which would scale the axis down by the stride.

Declaration
private void UpdateFrequencyAxis()

Implements

INotifyPropertyChanged
INotifyPropertyChanging
ISubscriber
IPublisher
IBaseBlockProps
IJsonExportable
IDisposable
IAsyncDisposable

Search documentation

Start typing to search the documentation.