Trends and signals

Spectrum <smart-spectrum>

Spectrum is a spectrum analyzer display.

<smart-spectrum> · 28 properties, 5 methods, 1 events · source/modules/smart.spectrum.js

Usage in Blazor

The Razor component is <Spectrum> in Smart.Blazor.Industrial: every property is a parameter in PascalCase, every event an EventCallback<Event> named On<Event> whose ev["Detail"] is the typed detail class, and the methods are called on the @ref. String choices are enums. In a Blazor Web App (.NET 8 and later) the page needs an interactive render mode, for example @rendermode InteractiveServer; a statically rendered component stays empty.

@using Smart.Blazor.Industrial
@* .NET 8 and later: an interactive render mode, on the page or for the whole app *@
@rendermode InteractiveServer

<Spectrum @ref="spectrum" SampleRate="42"
    OnSpectrumChange="OnSpectrumChange" />

@code {
    Spectrum spectrum;

    void OnSpectrumChange(Event ev)
    {
        SpectrumSpectrumChangeEventDetail detail = ev["Detail"];
    }

    // Task<bool> Push(IEnumerable<object> samples)
    void Call() => spectrum.Push(new object[] { });
}

Properties

ParameterType, defaultDescription
Signal
signal
objectSets or retrieves the most recent block of samples, as an array or a typed array. push sets it; an application with a complete record can assign it directly.
SampleRate
sampleRate
double
1
Sets or retrieves the sample rate in samples per second. Every frequency on the display follows from it; the default of 1 reads frequencies in cycles per sample. A value that is not a positive number (0, negative, NaN) analyses nothing rather than computing a spectrum on a made-up rate: the header and the plot say "Sample rate 0 is not valid", the accessible name says the same, and one console warning names the value.
Size
size
int
0
Sets or retrieves the FFT size. 0 uses the next power of two above the block length. A size larger than the block zero-pads the transform, which interpolates the display but does not add resolution; the bin width is still the sample rate divided by the length of the real data. Held to 0 to 16,777,216; not a number uses 0. Either is said once in a console warning.
Window
window
SpectrumWindow
hann
Sets or retrieves the window function applied before the transform. A flat-top window reads amplitude to within 0.01 dB but spreads the peak in frequency; a rectangular window does the opposite. The amplitude is corrected for the coherent gain of the window in every case.
Scaling
scaling
SpectrumScaling
amplitude
Sets or retrieves what a bin's value means: amplitude reads a sine's peak, power its square, density its power per hertz. They are not interchangeable, and the header says which one is showing.
LevelScale
levelScale
SpectrumLevelScale
db
Sets or retrieves whether levels are shown in decibels against reference or as linear magnitudes.
Reference
reference
double
1
Sets or retrieves the level that corresponds to 0 dB. 1 gives dBV levels for a signal in volts; the full scale of a converter gives dBFS.
FrequencyScale
frequencyScale
SpectrumFrequencyScale
linear
Sets or retrieves the frequency axis. A log axis starts at the first bin above DC, since it has no zero, and carries decade ticks with 2 and 5 minors.
MinFrequency
minFrequency
doubleSets or retrieves the lowest frequency shown. null follows the data: 0, or the first bin on a log axis. Set above maxFrequency, the two are swapped; a span that leaves nothing to show (equal limits, or a limit beyond the other end of the data or below the first bin on a log axis) shows the whole spectrum instead. Each is said once in a console warning.
MaxFrequency
maxFrequency
doubleSets or retrieves the highest frequency shown. null follows the data to the Nyquist frequency. See minFrequency for limits set the wrong way round.
MinLevel
minLevel
doubleSets or retrieves the bottom of the level axis. null follows the data with some headroom; a fixed value keeps the trace from rescaling as the signal changes, as on an analyzer. Set above maxLevel, the two are swapped; equal limits, or one limit that leaves no room against the data, let the axis follow the data. Each is said once in a console warning.
MaxLevel
maxLevel
doubleSets or retrieves the top of the level axis. null follows the data with headroom. See minLevel for limits set the wrong way round.
Averaging
averaging
SpectrumAveraging
none
Sets or retrieves the spectrum averaging mode. none draws each block as it arrives. linear averages the first averages blocks and then holds the result until clear restarts the average, like a bench analyzer. exponential weights each new block by 1/averages and continues indefinitely. Averaging is performed in the power domain regardless of the display scaling, because averaging amplitudes or decibels biases the noise floor.
Averages
averages
int
8
Sets or retrieves how many blocks the average spans. Held to 1 to 10,000; not a number uses 8. Either is said once in a console warning.
PeakHold
peakHold
bool
false
Determines whether the highest level ever seen in each bin is kept as a second trace, until clear.
ShowPeaks
showPeaks
int
3
Sets or retrieves how many peaks are marked on the trace and listed in the readout, strongest first. 0 marks none. Peaks are found on the displayed trace, after averaging, by prominence, not height. Held to 0 to 100.
MinProminence
minProminence
double
6
Sets or retrieves how far a peak must stand above its surroundings to be marked, in the level scale's own unit, decibels, or linear magnitude. It keeps one strong tone's sidelobes from being reported as three peaks. Not below 0; not a number uses 6.
ShowThd
showThd
bool
false
Determines whether total harmonic distortion is measured against the strongest peak and shown in the readout, as a percentage and in dB. Requires amplitude scaling.
Harmonics
harmonics
int
5
Sets or retrieves how many harmonics the THD measurement includes. Held to 1 to 100.
Markers
markers
IEnumerable<object>Sets or retrieves markers placed by the application as [{ frequency, label }], drawn as labelled vertical lines, for example at the line frequency, a shaft speed, a bearing defect frequency or a filter corner, so the peaks can be compared with the expected frequencies.
ShowGrid
showGrid
bool
true
Determines whether the grid and axis labels are drawn.
ShowReadout
showReadout
bool
true
Determines whether the peak list and the THD value are shown as text under the plot. The readout is what a screen reader and a report receive; the markers on the canvas show the same information graphically.
ShowHeader
showHeader
bool
true
Determines whether the header, sample rate, transform size, bin width, window, scaling and averaging state, is shown.
Paused
paused
bool
false
Determines whether pushed blocks are dropped. The display holds its last spectrum.
Label
label
stringSets or retrieves the name of the channel, shown in the header and in the accessible name.
Unit
unit
stringSets or retrieves the unit of the signal, for example V, g or Pa, so that a level is shown as dBV rather than dB and a linear level carries its unit.
PrecisionDigits
precisionDigits
int
1
Sets or retrieves how many decimal places a level is printed with in the readout and peak labels. Held to 0 to 20.
LineWidth
lineWidth
double
1.2
Sets or retrieves the width of the trace in pixels.

