计算 Python 中一维数组中最大点之后每个(右点和左点)的距离

Calculate distance of each (right and left point) after maximum point in 1D-array in Python

我需要帮助,我有一个数组 fft_line_log,谁给我一个图形曲线,我想找到这个数组中最大点之后每个点之间的距离。

import numpy as np
import matplotlib.pyplot as plt

fft_line_log = np.array([ 6.69444503,  6.62028965,  6.71395111,  6.70660448,  6.70821623,
    6.66914818,  6.7160331 ,  6.68638117,  6.75695725,  6.68171623,
    6.73955838,  6.68919801,  6.76332219,  6.78286017,  6.81555002,
    6.75381555,  6.75353165,  6.70909775,  6.74073086,  6.70899932,
    6.6908525 ,  6.59418955,  6.65966783,  6.65627975,  6.75186109,
    6.71053962,  6.77552219,  6.78359504,  6.81006956,  6.76919262,
    6.74450256,  6.74058832,  6.69536545,  6.6519225 ,  6.62621774,
    6.62584858,  6.67337241,  6.6917687 ,  6.75191211,  6.75534094,
    6.72788613,  6.68803099,  6.6613495 ,  6.71848276,  6.75129579,
    6.76199942,  6.73916056,  6.73957135,  6.76513941,  6.72795522,
    6.63711679,  6.55248903,  6.61974385,  6.74794642,  6.88099129,
    6.81151706,  6.7377934 ,  6.65227642,  6.65518817,  6.6474948 ,
    6.67242465,  6.69769372,  6.77683837,  6.71750359,  6.70146931,
    6.69701514,  6.77291463,  6.81000081,  6.73833814,  6.54288474,
    6.64435143,  6.65653662,  6.87412486,  6.77087266,  6.92963172,
    6.859477  ,  6.92261811,  6.90716066,  6.95589443,  6.97304283,
    6.97534776,  6.92405218,  6.8408661 ,  6.83609476,  7.00246089,
    7.05132823,  6.9246729 ,  6.77486571,  6.8026577 ,  6.91282969,
    6.93526752,  6.96253309,  6.98858486,  6.85929044,  6.84243664,
    6.77238593,  6.95158598,  6.91181849,  7.05094391,  6.97864081,
    7.00878382,  6.82880079,  6.87036188,  6.90655258,  6.93307678,
    6.94892631,  6.97922417,  7.0470304 ,  7.15139031,  7.15694858,
    7.1192599 ,  7.05265314,  7.07909791,  7.1168476 ,  7.12643587,
    7.15610456,  7.19698636,  7.09834314,  7.05124444,  7.06147316,
    7.29265349,  7.38762255,  7.19255318,  7.0914197 ,  6.99632408,
    7.28783749,  7.40642556,  7.50042657,  7.52199961,  7.38090861,
    7.31366312,  7.32918037,  7.41356871,  7.40686241,  7.34454923,
    7.35754063,  7.36483637,  7.52229303,  7.45715298,  7.54329464,
    7.35501191,  7.41806418,  7.37864369,  7.3268464 ,  7.20434605,
    6.99586801,  6.93583466,  7.11969473,  7.15938148,  7.44589042,
    7.34313421,  7.30155559,  7.28786179,  7.36995234,  7.3420818 ,
    7.17021967,  7.27735024,  7.37770573,  7.41216788,  7.32619126,
    7.48962489,  7.34112295,  7.35875597,  7.0881354 ,  7.46850535,
    7.47971955,  7.75637784,  7.64789912,  7.60647042,  7.65423208,
    7.75347939,  7.87979044,  7.68933715,  7.70211081,  7.51374563,
    7.57404798,  7.58926237,  7.84621756,  7.99034484,  8.04283399,
    7.97958613,  7.93807446,  7.98169401,  7.8776668 ,  7.73453558,
    7.74164658,  7.97903408,  8.10268243,  8.03542065,  8.10972688,
    8.04294297,  8.05633646,  8.03725912,  8.25983391,  8.38995519,
    8.36962264,  8.17699733,  8.21024941,  8.39719101,  8.62435624,
    8.66466254,  8.53202815,  8.59849915,  8.55345886,  8.7359906 ,
    8.61148309,  8.66471803,  8.58964817,  8.37817649,  8.4487452 ,
    8.29727521,  8.80497468,  9.13774425,  9.23056669,  9.05938092,
    8.92915832,  8.95605045,  9.19371233,  9.21134305,  9.32966008,
    9.44885488,  9.60703095,  9.68599202,  9.73857392,  9.58981375,
    9.69326948,  9.69908055,  9.61247299,  9.59321922,  9.72546618,
    9.82947886,  9.88234468,  9.80105886,  9.90076688,  9.88697706,
    9.93188788, 10.07941562, 10.1883549 , 10.26928882, 10.24549213,
   10.38841197, 10.56229069, 10.73973299, 10.68900618, 10.56318566,
   10.8720509 , 11.10384486, 11.21957039, 11.08780539, 11.1755498 ,
   11.29213701, 11.66953016, 11.85629452, 12.08136708, 12.31552502,
   12.54267264, 12.7436129 , 12.70261375, 12.63184165, 12.3234958 ,
   12.10415375, 11.75131106, 11.67532432, 11.51112149, 11.43912942,
   11.27077754, 11.12796411, 10.98608465, 10.95421553, 10.81817012,
   10.60892879, 10.34879649, 10.24336291, 10.18332674, 10.26584287,
   10.34229355, 10.35252445, 10.21730733, 10.10098059, 10.03168354,
    9.95282456,  9.79382044,  9.71416103,  9.59824455,  9.62126605,
    9.41602153,  9.43508821,  9.30142416,  9.37191569,  9.36895303,
    9.52453799,  9.32715386,  9.1983345 ,  9.06336403,  9.19879466,
    9.06028903,  8.93720321,  8.91570714,  8.91893331,  8.9922376 ,
    9.01113625,  9.14272431,  9.09864042,  9.00019683,  8.70994841,
    8.65760632,  8.71252674,  8.83044602,  8.7077489 ,  8.44856982,
    8.36202746,  8.49380112,  8.43758768,  8.51522822,  8.39732377,
    8.38316112,  8.16890344,  8.13487416,  8.10955735,  8.09696699,
    8.02992363,  8.17105347,  8.18225026,  8.13724563,  8.0722829 ,
    8.25107359,  8.17611697,  8.04802661,  7.80321978,  7.88329972,
    7.93738869,  7.90669758,  8.01759731,  8.04220377,  7.90675838,
    7.70493407,  7.53787673,  7.62073091,  7.52546029,  7.70095934,
    7.84623128,  8.0006942 ,  7.89516578,  7.74835171,  7.64721769,
    7.53282095,  7.64161662,  7.55674742,  7.62838586,  7.48794439,
    7.42979731,  7.47872707,  7.48249818,  7.45731704,  7.27393537,
    7.44293302,  7.50772531,  7.60515245,  7.43210472,  7.44673673,
    7.45097998,  7.47935287,  7.35754501,  7.15439062,  7.13837326,
    7.2174589 ,  7.25192887,  7.18308351,  7.09712443,  7.15393078,
    7.11611744,  6.98504757,  6.96222427,  7.23452273,  7.38547407,
    7.4523931 ,  7.44356096,  7.42744486,  7.40436835,  7.23342905,
    7.09705073,  7.12021193,  7.24969532,  7.23342225,  7.22111859,
    7.11009096,  7.23244932,  7.05444443,  7.11840265,  7.23738078,
    7.238834  ,  7.13244689,  7.02936601,  7.07470322,  7.18216904,
    7.06915501,  7.10676358,  6.99505972,  7.03908845,  7.19477338,
    7.28860964,  7.23505325,  7.09765474,  7.04976001,  7.04160299,
    7.04334479,  7.1015781 ,  7.18696615,  7.09222292,  6.97253299,
    6.93880034,  7.03973312,  7.11494813,  7.03234211,  6.98742809,
    6.77929055,  6.84197639,  6.95866474,  6.98850632,  6.99234258,
    6.80117981,  6.79377084,  6.67441952,  6.71621183,  6.6648103 ,
    6.75270579,  6.73731868,  6.86448484,  6.82328277,  6.74100421,
    6.73419727,  6.71847507,  6.87672801,  6.92205112,  7.02398639,
    6.9724521 ,  6.86443623,  6.76949467,  6.68317052,  6.67938603,
    6.64126311,  6.71279214,  6.77487357,  6.82779673,  6.71670699,
    6.61091739,  6.59593221,  6.69074946,  6.81515364,  6.76440899,
    6.78353573,  6.72608694,  6.7629007 ,  6.74374407,  6.71517095,
    6.67005636,  6.67293791,  6.72497808,  6.76154375,  6.74949199,
    6.70005328,  6.72457262,  6.76483267,  6.78789715,  6.78164388,
    6.7401589 ,  6.78448831,  6.77338756,  6.80087043,  6.75914855,
    6.73225717,  6.70224985,  6.70150418,  6.66315441,  6.67396018,
    6.6377038 ,  6.69045353,  6.68431725,  6.76329228,  6.7355242 ,
    6.70520747,  6.61466211,  6.61972572,  6.64595276,  6.69601337,
    6.70481754,  6.69784698,  6.63233624,  6.64712029,  6.76416368,
    6.73909511,  6.71532065,  6.5486919 ,  6.58517468,  6.61351347,
    6.63310043,  6.62013832,  6.62029447,  6.61951258,  6.6607056 ,
    6.6548232 ,  6.6497125 ,  6.69929709,  6.68842114,  6.74815417,
    6.73749318,  6.75484044,  6.71556177,  6.56280052,  6.48748225,
    6.49914832,  6.5760775 ])

