예제 #1
0
    def test_003_t(self):
        '''
        Measure performance.
        :return:
        '''
        # Overwrite variables
        self.n_samples = 1024 * 1000

        # Create signal
        d = np.random.randint(
            2, size=self.n_samples) * 2 - 1  # Random bipolar (-1,1) sequence

        # Channel model
        W = 2.9
        h = np.zeros(4)
        h[1:4] = 0.5 * (1 + np.cos(2 * np.pi / W * np.linspace(-1, 1, 3)))

        u = np.convolve(d, h, mode='valid')  # Distorted signal
        u += np.random.normal(0, np.sqrt(0.001), u.size)  # Received signal

        d_source = blocks.vector_source_c(d.tolist())
        u_source = blocks.vector_source_c(u.tolist())
        lms_filter_cc = adapt.lms_filter_cc(True, self.n_taps, self.mu,
                                            self.skip, self.decimation,
                                            self.adapt, self.reset)
        y_sink = blocks.vector_sink_c()
        e_sink = blocks.vector_sink_c()
        self.tb.connect(d_source, (lms_filter_cc, 0))
        self.tb.connect(u_source, (lms_filter_cc, 1))
        self.tb.connect((lms_filter_cc, 0), y_sink)
        self.tb.connect((lms_filter_cc, 1), e_sink)
        self.tb.run()
        throughput_avg = lms_filter_cc.pc_throughput_avg()
        print("pc_throughput_avg: {0:.3f} MSPS".format(throughput_avg / 1e6))
