def testGetDistribution3D(self):
        R = 0.0375
        H = 0.07
        x = np.array([[0, 0, H],
                        [R, 0, 0],
                        [R*math.cos(math.pi/3), R*math.sin(math.pi/3), 0],
                        [-R*math.cos(math.pi/3), R*math.sin(math.pi/3), 0],
                        [-R, 0, 0],
                        [-R*math.cos(math.pi/3), -R*math.sin(math.pi/3), 0],
                        [R*math.cos(math.pi/3), -R*math.sin(math.pi/3), 0]])
        nmics = 7

        # Setup plot
        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')
        plt.show(block=False)

        # Peform simulation
        N_trials = 10
        for n in range(N_trials):
            # Get random direction
            source = np.array([-100, -100, 0]) + 200 * np.random.rand(3)
            #source =np.array([70, 20, 100])
            # Compute distances and delays
            d = np.sqrt(np.sum((x - source) ** 2, axis=1))
            #d1 = d[0] - d
            delays = d / pa_tools.SPEED_OF_SOUND
            print "delays: " + str(delays)

            # Create audio sample
            Fs = 44100
            T = 1. / Fs
            nsecs = .25
            N = Fs * nsecs
            fund_freq = 50
            low_freq = 1 / (2 * nsecs)
            n = (np.tile(np.arange(N) * T, (nmics, 1)).T - delays).T
            s = np.sin(n * math.pi * low_freq)
            # Add different harmonics to signal
            for k in range(50):
                if k % 3 == 1:
                    s += 5 * np.sin(n * 2 * math.pi * fund_freq * k)
            # Add random noise to each signal
            #s += .35 * np.random.rand(nmics, s.shape[1])

            # Setup localizer
            window_len = 512
            N_THETA = 20
            N_PHI = N_THETA / 2
            loc = DistributionLocalizer(x, sample_rate=Fs, n_theta=N_THETA, dft_len=window_len, n_phi=N_PHI)

            # Get section of signal
            ind = round(random.random() * (N - 512 - 1))
            #ind = 200;
            g = s[:, ind:ind + window_len]
            #print g
            #f = plt.figure()
            #a = f.add_subplot(111)
            #a.plot(np.arange(g.shape[1]), g.T)
            #plt.show()
            G = np.fft.fft(g, n=window_len, axis=1)
            G_real = np.fft.rfft(g, n=window_len, axis=1)

            direcs = loc.get_directions()
            d_real = loc.get_distribution_real(G_real)
            d = loc.get_distribution_mat(G)
            #self.assertListFloatEqual(d, d_real)
            d = d_real
            print "max: " + str(np.max(d))
            print "min: " + str(np.min(d))
            maxind = np.argmax(d)
            u = 1.5 * direcs[:, maxind]
            v = 1.5 * source / np.linalg.norm(source, 2)
            #self.assertLessEqual(np.sqrt(np.sum((u / 1.5 - v / 1.5) ** 2)), .2)
            plt.cla()
            ax.scatter(direcs[0, :], direcs[1, :], direcs[2, :], s=30, c=d)
            ax.plot([0, v[0]], [0, v[1]], [0, v[2]])
            ax.plot([0, u[0]], [0, u[1]], [0, u[2]], c='r')
            #ax.view_init(azim=-90, elev=90)
            plt.draw()
            time.sleep(.5)
Exemple #2
0
    def testGetDistribution3D(self):
        R = 0.0375
        H = 0.07
        x = np.array(
            [[0, 0, H], [R, 0, 0],
             [R * math.cos(math.pi / 3), R * math.sin(math.pi / 3), 0],
             [-R * math.cos(math.pi / 3), R * math.sin(math.pi / 3), 0],
             [-R, 0, 0],
             [-R * math.cos(math.pi / 3), -R * math.sin(math.pi / 3), 0],
             [R * math.cos(math.pi / 3), -R * math.sin(math.pi / 3), 0]])
        nmics = 7

        # Setup plot
        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')
        plt.show(block=False)

        # Peform simulation
        N_trials = 10
        for n in range(N_trials):
            # Get random direction
            source = np.array([-100, -100, 0]) + 200 * np.random.rand(3)
            #source =np.array([70, 20, 100])
            # Compute distances and delays
            d = np.sqrt(np.sum((x - source)**2, axis=1))
            #d1 = d[0] - d
            delays = d / pa_tools.SPEED_OF_SOUND
            print "delays: " + str(delays)

            # Create audio sample
            Fs = 44100
            T = 1. / Fs
            nsecs = .25
            N = Fs * nsecs
            fund_freq = 50
            low_freq = 1 / (2 * nsecs)
            n = (np.tile(np.arange(N) * T, (nmics, 1)).T - delays).T
            s = np.sin(n * math.pi * low_freq)
            # Add different harmonics to signal
            for k in range(50):
                if k % 3 == 1:
                    s += 5 * np.sin(n * 2 * math.pi * fund_freq * k)
            # Add random noise to each signal
            #s += .35 * np.random.rand(nmics, s.shape[1])

            # Setup localizer
            window_len = 512
            N_THETA = 20
            N_PHI = N_THETA / 2
            loc = DistributionLocalizer(x,
                                        sample_rate=Fs,
                                        n_theta=N_THETA,
                                        dft_len=window_len,
                                        n_phi=N_PHI)

            # Get section of signal
            ind = round(random.random() * (N - 512 - 1))
            #ind = 200;
            g = s[:, ind:ind + window_len]
            #print g
            #f = plt.figure()
            #a = f.add_subplot(111)
            #a.plot(np.arange(g.shape[1]), g.T)
            #plt.show()
            G = np.fft.fft(g, n=window_len, axis=1)
            G_real = np.fft.rfft(g, n=window_len, axis=1)

            direcs = loc.get_directions()
            d_real = loc.get_distribution_real(G_real)
            d = loc.get_distribution_mat(G)
            #self.assertListFloatEqual(d, d_real)
            d = d_real
            print "max: " + str(np.max(d))
            print "min: " + str(np.min(d))
            maxind = np.argmax(d)
            u = 1.5 * direcs[:, maxind]
            v = 1.5 * source / np.linalg.norm(source, 2)
            #self.assertLessEqual(np.sqrt(np.sum((u / 1.5 - v / 1.5) ** 2)), .2)
            plt.cla()
            ax.scatter(direcs[0, :], direcs[1, :], direcs[2, :], s=30, c=d)
            ax.plot([0, v[0]], [0, v[1]], [0, v[2]])
            ax.plot([0, u[0]], [0, u[1]], [0, u[2]], c='r')
            #ax.view_init(azim=-90, elev=90)
            plt.draw()
            time.sleep(.5)