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_f(d.tolist()) u_source = blocks.vector_source_f(u.tolist()) nlms_filter_ff = adapt.nlms_filter_ff(True, self.n_taps, self.mu, self.skip, self.decimation, self.adapt, self.reset) y_sink = blocks.vector_sink_f() e_sink = blocks.vector_sink_f() self.tb.connect(d_source, (nlms_filter_ff, 0)) self.tb.connect(u_source, (nlms_filter_ff, 1)) self.tb.connect((nlms_filter_ff, 0), y_sink) self.tb.connect((nlms_filter_ff, 1), e_sink) self.tb.run() y_data = np.array(y_sink.data()) e_data = np.array(e_sink.data()) 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()
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_f(d.tolist()) u_source = blocks.vector_source_f(u.tolist()) nlms_filter_ff = adapt.nlms_filter_ff(True, self.n_taps, self.mu, self.skip, self.decimation, self.adapt, self.reset) y_sink = blocks.vector_sink_f() e_sink = blocks.vector_sink_f() self.tb.connect(d_source, (nlms_filter_ff, 0)) self.tb.connect(u_source, (nlms_filter_ff, 1)) self.tb.connect((nlms_filter_ff, 0), y_sink) self.tb.connect((nlms_filter_ff, 1), e_sink) self.tb.run() throughput_avg = nlms_filter_ff.pc_throughput_avg() print("pc_throughput_avg: {0:.3f} MSPS".format(throughput_avg / 1e6))
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 nlms_filter_ff = [] y_sink_nlms = [] e_sink_nlms = [] for j, _ in enumerate(W): nlms_filter_ff.append( adapt.nlms_filter_ff(True, self.n_taps, self.mu, self.skip, self.decimation, self.adapt, self.reset)) y_sink_nlms.append(blocks.vector_sink_f()) e_sink_nlms.append(blocks.vector_sink_f()) self.tb.connect((nlms_filter_ff[j], 0), y_sink_nlms[j]) self.tb.connect((nlms_filter_ff[j], 1), e_sink_nlms[j]) for i in range(0, self.n_ensemble): # Set taps to zero for j in range(0, len(W)): nlms_filter_ff[j].set_taps( np.zeros(self.n_taps, dtype=np.float)) # Some useful signal to be transmitted np.random.seed(i) d = np.random.randint(2, size=self.n_samples ) * 2 - 1 # Random bipolar (-1,1) sequence d_source = blocks.vector_source_f(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_f(u[j].tolist())) self.tb.connect(d_source, (nlms_filter_ff[j], 0)) self.tb.connect(u_source[j], (nlms_filter_ff[j], 1)) self.tb.run() for j in range(0, len(W)): self.tb.disconnect(d_source, (nlms_filter_ff[j], 0)) self.tb.disconnect(u_source[j], (nlms_filter_ff[j], 1)) e_data_nlms = [] for j in range(0, len(W)): e_data_nlms.append((np.array(e_sink_nlms[j].data())**2).reshape( (self.n_ensemble, self.n_samples - h[j].size * 2))) e_data_nlms[j] = np.mean(e_data_nlms[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_nlms[j], label="W={}".format(W[j])) plt.legend() plt.grid() plt.tight_layout() plt.show()
def test_001_t(self): # Create filters lms_filter_ff = adapt.lms_filter_ff(self.first_input, self.n_taps, self.mu_lms, self.skip, self.decimation, self.adapt, self.reset) y_sink_lms = blocks.vector_sink_f() e_sink_lms = blocks.vector_sink_f() self.tb.connect((lms_filter_ff, 0), y_sink_lms) self.tb.connect((lms_filter_ff, 1), e_sink_lms) nlms_filter_ff = adapt.nlms_filter_ff(self.first_input, self.n_taps, self.mu_nlms, self.skip, self.decimation, self.adapt, self.reset) y_sink_nlms = blocks.vector_sink_f() e_sink_nlms = blocks.vector_sink_f() self.tb.connect((nlms_filter_ff, 0), y_sink_nlms) self.tb.connect((nlms_filter_ff, 1), e_sink_nlms) 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) y_sink_rls = blocks.vector_sink_f() e_sink_rls = blocks.vector_sink_f() self.tb.connect((rls_filter_ff, 0), y_sink_rls) self.tb.connect((rls_filter_ff, 1), e_sink_rls) 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) y_sink_qrd_rls = blocks.vector_sink_f() e_sink_qrd_rls = blocks.vector_sink_f() self.tb.connect((qrd_rls_filter_ff, 0), y_sink_qrd_rls) self.tb.connect((qrd_rls_filter_ff, 1), e_sink_qrd_rls) 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) y_sink_iqrd_rls = blocks.vector_sink_f() e_sink_iqrd_rls = blocks.vector_sink_f() self.tb.connect((iqrd_rls_filter_ff, 0), y_sink_iqrd_rls) self.tb.connect((iqrd_rls_filter_ff, 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_ff.set_taps(np.zeros(self.n_taps, dtype=np.float)) nlms_filter_ff.set_taps(np.zeros(self.n_taps, dtype=np.float)) rls_filter_ff.set_taps(np.zeros(self.n_taps, dtype=np.float)) qrd_rls_filter_ff.set_taps(np.zeros(self.n_taps, dtype=np.float)) iqrd_rls_filter_ff.set_taps(np.zeros(self.n_taps, dtype=np.float)) # Some useful signal to be transmitted np.random.seed(i) 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 d_source = blocks.vector_source_f(d.tolist()) u_source = blocks.vector_source_f(u.tolist()) self.tb.connect(d_source, (lms_filter_ff, 0)) self.tb.connect(u_source, (lms_filter_ff, 1)) self.tb.connect(d_source, (nlms_filter_ff, 0)) self.tb.connect(u_source, (nlms_filter_ff, 1)) self.tb.connect(d_source, (rls_filter_ff, 0)) self.tb.connect(u_source, (rls_filter_ff, 1)) self.tb.connect(d_source, (qrd_rls_filter_ff, 0)) self.tb.connect(u_source, (qrd_rls_filter_ff, 1)) self.tb.connect(d_source, (iqrd_rls_filter_ff, 0)) self.tb.connect(u_source, (iqrd_rls_filter_ff, 1)) self.tb.run() self.tb.disconnect(d_source, (lms_filter_ff, 0)) self.tb.disconnect(u_source, (lms_filter_ff, 1)) self.tb.disconnect(d_source, (nlms_filter_ff, 0)) self.tb.disconnect(u_source, (nlms_filter_ff, 1)) self.tb.disconnect(d_source, (rls_filter_ff, 0)) self.tb.disconnect(u_source, (rls_filter_ff, 1)) self.tb.disconnect(d_source, (qrd_rls_filter_ff, 0)) self.tb.disconnect(u_source, (qrd_rls_filter_ff, 1)) self.tb.disconnect(d_source, (iqrd_rls_filter_ff, 0)) self.tb.disconnect(u_source, (iqrd_rls_filter_ff, 1)) e_data_lms = (np.array(e_sink_lms.data())**2).reshape( (self.n_ensemble, self.n_samples - h.size * 2)) e_data_nlms = (np.array(e_sink_nlms.data())**2).reshape( (self.n_ensemble, self.n_samples - h.size * 2)) e_data_rls = (np.array(e_sink_rls.data())**2).reshape( (self.n_ensemble, self.n_samples - h.size * 2)) e_data_qrd_rls = (np.array(e_sink_qrd_rls.data())**2).reshape( (self.n_ensemble, self.n_samples - h.size * 2)) e_data_iqrd_rls = (np.array(e_sink_iqrd_rls.data())**2).reshape( (self.n_ensemble, self.n_samples - h.size * 2)) 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()
def test_001_t (self): # Create 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.bypass, 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.bypass, 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 = blocks.vector_sink_f() e_sink = blocks.vector_sink_f() results = [] 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 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) self.tb.connect((filter_ff, 1), e_sink) self.tb.run() results.append(filter_ff.pc_throughput_avg() / 1e6) names.append(str(filter_ff.__class__.__name__).replace('_sptr','')) self.tb.disconnect(filter_ff) 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.6 # the width of the bars rects = ax.bar(index+width/2, results, width, label='Float (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 rects: 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() plt.savefig(fname='qa_performance_ff.png')