1

I'm sorry if i'm butchering the question but essentially i have 3 arrays. One array which is the signal data, and an array of peak indexes and an array of valley indexes. The problem however is that sometimes there will be multiple indexes for a peak in a row before there is a valley index. My goal is that for situations like this, i only want the index of the most extreme event.

For this particular problem you can assume i already have a method of picking peaks, i just need to find a way to select the best peak from already selected peak candidates. For example, the function needs to take these index arrays as arguments else it is kind of missing the point of my particular question.

For example if let's say my signal array is [1,2,0,1,0,-1,-2,0,2,1]

Then my peak indexes array is [0,1,3,8,9] My valley indexes array is [5,4]

Ideally i'd want my output to be [1,8] for the peak array, and [6] for the valley array.

I'm thinking i'd want to somehow transpose the indexes, replace with actual values, merge them, but use some sort of delimiter for when a peak group goes to a valley group to seperate groups. Then maybe do an argmax on the groups to get the best index.

Also computation speed is factor here, so maybe something in numpy would be best?

Edit: here is example data i want this to work on

arr = np.array([7441.5, 7445.12, 7446.17, 7441.81, 7441.56, 7441.2, 7440.33, 7446.93, 7448.07, 7442.92, 7448.42, 7450.12, 7460.35, 7456.22, 7450.71, 7449.74, 7445.41, 7447.37, 7444.9, 7442.76, 7440.11, 7445.36, 7440.48, 7443.32, 7443.03, 7443.97, 7445.42, 7444.48, 7442.95, 7439.68, 7441.68, 7443.33, 7444.03, 7447.52, 7443.99, 7443.47, 7444.29, 7448.01, 7447.5, 7451.22, 7450.66, 7451.64, 7450.49, 7449.06, 7447.65, 7451.92, 7450.0, 7451.57, 7450.64, 7450.0, 7448.27, 7448.64, 7448.19, 7445.09, 7442.06, 7443.49, 7440.62, 7438.81, 7438.92, 7443.25, 7443.49, 7445.81, 7440.87, 7442.09, 7441.48, 7443.05, 7445.15, 7443.78, 7442.85, 7444.33, 7443.58, 7446.96, 7454.49, 7453.25, 7453.93, 7452.53, 7448.32, 7448.84, 7448.66, 7450.12, 7448.78, 7447.28, 7447.81, 7447.54, 7449.03, 7453.83, 7457.14, 7457.3, 7456.27, 7456.87, 7459.0, 7462.51, 7463.2, 7464.8, 7462.19, 7458.84, 7461.06, 7462.22, 7458.29, 7456.01, 7456.39, 7452.45, 7451.59, 7450.89, 7448.07, 7447.12, 7448.3, 7448.51, 7451.03, 7449.42, 7451.51, 7451.01, 7450.64, 7451.77, 7451.62, 7456.78, 7455.0, 7456.56, 7456.02, 7456.87, 7457.06, 7456.12, 7454.63, 7454.1, 7456.45, 7457.54, 7458.9, 7462.79, 7464.2, 7464.9, 7465.66, 7466.22, 7463.78, 7467.25, 7468.45, 7470.34, 7472.76, 7469.48, 7468.66, 7461.69, 7459.6, 7455.01, 7453.74, 7457.08, 7454.99, 7455.0, 7454.07, 7451.0, 7447.45, 7449.14, 7447.6, 7448.79, 7456.93, 7452.14, 7451.43, 7451.71, 7450.16, 7456.18, 7453.77, 7455.0, 7455.73, 7461.42, 7464.22, 7464.39, 7468.93, 7464.26, 7460.81, 7459.01, 7455.22, 7455.86, 7454.82, 7452.06, 7448.3, 7448.0, 7447.06, 7447.66, 7449.25, 7449.99, 7450.78, 7451.0, 7449.13, 7447.86, 7446.52, 7445.18, 7447.52, 7445.63, 7445.53, 7444.61, 7444.1, 7443.06, 7442.24, 7449.86, 7451.68, 7449.78, 7456.82, 7452.73, 7450.21, 7451.17, 7447.99, 7447.58, 7456.91, 7454.96, 7455.0, 7452.01, 7446.36, 7450.3, 7450.66, 7457.03, 7452.5, 7456.81, 7454.02, 7455.25, 7450.81, 7447.67, 7450.42, 7453.24, 7451.37, 7447.56, 7443.17, 7444.71, 7449.08, 7447.15, 7446.78, 7444.62, 7449.17, 7447.11, 7447.43, 7446.39, 7445.45, 7446.13, 7448.75, 7446.45, 7447.36, 7446.71, 7450.9, 7448.62, 7453.63, 7451.32, 7452.14, 7451.62, 7451.27, 7448.85, 7447.09, 7447.95, 7447.59, 7446.91, 7446.23, 7450.8, 7459.14, 7455.11, 7450.01, 7450.68, 7453.6, 7456.19, 7456.5, 7456.12, 7449.64, 7454.07, 7453.06, 7451.14, 7450.35, 7452.72, 7454.74, 7456.4, 7455.68, 7461.65, 7465.55, 7464.14, 7467.1, 7469.03, 7468.09, 7462.62, 7461.85, 7465.76, 7467.41, 7471.29, 7494.03, 7482.29, 7507.32, 7512.99, 7518.08, 7514.59, 7499.95, 7502.13, 7502.36, 7492.58, 7484.07, 7490.81, 7485.87, 7492.0, 7503.42, 7508.07, 7518.99, 7537.78, 7526.03, 7540.8, 7565.91, 7575.0, 7548.14, 7566.46, 7587.75, 7569.83, 7590.92, 7586.58, 7594.67, 7573.29, 7577.17, 7583.92, 7588.55, 7586.7, 7579.3, 7575.32, 7568.09, 7579.52, 7588.03, 7583.27, 7584.99, 7590.74, 7600.96, 7597.08, 7593.53, 7604.15, 7611.99, 7613.49, 7633.72, 7620.0, 7624.71, 7618.0, 7611.25, 7618.86, 7613.47, 7605.01, 7600.88, 7607.51, 7604.82, 7610.15, 7611.83, 7608.05, 7597.32, 7600.03, 7602.1, 7598.03, 7592.9, 7601.34, 7598.72, 7589.16, 7592.57, 7586.38, 7587.19, 7594.99, 7595.58, 7594.28, 7592.7, 7594.52, 7588.63, 7592.0, 7590.53, 7593.77, 7593.68, 7587.04, 7586.54, 7584.22, 7585.25, 7583.78, 7577.43, 7574.7, 7568.03, 7581.1, 7588.0, 7591.91, 7596.84, 7601.0, 7604.74, 7605.64, 7599.75, 7595.06, 7606.26, 7615.0, 7610.27, 7613.05, 7610.5, 7608.57, 7614.09, 7602.72, 7600.02, 7605.85, 7613.8, 7605.11, 7607.28, 7609.91, 7608.84, 7610.44, 7611.0, 7627.0, 7625.52, 7630.95, 7631.58, 7622.78, 7631.74, 7630.12, 7636.2, 7641.72, 7641.22, 7631.24, 7634.83, 7629.6, 7628.88, 7622.34, 7623.94, 7621.13, 7621.12, 7622.66, 7624.69, 7620.02, 7621.7, 7616.28, 7619.33, 7617.79, 7618.99, 7626.87, 7631.58, 7634.12, 7636.67, 7632.5, 7629.6, 7625.02, 7623.06, 7619.04, 7633.73, 7631.13, 7629.81, 7624.33, 7629.22, 7635.73, 7632.38, 7627.79, 7612.69, 7618.51, 7617.99, 7622.68, 7617.61, 7616.49, 7614.0, 7618.02, 7624.99, 7624.11, 7623.68, 7618.75, 7620.05, 7619.89, 7618.89, 7625.29, 7624.36, 7625.39, 7623.69, 7623.75, 7629.52, 7630.17, 7629.06, 7628.95, 7631.44, 7633.58, 7635.21, 7633.25, 7631.77, 7630.02, 7625.27, 7625.07, 7627.5, 7628.34, 7629.85, 7632.71, 7633.0, 7633.06, 7638.81, 7644.12, 7660.0, 7655.18, 7664.29, 7670.01, 7667.35, 7668.54, 7682.48, 7679.2, 7669.93, 7679.79, 7676.21, 7668.34, 7671.45, 7678.32, 7686.23, 7687.47, 7681.56, 7670.71, 7681.12, 7675.63, 7665.06, 7653.43, 7653.23, 7658.87, 7664.59, 7668.87, 7664.52, 7663.67, 7669.31, 7666.76, 7663.56, 7659.05, 7661.13, 7660.02, 7663.35, 7657.97, 7654.0, 7646.29, 7646.19, 7646.0, 7650.0, 7643.65, 7652.91, 7658.65, 7655.71, 7662.92, 7667.63, 7670.65, 7666.03, 7659.09, 7660.0, 7664.31, 7662.66, 7663.63, 7662.33, 7665.96, 7670.49, 7676.31, 7665.48, 7663.99, 7658.61, 7664.22, 7658.9])
peaks = np.array([1, 2, 7, 8, 10, 11, 12, 13, 14, 15, 17, 39, 40, 41, 42, 43, 45, 46, 47, 48, 49, 72, 73, 74, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 90, 91, 92, 93, 94, 95, 96, 97, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 143, 152, 157, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 194, 200, 201, 202, 207, 208, 209, 210, 211, 215, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 194, 278, 279, 280, 281, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 280, 293, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 343, 344, 318, 319, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 377, 379, 382, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 411, 412, 419, 420, 421, 422, 423, 424, 428, 429, 430, 432, 433, 434, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 420, 421, 422, 423, 428, 433, 476, 477, 478, 479, 480, 481, 482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494, 495, 496, 500, 501, 502, 503, 504, 505, 506, 523, 533])
valleys = np.array([5, 6, 19, 20, 22, 23, 24, 28, 29, 30, 31, 32, 34, 35, 36, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 76, 77, 78, 80, 81, 82, 83, 84, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 123, 141, 142, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 77, 78, 79, 80, 81, 82, 83, 84, 85, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 147, 148, 149, 150, 151, 154, 155, 156, 172, 173, 174, 175, 176, 177, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 198, 199, 204, 213, 217, 218, 219, 221, 222, 223, 225, 226, 227, 228, 229, 213, 214, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 277, 286, 287, 288, 289, 242, 243, 244, 245, 246, 247, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 277, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 309, 310, 311, 312, 313, 314, 315, 316, 342, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 374, 375, 383, 384, 385, 387, 388, 436, 437, 438, 440, 441, 442, 443, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 385, 387, 388, 389, 390, 391, 392, 415, 416, 417, 418, 426, 427, 431, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468, 469, 470, 471, 472, 473, 474, 512, 513, 514, 515, 516, 517, 518, 519, 520, 525, 526, 528, 530, 535, 536, 537, 538])
  • Would it be acceptable to find the max and min indecies of the signal array, or must it go through the peak and valleys index array? – tybug Oct 25 '19 at 13:41
  • That's a good question. I do want to use the peak/valley indexes array specifically because how i find my peaks and valleys is a different process that i already have figured out. – learningthemachine Oct 25 '19 at 13:43
  • Also can you clarify your peak indexes array? Currently it is `[0,1,3,8,9]`, which is out of bounds for a signal array of length 9. – tybug Oct 25 '19 at 13:48
  • I had changed the signal array too. Trying to make sure it captures the behaviors i need the function to be able to deal with. – learningthemachine Oct 25 '19 at 13:50