예제 #2
0
    def test_001_t (self):
        # Create filters
        lms_filter_cc = adapt.lms_filter_cc(self.first_input, self.n_taps, self.mu_lms, self.skip, self.decimation, self.adapt, self.reset)
        y_sink_lms = blocks.vector_sink_c()
        e_sink_lms = blocks.vector_sink_c()
        self.tb.connect((lms_filter_cc, 0), y_sink_lms)
        self.tb.connect((lms_filter_cc, 1), e_sink_lms)

        nlms_filter_cc = adapt.nlms_filter_cc(self.first_input, self.n_taps, self.mu_nlms, self.skip, self.decimation, self.adapt, self.reset)
        y_sink_nlms = blocks.vector_sink_c()
        e_sink_nlms = blocks.vector_sink_c()
        self.tb.connect((nlms_filter_cc, 0), y_sink_nlms)
        self.tb.connect((nlms_filter_cc, 1), e_sink_nlms)

        rls_filter_cc = adapt.rls_filter_cc(self.first_input, self.n_taps, self.delta_rls, self.lambda_rls, self.skip, self.decimation, self.adapt, self.reset)
        y_sink_rls = blocks.vector_sink_c()
        e_sink_rls = blocks.vector_sink_c()
        self.tb.connect((rls_filter_cc, 0), y_sink_rls)
        self.tb.connect((rls_filter_cc, 1), e_sink_rls)

        qrd_rls_filter_cc = adapt.qrd_rls_filter_cc(self.n_taps, self.delta_qrd_rls, self.lambda_qrd_rls, self.skip, self.decimation, self.adapt, self.reset)
        y_sink_qrd_rls = blocks.vector_sink_c()
        e_sink_qrd_rls = blocks.vector_sink_c()
        self.tb.connect((qrd_rls_filter_cc, 0), y_sink_qrd_rls)
        self.tb.connect((qrd_rls_filter_cc, 1), e_sink_qrd_rls)

        iqrd_rls_filter_cc = adapt.iqrd_rls_filter_cc(self.n_taps, self.delta_iqrd_rls, self.lambda_iqrd_rls, self.skip, self.decimation, self.adapt, self.reset)
        y_sink_iqrd_rls = blocks.vector_sink_c()
        e_sink_iqrd_rls = blocks.vector_sink_c()
        self.tb.connect((iqrd_rls_filter_cc, 0), y_sink_iqrd_rls)
        self.tb.connect((iqrd_rls_filter_cc, 1), e_sink_iqrd_rls)

        # Channel model
        W = 3.1
        h = np.zeros(4)
        h[1:4] = 0.5 * (1 + np.cos(2*np.pi/W * np.linspace(-1,1,3)))

        for i in range(0, self.n_ensemble):
            # Set taps to zero
            lms_filter_cc.set_taps(np.zeros(self.n_taps, dtype=np.complex128))
            nlms_filter_cc.set_taps(np.zeros(self.n_taps, dtype=np.complex128))
            rls_filter_cc.set_taps(np.zeros(self.n_taps, dtype=np.complex128))
            qrd_rls_filter_cc.set_taps(np.zeros(self.n_taps, dtype=np.complex128))
            iqrd_rls_filter_cc.set_taps(np.zeros(self.n_taps, dtype=np.complex128))

            # Some useful signal to be transmitted
            np.random.seed(i)
            d = np.zeros(self.n_samples, dtype=np.complex128)
            d.real = np.random.randint(2, size=self.n_samples)*2-1 # Random bipolar (-1,1) sequence
            d.imag = np.random.randint(2, size=self.n_samples)*2-1

            u = np.convolve(d, h, mode='valid') # Distorted signal
            u += np.random.normal(0, np.sqrt(0.001), u.size) # Received signal

            d_source = blocks.vector_source_c(d.tolist())
            u_source = blocks.vector_source_c(u.tolist())

            self.tb.connect(d_source, (lms_filter_cc, 0))
            self.tb.connect(u_source, (lms_filter_cc, 1))

            self.tb.connect(d_source, (nlms_filter_cc, 0))
            self.tb.connect(u_source, (nlms_filter_cc, 1))

            self.tb.connect(d_source, (rls_filter_cc, 0))
            self.tb.connect(u_source, (rls_filter_cc, 1))

            self.tb.connect(d_source, (qrd_rls_filter_cc, 0))
            self.tb.connect(u_source, (qrd_rls_filter_cc, 1))

            self.tb.connect(d_source, (iqrd_rls_filter_cc, 0))
            self.tb.connect(u_source, (iqrd_rls_filter_cc, 1))

            self.tb.run()

            self.tb.disconnect(d_source, (lms_filter_cc, 0))
            self.tb.disconnect(u_source, (lms_filter_cc, 1))

            self.tb.disconnect(d_source, (nlms_filter_cc, 0))
            self.tb.disconnect(u_source, (nlms_filter_cc, 1))

            self.tb.disconnect(d_source, (rls_filter_cc, 0))
            self.tb.disconnect(u_source, (rls_filter_cc, 1))

            self.tb.disconnect(d_source, (qrd_rls_filter_cc, 0))
            self.tb.disconnect(u_source, (qrd_rls_filter_cc, 1))

            self.tb.disconnect(d_source, (iqrd_rls_filter_cc, 0))
            self.tb.disconnect(u_source, (iqrd_rls_filter_cc, 1))

        e_data_lms = (np.abs(e_sink_lms.data()) ** 2).reshape((self.n_ensemble, self.n_samples-h.size))
        e_data_nlms = (np.abs(e_sink_nlms.data()) ** 2).reshape((self.n_ensemble, self.n_samples-h.size))
        e_data_rls = (np.abs(e_sink_rls.data()) ** 2).reshape((self.n_ensemble, self.n_samples-h.size))
        e_data_qrd_rls = (np.abs(e_sink_qrd_rls.data()) ** 2).reshape((self.n_ensemble, self.n_samples-h.size))
        e_data_iqrd_rls = (np.abs(e_sink_iqrd_rls.data()) ** 2).reshape((self.n_ensemble, self.n_samples-h.size))

        e_data_lms = np.mean(e_data_lms, axis=0)
        e_data_nlms = np.mean(e_data_nlms, axis=0)
        e_data_rls = np.mean(e_data_rls, axis=0)
        e_data_qrd_rls = np.mean(e_data_qrd_rls, axis=0)
        e_data_iqrd_rls = np.mean(e_data_iqrd_rls, axis=0)

        if self.plot_enabled:
            plt.figure(figsize=(5, 4))
            plt.rc('axes', prop_cycle=(cycler('color', ['r', 'g', 'b', 'k', 'm'])))
            plt.title("Learning curves for different algorithms")
            plt.xlabel("Number of iterations")
            plt.ylabel("Ensemble-averaged square error")
            plt.semilogy(e_data_lms, label=u"LMS (μ={})".format(self.mu_lms))
            plt.semilogy(e_data_nlms, label=u"NLMS (μ={})".format(self.mu_nlms))
            plt.semilogy(e_data_rls, label=u"RLS (δ={}, λ={})".format(self.delta_rls, self.lambda_rls))
            plt.semilogy(e_data_qrd_rls, label=u"QRD RLS (δ={}, λ={})".format(self.delta_qrd_rls, self.lambda_qrd_rls))
            plt.semilogy(e_data_iqrd_rls, label=u"IQRD RLS (δ={}, λ={})".format(self.delta_iqrd_rls, self.lambda_iqrd_rls))
            plt.legend()
            plt.grid()
            plt.tight_layout()
            plt.show()