Methods

MethodDescription
Task<bool> Push(IEnumerable<object> samples)Analyses the next block of samples and redraws. The block is copied, so a DAQ can pass the same buffer each time. Averaging and max hold accumulate across pushes. Samples that are not numbers (NaN, null, an infinite value) are analysed as zeros, which lowers the levels by an amount that depends on how many there were, so the block is never passed off as clean: the header shows "2 of 4096 samples were not numbers (analysed as 0)", the accessible name and spectrumChange carry the count, and the console says so once per run of such blocks. A block with no number in it is not analysed. Returns false when the analyzer is paused and the block was dropped.
samples array The samples, as an array or a typed array.
void Clear()Clears the running average and the held maximum and restarts them from the block on screen.
spectrum(): objectReturns the spectrum as currently shown, as { frequencies, magnitudes, levels, hold, binWidth, size, window, invalidSamples }, where levels are in the level scale, magnitudes are the linear values after averaging, hold is the max-hold trace or null, and invalidSamples is how many samples of the block were not numbers. Returns null before the first signal, and while the sample rate is not a positive number.
Task<IEnumerable<object>> Peaks()Returns the marked peaks, strongest first, as [{ frequency, level, magnitude, index, prominence }].
void Invalidate()Redraws the plot on the next animation frame, so that many pushes between two frames cost one draw.

Events

Every event is an EventCallback<Event>; Event is a dictionary and ev["Detail"] converts to the detail class named under the event.

EventDescription and detail
OnSpectrumChange
spectrumChange
SpectrumSpectrumChangeEventDetail
This event is triggered after each block is analysed, with the peaks found on the displayed trace.
Peaks dynamic The marked peaks, strongest first, as [{ frequency, level, magnitude, index, prominence }].
BinWidth string The frequency resolution in hertz.
Size int The transform size.
InvalidSamples string How many samples of the block were not numbers and were analysed as zeros; 0 for a clean block.

Types

enum SpectrumWindow

SpectrumWindow.Rectangular "rectangular" SpectrumWindow.Hann "hann" SpectrumWindow.Hamming "hamming" SpectrumWindow.Blackman "blackman" SpectrumWindow.BlackmanHarris "blackman-harris" SpectrumWindow.FlatTop "flat-top"

enum SpectrumScaling

SpectrumScaling.Amplitude "amplitude" SpectrumScaling.Power "power" SpectrumScaling.Density "density"

enum SpectrumLevelScale

SpectrumLevelScale.Db "db" SpectrumLevelScale.Linear "linear"

enum SpectrumFrequencyScale

SpectrumFrequencyScale.Linear "linear" SpectrumFrequencyScale.Log "log"

enum SpectrumAveraging

SpectrumAveraging.None "none" SpectrumAveraging.Linear "linear" SpectrumAveraging.Exponential "exponential"

CSS variables

The component declares 3 CSS variables; the CSS page shows how to set them.

--smart-spectrum-height --smart-spectrum-plot-background --smart-spectrum-grid-color