# spectralFlatness

Spectral flatness for signals and spectrograms

## Syntax

``flatness = spectralFlatness(x,f)``
``flatness = spectralFlatness(x,f,Name=Value)``
``[flatness,arithmeticMean,geometricMean] = spectralFlatness(___)``
``spectralFlatness(___)``

## Description

example

````flatness = spectralFlatness(x,f)` returns the spectral flatness of the signal, `x`, over time. How the function interprets `x` depends on the shape of `f`.```

example

````flatness = spectralFlatness(x,f,Name=Value)` specifies options using one or more name-value arguments.```
````[flatness,arithmeticMean,geometricMean] = spectralFlatness(___)` returns the spectral arithmetic mean and spectral geometric mean. You can specify an input combination from any of the previous syntaxes.```

example

````spectralFlatness(___)` with no output arguments plots the spectral flatness. If the input is in the time domain, the spectral flatness is plotted against time.If the input is in the frequency domain, the spectral flatness is plotted against frame number. ```

## Examples

collapse all

Create a chirp signal with white Gaussian noise and calculate the flatness using default parameters.

```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);```

Plot the spectral flatness against time.

`spectralFlatness(x,fs)`

Create a chirp signal with white Gaussian noise and then calculate the spectrogram using the `stft` function.

```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 flatness of the spectrogram over time.

`flatness = spectralFlatness(s,f);`

Plot the spectral flatness against the frame number.

`spectralFlatness(s,f)`

Create a chirp signal with white Gaussian noise.

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

Calculate the flatness of the power spectrum over time. Calculate the flatness for 50 ms Hamming windows of data with 25 ms overlap. Use the range from 62.5 Hz to `fs`/2 for the flatness calculation.

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

Plot the flatness against time.

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

## Input Arguments

collapse all

Input signal, specified as a vector, matrix, or 3-D array. How the function interprets `x` depends on the shape of `f`.

Data Types: `single` | `double`

Sample rate or frequency vector in Hz, specified as a scalar or vector, respectively. How the function interprets `x` depends on the shape of `f`:

• If `f` is a scalar, `x` is interpreted as a time-domain signal, and `f` is interpreted as the sample rate. In this case, `x` must be a real vector or matrix. If `x` is specified as a matrix, the columns are interpreted as individual channels.

• If `f` is a vector, `x` is interpreted as a frequency-domain signal, and `f` is interpreted as the frequencies, in Hz, corresponding to the rows of `x`. In this case, `x` must be a real L-by-M-by-N array, where L is the number of spectral values at given frequencies of `f`, M is the number of individual spectra, and N is the number of channels.

• The number of rows of `x`, L, must be equal to the number of elements of `f`.

Data Types: `single` | `double`

### Name-Value Arguments

Specify optional pairs of arguments as `Name1=Value1,...,NameN=ValueN`, where `Name` is the argument name and `Value` is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose `Name` in quotes.

Example: `Window=hamming(256)`

Note

The following name-value arguments apply if `x` is a time-domain signal. If `x` is a frequency-domain signal, name-value arguments are ignored.

Window applied in the time domain, specified as a real vector. The number of elements in the vector must be in the range [1, `size(x,1)`]. The number of elements in the vector must also be greater than `OverlapLength`.

Data Types: `single` | `double`

Number of samples overlapped between adjacent windows, specified as an integer in the range [0, `size(Window,1)`).

Data Types: `single` | `double`

Number of bins used to calculate the DFT of windowed input samples, specified as a positive scalar integer. If unspecified, `FFTLength` defaults to the number of elements in the `Window`.

Data Types: `single` | `double`

Frequency range in Hz, specified as a two-element row vector of increasing real values in the range [0, `f`/2].

Data Types: `single` | `double`

Spectrum type, specified as `"power"` or `"magnitude"`:

• `"power"` –– The spectral flatness is calculated for the one-sided power spectrum.

• `"magnitude"` –– The spectral flatness is calculated for the one-sided magnitude spectrum.

Data Types: `char` | `string`

## Output Arguments

collapse all

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

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

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

## Algorithms

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

`$\text{flatness}=\frac{{\left(\prod _{k={b}_{1}}^{{b}_{2}}{s}_{k}\right)}^{\frac{1}{{b}_{2}-{b}_{1}}}}{\frac{1}{{b}_{2}-{b}_{1}}\sum _{k={b}_{1}}^{{b}_{2}}{s}_{k}}$`

where

• sk is the spectral value at bin k.

• b1 and b2 are the band edges, in bins, over which to calculate the spectral spread.

## References

[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.

## Version History

Introduced in R2019a