Comparing Bandpower in MATLAB and Python Using Numpy

Bandpower In Matlab And Its Python Equivalent

Signal processing is a technique that refers to the manipulation and analysis of signals, often the representations of physical data. A signal can take any form; we have audio signals, electrical signals, and image signals.

Since there are different signals, the applications of signal processing are also widespread. In the case of audio signal processing, it is used for audio compression, noise reduction, and speech recognition.

In image and video signal processing, techniques like enhancement, compression, and detection take place. It is also useful in medical imaging, control systems, environmental and financial analysis, and so on.

Matlab is a useful tool that can be used for various tasks. We can effortlessly deal with signal processing using Matlab. We are going to take a look at one such method of Matlab called bandpower.

Check out how to perform signal smoothing

Bandpower is a method or function used to compute the average power of a signal curve. We are going to take a look at how it can be used in Matlab and if it has an equivalent in Python.

To explore the bandpower method in matlab, you might may need to install the Signal Processing Toolkit.

Understanding Bandpower in MATLAB

The bandpower of a signal is the measure of the power of the signal. The bandpower of a signal refers to the power stored within a specific frequency bandwidth or range of frequencies. The strength and bandpower go hand in hand(directly proportional). Higher the bandpower, the stronger the signal, while a lower bandpower means a weaker signal.

Let us see how to use the bandpower of Matlab. According to the signal and requirement, the bandpower can have the following syntaxes.

p = bandpower(x)
p = bandpower(x,fs,freqrange)
p = bandpower(pxx,f,"psd")
p = bandpower(pxx,f,freqrange,"psd")

The arguments this function can take are multiple :

  • x – sample input(can be a matrix)
  • fs – sample rate
  • freqrange – frequency range
  • pxx- Power Spectral Density rate
  • f – frequency vector

In this example, we are focused on the second syntax.

fs = 1000;  
t = 0:1/fs:1-1/fs;  
x = sin(2*pi*50*t) + 0.5*sin(2*pi*120*t);
freqrange = [45, 55]; 
p = bandpower(x, fs, freqrange);
fprintf('Bandpower in the range %d-%d Hz: %f\n', freqrange(1), freqrange(2), p);

In the first line, we define the sample frequency rate as 1000, followed by a time series that is later used to create a sample input, x. The frequency range is defined between 45 and 55. Lastly, the method bandpower is called with the above-discussed parameters and the results are printed in the last line.

Bandpower in MATLAB
Bandpower in MATLAB

Is there a Pythonic way to achieve the same(almost same) result?

Python’s Approach to Bandpower Using Numpy and Scipy

We can use Numpy to achieve similar results, but it alone may not be sufficient to carry out the process. We also should incorporate the Scipy library(which is technically built on Numpy) for computing the bandpower of a signal.

Let us see how we can use numpy and scipy to calculate the power of a signal.

import numpy as np
from scipy import signal
fs = 1000 
t = np.arange(0, 1, 1/fs)
x = np.sin(2 * np.pi * 50 * t) + 0.5 * np.sin(2 * np.pi * 120 * t)
freqrange = [45, 55] 
frequencies, psd = signal.welch(x, fs, nperseg=1024)
freq_indices = np.where((frequencies >= freqrange[0]) & (frequencies <= freqrange[1]))
p = np.trapz(psd[freq_indices], frequencies[freq_indices])
print(f'Bandpower in the range {freqrange[0]}-{freqrange[1]} Hz: {p}')

The very first thing to do is to import the Numpy and Scipy libraries. Next, we define the frequency rate(fs), a time series with the help of NumPy’s arrange method to create a sample input called x. The signal is of a sine curve. The frequency range is defined to be between 45 and 55.

The welch method of the Scipy library is used to calculate the Power Spectral Density(PSD). Finally, the numpy’s trapz method is used to compute the bandpower using the trapezoidal rule.

The trapezoidal rule is an important part of integration, which evaluates the area under the curve by dividing it into small trapeziums instead of rectangles.

Numpy equivalent of bandpower
Numpy equivalent of bandpower

Notice how there is a small difference in computation? That is natural because there is no direct method in Python that can do the task as bandpower in Matlab and the example shown is an implementation of bandpower.


To end it here, we have looked at the bandpower method of MATLAB, explored its syntax, and understood the implementation with the help of an example.

Following that, we also tried to find its equivalent in Python and settled with Numpy and Scipy libraries for a close implementation of the method in Matlab.


Matlab Bandpower documentation

Scipy documentation