## Estimate the Power Spectrum in Simulink

The power spectrum (PS) of a time-domain signal is the distribution of power contained within the signal over frequency, based on a finite set of data. The frequency-domain representation of the signal is often easier to analyze than the time-domain representation. Many signal processing applications, such as noise cancellation and system identification, are based on the frequency-specific modifications of signals. The goal of the power spectral estimation is to estimate the power spectrum of a signal from a sequence of time samples. Depending on what is known about the signal, estimation techniques can involve parametric or nonparametric approaches and can be based on time-domain or frequency-domain analysis. For example, a common parametric technique involves fitting the observations to an autoregressive model. A common nonparametric technique is the periodogram. The power spectrum is estimated using Fourier transform methods such as the Welch method and the filter bank method. For signals with relatively small length, the filter bank approach produces a spectral estimate with a higher resolution, a more accurate noise floor, and peaks more precise than the Welch method, with low or no spectral leakage. These advantages come at the expense of increased computation and slower tracking. For more details on these methods, see Spectral Analysis. You can also use other techniques such as the maximum entropy method.

In Simulink^{®}, you can perform real-time spectral analysis of a dynamic signal using the
Spectrum Analyzer block. You can view the spectral data in the spectrum
analyzer. To acquire the last spectral data for further processing, create a `SpectrumAnalyzerConfiguration`

object and run the `getSpectrumData`

function on this object. Alternately, you can use the
Spectrum Estimator block from the
`dspspect3`

library to compute the power spectrum, and Array Plot block to view the spectrum.

### Estimate the Power Spectrum Using the Spectrum Analyzer

You can view the power spectrum (PS) of a signal using the Spectrum Analyzer block. The PS is computed in real time and varies with the input signal, and with changes in the properties of the Spectrum Analyzer block. You can change the dynamics of the input signal and see what effect those changes have on the spectrum of the signal in real time.

The model `ex_psd_sa`

feeds a noisy sine wave signal to the
Spectrum Analyzer block. The sine wave signal is a sum of two
sinusoids: one at a frequency of 5000 Hz and the other at a frequency of 10,000 Hz.
The noise at the input is Gaussian, with zero mean and a standard deviation of
0.01.

**Open and Inspect the Model**

To open the model, enter `ex_psd_sa`

in the MATLAB^{®} command prompt.

Here are the settings of the blocks in the model.

Block | Parameter Changes | Purpose of the block |
---|---|---|

Sine Wave 1 |
**Frequency**to 5000**Sample time**to 1/44100**Samples per frame**to 1024
| Sinusoid signal with frequency at 5000 Hz |

Sine Wave 2 |
**Frequency**to 10000**Phase offset (rad)**to 10**Sample time**to 1/44100**Samples per frame**to 1024
| Sinusoid signal with frequency at 10000 Hz |

Random Source |
**Source type**to`Gaussian` **Variance**to 1e-4**Sample time**to 1/44100**Samples per frame**to 1024
| Random Source block generates a random noise signal with properties specified through the block dialog box |

Add | List of signs to
`+++` . | Add block adds random noise to the input signal |

Spectrum Analyzer | Click the In the **Main options**pane, under**Type**, select`Power` . Under**Method**, select`Filter bank` .In the **Trace options**pane, clear the**Two-sided spectrum**check box. This shows only the real-half of the spectrum.If needed, select the **Max-hold trace**and**Min-hold trace**check boxes.
Click the
| Spectrum Analyzer block shows the Power Spectrum Density of the signal |

Play the model. Open the Spectrum Analyzer block to view the power spectrum of the sine wave signal. There are two tones at frequencies 5000 Hz and 10,000 Hz, which correspond to the two frequencies at the input.

*RBW*, the resolution bandwidth is the minimum frequency
bandwidth that can be resolved by the spectrum analyzer. By default, **RBW
(Hz)** is set to `Auto`

. In the
`Auto`

mode, RBW is the ratio of the frequency span to
1024. In a two-sided spectrum, this value is F_{s}/1024, while in a one-sided spectrum, it is (F_{s}/2)/1024. The spectrum analyzer in `ex_psd_sa`

is
configured to show one-sided spectrum. Hence, the RBW is (44100/2)/1024 or 21.53
Hz.

Using this value of RBW, the number of input samples used to compute one spectral
update is given by N_{samples} = Fs/RBW, which is 44100/21.53 or 2048.

RBW calculated in this mode gives a good frequency resolution.

To distinguish between two frequencies in the display, the distance between the
two frequencies must be at least RBW. In this example, the distance between the two
peaks is 5000 Hz, which is greater than RBW. Hence, you can see the peaks
distinctly. Change the frequency of the second sine wave from 10000 Hz to 5015 Hz.
The difference between the two frequencies is less
than *RBW*.

