2
\$\begingroup\$

I have a 9-dimensional signal (as a csv from this Gist) that looks like this: signal_plot

A signal peaks every 30 steps. I want to get the maximum values of the peaks in that 30 second window. Here's what I've hacked together so far, where sig is the signal loaded from the csv file and max_res is the desired result:

trial_num = 8
dims = 9
step = 30
max_res = np.zeros(trial_num)

tmp = sig.reshape((trial_num, step, dims))
max_dim = np.argmax(np.sum(tmp, axis=1), axis=1)

sing_dim = np.zeros((trial_num, step))
for t_i in range(trial_num):
    sing_dim[t_i] = tmp[t_i, :, max_dim[t_i]]

max_res = np.max(sing_dim, axis=1)

How can I replace the for-loop with a vectorized operation?

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Instead of getting the positions of the maximum value and then retrieving the values thanks to the positions, you can directly ask numpy to retrieve the maximum values:

tmp = sig.reshape((trial_num, step, dims))
max_res = np.max(np.max(tmp, axis=1), axis=1)

I suggest you, however, to use a function to encapsulate this behaviour. It could take the amount of steps per cycle as a parameter and compute the rest from there:

def max_peak_values(data, steps_per_cycle=30):
    length, dims = data.shape
    trials = length // steps_per_cycle
    new_shape = trials, steps_per_cycle, dims

    return np.max(np.max(data.reshape(new_shape), axis=1), axis=1)

and use it like:

max_res = max_peak_values(sig, 30)
\$\endgroup\$
3
  • \$\begingroup\$ For my benefit, could you explain in English what the result of the np.max(tmp, axis=1)? Can I think of it as something like "the peaks in each dimension across all trials"? \$\endgroup\$
    – Seanny123
    Oct 7, 2016 at 13:34
  • 1
    \$\begingroup\$ @Seanny123 Not sure of the proper terminology since English isn't my native language, but, as regard to the reshape, I’d say it's rather "the maximal value in each trial and each dimensions across all values for that trial and dimension" (not necessarily peak, as a negative peak will be the minimal value). This will give you an array of shape trials, dims (8, 9) in your case. Then, the second np.max will be "the peak for each trial across all dimensions" which is the value you are looking for. \$\endgroup\$ Oct 7, 2016 at 14:03
  • 2
    \$\begingroup\$ @Seanny123 Note that this will only work if, for each trial, there is at least one peak with positive values. You may want to throw an np.abs somewhere if you want to take into account peaks with negative values. \$\endgroup\$ Oct 7, 2016 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.