def testAngularMethod(self): sample_rate = 16000 sample_delay = 5 angle = math.pi / 6 if abs(math.cos(angle)) > 1e-10: dist = sample_delay * pa_tools.SPEED_OF_SOUND / (sample_rate * math.cos(angle)) else: dist = 1 sample_delay = 0 print "distance: " + str(dist) mics = np.array([[0., 0.], [dist, 0.]], dtype=np.float32) 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]) ffts = np.array([fft1, fft2]) loc = DirectionLocalizer(mics, sample_rate=sample_rate) direction = loc.get_direction_np(ffts) print "direction: " + str(direction) # Plot plt.figure() plt.plot(mics[:, 0], mics[:, 1], 'bo') plt.quiver(0, 0, direction[0], direction[1], scale=20) plt.show()
def testAngularMethod(self): sample_rate = 16000 sample_delay = 5 angle = math.pi / 6 if abs(math.cos(angle)) > 1e-10: dist = sample_delay * pa_tools.SPEED_OF_SOUND / (sample_rate * math.cos(angle)) else: dist = 1 sample_delay = 0 print "distance: " + str(dist) mics = np.array([[0., 0.], [dist, 0.]], dtype=np.float32) 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]) ffts = np.array([fft1, fft2]) loc = DirectionLocalizer(mics, sample_rate=sample_rate) direction = loc.get_direction_np(ffts) print "direction: " + str(direction) # Plot plt.figure() plt.plot(mics[:, 0], mics[:, 1], 'bo') plt.quiver(0, 0, direction[0], direction[1], scale=20) plt.show()
def testGetDirectionOrthogonal(self): sample_rate = 44100 mics = np.array([[-.025], [.025]], dtype=np.float32) source_loc = np.array([10]) dist_1 = np.linalg.norm(source_loc - mics[0, :], 2) dist_2 = np.linalg.norm(source_loc - mics[1, :], 2) loc = DirectionLocalizer(mic_layout=mics, 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]) direction = loc.get_direction_np(ffts) self.assertListFloatEqual([0.0], direction)
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()