# LabVIEW

cancel
Showing results for
Did you mean:

Highlighted

## How to find local extrema in matrices / 2D-arrays

I am looking for a solution to find local maxima in 2D arrays. Something like the PeakDetector.vi does but for 2D data.

Message 1 of 12
(2,935 Views)

## Re: How to find local extrema in matrices / 2D-arrays

For absolute extrema, you can use Array Min & Max. It is polymorphic to accept multiple dimension arrays and will give you the value and location of the extremum.

Local extrema are a bit harder. I don't know of any out of the box tool to do that for you, but basically all you need to do mathematically is find the gradient and the location where it is equal to 0 (or close enough to 0). The gradient is just [dz/dx, dz/dy].

You can use transpose array and then apply the derivative VI for each row. Then look for where both are 0 (or within a deadband). To determine whether they are maxima or minima, take the second derivative in one direction (there is no real need to do it in both directions, because if it is truly a local extremum, then both second derivatives will have the same sign.

I coded this up, quick and dirty. Hopefully it is a starting place for you.

Wes P | Certified LabVIEW Architect
Message 2 of 12
(2,906 Views)

## Re: How to find local extrema in matrices / 2D-arrays

Do you have more information on the type of the 2D peaks (e.g. are they approximately 2D Gaussian?). The gradient method mentioned above does not work well for data in a discrete grid, especially if there is any noise.
How many local maxima do you expect and are they always in approximately the same location. Do you only need the quantized location or can the peak indices be fractional? Do all peaks have similar width?
Do you have some typical data?

LabVIEW Champion Do more with less code and in less time
Message 3 of 12
(2,892 Views)

## Re: How to find local extrema in matrices / 2D-arrays

Hi Christian,

I put some sample data to a constant in the VI attached.

Message 4 of 12
(2,873 Views)

Message 5 of 12
(2,868 Views)

## Re: How to find local extrema in matrices / 2D-arrays

That will just give me one maximum and not the other local maxima...

Message 6 of 12
(2,864 Views)

## Re: How to find local extrema in matrices / 2D-arrays

@Standardnutzer wrote:

I put some sample data to a constant in the VI attached.

Well, so which local peaks do you want? I can see about three, see image.

As I asked before, are all data sets similar (e.g. similar width of the peaks, similar locations, similar shape and symmetry, etc.) or is each dataset very different?

The data looks like some low order spherical harmonics projected on a plane. Is this theoretical noiseless data or a real measurement (I cannot easily tell if there is noise).

If it is simulated data, what is the math behind it? maybe you can fit to the actual model function and get the entire shape.

LabVIEW Champion Do more with less code and in less time
Message 7 of 12
(2,835 Views)

## Re: How to find local extrema in matrices / 2D-arrays

Hi Christian,

There is no function to describe the data.

In the mean time I just took the easy approch and looked for the maximum values with certain minimum distances to each other.

I think this will do the job for the moment.

Thanks!

Message 8 of 12
(2,789 Views)

## Re: How to find local extrema in matrices / 2D-arrays

I remember a similar post a long (relatively) time ago. Attached is a way to get the local maxima. It is a crude version that badly need to be improve for efficiency (it takes 20-25 sec to get the local maxima of your 2D array!) but it probably be used as a starting point.

Ben64

--------------------------------------------------
The best way to say thanks is to give kudos!
Message 9 of 12
(2,776 Views)

## Re: How to find local extrema in matrices / 2D-arrays

Hi Ben,

Thanks for your VI. I will give it a try.

Message 10 of 12
(2,764 Views)