Periodogram Python

Introduction

  • The power spectral density (PSD) of a signal depicts how its power is divided across frequencies. It has signal-processing applications in many technical disciplines.
  • PSD is used to assess channel occupancy and associated frequencies in communications systems such as radios and radars. PSD is widely used in spectrum analysis to estimate the distribution of signal strength over a frequency range.

Periodogram

  • Periodograms are a type of PSD that is used for discrete-time signals. In this section, we will focus on periodograms, as is common in signal processing for digital systems.
  • The following definitions of PSD and periodogram are provided in brief. Then, we'll look at how PSD is calculated in Python using the important signal processing libraries scipy and matplotlib.

Power Spectral Density

We jump right into discrete-time signals, skipping the entire top-down scenario that begins with continuous signals. Our data is frequently sampled; thus, discrete-time processing is the one that we employ in practice.

Consider a discrete signal, where is the signal's length. This can be the entire signal or a -length window of a larger signal.

Let us also suppose that the signal is sampled at frequency, where it is the time interval between samples in seconds.

Python solution for PSD

Test data:

Before we can calculate the actual PSD, we need to generate some test data. For this, we employ two sine waves with a frequency of 10Hz and 60Hz. We then introduce some Gaussian noise to see whether we can separate these two frequency components from the data.

Code:

Test setup configuration.

Using Scipy

The standard signal analytics package scipy.signal.periodogram is a convenient implementation for calculating periodograms. We can simply determine power spectral density using this method. Scipy is easy to use; all we need to provide the periodogram method with are the real signal data and sample frequency. To ensure that the procedure returns the PSD rather than the power spectrum, we also set scaling='density'.

The approach yields the frequency components and the corresponding power density.

Plotting the Data

At their respective frequencies of 10Hz and 60Hz, We can easily distinguish the two frequency components. Both have the same amplitude, which makes sense given sine wave amplitudes.

Estimating PSD using Scipy& Welch's Method

PSD can be time-consuming to compute for lengthy transmissions. One well-known approach for estimating the PSD is Welch's method. Scipy also has a technique for employing this estimating strategy readily available.

As can be observed, Welch's approach approximates the amplitudes and frequency components of our test signal fairly well. The correct frequency components may be easily distinguished from the noise.

We can achieve a more accurate estimate by increasing the segment length.

Using a larger segment length distinguishes the frequency components. This is useful when signl components are near to one another.

Using Matplotlib

Matplotlib also includes a method for calculating and displaying PSD. It computes the PSD using the previously discussed Welch's approach.

The results are comparable to Welch's Scipy approach. Matplotlib can be useful if you wish to avoid the Scipy requirement.

Naive Python Implementation

We can write a simple Python implementation that only requires numpy. This implementation follows the definition exactly. It is quite slow. It may be handy, though, if you only need to calculate PSD for a few frequencies.

Code:

Then, to compute PSD for frequencies, we can just use the following;

Code:

Conclusion

You now have the essential tools for the calculation of PSDs and periodograms. It is easy with conventional signal processing packages. You should also have an idea of how to implement the PSD method by yourself, strictly following the mathematical definition.