Finding beginning & end index of two hills in numpy array

Question:

Information

I have this numpy array

[ 23  22  21  22  21  22  23  23  23  21  21  22  22  22  22  22  22  22
  22  22  21  21  22  22  22  22  23  22  22  21  21  20  20  19  19  19
  19  19  19  19  19  20  21  22  23  23  23  22  22  22  22  21  20  20
  19  19  19  19  19  19  19  19  19  19  20  20  21  21  23  24  24  25
  25  26  26  25  24  24  24  24  23  23  23  23  23  22  21  20  19  19
  19  18  18  18  18  18  19  18  18  18  18  18  18  18  18  18  18  17
  17  17  17  17  17  18  19  19  19  19  19  19  19  19  19  19  19  19
  19  19  19  19  19  19  19  18  18  18  17  16  16  16  15  14  15  14
  14  15  15  16  16  16  16  16  15  15  15  14  14  14  14  14  13  13
  13  13  13  13  11  11  10  10  11   9   9   8   9  11  11  12  12  10
  10  10  11  12  12  12  13  15  14  14  12  14  13  18  24  32  40  51
  63  62  68  78  82  90  92  93  95  96 106 112 118 123 123 125 130 134
 136 137 138 140 144 148 153 158 163 163 163 159 161 164 165 163 166 176
 179 184 190 200 207 210 213 214 216 218 221 228 233 236 237 238 238 241
 243 243 243 243 242 236 208 192 177 165 163 160 152 148 143 130 121 113
  94  81  61  53  48  41  31  30  29  28  21  19  13   5   3   7   9   9
  10   9  10  10  10  10  10  10   9  10  10  10  10  10  10  11  11  13
  16  21  22  21  22  22  26  24  22  22  19  19  19  19  19  19  24  32
  40  47  49  58  62  65  63  70  79  82  84  89  93  95  96 101 105 105
 111 111 117 117 117 118 122 123 126 128 133 134 138 141 150 154 157 159
 160 160 160 173 183 201 220 227 241 253 254 266 272 272 271 276 278 280
 284 285 284 271 262 248 247 248 252 253 256 259 266 266 266 258 256 268
 265 271 278 281 288 285 286 277 266 250 240 207 180 137  99  92  82  81
  82  80  81  80  80  80  77  77  78  79  75  73  72  72  71  72  70  71
  73  73  72  70  71  70  69  68  68  70  67  65  64  65  64  63  63  63
  63  63  61  62  62  62  62  63  63  63  63  63  63  63  63  64  61  60
  62  61  60  60  60  60  60  60  59  57  57  55  55  53  52  50  49  47
  46  44  44  42  40  39  37  36  32  30  27  24  25  26  22  18   6   7
   7   8  11  12  17  18  20  20  20  19  20  20  20  19  20  21  22  21
  24  22  22  20  20  19  19  17  17  16  17  15  15  15  14  14  15  13]

enter image description here

Question

I would like to know the index of the beginning & end of the left & right hill. This should be automated so a python function or a math approach to this problem would be very welcome.

Required Result

left = [ 181, 284 ]
right = [ 320, 502 ]

enter image description here

Asked By: Tdemunck

||

Answers:

It sounds like you’re going to want some combination of peak_width https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.peak_widths.html and find_peaks https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.find_peaks.html.

Use find_peaks to get the x position of the peaks, and then use those x position values as input to peak_widths. In peak_widths set the rel_height flag to something in the range of 0.1-0.2 0.95.

EDIT: Changed relative height flag recommendation as per Tdemunck’s comment

Answered By: AtomLaurent
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.