On zooming, you can see that the peaks are not distinguishable.

To increase the frequency resolution, decrease *RBW* to 1 Hz
and run the simulation.

On zooming, the two peaks, which are 15 Hz apart, are now distinguishable

When you increase the frequency resolution, the time resolution decreases. To
maintain a good balance between the frequency resolution and time resolution, change
the **RBW (Hz)** to `Auto`

.

**Change the Input Signal**

When you change the dynamics of the input signal during simulation, the power
spectrum of the signal also changes in real time. While the simulation is running,
change the **Frequency** of the Sine Wave 1 block to
`8000`

and click **Apply**. The second tone in
the spectral analyzer output shifts to 8000 Hz and you can see the change in real
time.

**Change the Spectrum Analyzer Settings**

When you change the settings in the Spectrum Analyzer block, the effect can be seen on the spectral data in real time.

When the model is running, in the **Trace** options pane of the
Spectrum Analyzer block, change the **Scale** to
`Log`

. The PS is now displayed on a log scale.

For more information on how the Spectrum Analyzer settings affect the power spectrum data, see the 'Algorithms' section of the Spectrum Analyzer block reference page.

### Convert the Power Between Units

The spectrum analyzer provides three units to specify the power spectral density:
`Watts/Hz`

, `dBm/Hz`

, and
`dBW/Hz`

. Corresponding units of power are
`Watts`

, `dBm`

, and
`dBW`

. For electrical engineering applications, you can also
view the RMS of your signal in `Vrms`

or
`dBV`

. The default spectrum type is **Power**
in `dBm`

.

#### Convert the Power in Watts to dBW and dBm

Power in `dBW`

is given by:

$${P}_{dBW}=10\mathrm{log}10(power\text{\hspace{0.17em}}in\text{\hspace{0.17em}}watt/1\text{\hspace{0.17em}}watt)$$

Power in `dBm`

is given by:

$${P}_{dBm}=10\mathrm{log}10(power\text{\hspace{0.17em}}in\text{\hspace{0.17em}}watt/1\text{\hspace{0.17em}}milliwatt)$$

For a sine wave signal with an amplitude of 1 V, the power of
a one-sided spectrum in `Watts`

is given
by:

$$\begin{array}{l}{P}_{Watts}={A}^{2}/2\\ {P}_{Watts}=1/2\end{array}$$

In this example, this power equals 0.5 W. Corresponding power in dBm is given by:

$$\begin{array}{l}{P}_{dBm}=10\mathrm{log}10(power\text{\hspace{0.17em}}in\text{\hspace{0.17em}}watt/1\text{\hspace{0.17em}}milliwatt)\\ {P}_{dBm}=10\mathrm{log}10(0.5/{10}^{-3})\end{array}$$

Here, the power equals
26.9897 dBm. To confirm this value with a peak finder, click **Tools** > **Measurements** > **Peak Finder**.

For a white noise signal, the spectrum is flat for all frequencies.
The spectrum analyzer in this example shows a one-sided spectrum in
the range [0 Fs/2]. For a white noise signal with a variance of 1e-4,
the power per unit bandwidth (P_{unitbandwidth})
is 1e-4. The total power of white noise in **watts** over
the entire frequency range is given by:

$$\begin{array}{l}{P}_{whitenoise}={P}_{unitbandwidth}*number\text{\hspace{0.17em}}of\text{\hspace{0.17em}}frequency\text{\hspace{0.17em}}bins,\\ {P}_{whitenoise}=({10}^{-4})*\left(\frac{Fs/2}{RBW}\right),\\ {P}_{whitenoise}=({10}^{-4})*\left(\frac{22050}{21.53}\right)\end{array}$$

The number of frequency
bins is the ratio of total bandwidth to RBW. For a one-sided spectrum,
the total bandwidth is half the sampling rate. RBW in this example
is 21.53 Hz. With these values, the total power of white noise in **watts** is 0.1024 W. In dBm, the power of white
noise can be calculated using 10*log10(0.1024/10^-3),
which equals 20.103 dBm.

#### Convert Power in Watts to dBFS

If you set the spectral units to `dBFS`

and set the full scale (`FullScaleSource`

) to
`Auto`

, power in `dBFS`

is computed as:

$${P}_{dBFS}=20\cdot {\mathrm{log}}_{10}\left(\sqrt{{P}_{watts}}/Full\_Scale\right)$$

where:

`P`

is the power in watts_{watts}For double and float signals,

*Full_Scale*is the maximum value of the input signal.For fixed point or integer signals,

*Full_Scale*is the maximum value that can be represented.

If you specify a manual full scale (set `FullScaleSource`

to
`Property`

), power in `dBFS`

is given by:

$${P}_{FS}=20\cdot {\mathrm{log}}_{10}\left(\sqrt{{P}_{watts}}/FS\right)$$