예제 #3
0
    def test_001_t(self):
        # Create float filters
        filters_ff = []
        lms_filter_ff = adapt.lms_filter_ff(self.first_input, self.n_taps,
                                            self.mu_lms, self.skip,
                                            self.decimation, self.adapt,
                                            self.reset)
        filters_ff.append(lms_filter_ff)

        nlms_filter_ff = adapt.nlms_filter_ff(self.first_input, self.n_taps,
                                              self.mu_nlms, self.skip,
                                              self.decimation, self.adapt,
                                              self.reset)
        filters_ff.append(nlms_filter_ff)

        rls_filter_ff = adapt.rls_filter_ff(self.first_input, self.n_taps,
                                            self.delta_rls, self.lambda_rls,
                                            self.skip, self.decimation,
                                            self.adapt, self.reset)
        filters_ff.append(rls_filter_ff)

        qrd_rls_filter_ff = adapt.qrd_rls_filter_ff(self.n_taps,
                                                    self.delta_qrd_rls,
                                                    self.lambda_qrd_rls,
                                                    self.skip, self.decimation,
                                                    self.adapt, self.reset)
        filters_ff.append(qrd_rls_filter_ff)

        iqrd_rls_filter_ff = adapt.iqrd_rls_filter_ff(
            self.n_taps, self.delta_iqrd_rls, self.lambda_iqrd_rls, self.skip,
            self.decimation, self.adapt, self.reset)
        filters_ff.append(iqrd_rls_filter_ff)

        y_sink_f = blocks.vector_sink_f()
        e_sink_f = blocks.vector_sink_f()

        # Create complex filters
        filters_cc = []
        lms_filter_cc = adapt.lms_filter_cc(self.first_input, self.n_taps,
                                            self.mu_lms, self.skip,
                                            self.decimation, self.adapt,
                                            self.reset)
        filters_cc.append(lms_filter_cc)

        nlms_filter_cc = adapt.nlms_filter_cc(self.first_input, self.n_taps,
                                              self.mu_nlms, self.skip,
                                              self.decimation, self.adapt,
                                              self.reset)
        filters_cc.append(nlms_filter_cc)

        rls_filter_cc = adapt.rls_filter_cc(self.first_input, self.n_taps,
                                            self.delta_rls, self.lambda_rls,
                                            self.skip, self.decimation,
                                            self.adapt, self.reset)
        filters_cc.append(rls_filter_cc)

        qrd_rls_filter_cc = adapt.qrd_rls_filter_cc(self.n_taps,
                                                    self.delta_qrd_rls,
                                                    self.lambda_qrd_rls,
                                                    self.skip, self.decimation,
                                                    self.adapt, self.reset)
        filters_cc.append(qrd_rls_filter_cc)

        iqrd_rls_filter_cc = adapt.iqrd_rls_filter_cc(
            self.n_taps, self.delta_iqrd_rls, self.lambda_iqrd_rls, self.skip,
            self.decimation, self.adapt, self.reset)
        filters_cc.append(iqrd_rls_filter_cc)

        y_sink_c = blocks.vector_sink_c()
        e_sink_c = blocks.vector_sink_c()

        results_f = []
        results_c = []
        names = []

        # Channel model
        W = 3.1
        h = np.zeros(4)
        h[1:4] = 0.5 * (1 + np.cos(2 * np.pi / W * np.linspace(-1, 1, 3)))

        # Some useful signal to be transmitted
        np.random.seed(2701)
        d = np.random.randint(
            2, size=self.n_samples) * 2 - 1  # Random bipolar (-1,1) sequence

        u = np.convolve(d, h, mode='valid')  # Distorted signal
        u += np.random.normal(0, np.sqrt(0.001), u.size)  # Received signal

        np.random.seed(2701)
        d_c = np.empty(self.n_samples, dtype=np.complex64)
        d_c.real = np.random.randint(
            2, size=self.n_samples) * 2 - 1  # Random bipolar (-1,1) sequence
        d_c.imag = np.random.randint(2, size=self.n_samples) * 2 - 1

        u_c = np.convolve(d_c, h, mode='valid')  # Distorted signal
        u_c += np.random.normal(0, np.sqrt(0.001), u_c.size)  # Received signal

        for filter_ff in filters_ff:
            d_source = blocks.vector_source_f(d.tolist())
            u_source = blocks.vector_source_f(u.tolist())

            self.tb.connect(d_source, (filter_ff, 0))
            self.tb.connect(u_source, (filter_ff, 1))

            self.tb.connect((filter_ff, 0), y_sink_f)
            self.tb.connect((filter_ff, 1), e_sink_f)

            self.tb.run()

            results_f.append(filter_ff.pc_throughput_avg() / 1e6)
            names.append(
                str(filter_ff.__class__.__name__).replace('_ff_sptr', ''))
            self.tb.disconnect(filter_ff)

        for filter_cc in filters_cc:
            d_source = blocks.vector_source_c(d_c.tolist())
            u_source = blocks.vector_source_c(u_c.tolist())

            self.tb.connect(d_source, (filter_cc, 0))
            self.tb.connect(u_source, (filter_cc, 1))

            self.tb.connect((filter_cc, 0), y_sink_c)
            self.tb.connect((filter_cc, 1), e_sink_c)

            self.tb.run()

            results_c.append(filter_cc.pc_throughput_avg() / 1e6)
            # names.append(str(filter_cc.__class__.__name__).replace('_sptr',''))
            self.tb.disconnect(filter_cc)

        if self.plot_enabled:
            # plt.rc('axes', prop_cycle=(cycler('color', ['r', 'g', 'b', 'k'])))
            fig, ax = plt.subplots(figsize=(5, 4))

            index = np.arange(len(names))  # the x locations for the groups
            width = 0.3  # the width of the bars

            rects1 = ax.bar(index,
                            results_f,
                            width,
                            label='Float (n={})'.format(self.n_taps))
            rects2 = ax.bar(index + width,
                            results_c,
                            width,
                            label='Complex (n={})'.format(self.n_taps))

            ax.set_ylabel('Throughput (MSPS)')
            ax.set_title('Throughput for different algorithms\n{}'.format(
                cpuinfo.get_cpu_info()['brand']))
            ax.set_xticks(index + width / 2)
            ax.set_xticklabels(names, rotation=45, ha="right")
            ax.legend()

            xpos = 'center'
            xpos = xpos.lower()  # normalize the case of the parameter
            ha = {'center': 'center', 'right': 'left', 'left': 'right'}
            offset = {
                'center': 0.5,
                'right': 0.57,
                'left': 0.43
            }  # x_txt = x + w*off

            for rect in rects1:
                height = rect.get_height()
                ax.text(rect.get_x() + rect.get_width() * offset[xpos],
                        1.01 * height,
                        '{0:.2f}'.format(height),
                        ha=ha[xpos],
                        va='bottom')

            for rect in rects2:
                height = rect.get_height()
                ax.text(rect.get_x() + rect.get_width() * offset[xpos],
                        1.01 * height,
                        '{0:.2f}'.format(height),
                        ha=ha[xpos],
                        va='bottom')

            fig.tight_layout()
            plt.show()
