简体   繁体   中英

How can I find the critical points of a 2D array in python?

I have a (960,960) array an I am trying to find the critical points so I can find the local extrema.

I have tried using the np.diff and np.gradient, but I have run into some trouble and I am not sure which function to use.

np.diff offers the option of calculating the second order diff, but the gradient doesn't.

How should I go about getting the critical points?

I've tried

diff = np.diff(storm, n=2)                    

dxx = diff[0]                                                                                                                                  
dyy = diff[1]                                                                                                                                  

derivative = dyy/dxx 

I run into problems here because some of the values along the dxx are equal to zero.

Then there's the option of

gradient = np.gradient(storm)
g2 = np.gradient(gradient)

but will this give me what I'm looking for?

Critical point is the point where the first derivative (or gradient in multi-dimensional case) of a function is 0. Thus, you should check the x- and y- difference of your function. numpy 's diff function is good for this case.

So, if the differences between two neighboring elements in x- y- directions are close to 0, then you can say that that point is a critical point. That's when the difference changes its sign (from negative to positive, or vice versa), assuming the your function is smooth.

# get difference in x- and y- direction
sec_grad_x = np.diff(storm,n=1,axis=0)
sec_grad_y = np.diff(storm,n=1,axis=1)

cp = []
# starts from 1 because diff function gives a forward difference
for i in range(1,n-1):
    for j in range(1,n-1):
        # check when the difference changes its sign
        if ((sec_grad_x[i-1,j]<0) != (sec_grad_x[i-1+1,j]<0)) and \
           ((sec_grad_y[i,j-1]<0) != (sec_grad_y[i,j-1+1]<0)):
            cp.append([i,j,  storm[i,j]])

cp = np.array(cp)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM