def testGetPeaksSame(self): sample_rate = 44100 loc = DirectionLocalizer(mic_layout=None, sample_rate=sample_rate, shift_n=20, shift_max=2) data = np.array([1, -2, 3, 4, 0, 0, 1, 2], dtype=np.float32) fft = fftp.fft(data) ffts = np.array([fft, fft]) peaks = loc.get_peaks(ffts) inds = np.argmax(peaks, 1) delays = peaks[0, inds[1:]] delays *= pa_tools.SPEED_OF_SOUND / sample_rate self.assertListFloatEqual([0.0], delays)
def testGetPeaks(self): g = np.array([[1, 2, 2, 1, 1, 2, 3, 4], [2, 3, 4, 1, 2, 2, 1, 1], [1, 1, 2, 3, 4, 1, 2, 2], [1, 2, 2, 1, 1, 2, 3, 4]]) G = fftp.ifft(g) shift_max = 4 shift_n = 2 * shift_max + 1 loc = DirectionLocalizer(mic_layout=None, shift_n=shift_n, shift_max=shift_max) peaks = loc.get_peaks(G) print peaks max_ind = np.argmax(peaks, 1) shifts = peaks[0, max_ind] self.assertListFloatEqual(np.array([4, 3, -3, 0]), shifts)
def testGetPeaksSame(self): sample_rate = 44100 loc = DirectionLocalizer(mic_layout=None, sample_rate=sample_rate, shift_n=20, shift_max=2) data = np.array([1, -2, 3, 4, 0, 0, 1, 2], dtype=np.float32) fft = fftp.fft(data) ffts = np.array([fft, fft]) peaks = loc.get_peaks(ffts) inds = np.argmax(peaks, 1) delays = peaks[0, inds[1:]] delays *= pa_tools.SPEED_OF_SOUND / sample_rate self.assertListFloatEqual([0.0], delays)
def testGetPeaks(self): g = np.array([[1, 2, 2, 1, 1, 2, 3, 4], [2, 3, 4, 1, 2, 2, 1, 1], [1, 1, 2, 3, 4, 1, 2, 2], [1, 2, 2, 1, 1, 2, 3, 4]]) G = fftp.ifft(g) shift_max = 4 shift_n = 2 * shift_max + 1 loc = DirectionLocalizer(mic_layout=None, shift_n=shift_n, shift_max=shift_max) peaks = loc.get_peaks(G) print peaks max_ind = np.argmax(peaks, 1) shifts = peaks[0, max_ind] self.assertListFloatEqual(np.array([4, 3, -3, 0]), shifts)
def testGetDirection3Mic(self): sample_rate = 16000 sample_delay = 3 # Get side_length of mic triangle so that the sample # delay will be an integer if source comes from direction # perpendicular to some side of the triangle side_length = 2 * sample_delay * pa_tools.SPEED_OF_SOUND / ( np.sqrt(3) * sample_rate) mics = np.array([[0, side_length / np.sqrt(3)], [side_length / 2, -side_length / (2 * np.sqrt(3))], [-side_length / 2, -side_length / (2 * np.sqrt(3))]]) # Sides are orthogonal to directions (sqrt(3)/2, 1/2), (-sqrt(3)/2, 1/2), (0, 1) data_len = 100 data1 = np.random.rand(1, data_len) if sample_delay > 0: data2 = np.concatenate( (np.random.rand(1, sample_delay), [data1[0, :-sample_delay]]), axis=1) else: data2 = data1 # Get dfts fft1 = fftp.fft(data1[0]) fft2 = fftp.fft(data2[0]) loc = DirectionLocalizer(mic_layout=mics, sample_rate=sample_rate, shift_max=data_len / 2, shift_n=100) ffts = np.array([fft1, fft1, fft2]) # Get peaks and direction peaks = loc.get_peaks(ffts) print "Sample delay from mic 1: " + str( peaks[0, (np.argmax(peaks, 1))[1:]]) direction = loc.get_direction_np(ffts) print "Direction to source: " + str(direction) direction /= np.linalg.norm(direction, 2) # Normalize direction *= 10 # Scale for plotting print mics # Plot plt.figure() plt.plot(mics[:, 0], mics[:, 1], 'bo') plt.quiver(0, 0, direction[0], direction[1], scale=20) plt.show()
def testGetDirection3Mic(self): sample_rate = 16000 sample_delay = 3 # Get side_length of mic triangle so that the sample # delay will be an integer if source comes from direction # perpendicular to some side of the triangle side_length = 2 * sample_delay * pa_tools.SPEED_OF_SOUND / (np.sqrt(3) * sample_rate) mics = np.array([[0, side_length / np.sqrt(3)], [side_length / 2, -side_length / (2 * np.sqrt(3))], [-side_length / 2, -side_length / (2 * np.sqrt(3))]]) # Sides are orthogonal to directions (sqrt(3)/2, 1/2), (-sqrt(3)/2, 1/2), (0, 1) data_len = 100 data1 = np.random.rand(1, data_len) if sample_delay > 0: data2 = np.concatenate((np.random.rand(1, sample_delay), [data1[0, :-sample_delay]]), axis=1) else: data2 = data1 # Get dfts fft1 = fftp.fft(data1[0]) fft2 = fftp.fft(data2[0]) loc = DirectionLocalizer(mic_layout=mics, sample_rate=sample_rate, shift_max=data_len / 2, shift_n=100) ffts = np.array([fft1, fft1, fft2]) # Get peaks and direction peaks = loc.get_peaks(ffts) print "Sample delay from mic 1: " + str(peaks[0, (np.argmax(peaks, 1))[1:]]) direction = loc.get_direction_np(ffts) print "Direction to source: " + str(direction) direction /= np.linalg.norm(direction, 2) # Normalize direction *= 10 # Scale for plotting print mics # Plot plt.figure() plt.plot(mics[:, 0], mics[:, 1], 'bo') plt.quiver(0, 0, direction[0], direction[1], scale=20) plt.show()