pt_max=np.max(fft_line_log)
ecart=np.array([])

for step in np.arange(pt_max,pt_max-4,-0.1):
    print("step= "+str(step))   
    
    e=np.where((fft_line_log<step)&(fft_line_log>step-0.1))
    ecart=np.append(ecart,e[0][-1]-e[0][0])
                
plt.figure(33)
plt.plot(ecart,'*-')

预览:

]

这应该可以解决问题:

import numpy as np
import matplotlib.pyplot as plt

array = np.array([ 6.69444503, 6.62028965, 6.71395111, 6.70660448, 6.70821623, 6.66914818, 6.7160331 , 6.68638117, 6.75695725, 6.68171623, 6.73955838, 6.68919801, 6.76332219, 6.78286017, 6.81555002, 6.75381555, 6.75353165, 6.70909775, 6.74073086, 6.70899932, 6.6908525 , 6.59418955, 6.65966783, 6.65627975, 6.75186109, 6.71053962, 6.77552219, 6.78359504, 6.81006956, 6.76919262, 6.74450256, 6.74058832, 6.69536545, 6.6519225 , 6.62621774, 6.62584858, 6.67337241, 6.6917687 , 6.75191211, 6.75534094, 6.72788613, 6.68803099, 6.6613495 , 6.71848276, 6.75129579, 6.76199942, 6.73916056, 6.73957135, 6.76513941, 6.72795522, 6.63711679, 6.55248903, 6.61974385, 6.74794642, 6.88099129, 6.81151706, 6.7377934 , 6.65227642, 6.65518817, 6.6474948 , 6.67242465, 6.69769372, 6.77683837, 6.71750359, 6.70146931, 6.69701514, 6.77291463, 6.81000081, 6.73833814, 6.54288474, 6.64435143, 6.65653662, 6.87412486, 6.77087266, 6.92963172, 6.859477 , 6.92261811, 6.90716066, 6.95589443, 6.97304283, 6.97534776, 6.92405218, 6.8408661 , 6.83609476, 7.00246089, 7.05132823, 6.9246729 , 6.77486571, 6.8026577 , 6.91282969, 6.93526752, 6.96253309, 6.98858486, 6.85929044, 6.84243664, 6.77238593, 6.95158598, 6.91181849, 7.05094391, 6.97864081, 7.00878382, 6.82880079, 6.87036188, 6.90655258, 6.93307678, 6.94892631, 6.97922417, 7.0470304 , 7.15139031, 7.15694858, 7.1192599 , 7.05265314, 7.07909791, 7.1168476 , 7.12643587, 7.15610456, 7.19698636, 7.09834314, 7.05124444, 7.06147316, 7.29265349, 7.38762255, 7.19255318, 7.0914197 , 6.99632408, 7.28783749, 7.40642556, 7.50042657, 7.52199961, 7.38090861, 7.31366312, 7.32918037, 7.41356871, 7.40686241, 7.34454923, 7.35754063, 7.36483637, 7.52229303, 7.45715298, 7.54329464, 7.35501191, 7.41806418, 7.37864369, 7.3268464 , 7.20434605, 6.99586801, 6.93583466, 7.11969473, 7.15938148, 7.44589042, 7.34313421, 7.30155559, 7.28786179, 7.36995234, 7.3420818 , 7.17021967, 7.27735024, 7.37770573, 7.41216788, 7.32619126, 7.48962489, 7.34112295, 7.35875597, 7.0881354 , 7.46850535, 7.47971955, 7.75637784, 7.64789912, 7.60647042, 7.65423208, 7.75347939, 7.87979044, 7.68933715, 7.70211081, 7.51374563, 7.57404798, 7.58926237, 7.84621756, 7.99034484, 8.04283399, 7.97958613, 7.93807446, 7.98169401, 7.8776668 , 7.73453558, 7.74164658, 7.97903408, 8.10268243, 8.03542065, 8.10972688, 8.04294297, 8.05633646, 8.03725912, 8.25983391, 8.38995519, 8.36962264, 8.17699733, 8.21024941, 8.39719101, 8.62435624, 8.66466254, 8.53202815, 8.59849915, 8.55345886, 8.7359906 , 8.61148309, 8.66471803, 8.58964817, 8.37817649, 8.4487452 , 8.29727521, 8.80497468, 9.13774425, 9.23056669, 9.05938092, 8.92915832, 8.95605045, 9.19371233, 9.21134305, 9.32966008, 9.44885488, 9.60703095, 9.68599202, 9.73857392, 9.58981375, 9.69326948, 9.69908055, 9.61247299, 9.59321922, 9.72546618, 9.82947886, 9.88234468, 9.80105886, 9.90076688, 9.88697706, 9.93188788, 10.07941562, 10.1883549 , 10.26928882, 10.24549213, 10.38841197, 10.56229069, 10.73973299, 10.68900618, 10.56318566, 10.8720509 , 11.10384486, 11.21957039, 11.08780539, 11.1755498 , 11.29213701, 11.66953016, 11.85629452, 12.08136708, 12.31552502, 12.54267264, 12.7436129 , 12.70261375, 12.63184165, 12.3234958 , 12.10415375, 11.75131106, 11.67532432, 11.51112149, 11.43912942, 11.27077754, 11.12796411, 10.98608465, 10.95421553, 10.81817012, 10.60892879, 10.34879649, 10.24336291, 10.18332674, 10.26584287, 10.34229355, 10.35252445, 10.21730733, 10.10098059, 10.03168354, 9.95282456, 9.79382044, 9.71416103, 9.59824455, 9.62126605, 9.41602153, 9.43508821, 9.30142416, 9.37191569, 9.36895303, 9.52453799, 9.32715386, 9.1983345 , 9.06336403, 9.19879466, 9.06028903, 8.93720321, 8.91570714, 8.91893331, 8.9922376 , 9.01113625, 9.14272431, 9.09864042, 9.00019683, 8.70994841, 8.65760632, 8.71252674, 8.83044602, 8.7077489 , 8.44856982, 8.36202746, 8.49380112, 8.43758768, 8.51522822, 8.39732377, 8.38316112, 8.16890344, 8.13487416, 8.10955735, 8.09696699, 8.02992363, 8.17105347, 8.18225026, 8.13724563, 8.0722829 , 8.25107359, 8.17611697, 8.04802661, 7.80321978, 7.88329972, 7.93738869, 7.90669758, 8.01759731, 8.04220377, 7.90675838, 7.70493407, 7.53787673, 7.62073091, 7.52546029, 7.70095934, 7.84623128, 8.0006942 , 7.89516578, 7.74835171, 7.64721769, 7.53282095, 7.64161662, 7.55674742, 7.62838586, 7.48794439, 7.42979731, 7.47872707, 7.48249818, 7.45731704, 7.27393537, 7.44293302, 7.50772531, 7.60515245, 7.43210472, 7.44673673, 7.45097998, 7.47935287, 7.35754501, 7.15439062, 7.13837326, 7.2174589 , 7.25192887, 7.18308351, 7.09712443, 7.15393078, 7.11611744, 6.98504757, 6.96222427, 7.23452273, 7.38547407, 7.4523931 , 7.44356096, 7.42744486, 7.40436835, 7.23342905, 7.09705073, 7.12021193, 7.24969532, 7.23342225, 7.22111859, 7.11009096, 7.23244932, 7.05444443, 7.11840265, 7.23738078, 7.238834 , 7.13244689, 7.02936601, 7.07470322, 7.18216904, 7.06915501, 7.10676358, 6.99505972, 7.03908845, 7.19477338, 7.28860964, 7.23505325, 7.09765474, 7.04976001, 7.04160299, 7.04334479, 7.1015781 , 7.18696615, 7.09222292, 6.97253299, 6.93880034, 7.03973312, 7.11494813, 7.03234211, 6.98742809, 6.77929055, 6.84197639, 6.95866474, 6.98850632, 6.99234258, 6.80117981, 6.79377084, 6.67441952, 6.71621183, 6.6648103 , 6.75270579, 6.73731868, 6.86448484, 6.82328277, 6.74100421, 6.73419727, 6.71847507, 6.87672801, 6.92205112, 7.02398639, 6.9724521 , 6.86443623, 6.76949467, 6.68317052, 6.67938603, 6.64126311, 6.71279214, 6.77487357, 6.82779673, 6.71670699, 6.61091739, 6.59593221, 6.69074946, 6.81515364, 6.76440899, 6.78353573, 6.72608694, 6.7629007 , 6.74374407, 6.71517095, 6.67005636, 6.67293791, 6.72497808, 6.76154375, 6.74949199, 6.70005328, 6.72457262, 6.76483267, 6.78789715, 6.78164388, 6.7401589 , 6.78448831, 6.77338756, 6.80087043, 6.75914855, 6.73225717, 6.70224985, 6.70150418, 6.66315441, 6.67396018, 6.6377038 , 6.69045353, 6.68431725, 6.76329228, 6.7355242 , 6.70520747, 6.61466211, 6.61972572, 6.64595276, 6.69601337, 6.70481754, 6.69784698, 6.63233624, 6.64712029, 6.76416368, 6.73909511, 6.71532065, 6.5486919 , 6.58517468, 6.61351347, 6.63310043, 6.62013832, 6.62029447, 6.61951258, 6.6607056 , 6.6548232 , 6.6497125 , 6.69929709, 6.68842114, 6.74815417, 6.73749318, 6.75484044, 6.71556177, 6.56280052, 6.48748225, 6.49914832, 6.5760775 ])

ptMax = np.argmax(array), np.max(array)
nbPairs = min(ptMax[0], len(array) - ptMax[0])

for i in range(1, nbPairs):
    xDist = 2*i
    yDist = array[ptMax[0]+i] - array[ptMax[0]-i]
    # print(xDist, yDist)
    plt.plot([ptMax[0] - i, ptMax[0] - i + xDist], [array[ptMax[0]-i], array[ptMax[0]-i] + yDist], color="black")

plt.plot(array, '+', markersize=6)
plt.plot(ptMax[0], ptMax[1], 'ro')
plt.show()

您只需要注意点在数组中的顺序是否正确。

结果: