Engee documentation

spectralFlatness

Spectral unevenness of signals and spectrograms.

Library

EngeeDSP

Syntax

Function call

  • flatness,arithmeticMean,geometricMean = spectralFlatness(x,f) — returns the spectral unevenness, the arithmetic mean, and the geometric mean of the spectral value of the signal x over time. Interpretation x the function depends on the form f.

  • spectralFlatness(___;out=:plot) — plots spectral irregularities.

    • If the input signal is in the time domain, a graph of spectral unevenness is plotted as a function of time.

    • If the input signal is in the frequency domain, the spectral unevenness graph is plotted depending on the frame number.

Arguments

Input arguments

# x — input signal

+ column vector | the matrix

Details

An input signal specified as a vector or matrix. Interpretation x the function depends on the form f.

Типы данных

Float32, Float64

# f is the sampling frequency or frequency vector (Hz)

+ scalar | vector

Details

The sampling frequency or frequency vector in Hz, specified as a scalar or vector, respectively. Interpretation x the function depends on the form f:

  • If f — scalar, x it is interpreted as a signal in the time domain, and f — as a sampling rate. In this case x must be a real vector or a matrix. If x set as a matrix, the columns are interpreted as separate channels.

  • If f — vector, x it is interpreted as a signal in the frequency domain, and f — how are the frequencies in Hz corresponding to the strings x. In this case x must be a real array of size , where — the number of spectral values at the specified frequencies f, — the number of individual spectra, and — the number of channels.

  • Number of lines x, , must be equal to the number of elements f.

Типы данных

Float32, Float64

Name-value input arguments

Specify optional argument pairs as Name,Value, where Name — the name of the argument, and Value — the appropriate value. Name-value arguments should be placed after other arguments, but the order of the pairs does not matter.

Use commas to separate the name and value, and Name put it in quotation marks.

The following name-value arguments apply if x — a signal in the time domain. If x — a signal in the frequency domain, arguments of the "name-value" type are ignored.

# Window — the window used in the time domain

+ rectwin(round(f*0.03)) (default) | vector

Details

The window used in the time domain, defined as a real vector. The number of vector elements must be in the range [1,size(x,1)]. The number of vector elements must also be greater. OverlapLength.

Типы данных

Float32, Float64

# OverlapLength — the number of samples overlapping between adjacent windows

+ round(f*0.02) (by default) | a non-negative scalar

Details

The number of samples overlapping between adjacent windows, set as an integer in the range [0,size(Window,1)).

Типы данных

Float32, Float64

# FFTLength — the number of elements in the DFT

+ numel(Window) (by default) | a positive integer scalar

Details

The number of elements used to calculate the DFT of window input samples, set as a positive integer scalar. If no argument is given, FFTLength by default, it is equal to the number of elements in Window.

Типы данных

Float32, Float64

# Range — frequency range (Hz)

+ [0,f/2] (by default) | A two-element vector is a string

Details

The frequency range in Hz, defined as a two-element vector, is a string of increasing real values in the range [0,f/2].

Типы данных

Float32, Float64

# SpectrumType — spectrum type

+ "power" (by default) | "magnitude"

Details

The type of spectrum specified as "power" or "magnitude":

  • "power" — spectral unevenness is calculated for a one-sided power spectrum;

  • "magnitude" — spectral unevenness is calculated for a one-sided amplitude spectrum.

Типы данных

String

# out — type of output data

+ :data (by default) | :plot

Details

Type of output data:

  • :data — the function returns data;

  • :plot — the function returns a graph.

Output arguments

# flatness — spectral ridge

+ scalar | vector | the matrix

Details

Spectral unevenness returned as a scalar, vector, or matrix. Each line flatness corresponds to the spectral ridge of the window x. Each column flatness corresponds to an independent channel.

# arithmeticMean — the arithmetic mean of the spectrum

+ scalar | vector | the matrix

Details

