Matlab

How to Find Local Maxima Using MATLAB’s findpeaks() Function

A maxima of a function is an important mathematical concept. There are two kinds of maxima; one is absolute maxima and the other is local maxima. An absolute maxima is the largest functional value over the entire interval while a local maxima is the largest functional value over a particular interval.

Finding the local maxima of a function can be done in MATLAB using the findpeaks() function.

This guide will explain how to find the local maxima of a function in MATLAB using the findpeaks() function.

Why is it Necessary to Find Local Maxima

Finding local maxima is important for solving optimization problems where we have to find optimal solutions when multiple solutions exist in a problem. The local maxima can also help us understand the complex system, such as spending money in a stock market. We can do this by looking at the trends of the market, thus allowing us to make informed decisions based on the trends.

How to Find the Local Maxima of a Function in MATLAB?

We can easily determine the local maxima of a function in MATLAB using the built-in findpeaks() function. This function allows us to determine the local maxima of an input data or a signal. This function takes a data set as a mandatory input argument and provides the computed local maxima.

Syntax

In MATLAB, you can use the findpeaks() function in the following ways:

pks = findpeaks(data)
[pks,locs] = findpeaks(data)
[pks,locs,w,p] = findpeaks(data)

Here,

The function pks = findpeaks(data) provides a vector containing the local maxima of the given input data set data. The local maxima pks is a data sample that is greater than its neighboring data samples.

The function [pks,locs] = findpeaks(data) provides the local maxima of the given input data set along the indices at which the local maxima occur.

The function [pks,locs,w,p] = findpeaks(data) provides the local maxima of the given input along the width of the peaks vector w and the prominence of peaks contained by vector p.

Example 1: How to Find Local Maxima in a Vector?

This MATLAB code determines the local maxima of the given vector using the findpeaks() function, where we have generated a vector of 10 random numbers from 1 to 100.

v = randi(100,1,10);
plot(v)
pks = findpeaks(v)

Example 2: How to Find Local Maxima Along the Corresponding Indices?

In the following example, we use the findpeaks() function to determine the local maxima along the indices of the given data. The code initially generated a vector of 10 random numbers from 1 to 100, plotted the vector v, then found the local maxima by returning two input vectors pks (local maxima value) and locs (indices of local maxima).

v = randi(100,1,10);
plot(v)
[pks,locs] = findpeaks(v)

Example 3: How to Find Local Maxima Along the Corresponding Indices Peaks’ Width and Prominence?

In this MATLAB code, we use the findpeaks() function to determine the local maxima along the indices of the given data and the peak’s width and prominence.

v = randi(100,1,10);
plot(v)
[pks,locs,w,p] = findpeaks(v)

Conclusion

Finding the local maxima of a function is the most common mathematical problem, which might be hard to calculate normally, but can easily be done through the built-in findpeaks() function in MATLAB. This guide has covered the basics of local maxima, highlighted its importance, and provided different syntaxes and examples to help us understand the workings of the findpeaks() function.