Home > other >  Slice voice signal based on time vector
Slice voice signal based on time vector

Time:07-19

I have a stocastic (random) voice signal and a time vector.

time = np.linspace(0,51.0,6528)
voice = np.random.random(6528)*time

It does not matter the form of the voice signal.

How to slice the voice between, let's say, 20 and 25 seconds?

CodePudding user response:

For a uniformly sampled signal, you can do simple arithmetic to convert time to index.

Each sample in your signal is 51 / (6528 - 1) seconds apart, so you can divide the desired times by this quantity to get bin numbers, after appropriate truncation or rounding:

start = 20 * 6527 // 51
stop = int(np.ceil(21 * 6527 / 51))   1
t = time[start:stop]
v = voice[start:stop]

You may find that the exact times you want don't appear exactly in your time array.

Another case would be a non-uniformly sampled signal. In this case, using a mask is the simplest approach:

mask = (time >= 20) & (time <= 25)
t = time[mask]
v = voice[mask]

A more nuanced approach to the same case would be to assume that the time array is monotonically increasing, and do a binary search:

start, stop = np.clip(np.searchsorted(time, [20, 25]), 0, len(time) - 1)
stop  = 1
t = time[start:stop]
v = voice[start:stop]
  • Related