Home > Software engineering >  How to get the minimum and maximum of each period in the list?
How to get the minimum and maximum of each period in the list?

Time:10-17

This is the graph that I got in the list enter image description here

My question is how to get each minimum and maximum of each period in the list? Following the graph I want to get the 1st maximum at point 1, 1st minimum at point 30, 2nd maximum at point 62, 2nd minimum at point 94

I have tried but it got the wrong point since the value in the list is not stable as you can see on the graph

This is my code that I have tried

import matplotlib.pyplot as plt

max_1 = 0
max_1_pos = 0
min_1_pos = 0
max_2 = 0
max_2_pos = 0
min_2_pos = 0
img_list_pix =[218, 225, 224, 224, 224, 220, 217, 215, 216, 216,
               216, 215, 214, 215, 214, 214, 213, 210, 210, 205,
               207, 204, 205, 201, 200, 201, 201, 197, 202, 203,
               196, 196, 196, 196, 198, 198, 199, 202, 202, 205,
               205, 205, 207, 206, 208, 207, 209, 214, 212, 213,
               215, 218, 220, 219, 218, 220, 223, 222, 224, 224,
               224, 222, 225, 222, 222, 222, 222, 223, 223, 221,
               223, 222, 223, 219, 218, 216, 215, 214, 212, 212,
               211, 211, 207, 206, 205, 204, 202, 199, 199, 198,
               197, 195, 193, 191, 191, 193, 191, 196]
# index = []
# cnt = 0
# for i in range(len(img_list_pix)):
#     index.append(cnt)
#     cnt  = 1
# plt.plot(index, img_list_pix, marker = '.', markerfacecolor = 'red')
# for x, y in zip(index, img_list_pix):
#     plt.text(x, y, str(x), color="red", fontsize=8)
# plt.show()

for i in range(len(img_list_pix)):
    if img_list_pix[i] >= max_1:
        max_1 = img_list_pix[i]
        max_1_pos = i
    else:
        break
min_1 = max_1
print(f"max_1:{max_1} | i:{i}")

for i in range(max_1_pos   1, len(img_list_pix)):
    if img_list_pix[i] <= min_1:
        min_1 = img_list_pix[i]
        min_1_pos = i
    else:
        break
max_2 = min_1
print(f"min_1:{min_1} | i:{i}")

for i in range(min_1_pos   1, len(img_list_pix)):
    if img_list_pix[i] >= max_2:
        max_2 = img_list_pix[i]
        max_2_pos = i
    else:
        break
min_2 = max_2
print(f"max_2:{max_2} | i:{i}")

for i in range(max_2_pos   1, len(img_list_pix)):
    if img_list_pix[i] <= min_2:
        min_2 = img_list_pix[i]
        min_2_pos = i
    else:
        break
print(f"min_2:{min_2} | i:{i}")

The results

max_1:225 | i:2
min_1:215 | i:8
max_2:216 | i:11
min_2:214 | i:13

Thanks in Advance.

CodePudding user response:

You should first filter your list by using something like :

def low_pass_filter(L, alpha = 0.2):
    filtered_L = []
    for i in range(1,len(L)-1):
        filtered_L.append(L[i-1] * alpha   L[i] * (1-2*alpha)   L[i 1]*alpha)
    return filtered_L

Then you can find the extremums of your list by using :

def extremums(L):
    """
    This function will only find the local extremums where the derivative should be zero
    """
    maximums = {} #index -> value
    minimums = {} #index -> value
    for i in range(1,len(L)-1):
        if  L[i-1] <= L[i] > L[i 1] or L[i-1] < L[i] >= L[i 1]:
            maximums[i] = L[i] #add this value to maximums
        elif L[i-1] >= L[i] < L[i 1] or L[i-1] > L[i] <= L[i 1]:
            minimums[i] = L[i] #add this value to manimums
    return maximums, minimums
    

L = [1, 2, 3, 1, 2, 0, 5, 8, 7]

print("Maximums :",extremums(L)[0])
print("Minimums :",extremums(L)[1])

outputs :

Maximums : {2: 3, 4: 2, 7: 8}
Minimums : {3: 1, 5: 0}
  • If an extremum was in the beginning or the end of the list, it won't be outputed
  • The output is a dictionnary that contains "index->value"

CodePudding user response:

Does this help:

img_list_pix = [218, 225, 224, 224, 224, 220, 217, 215, 216, 216,
                216, 215, 214, 215, 214, 214, 213, 210, 210, 205,
                207, 204, 205, 201, 200, 201, 201, 197, 202, 203,
                196, 196, 196, 196, 198, 198, 199, 202, 202, 205,
                205, 205, 207, 206, 208, 207, 209, 214, 212, 213,
                215, 218, 220, 219, 218, 220, 223, 222, 224, 224,
                224, 222, 225, 222, 222, 222, 222, 223, 223, 221,
                223, 222, 223, 219, 218, 216, 215, 214, 212, 212,
                211, 211, 207, 206, 205, 204, 202, 199, 199, 198,
                197, 195, 193, 191, 191, 193, 191, 196]


def getminmax(L, i):
    _min = min(L[i:])
    _max = max(L[i:])
    return (_min, L[i:].index(_min) i), (_max, L[i:].index(_max) i)


for i in [0, 29, 61, 93]:
    print(getminmax(img_list_pix, i))
  • Related