예제 #4
0
    def test_001_t(self):
        '''
        Simulate convergence for different channel models.
        :return:
        '''
        # Channel model
        W = [2.9, 3.1, 3.3, 3.5]
        h = np.zeros((len(W), 4))
        for j, Wj in enumerate(W):
            h[j][1:4] = 0.5 * (1 +
                               np.cos(2 * np.pi / Wj * np.linspace(-1, 1, 3)))

        # Filters
        lms_filter_cc = []
        y_sink_lms = []
        e_sink_lms = []
        for j, _ in enumerate(W):
            lms_filter_cc.append(
                adapt.lms_filter_cc(True, self.n_taps, self.mu, self.skip,
                                    self.decimation, self.adapt, self.reset))
            y_sink_lms.append(blocks.vector_sink_c())
            e_sink_lms.append(blocks.vector_sink_c())
            self.tb.connect((lms_filter_cc[j], 0), y_sink_lms[j])
            self.tb.connect((lms_filter_cc[j], 1), e_sink_lms[j])

        for i in range(0, self.n_ensemble):
            # Set taps to zero
            for j in range(0, len(W)):
                lms_filter_cc[j].set_taps(
                    np.zeros(self.n_taps, dtype=np.complex128))

            # Some useful signal to be transmitted
            np.random.seed(i)
            d = np.zeros(self.n_samples, dtype=np.complex128)
            d.real = np.random.randint(
                2,
                size=self.n_samples) * 2 - 1  # Random bipolar (-1,1) sequence
            d.imag = np.random.randint(2, size=self.n_samples) * 2 - 1
            d_source = blocks.vector_source_c(d.tolist())

            u = []
            u_source = []
            for j in range(0, len(W)):
                u.append(np.convolve(d, h[j],
                                     mode='valid'))  # Distorted signal
                u[j] += np.random.normal(0, np.sqrt(0.001),
                                         u[j].size)  # Received signal

                u_source.append(blocks.vector_source_c(u[j].tolist()))

                self.tb.connect(d_source, (lms_filter_cc[j], 0))
                self.tb.connect(u_source[j], (lms_filter_cc[j], 1))

            self.tb.run()

            for j in range(0, len(W)):
                self.tb.disconnect(d_source, (lms_filter_cc[j], 0))
                self.tb.disconnect(u_source[j], (lms_filter_cc[j], 1))

        e_data_lms = []
        for j in range(0, len(W)):
            e_data_lms.append((np.abs(e_sink_lms[j].data())**2).reshape(
                (self.n_ensemble, self.n_samples - h[j].size)))
            e_data_lms[j] = np.mean(e_data_lms[j], axis=0)

        if self.plot_enabled:
            plt.figure(figsize=(5, 4))
            plt.rc('axes', prop_cycle=(cycler('color', ['r', 'g', 'b', 'k'])))
            plt.title("Learning curves for LMS algorithm")
            plt.xlabel("Number of iterations")
            plt.ylabel("Ensemble-averaged square error")
            for j in range(0, len(W)):
                plt.semilogy(e_data_lms[j], label="W={}".format(W[j]))
            plt.legend()
            plt.grid()
            plt.tight_layout()
            plt.show()
