0

what i need to do is to record a short audio of 2 seconds. my approach is to record 3 seconds and crop two seconds from it.

for now i managed to achieve this by using a while loop and calculate the average of every 2sec ranges possible. as the rate is 40100 and the original length is 3 of that, this takes a long time to finish.

i = 0
while i < len(channel_1) - (2 * rate):
    avgs.append(get_average(channel_1[i:i + (2 * rate)]))
    i += 10   
max_int = np.argmax(avgs) * 10
new_audio = audio[max_int:max_int + (2 * rate)]

is there anyway how i can do this faster and more effectively than this.

enter image description here

Eshaka
  • 974
  • 1
  • 14
  • 38
  • Does your solution produce the result you want? – wwii Nov 08 '19 at 04:31
  • Your making ten sample moving average (.25 mS smoothing filter)? Using the max of those averages as the start of the section you want to crop? – wwii Nov 08 '19 at 04:35
  • Related: [Moving average or running mean](https://stackoverflow.com/questions/13728392/moving-average-or-running-mean) ... [How to calculate moving average using NumPy?](https://stackoverflow.com/questions/14313510/how-to-calculate-moving-average-using-numpy) ... [Using strides for an efficient moving average filter](https://stackoverflow.com/questions/4936620/using-strides-for-an-efficient-moving-average-filter) – wwii Nov 08 '19 at 04:43
  • @wwii, yes. this code does what i need.. but it take a long time.. i dont think you explanation is accurate. i am getting the average of two seconds and i am moving the frame by 10 in every loop. moving 1 by 1 is a better solution i think.. but it makes is unacceptably slow – Eshaka Nov 09 '19 at 00:44
  • Did you try any of the methods in those links? The `rollavg_cumsum` function from the second link is pretty decent for finding all the two second averages - replacing your while loop. – wwii Nov 09 '19 at 04:35

0 Answers0