Where `FS`

is the full scaling factor specified in the `FullScale`

property.

For a sine wave signal with an amplitude of 1 V, the power of a one-sided spectrum in
`Watts`

is given by:

$$\begin{array}{l}{P}_{Watts}={A}^{2}/2\\ {P}_{Watts}=1/2\end{array}$$

In this example, this power equals 0.5 W and the maximum input signal for a sine wave is 1 V. The corresponding power in dBFS is given by:

$${P}_{FS}=20\cdot {\mathrm{log}}_{10}\left(\sqrt{1/2}/1\right)$$

Here, the power equals -3.0103. To confirm this value in the spectrum analyzer, run these commands:

Fs = 1000; % Sampling frequency sinef = dsp.SineWave('SampleRate',Fs,'SamplesPerFrame',100); scope = dsp.SpectrumAnalyzer('SampleRate',Fs,... 'SpectrumUnits','dBFS','PlotAsTwoSidedSpectrum',false) %% for ii = 1:100000 xsine = sinef(); scope(xsine) end

**Tools**>

**Measurements**>

**Peak Finder**.

#### Convert the Power in dBm to RMS in Vrms

Power in `dBm`

is given by:

$${P}_{dBm}=10\mathrm{log}10(power\text{\hspace{0.17em}}in\text{\hspace{0.17em}}watt/1\text{\hspace{0.17em}}milliwatt)$$

Voltage in RMS is given by:

$${V}_{rms}={10}^{{P}_{dBm}/20}\sqrt{{10}^{-3}}$$

From the previous example, P_{dBm} equals
26.9897 dBm. The V_{rms} is calculated as

$${V}_{rms}={10}^{26.9897/20}\sqrt{0.001}$$

which equals 0.7071.

To confirm this value:

Change

**Type**to`RMS`

.Open the peak finder by clicking

**Tools**>**Measurements**>**Peak Finder**.

### Estimate Power Spectrum Using the Spectrum Estimator Block

Alternately, you can compute the power spectrum of the signal using the Spectrum Estimator block in the `dspspect3`

library.
You can acquire the output of the spectrum estimator and store the data for further
processing.

Replace the Spectrum Analyzer block in `ex_psd_sa`

with the Spectrum Estimator block followed by an
Array Plot block. To view the model,
enter `ex_psd_estimatorblock`

in the MATLAB command prompt. In addition, to access the spectral estimation data in
MATLAB, connect the To Workspace (Simulink) block to the output of
the Spectrum Estimator block. Here are the changes to the settings of
the Spectrum Estimator block and the Array Plot
block.

Block | Parameter Changes | Purpose of the block |
---|---|---|

Spectrum Estimator |
**Frequency resolution method**to`Number of frequency bands` .**Frequency range**to`One-sided` .
| Computes the power spectrum of the input signal using the filter bank approach. |

Array Plot | Click select `Style` . In the Style window, select the**Plot type**as`Stairs` .select `Configuration Properties` . In the Configuration Properties window, on the**Main**tab, set the**Sample increment**as`44.1/1024` . On the**Display**tab, change**X-label**to`Frequency (kHz)` ,**Y-label**to`Power (dBm)` . For details, see the section 'Convert`x` -axis to Represent Frequency'. In addition, set**Y-limits (Minimum)**to`-100` and**Y-limits (Maximum)**to`40` .
| Displays the power spectrum data. |

The spectrum displayed in the Array Plot block is similar to the
spectrum seen in the Spectrum Analyzer block in
`ex_psd_sa`

.

The filter bank approach produces peaks that have very minimal spectral leakage.

**Convert x-axis to Represent
Frequency**

By default, the Array Plot block plots the PS data with respect to
the number of samples per frame. The number of points on the x-axis equals the
length of the input frame. The spectrum analyzer plots the PS data with respect to
frequency. For a one-sided spectrum, the frequency varies in the range [0 Fs/2]. For
a two-sided spectrum, the frequency varies in the range [-Fs/2 Fs/2]. To convert the
`x`

-axis of the array plot from sample-based to
frequency-based, do the following:

Click on the

**Configuration Properties**icon . On**Main**tab, set**Sample increment**to`Fs/FrameLength`

.For a one-sided spectrum, set

**X-offset**to`0`

.For a two-sided spectrum, set

**X-offset**to`-Fs/2`

.

In this example, the spectrum is one-sided and hence, the
**Sample increment** and **X-offset** are set
to `44100/1024`

and `0`

, respectively. To specify
the frequency in `kHz`

, set the **Sample
increment** to `44.1/1024`

.

**Live Processing**

The output of the Spectrum Estimator block contains the spectral
data and is available for further processing. The data can be processed in real-time
or it can be stored in the workspace using the To Workspace block.
This example writes the spectral data to the workspace variable
`Estimate`

.