The arithmetic mean of the spectrum, returned as a scalar, vector, or matrix. Each line arithmeticMean corresponds to the arithmetic mean of the window spectrum x. Each column arithmeticMean corresponds to an independent channel.

# geometricMean — geometric mean of the spectrum

+ scalar | vector | the matrix

Details

The geometric mean of the spectrum, returned as a scalar, vector, or matrix. Each line geometricMean corresponds to the average geometric spectrum of the window x. Each column geometricMean corresponds to an independent channel.

Examples

Spectral unevenness of the signal in the time domain

Details

Let’s create an LFM signal with white Gaussian noise and calculate the unevenness using the default parameters.

import EngeeDSP.Functions: chirp, randn, spectralFlatness

fs = 1000
t = (0:1/fs:10)
f1 = 300
f2 = 400
x = chirp(t, f1, 10, f2) + randn(length(t), 1)

flatness = spectralFlatness(x, fs)

Let’s plot the dependence of spectral unevenness on time.

spectralFlatness(x, fs, out=:plot)

spectralFlatness 1

([0.5327040255782655, 0.552964530108542, 0.47742468036057234, 0.37293317041480095, 0.44287635975879003, 0.4119379715559745, 0.5013730779675295, 0.2015608067144641, 0.33548839011069864, 0.5369805014970516  …  0.1727536936022771, 0.35070131234444546, 0.25693379071386535, 0.255058829804521, 0.3677277425236599, 0.23687507452160672, 0.42585525405176444, 0.2835981233091784, 0.37349949783684333, 0.24033135785583964], [0.07786759765793372, 0.08135374655336428, 0.08637079824524838, 0.11933620888263814, 0.12350546278299357, 0.14497737376909484, 0.15042289056894764, 0.16892023682888072, 0.13419976354764332, 0.09558842868870962  …  0.10961556063903502, 0.11750795091292063, 0.10693418095926478, 0.09961268609163781, 0.10747394543349716, 0.15058502472661675, 0.14519245631195274, 0.15333273649350335, 0.13430846482161657, 0.12333737035602271], [0.04148038273449001, 0.04498573623545049, 0.04123555074472519, 0.04450443072388517, 0.05469764976765691, 0.05972168527195327, 0.07541798764132614, 0.03404769920562753, 0.04502246262583528, 0.05132912237457844  …  0.018936492976677682, 0.041210192596067945, 0.027475004470746342, 0.025407095148218223, 0.03952115133437092, 0.035669838953955334, 0.06183097036912634, 0.04348487631141832, 0.050164144166111126, 0.029641837692031523], Plot{Plots.PlotlyJSBackend() n=1})

Spectral unevenness of the signal in the frequency domain

Details

Let’s create an LFM signal with white Gaussian noise, and then calculate the spectrogram using the function stft.

import EngeeDSP.Functions: chirp, randn, stft

fs = 1000
t = (0:1/fs:10)
f1 = 300
f2 = 400
x = chirp(t, f1, 10, f2) + randn(length(t), 1)

s, f = stft(x, fs, "FrequencyRange", "onesided")
s = abs.(s).^2

Calculate the unevenness of the spectrogram over time.

import EngeeDSP.Functions: spectralFlatness

flatness = spectralFlatness(s, f)

Let’s plot the dependence of the spectral unevenness on the frame number.

spectralFlatness(s, f, out=:plot)

spectralFlatness 2

