Ejemplo n.º 1
0
    def test_002_t (self):
        # Create signal
        Fs = 40e6
        Fc = Fs/32

        phaseAcc = 0
        phaseInc = 2 * np.pi * Fc / Fs
        phaseAccNext = phaseAcc + self.n_samples * phaseInc

        d = 1 * np.exp(1j * np.linspace(phaseAcc, phaseAccNext, self.n_samples)).astype(np.complex64)
        h = np.array((1.0+0.4j, 1.0+0.3j, 1.0+0.4j, 0.2+0.3j, 1.0+0.4j, 1.0+0.3j,
                      0.1+0.02j, 0.2+0.3j, 0.4+0.5j, 0.6+0.7j, 0.8+0.9j, 1.0+0.1j)).astype(np.complex64)
        u = np.convolve(d, h, mode='valid')
        d = d[h.size-1:]

        d_source = blocks.vector_source_c(d.tolist())
        u_source = blocks.vector_source_c(u.tolist())
        rls_filter_cc = adapt.rls_filter_cc(True, self.n_taps, self.delta, self._lambda, 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, (rls_filter_cc, 0))
        self.tb.connect(u_source, (rls_filter_cc, 1))
        self.tb.connect((rls_filter_cc, 0), y_sink)
        self.tb.connect((rls_filter_cc, 1), e_sink)
        self.tb.run()
        throughput_avg = rls_filter_cc.pc_throughput_avg()
        print("pc_throughput_avg: {0:.3f} MSPS".format(throughput_avg / 1e6))
        y_data = np.array(y_sink.data())
        e_data = np.array(e_sink.data())
        m = np.median(e_data[e_data > 0])
        e_data[e_data == 0] = m

        if self.plot_enabled:
            plt.figure(figsize=(15, 9))
            plt.subplot(211)
            plt.title("Adaptation")
            plt.xlabel("samples - k")
            plt.plot(d.real, "b", label="d - reference")
            plt.plot(d.imag, "b", label="d - reference")
            plt.plot(u.real, "r", label="u - input")
            plt.plot(u.imag, "r", label="u - input")
            plt.plot(y_data.real, "g", label="y - output")
            plt.plot(y_data.imag, "g", label="y - output")
            plt.legend()
            plt.subplot(212)
            plt.title("Filter error")
            plt.xlabel("samples - k")
            plt.plot(10 * np.log10(e_data.real ** 2), "r", label="e - error [dB]")
            plt.plot(10 * np.log10(e_data.imag ** 2), "b", label="e - error [dB]")
            plt.legend()
            plt.tight_layout()
            plt.show()
Ejemplo n.º 2
0
    def test_003_t (self):
        # Create noise
        mu, sigma = 0, 0.1 # mean and standard deviation
        d = np.empty(self.n_samples, dtype=np.complex64)
        d.real = np.random.normal(mu, sigma, self.n_samples)
        d.imag = np.random.normal(mu, sigma, self.n_samples)
        h = np.array((1.0+0.4j, 1.0+0.3j, 1.0+0.4j, 0.2+0.3j, 1.0+0.4j, 1.0+0.3j,
                      0.1+0.02j, 0.2+0.3j, 0.4+0.5j, 0.6+0.7j, 0.8+0.9j, 1.0+0.1j)).astype(np.complex64)
        u = np.convolve(d, h, mode='valid')
        d = d[h.size-1:]

        d_source = blocks.vector_source_c(d.tolist())
        u_source = blocks.vector_source_c(u.tolist())
        rls_filter_cc = adapt.rls_filter_cc(True, self.n_taps, self.delta, self._lambda, 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, (rls_filter_cc, 0))
        self.tb.connect(u_source, (rls_filter_cc, 1))
        self.tb.connect((rls_filter_cc, 0), y_sink)
        self.tb.connect((rls_filter_cc, 1), e_sink)
        self.tb.run()
        throughput_avg = rls_filter_cc.pc_throughput_avg()
        print("pc_throughput_avg: {0:.3f} MSPS".format(throughput_avg / 1e6))
        y_data = np.array(y_sink.data())
        e_data = np.array(e_sink.data())
        m = np.median(e_data[e_data > 0])
        e_data[e_data == 0] = m

        if self.plot_enabled:
            plt.figure(figsize=(15, 9))
            plt.subplot(211)
            plt.title("Adaptation")
            plt.xlabel("samples - k")
            plt.plot(d.real, "b", label="d - reference")
            plt.plot(d.imag, "b", label="d - reference")
            plt.plot(u.real, "r", label="u - input")
            plt.plot(u.imag, "r", label="u - input")
            plt.plot(y_data.real, "g", label="y - output")
            plt.plot(y_data.imag, "g", label="y - output")
            plt.legend()
            plt.subplot(212)
            plt.title("Filter error")
            plt.xlabel("samples - k")
            plt.plot(10 * np.log10(e_data.real ** 2), "r", label="e - error [dB]")
            plt.plot(10 * np.log10(e_data.imag ** 2), "b", label="e - error [dB]")
            plt.legend()
            plt.tight_layout()
            plt.show()
Ejemplo n.º 3
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
        rls_filter_cc = []
        y_sink_rls = []
        e_sink_rls = []
        for j, _ in enumerate(W):
            rls_filter_cc.append(adapt.rls_filter_cc(True, self.n_taps, self.delta, self._lambda, self.skip, self.decimation, self.adapt, self.reset))
            y_sink_rls.append(blocks.vector_sink_c())
            e_sink_rls.append(blocks.vector_sink_c())
            self.tb.connect((rls_filter_cc[j], 0), y_sink_rls[j])
            self.tb.connect((rls_filter_cc[j], 1), e_sink_rls[j])

        for i in range(0, self.n_ensemble):
            # Set taps to zero
            for j in range(0, len(W)):
                rls_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, (rls_filter_cc[j], 0))
                self.tb.connect(u_source[j], (rls_filter_cc[j], 1))

            self.tb.run()

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

        e_data_rls = []
        for j in range(0, len(W)):
            e_data_rls.append((np.abs(e_sink_rls[j].data()) ** 2).reshape((self.n_ensemble, self.n_samples-h[j].size*1)))
            e_data_rls[j] = np.mean(e_data_rls[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 NLMS algorithm")
            plt.xlabel("Number of iterations")
            plt.ylabel("Ensemble-averaged square error")
            for j in range(0, len(W)):
                plt.semilogy(e_data_rls[j], label="W={}".format(W[j]))
            plt.legend()
            plt.grid()
            plt.tight_layout()
            plt.show()
Ejemplo n.º 4
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()
Ejemplo n.º 5
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()