Mapping a numpy array in pairs

I am currently implementing a beat detection algorithm using python and numpy / scipy. I basically need to read the WAV file and process it. Here is the code:

sampling_rate, wave_data = scipy.io.wavfile.read(argv[1]) 

wave_data is an 1-D numpy array with approximately 441,000 elements (10 seconds of sound with a sampling frequency of 44.1 kHz). Now I need to do some basic math for every two elements in this array. Here's how I do it now:

 wave_data = [sampling_rate * (wave_data[i+1] - wave_data[i]) for i in xrange(len(wave_data)-1)] 

This distraction takes too much time (noticeably without profiling). I need to map the array in pairs "in place" without creating a new python list. I know that there is numpy.vectorize , but I do not know how I can do the pairwise matching (match every two elements of the array).

+4
source share
1 answer

One of the following actions will be performed:

 wave_date = sampling_rate * np.diff(wave_data) 

or

 wave_date = sampling_rate * (wave_data[1:] - wave_data[:-1]) 

For instance:

 In [7]: sampling_rate = 2 In [8]: wave_data = np.array([1, 3, 5, 2, 8, 10]) In [9]: sampling_rate * (wave_data[1:] - wave_data[:-1]) Out[9]: array([ 4, 4, -6, 12, 4]) 

In terms of performance, both of these approaches are about 500 times faster than list comprehension:

 In [16]: wave_data = np.array([1., 3, 5, 2, 8, 10, 5, 2, 4, 7] * 44100) In [17]: %timeit sampling_rate * np.diff(wave_data) 100 loops, best of 3: 2.2 ms per loop In [18]: %timeit sampling_rate * (wave_data[1:] - wave_data[:-1]) 100 loops, best of 3: 2.15 ms per loop In [19]: %timeit [sampling_rate * (wave_data[i+1] - wave_data[i]) for i in xrange(len(wave_data)-1)] 1 loops, best of 3: 970 ms per loop 
+4
source

All Articles