([0.41698805310198656, 0.46683201868098656, 0.3883287575836514, 0.4907688270016035, 0.4091462305599816, 0.3191585704065677, 0.3577051127741075, 0.3896555430416278, 0.598856100869491, 0.5385492705509978  …  0.3819877611783831, 0.41721990894367383, 0.38697190520523694, 0.46858320991071867, 0.48587071241197366, 0.38864151988166845, 0.5335128495308832, 0.4664062512134147, 0.4145737219480951, 0.477601293218658], [66.18935543214769, 71.25953873838431, 67.29284380517325, 72.27692418494, 77.76383362649338, 74.80810295770037, 65.91505570861669, 67.23287211343407, 73.51318020109804, 61.9977680185544  …  67.19403006538472, 79.16107520226234, 79.39008525296707, 70.97130285799408, 68.37996747931281, 63.08170611779957, 68.8590471648835, 79.56916556123606, 53.21814023726604, 53.157840474425484], [27.600170457726666, 33.26623431951591, 26.13174642913364, 35.47126130152683, 31.81677940217331, 23.87564719480698, 23.578152435762313, 26.197661293608466, 44.02381645774584, 33.38885274218245  …  25.667297109229274, 33.02757658777121, 30.721732544746853, 33.255960904744626, 33.22382351388131, 24.51617014235037, 36.73718646891848, 37.111556221595656, 22.062842473319066, 25.388253355296733], Plot{Plots.PlotlyJSBackend() n=1})

Specifying parameters other than the standard ones

Details

Let’s create an FM signal with white Gaussian noise.

import EngeeDSP.Functions: chirp, randn

fs = 1000
t = (0:1/fs:10)
f1 = 300
f2 = 400
x = chirp(t, f1, 10, f2) + randn(length(t), 1)

Let’s calculate the unevenness of the power spectrum over time. Calculate the unevenness for Hamming windows with a duration of 50 ms with overlap 25 ms. We use the range from 62.5 Hz to fs/2 to calculate the unevenness.

import EngeeDSP.Functions: spectralFlatness, hamming

flatness = spectralFlatness(x, fs,
                      "Window", hamming(round(Int, 0.05*fs)),
                      "OverlapLength", round(Int, 0.025*fs),
                      "Range", [62.5, fs/2])

Let’s plot the dependence of the unevenness on time.

spectralFlatness(x, fs,
              "Window", hamming(round(Int, 0.05*fs)),
              "OverlapLength", round(Int, 0.025*fs),
              "Range", [62.5, fs/2],
              out=:plot)

spectralFlatness 3

([0.44393966543497354, 0.4695783799925332, 0.49431342254249894, 0.6021106597242966, 0.44236146074090665, 0.5951722206261713, 0.6452967394540707, 0.46437619283350273, 0.3888478354209635, 0.6150829750332204  …  0.5454458473997145, 0.5030744478476047, 0.45828271847210195, 0.3342891893548354, 0.5377916650534941, 0.2390323362290697, 0.41558590905052906, 0.5762897059640517, 0.5368836411577717, 0.5281049952129195], [0.09501542410874092, 0.09365413839399048, 0.08501344767652717, 0.08096139641217256, 0.03234155232658504, 0.0508716873101426, 0.08013355128737082, 0.09856405316445577, 0.0713312402132862, 0.08216872345317404  …  0.09695049793640315, 0.0824351178183989, 0.1008633635765253, 0.09848674637522525, 0.0885626532954448, 0.08363028719440331, 0.10550801325677596, 0.08844347795474994, 0.06239257287735799, 0.1156749436919185], [0.042181115589996564, 0.043977958586646555, 0.0420232882831218, 0.04874771980593352, 0.014306656329816626, 0.030277415103377784, 0.051709919366615946, 0.045770799758748926, 0.027736998354829127, 0.05054058287626024  …  0.052881246502745685, 0.041471001379743265, 0.04622393645409, 0.03292305460796933, 0.04762825677731257, 0.019990342927586273, 0.04384764360143251, 0.05096906590498093, 0.033497551707597585, 0.06108851558467535], Plot{Plots.PlotlyJSBackend() n=1})

Algorithms

The spectral unevenness is calculated as described in [1]:

where

  • — spectral value in bin ;

  • and — band boundaries in bins, which are used to calculate spectral unevenness.

Literature

  1. Johnston, J. D. «Transform Coding of Audio Signals Using Perceptual Noise Criteria.» IEEE Journal on Selected Areas in Communications. Vol. 6, Number 2, 1988, pp. 314–323.