title: Find the highest peak between width range and window integral
body: <p>I have frequency and PSD arrays which I want to find the highest peak within a certain Hz range. From the highest peak, constructing a window of 0.015 Hz and integral the area.</p>
  
  <p><strong>Find Peak and Integral</strong></p>
  
  <pre><code>freq=np.array([0.0, 0.0009765625, 0.001953125, 0.0029296875, 0.00390625, 0.0048828125, 0.005859375, 0.0068359375, 0.0078125, 0.0087890625, 0.009765625, 0.0107421875, 0.01171875, 0.0126953125, 0.013671875, 0.0146484375, 0.015625, 0.0166015625, 0.017578125, 0.0185546875, 0.01953125, 0.0205078125, 0.021484375, 0.0224609375, 0.0234375, 0.0244140625, 0.025390625, 0.0263671875, 0.02734375, 0.0283203125, 0.029296875, 0.0302734375, 0.03125, 0.0322265625, 0.033203125, 0.0341796875, 0.03515625, 0.0361328125, 0.037109375, 0.0380859375, 0.0390625, 0.0400390625, 0.041015625, 0.0419921875, 0.04296875, 0.0439453125, 0.044921875, 0.0458984375, 0.046875, 0.0478515625, 0.048828125, 0.0498046875, 0.05078125, 0.0517578125, 0.052734375, 0.0537109375, 0.0546875, 0.0556640625, 0.056640625, 0.0576171875, 0.05859375, 0.0595703125, 0.060546875, 0.0615234375, 0.0625, 0.0634765625, 0.064453125, 0.0654296875, 0.06640625, 0.0673828125, 0.068359375, 0.0693359375, 0.0703125, 0.0712890625, 0.072265625, 0.0732421875, 0.07421875, 0.0751953125, 0.076171875, 0.0771484375, 0.078125, 0.0791015625, 0.080078125, 0.0810546875, 0.08203125, 0.0830078125, 0.083984375, 0.0849609375, 0.0859375, 0.0869140625, 0.087890625, 0.0888671875, 0.08984375, 0.0908203125, 0.091796875, 0.0927734375, 0.09375, 0.0947265625, 0.095703125, 0.0966796875, 0.09765625, 0.0986328125, 0.099609375, 0.1005859375, 0.1015625, 0.1025390625, 0.103515625, 0.1044921875, 0.10546875, 0.1064453125, 0.107421875, 0.1083984375, 0.109375, 0.1103515625, 0.111328125, 0.1123046875, 0.11328125, 0.1142578125, 0.115234375, 0.1162109375, 0.1171875, 0.1181640625, 0.119140625, 0.1201171875, 0.12109375, 0.1220703125, 0.123046875, 0.1240234375, 0.125, 0.1259765625, 0.126953125, 0.1279296875, 0.12890625, 0.1298828125, 0.130859375, 0.1318359375, 0.1328125, 0.1337890625, 0.134765625, 0.1357421875, 0.13671875, 0.1376953125, 0.138671875, 0.1396484375, 0.140625, 0.1416015625, 0.142578125, 0.1435546875, 0.14453125, 0.1455078125, 0.146484375, 0.1474609375, 0.1484375, 0.1494140625, 0.150390625, 0.1513671875, 0.15234375, 0.1533203125, 0.154296875, 0.1552734375, 0.15625, 0.1572265625, 0.158203125, 0.1591796875, 0.16015625, 0.1611328125, 0.162109375, 0.1630859375, 0.1640625, 0.1650390625, 0.166015625, 0.1669921875, 0.16796875, 0.1689453125, 0.169921875, 0.1708984375, 0.171875, 0.1728515625, 0.173828125, 0.1748046875, 0.17578125, 0.1767578125, 0.177734375, 0.1787109375, 0.1796875, 0.1806640625, 0.181640625, 0.1826171875, 0.18359375, 0.1845703125, 0.185546875, 0.1865234375, 0.1875, 0.1884765625, 0.189453125, 0.1904296875, 0.19140625, 0.1923828125, 0.193359375, 0.1943359375, 0.1953125, 0.1962890625, 0.197265625, 0.1982421875, 0.19921875, 0.2001953125, 0.201171875, 0.2021484375, 0.203125, 0.2041015625, 0.205078125, 0.2060546875, 0.20703125, 0.2080078125, 0.208984375, 0.2099609375, 0.2109375, 0.2119140625, 0.212890625, 0.2138671875, 0.21484375, 0.2158203125, 0.216796875, 0.2177734375, 0.21875, 0.2197265625, 0.220703125, 0.2216796875, 0.22265625, 0.2236328125, 0.224609375, 0.2255859375, 0.2265625, 0.2275390625, 0.228515625, 0.2294921875, 0.23046875, 0.2314453125, 0.232421875, 0.2333984375, 0.234375, 0.2353515625, 0.236328125, 0.2373046875, 0.23828125, 0.2392578125, 0.240234375, 0.2412109375, 0.2421875, 0.2431640625, 0.244140625, 0.2451171875, 0.24609375, 0.2470703125, 0.248046875, 0.2490234375, 0.25, 0.2509765625, 0.251953125, 0.2529296875, 0.25390625, 0.2548828125, 0.255859375, 0.2568359375, 0.2578125, 0.2587890625, 0.259765625, 0.2607421875, 0.26171875, 0.2626953125, 0.263671875, 0.2646484375, 0.265625, 0.2666015625, 0.267578125, 0.2685546875, 0.26953125, 0.2705078125, 0.271484375, 0.2724609375, 0.2734375, 0.2744140625, 0.275390625, 0.2763671875, 0.27734375, 0.2783203125, 0.279296875, 0.2802734375, 0.28125, 0.2822265625, 0.283203125, 0.2841796875, 0.28515625, 0.2861328125, 0.287109375, 0.2880859375, 0.2890625, 0.2900390625, 0.291015625, 0.2919921875, 0.29296875, 0.2939453125, 0.294921875, 0.2958984375, 0.296875, 0.2978515625, 0.298828125, 0.2998046875, 0.30078125, 0.3017578125, 0.302734375, 0.3037109375, 0.3046875, 0.3056640625, 0.306640625, 0.3076171875, 0.30859375, 0.3095703125, 0.310546875, 0.3115234375, 0.3125, 0.3134765625, 0.314453125, 0.3154296875, 0.31640625, 0.3173828125, 0.318359375, 0.3193359375, 0.3203125, 0.3212890625, 0.322265625, 0.3232421875, 0.32421875, 0.3251953125, 0.326171875, 0.3271484375, 0.328125, 0.3291015625, 0.330078125, 0.3310546875, 0.33203125, 0.3330078125, 0.333984375, 0.3349609375, 0.3359375, 0.3369140625, 0.337890625, 0.3388671875, 0.33984375, 0.3408203125, 0.341796875, 0.3427734375, 0.34375, 0.3447265625, 0.345703125, 0.3466796875, 0.34765625, 0.3486328125, 0.349609375, 0.3505859375, 0.3515625, 0.3525390625, 0.353515625, 0.3544921875, 0.35546875, 0.3564453125, 0.357421875])
  psd=np.array([2117.8273051302376, 4236.140291493467, 4237.59724589046, 4240.0252055507635, 4243.423723990633, 4247.792176169334, 4253.129758525505, 4259.43548902374, 4266.708207211219, 4274.946574284208, 4284.149073164268, 4294.314008583894, 4305.439507181355, 4317.523517604426, 4330.563810622718, 4344.557979248252, 4359.503438863938, 4375.397427359547, 4392.237005274798, 4410.019055949134, 4428.740285677746, 4448.397223873376, 4468.986223233491, 4490.503459912274, 4512.944933697013, 4536.306468188381, 4560.583710984068, 4585.772133865365, 4611.867032986107, 4638.863529063539, 4666.756567570628, 4695.540918929281, 4725.211178704107, 4755.761767796138, 4787.186932636233, 4819.480745377575, 4852.637104087019, 4886.649732934814, 4921.512182382421, 4957.2178293680645, 4993.75987748977, 5031.131357185633, 5069.325125911084, 5108.333868313019, 5148.150096400635, 5188.766149712915, 5230.174195482698, 5272.3662287973875, 5315.3340727562945, 5359.069378624859, 5403.563625985769, 5448.808122887352, 5494.794005989443, 5541.512240707146, 5588.953621352853, 5637.108771277072, 5685.968143008554, 5735.522018394388, 5785.760508740738, 5836.673554954971, 5888.250927690054, 5940.482227492049, 5993.356884951789, 6046.864160861696, 6100.993146378917, 6155.732763195987, 6211.071763720252, 6266.998731263484, 6323.502080242999, 6380.570056395907, 6438.190737007983, 6496.352031158834, 6555.041679985071, 6614.247256963332, 6673.9561682149115, 6734.15565283404, 6794.832783241735, 6855.974465567324, 6917.567440059729, 6979.59828153076, 7042.0533998326055, 7104.919040371838, 7168.181284662325, 7231.826050919431, 7295.8390946979425, 7360.206009576277, 7424.91222788951, 7489.9430215137345, 7555.283502704499, 7620.918624991892, 7686.83318413497, 7753.0118191382635, 7819.43901333309, 7886.099095526382, 7952.976241219845, 8020.054473902169, 8087.317666417102, 8154.749542410103, 8222.333677856454, 8290.053502673405, 8357.892302419297, 8425.833220082226, 8493.85925796106, 8561.953279641362, 8630.098012068998, 8698.276047723873, 8766.469846896473, 8834.661740069614, 8902.833930407936, 8970.968496357436, 9039.047394357443, 9107.05246166722, 9174.96541930941, 9242.767875132417, 9310.441326993718, 9377.967166066008, 9445.326680268068, 9512.501057821986, 9579.471390938439, 9646.218679631478, 9712.72383566423, 9778.967686626835, 9844.930980147667, 9910.59438823897, 9975.938511777736, 10040.943885122573, 10105.590980867222, 10169.860214731156, 10233.731950587562, 10297.186505628928, 10360.204155670208, 10422.765140589436, 10484.849669905441, 10546.43792849227, 10607.510082429611, 10668.046284988406, 10728.02668275076, 10787.431421862855, 10846.240654419666, 10904.434544979895, 10961.993277209445, 11018.897060651627, 11075.126137621917, 11130.660790225245, 11185.481347493138, 11239.56819263836, 11292.901770424143, 11345.462594645065, 11397.231255716446, 11448.18842836896, 11498.314879444895, 11547.591475792453, 11595.999192254161, 11643.519119745413, 11690.13247341888, 11735.820600910438, 11780.564990662067, 11824.347280316962, 11867.149265182015, 11908.952906752595, 11949.740341294459, 11989.493888477404, 12028.196060055176, 12065.829568585987, 12102.377336187863, 12137.822503322863, 12172.148437604206, 12205.338742619902, 12237.377266766951, 12268.248112089314, 12297.935643113355, 12326.424495674115, 12353.699585725537, 12379.74611812801, 12404.549595406235, 12428.095826470408, 12450.3709352937, 12471.361369538983, 12491.053909127475, 12509.435674742223, 12526.49413625906, 12542.217121097825, 12556.59282248638, 12569.609807630239, 12581.257025780276, 12591.523816191331, 12600.399915964148, 12607.875467763452, 12613.941027404748, 12618.587571302576, 12621.806503772925, 12623.589664182578, 12623.92933393827, 12622.81824330844, 12620.249578070656, 12616.216985977637, 12610.71458303502, 12603.736959584136, 12595.279186183037, 12585.33681927916, 12573.905906667287, 12560.982992726325, 12546.56512342876, 12530.6498511167, 12513.235239038597, 12494.319865640842, 12473.902828608632, 12451.983748650691, 12428.562773022584, 12403.640578783443, 12377.218375781365, 12349.297909362664, 12319.881462800484, 12288.971859438554, 12256.572464545938, 12222.687186878935, 12187.320479946487, 12150.477342975766, 12112.16332157456, 12072.384508087836, 12031.147541645474, 11988.459607898922, 11944.328438444576, 11898.762309931815, 11851.770042854136, 11803.361000021965, 11753.545084715939, 11702.332738519755, 11649.734938832122, 11595.76319605723, 11540.42955047392, 11483.746568783563, 11425.727340337262, 11366.38547304298, 11305.735088953863, 11243.790819538764, 11180.567800636869, 11116.081667098035, 11050.348547111098, 10983.385056222602, 10915.208291048511, 10845.835822681987, 10775.285689800387, 10703.576391474995, 10630.726879687287, 10556.756551555709, 10481.685241277257, 10405.533211788425, 10328.321146150294, 10250.070138662817, 10170.801685713559, 10090.537676366457, 10009.300382696316, 9927.112449875054, 9843.996886015937, 9759.97705178216, 9675.07664976651, 9589.319713648907, 9502.730597138887, 9415.333962710305, 9327.154770135674, 9238.218264827734, 9148.549965996126, 9058.175654627017, 8967.121361293937, 8875.413353807926, 8783.078124715606, 8690.14237865355, 8596.633019567777, 8502.577137807053, 8408.00199709902, 8312.93502141808, 8217.403781754238, 8121.435982792007, 8025.059449508735, 7928.302113701657, 7831.1920004531075, 7733.757214543326, 7636.025926820366, 7538.026360536673, 7439.786777661854, 7341.335465181225, 7242.700721389741, 7143.910842190874, 7044.994107410017, 6945.978767131936, 6846.893028071855, 6747.765039989605, 6648.622882156285, 6549.494549882854, 6450.407941119925, 6351.390843138042, 6252.470919297591, 6153.675695917459, 6055.032549251374, 5956.56869258081, 5858.311163433282, 5760.286810934598, 5662.522283303618, 5565.044015497904, 5467.87821701848, 5371.050859881796, 5274.587666766838, 5178.514099345106, 5082.855346801102, 4987.636314550716, 4892.881613164738, 4798.6155475045625, 4704.862106076914, 4611.64495061424, 4518.987405887232, 4426.912449755688, 4335.442703463726, 4244.600422185165, 4154.407485824646, 4064.885390079761, 3976.0552377694416, 3887.937730433306, 3800.553160206767, 3713.921401976139, 3628.0619058180105, 3542.9936897266894, 3458.7353326334432, 3375.304967720854, 3292.720276035498, 3210.9984804017736, 3130.156339639574, 3050.2101430880944, 2971.1757054379686, 2893.0683618735297, 2815.9029635268207, 2739.693873244689, 2664.4549616700324, 2590.1996036380365, 2516.9406748879455, 2444.690549090702, 2373.4610951925006, 2303.263675074042, 2234.109141525068, 2166.0078365334807, 2098.969589888064, 2033.003718093685, 1968.1190235974905, 1904.323794324486, 1841.6258035205565, 1780.032309900832, 1719.55005810103, 1660.1852794291933, 1601.943692915033, 1544.8305066538571, 1488.8504194418663, 1434.0076226993917, 1380.3058026784188, 1327.7481429506104, 1276.3373271717776, 1226.0755421186095, 1176.9644809932681, 1129.0053469913032, 1082.1988571281443, 1036.545246319275, 992.0442717090511, 948.6952172429552, 906.4968984779233, 865.4476676252818, 825.5454188206523, 786.7875936150785, 749.1711866815149])
  peaks, _ = find_peaks(psd)
  intervalPeak = []
  for peak in peaks:
      f = freq[peak]
      if f &gt;= 0.04 and f &lt;= 0.26:
          intervalPeak.append(peak)
  
  print('intervalPeak: ', intervalPeak)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/aizp4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aizp4.png" alt="Find the highest peak and integral"></a></p>
  
  <p><strong>Problems</strong></p>
  
  <blockquote>
    <p>(Solved by @tstanisl) Find peak by using a list of PSD and find interval later.</p>
  </blockquote>
  
  <hr>
  
  <p><strong>My Solution</strong></p>
  
  <pre><code>hzWindow = 0.015/2
  peaks, _ = find_peaks(psd)
  
  intervalPeak = []
  totalPeakPower = 0;
  for peakIndex in peaks:
      f = freq[peakIndex]
      if f &gt;= 0.04 and f &lt;= 0.26:
          beginFreq = freq[peakIndex] - hzWindow;
          endFreq = freq[peakIndex] + hzWindow;
          rangePeakIndex = np.where(np.logical_and(freq&gt;=beginFreq, freq&lt;=endFreq))
          rangePeakY = []
          rangePeakX = []
          for i in rangePeakIndex:
              rangePeakY.append(psd[i])
              rangePeakX.append(freq[i])
          peakPower = np.trapz(rangePeakY, x=rangePeakX)[0]
          totalPeakPower = totalPeakPower + peakPower
  </code></pre>