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?

200_success
146k22 gold badges190 silver badges478 bronze badges
asked Oct 6, 2016 at 23:05
\$\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)
answered Oct 7, 2016 at 12:29
\$\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\$ Commented 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\$ Commented 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\$ Commented Oct 7, 2016 at 14:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.