From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

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.

 

0 Kudos
Message 1 of 12
(9,469 Views)

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.

 

2D Extrema.png

 

 

Wes Pierce
Principal Engineer
Pierce Controls
0 Kudos
Message 2 of 12
(9,440 Views)
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?
0 Kudos
Message 3 of 12
(9,426 Views)

Hi Christian,

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

0 Kudos
Message 4 of 12
(9,407 Views)

What about that?

 

Unbenannt.PNG

0 Kudos
Message 5 of 12
(9,402 Views)

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

0 Kudos
Message 6 of 12
(9,398 Views)

@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.

 

0 Kudos
Message 7 of 12
(9,369 Views)

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!

0 Kudos
Message 8 of 12
(9,323 Views)

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

Message 9 of 12
(9,310 Views)

Hi Ben,

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

0 Kudos
Message 10 of 12
(9,298 Views)