예제 #5
0
    def test_004_t(self):
        '''
        Check if decimation works.
        :return:
        '''
        # Overwrite variables
        self.n_samples = 1024
        self.decimation = 2

        # Create signal
        Fs = 2e6
        Fc = 100e3

        phaseAcc = 0
        phaseInc = 2 * np.pi * Fc / Fs
        phaseAccNext = phaseAcc + self.n_samples * phaseInc
        d = 1 * np.sin(np.linspace(phaseAcc, phaseAccNext,
                                   self.n_samples)).astype(np.float32)

        # Channel model
        W = 2.9
        h = np.zeros(4)
        h[1:4] = 0.5 * (1 + np.cos(2 * np.pi / W * np.linspace(-1, 1, 3)))

        u = np.convolve(d, h, mode='valid')  # Distorted signal
        u += np.random.normal(0, np.sqrt(0.001), u.size)  # Received signal

        d_source = blocks.vector_source_c(d.tolist())
        u_source = blocks.vector_source_c(u.tolist())
        lms_filter_cc = adapt.lms_filter_cc(True, self.n_taps, self.mu,
                                            self.skip, self.decimation,
                                            self.adapt, self.reset)
        y_sink = blocks.vector_sink_c()
        e_sink = blocks.vector_sink_c()
        self.tb.connect(d_source, (lms_filter_cc, 0))
        self.tb.connect(u_source, (lms_filter_cc, 1))
        self.tb.connect((lms_filter_cc, 0), y_sink)
        self.tb.connect((lms_filter_cc, 1), e_sink)
        self.tb.run()

        y_data = np.array(y_sink.data())
        e_data = np.abs(e_sink.data())
        m = np.median(e_data[e_data > 0])
        e_data[e_data == 0] = m

        plt.figure(figsize=(15, 9))
        plt.subplot(211)
        plt.title("Adaptation")
        plt.xlabel("samples - k")
        plt.plot(np.delete(d, np.arange(0, d.size, 2)),
                 "b",
                 label="d - reference")
        plt.plot(np.delete(u, np.arange(0, u.size, 2)), "r", label="u - input")
        plt.plot(y_data, "g", label="y - output")
        plt.legend()
        plt.subplot(212)
        plt.title("Filter error")
        plt.xlabel("samples - k")
        plt.plot(10 * np.log10(e_data**2), "r", label="e - error [dB]")
        plt.legend()
        plt.tight_layout()
        plt.show()