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 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 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 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()
def setUp(self): self.sampling_rate = 44100 self.dirloc = DirectionLocalizer(mic_layout=None, sample_rate=44100) self.dft_len = 8 self.stft = StftManager(dft_length=self.dft_len, window_length=self.dft_len, hop_length=self.dft_len, use_window_fcn=False) mic_positions = np.array([[1, 1, 0], [-1, 1, 0], [-1, -1, 0], [1, -1, 0], [0, 0, 1]]) self._n_mics = mic_positions.shape[0] self._n_theta = 4 self._n_phi = 4 self.distrloc = DistributionLocalizer(mic_positions=mic_positions, dft_len=self.dft_len, sample_rate=44100, n_theta=self._n_theta, n_phi=self._n_phi) pass