2 Answers2

0

This may help:

a=[1,2,0,1,0,-1,-2,0,2,1]

import numpy as np

np.where(a==np.max(a))[0]

array([1, 8])

np.where(a==np.min(a))[0]

array([6])
razimbres
  • 4,715
  • 5
  • 23
  • 50
0
import numpy as np

def get_values_from_peak_valley(arr, peak_indecies, valley_indecies):

    peak_vals = arr[peak_indecies]
    valley_vals = arr[valley_indecies]

    # https://stackoverflow.com/a/17568803/12164878
    max_peak_indecies = np.argwhere(peak_vals == peak_vals.max()).flatten().tolist()
    arr_peak_indecies = peak_indecies[max_peak_indecies]

    max_valley_indecies = np.argwhere(valley_vals == valley_vals.min()).flatten().tolist()
    arr_valley_indecies = valley_indecies[max_valley_indecies]

    return (arr_peak_indecies, arr_valley_indecies)


arr = np.array([1,2,0,1,0,-1,-2,0,2,1])
peak_indecies = np.array([0,1,3,8,9]) # want [1,8]
valley_indecies = np.array([5,6]) # want [6]

print(get_values_from_peak_valley(arr, peak_indecies, valley_indecies))
# (array([1, 8]), array([6]))

I could say a lot about what requiring this type of function says about the nature of your program (this is a code smell to me), but I appreciate there are scenarios this is useful. Not especially happy about the flatten().tolist() business, maybe someone has a better way of doing that, but argmax only returned the first index. If you're okay with that, argmax is much cleaner.

tybug
  • 68
  • 1
  • 6
  • this doesn't really work on use cases outside of the sample i have (ie when there are multiple groups of peaks and valleys). I added real data to the original question and when i run this function on that, it doesn't return the correct indexes – learningthemachine Oct 25 '19 at 15:31