From 92076c98302da1a04fdf4d57d7f07aa46ccde22e Mon Sep 17 00:00:00 2001 From: andreas128 Date: Tue, 12 Sep 2017 17:42:33 +0200 Subject: Remove raw logging --- dpd/src/Measure.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/dpd/src/Measure.py b/dpd/src/Measure.py index 2450b8a..7a9246c 100644 --- a/dpd/src/Measure.py +++ b/dpd/src/Measure.py @@ -104,10 +104,6 @@ class Measure: txframe, tx_ts, rxframe, rx_ts = self.receive_tcp() - if logging.getLogger().getEffectiveLevel() == logging.DEBUG: - dt = datetime.datetime.now().isoformat() - txframe.tofile(logging_path + "/txframe_" + dt + ".iq") - # Normalize received signal with sent signal rx_median = np.median(np.abs(rxframe)) rxframe = rxframe / rx_median * np.median(np.abs(txframe)) -- cgit v1.2.3 From cc4a2fa49620306a16f131a54fbdc965a3d44056 Mon Sep 17 00:00:00 2001 From: andreas128 Date: Tue, 12 Sep 2017 17:46:25 +0200 Subject: Switch to linear regression; Cleanup --- dpd/src/Model.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/dpd/src/Model.py b/dpd/src/Model.py index 827027a..1606441 100644 --- a/dpd/src/Model.py +++ b/dpd/src/Model.py @@ -15,6 +15,7 @@ import numpy as np import matplotlib.pyplot as plt from sklearn import linear_model + class Model: """Calculates new coefficients using the measurement and the old coefficients""" @@ -25,8 +26,8 @@ class Model: MER, coefs_am, coefs_pm, - learning_rate_am=1., - learning_rate_pm=1., + learning_rate_am=0.1, + learning_rate_pm=0.1, plot=False): self.c = c self.SA = SA @@ -122,11 +123,9 @@ class Model: err = np.abs(rx_dpd) - np.abs(tx_choice) mse = np.mean(np.abs((rx_dpd - tx_choice) ** 2)) self.mses_am.append(mse) - self.errs_am.append(np.mean(err**2)) + self.errs_am.append(np.mean(err ** 2)) - reg = linear_model.Ridge(alpha=0.00001) - reg.fit(rx_A, err) - a_delta = reg.coef_ + a_delta = np.linalg.lstsq(rx_A, err)[0] new_coefs_am = self.coefs_am - self.learning_rate_am * a_delta new_coefs_am = new_coefs_am * (self.coefs_am[0] / new_coefs_am[0]) return new_coefs_am @@ -139,15 +138,13 @@ class Model: (np.abs(rx_choice) * np.abs(tx_choice)) ) plt.hist(phase_diff_choice) - plt.savefig('/tmp/hist_' + str(np.random.randint(0,1000)) + '.svg') + plt.savefig('/tmp/hist_' + str(np.random.randint(0, 1000)) + '.svg') plt.clf() phase_diff_est, phase_A = self.dpd_phase(rx_choice) err_phase = phase_diff_est - phase_diff_choice self.errs_pm.append(np.mean(np.abs(err_phase ** 2))) - reg = linear_model.Ridge(alpha=0.00001) - reg.fit(phase_A, err_phase) - p_delta = reg.coef_ + p_delta = np.linalg.lstsq(phase_A, err_phase)[0] new_coefs_pm = self.coefs_pm - self.learning_rate_pm * p_delta return new_coefs_pm, phase_diff_choice @@ -164,6 +161,11 @@ class Model: np.median(np.abs(tx_dpd)) + np.median(np.abs(rx_received))) assert normalization_error < 0.01, "Non normalized signals" + if logging.getLogger().getEffectiveLevel() == logging.DEBUG: + dt = datetime.datetime.now().isoformat() + tx_dpd.tofile(logging_path + "/tx_dpd_" + dt + ".iq") + rx_received.tofile(logging_path + "/rx_received_" + dt + ".iq") + tx_choice, rx_choice = self.sample_uniformly(tx_dpd, rx_received) new_coefs_am = self._next_am_coefficent(tx_choice, rx_choice) new_coefs_pm, phase_diff_choice = self._next_pm_coefficent(tx_choice, rx_choice) @@ -303,7 +305,7 @@ class Model: np.rad2deg(phase_range_dpd_new), linewidth=0.25, label="next") - ax.set_ylim(-60, 60) + ax.set_ylim(-180, 180) ax.set_xlim(0, 1) ax.legend() ax.set_title("Amplifier Characteristic") -- cgit v1.2.3 From 895a420ea692f2c32aa206e1cfa2758bfa79b8cd Mon Sep 17 00:00:00 2001 From: andreas128 Date: Wed, 13 Sep 2017 14:33:20 +0200 Subject: Change comment --- dpd/src/Model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dpd/src/Model.py b/dpd/src/Model.py index 1606441..a051927 100644 --- a/dpd/src/Model.py +++ b/dpd/src/Model.py @@ -17,7 +17,7 @@ from sklearn import linear_model class Model: - """Calculates new coefficients using the measurement and the old + """Calculates new coefficients using the measurement and the previous coefficients""" def __init__(self, -- cgit v1.2.3 From 5e2ea8d81bfb2d4916c57c6083cfbc874c723076 Mon Sep 17 00:00:00 2001 From: andreas128 Date: Wed, 13 Sep 2017 16:52:04 +0200 Subject: Add ExtractStatistic to condense infromation from several measurements --- dpd/src/ExtractStatistic.py | 158 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 dpd/src/ExtractStatistic.py diff --git a/dpd/src/ExtractStatistic.py b/dpd/src/ExtractStatistic.py new file mode 100644 index 0000000..8ae48ac --- /dev/null +++ b/dpd/src/ExtractStatistic.py @@ -0,0 +1,158 @@ +# -*- coding: utf-8 -*- +# +# DPD Calculation Engine, +# Extract statistic from data to use in Model +# +# http://www.opendigitalradio.org +# Licence: The MIT License, see notice at the end of this file + +import numpy as np +import pickle +import matplotlib.pyplot as plt + +import datetime +import os +import logging + +logging_path = os.path.dirname(logging.getLoggerClass().root.handlers[0].baseFilename) + + +def _check_input_extract(tx_dpd, rx_received): + # Check data type + assert tx_dpd[0].dtype == np.complex64, \ + "tx_dpd is not complex64 but {}".format(tx_dpd[0].dtype) + assert rx_received[0].dtype == np.complex64, \ + "rx_received is not complex64 but {}".format(rx_received[0].dtype) + # Check if signals have same normalization + normalization_error = np.abs(np.median(np.abs(tx_dpd)) - + np.median(np.abs(rx_received))) / ( + np.median(np.abs(tx_dpd)) + np.median(np.abs(rx_received))) + assert normalization_error < 0.01, "Non normalized signals" + + +class ExtractStatistic: + """Calculate a low variance RX value for equally spaced tx values + of a predefined range""" + + def __init__(self, + c, + plot=False): + self.c = c + + self.tx_boundaries = np.linspace(c.ES_start, c.ES_end, c.ES_n_bins + 1) + self.n_per_bin = c.ES_n_per_bin + + self.rx_values_lists = [] + for i in range(c.ES_n_bins): + self.rx_values_lists.append([]) + + self.tx_values_lists = [] + for i in range(c.ES_n_bins): + self.tx_values_lists.append([]) + + self.tx_values = self._tx_value_per_bin() + self.rx_values = [] + for i in range(c.ES_n_bins): + self.rx_values.append(None) + + self.plot = plot + + def _plot_and_log(self): + if logging.getLogger().getEffectiveLevel() == logging.DEBUG and self.plot: + dt = datetime.datetime.now().isoformat() + fig_path = logging_path + "/" + dt + "_ExtractStatistic.png" + sub_rows = 2 + sub_cols = 1 + fig = plt.figure(figsize=(sub_cols * 6, sub_rows / 2. * 6)) + i_sub = 0 + + i_sub += 1 + ax = plt.subplot(sub_rows, sub_cols, i_sub) + ax.plot(self.tx_values, self.rx_values, + label="Estimated Values", + color="red") + for i, tx_value in enumerate(self.tx_values): + rx_values = self.rx_values_lists[i] + ax.scatter(np.ones(len(rx_values)) * tx_value, + np.abs(rx_values), + s=0.1, + color="black") + ax.set_title("Extracted Statistic") + ax.set_xlabel("TX Amplitude") + ax.set_ylabel("RX Amplitude") + ax.set_ylim(0, 0.8) + ax.set_xlim(0, 1.1) + ax.legend(loc=4) + + num = [] + for i, tx_value in enumerate(self.tx_values): + rx_values = self.rx_values_lists[i] + num.append(len(rx_values)) + i_sub += 1 + ax = plt.subplot(sub_rows, sub_cols, i_sub) + ax.plot(num) + ax.set_xlabel("TX Amplitude") + ax.set_ylabel("Number of Samples") + ax.set_ylim(0, self.n_per_bin * 1.2) + + fig.tight_layout() + fig.savefig(fig_path) + fig.clf() + + pickle.dump(self.rx_values_lists, open("/tmp/rx_values", "wb")) + pickle.dump(self.tx_values, open("/tmp/tx_values", "wb")) + + def _rx_value_per_bin(self): + rx_values = [] + for values in self.rx_values_lists: + rx_values.append(np.mean(np.abs(values))) + return rx_values + + def _tx_value_per_bin(self): + tx_values = [] + for start, end in zip(self.tx_boundaries, self.tx_boundaries[1:]): + tx_values.append(np.mean((start, end))) + return tx_values + + def extract(self, tx_dpd, rx): + _check_input_extract(tx_dpd, rx) + + tx_abs = np.abs(tx_dpd) + for i, (tx_start, tx_end) in enumerate(zip(self.tx_boundaries, self.tx_boundaries[1:])): + mask = (tx_abs > tx_start) & (tx_abs < tx_end) + n_add = max(0, self.n_per_bin - len(self.rx_values_lists[i])) + self.rx_values_lists[i] += \ + list(rx[mask][:n_add]) + self.tx_values_lists[i] += \ + list(tx_dpd[mask][:n_add]) + + self.rx_values = self._rx_value_per_bin() + self.tx_values = self._tx_value_per_bin() + + self._plot_and_log() + + n_per_bin = [len(values) for values in self.rx_values_lists] + + return np.array(self.tx_values, dtype=np.float32), np.array(self.rx_values, dtype=np.float32), n_per_bin + +# The MIT License (MIT) +# +# Copyright (c) 2017 Andreas Steger +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. -- cgit v1.2.3 From d87d4e3931dbdc21b5b4678da782498cb4040b84 Mon Sep 17 00:00:00 2001 From: andreas128 Date: Wed, 13 Sep 2017 16:54:08 +0200 Subject: Add model_AM to calculate amplitude coefficients --- dpd/src/Model_AM.py | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 dpd/src/Model_AM.py diff --git a/dpd/src/Model_AM.py b/dpd/src/Model_AM.py new file mode 100644 index 0000000..5281dde --- /dev/null +++ b/dpd/src/Model_AM.py @@ -0,0 +1,115 @@ +# -*- coding: utf-8 -*- +# +# DPD Calculation Engine, model implementation for Amplitude and not Phase +# +# http://www.opendigitalradio.org +# Licence: The MIT License, see notice at the end of this file + +import datetime +import os +import logging + +logging_path = os.path.dirname(logging.getLoggerClass().root.handlers[0].baseFilename) + +import numpy as np +import matplotlib.pyplot as plt +from sklearn import linear_model + + +def check_input_get_next_coefs(tx_dpd, rx_received): + is_float32 = lambda x: (isinstance(x, np.ndarray) and + x.dtype == np.float32 and + x.flags.contiguous) + assert is_float32(tx_dpd), \ + "tx_dpd is not float32 but {}".format(tx_dpd[0].dtype) + assert is_float32(tx_dpd), \ + "rx_received is not float32 but {}".format(tx_dpd[0].dtype) + + +class Model_AM: + """Calculates new coefficients using the measurement and the previous + coefficients""" + + def __init__(self, + c, + learning_rate_am=0.1, + plot=False): + self.c = c + + self.learning_rate_am = learning_rate_am + self.plot = plot + + def _plot(self, tx_dpd, rx_received, coefs_am, coefs_am_new): + if logging.getLogger().getEffectiveLevel() == logging.DEBUG and self.plot: + tx_range, rx_est = self.calc_line(coefs_am, 0, 0.6) + tx_range_new, rx_est_new = self.calc_line(coefs_am_new, 0, 0.6) + + dt = datetime.datetime.now().isoformat() + fig_path = logging_path + "/" + dt + "_Model_AM.svg" + sub_rows = 1 + sub_cols = 1 + fig = plt.figure(figsize=(sub_cols * 6, sub_rows / 2. * 6)) + i_sub = 0 + + i_sub += 1 + ax = plt.subplot(sub_rows, sub_cols, i_sub) + ax.plot(tx_range, rx_est, + label="Estimated TX", + alpha=0.3, + color="gray") + ax.plot(tx_range_new, rx_est_new, + label="New Estimated TX", + color="red") + ax.scatter(tx_dpd, rx_received, + label="Binned Data", + color="blue", + s=0.1) + ax.set_title("Model_AM") + ax.set_xlabel("RX Amplitude") + ax.set_ylabel("TX Amplitude") + ax.legend(loc=4) + + fig.tight_layout() + fig.savefig(fig_path) + fig.clf() + + def poly(self, sig): + return np.array([sig ** i for i in range(1, 6)]).T + + def fit_poly(self, tx_abs, rx_abs): + return np.linalg.lstsq(self.poly(rx_abs), tx_abs)[0] + + def calc_line(self, coefs, min_amp, max_amp): + rx_range = np.linspace(min_amp, max_amp) + tx_est = np.sum(self.poly(rx_range) * coefs, axis=1) + return tx_est, rx_range + + def get_next_coefs(self, tx_dpd, rx_received, coefs_am): + check_input_get_next_coefs(tx_dpd, rx_received) + + coefs_am_new = self.fit_poly(tx_dpd, rx_received) + self._plot(tx_dpd, rx_received, coefs_am, coefs_am_new) + + return coefs_am_new + +# The MIT License (MIT) +# +# Copyright (c) 2017 Andreas Steger +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. -- cgit v1.2.3 From d2562d60af9f5d32c4e5ff89a1085962a9089d23 Mon Sep 17 00:00:00 2001 From: andreas128 Date: Wed, 13 Sep 2017 16:55:13 +0200 Subject: Add FSM to main.py --- dpd/main.py | 57 ++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/dpd/main.py b/dpd/main.py index 5e67c90..99bcf31 100755 --- a/dpd/main.py +++ b/dpd/main.py @@ -42,6 +42,8 @@ import numpy as np import traceback import src.Measure as Measure import src.Model as Model +import src.Model_AM as Model_AM +import src.ExtractStatistic as ExtractStatistic import src.Adapt as Adapt import src.Agc as Agc import src.TX_Agc as TX_Agc @@ -64,7 +66,7 @@ parser.add_argument('--samplerate', default='8192000', parser.add_argument('--coefs', default='poly.coef', help='File with DPD coefficients, which will be read by ODR-DabMod', required=False) -parser.add_argument('--txgain', default=71, +parser.add_argument('--txgain', default=73, help='TX Gain', required=False, type=int) @@ -103,17 +105,21 @@ MER = src.MER.MER(samplerate) c = src.const.const(samplerate) meas = Measure.Measure(samplerate, port, num_req) - +extStat = ExtractStatistic.ExtractStatistic(c, plot=True) adapt = Adapt.Adapt(port_rc, coef_path) -coefs_am, coefs_pm = adapt.get_coefs() + if cli_args.load_poly: + coefs_am, coefs_pm = adapt.get_coefs() model = Model.Model(c, SA, MER, coefs_am, coefs_pm, plot=True) else: - model = Model.Model(c, SA, MER, [1.0, 0, 0, 0, 0], [0, 0, 0, 0, 0], plot=True) + coefs_am, coefs_pm = [[1.0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] + model = Model.Model(c, SA, MER, coefs_am, coefs_pm, plot=True) +model_am = Model_AM.Model_AM(c, plot=True) adapt.set_coefs(model.coefs_am, model.coefs_pm) adapt.set_digital_gain(digital_gain) adapt.set_txgain(txgain) adapt.set_rxgain(rxgain) +print(coefs_am) tx_gain = adapt.get_txgain() rx_gain = adapt.get_rxgain() @@ -132,23 +138,36 @@ tx_agc = TX_Agc.TX_Agc(adapt) agc = Agc.Agc(meas, adapt) agc.run() -for i in range(num_iter): +state = "measure" +i = 0 +while i < num_iter: try: - txframe_aligned, tx_ts, rxframe_aligned, rx_ts, rx_median = meas.get_samples() - logging.debug("tx_ts {}, rx_ts {}".format(tx_ts, rx_ts)) - assert tx_ts - rx_ts < 1e-5, "Time stamps do not match." - - if tx_agc.adapt_if_necessary(txframe_aligned): - continue - - coefs_am, coefs_pm = model.get_next_coefs(txframe_aligned, rxframe_aligned) - adapt.set_coefs(coefs_am, coefs_pm) + # Measure + if state == "measure": + txframe_aligned, tx_ts, rxframe_aligned, rx_ts, rx_median = meas.get_samples() + tx, rx, n_per_bin = extStat.extract(txframe_aligned, rxframe_aligned) + n_use = int(len(n_per_bin) * 0.6) + tx = tx[:n_use] + rx = rx[:n_use] + if all(c.ES_n_per_bin == np.array(n_per_bin)[0:n_use]): + state = "model" + else: + state = "measure" + + # Model + elif state == "model": + coefs_am = model_am.get_next_coefs(tx, rx, coefs_am) + del extStat + extStat = ExtractStatistic.ExtractStatistic(c, plot=True) + state = "adapt" + + # Adapt + elif state == "adapt": + print(coefs_am) + adapt.set_coefs(coefs_am, coefs_pm) + state = "measure" + i += 1 - off = SA.calc_offset(txframe_aligned) - tx_mer = MER.calc_mer(txframe_aligned[off:off + c.T_U]) - rx_mer = MER.calc_mer(rxframe_aligned[off:off + c.T_U], debug=True) - logging.info("MER with lag in it. {}: TX {}, RX {}". - format(i, tx_mer, rx_mer)) except Exception as e: logging.warning("Iteration {} failed.".format(i)) logging.warning(traceback.format_exc()) -- cgit v1.2.3 From bd5152da189b429496e8f88f60b61f75e7fe3436 Mon Sep 17 00:00:00 2001 From: andreas128 Date: Wed, 13 Sep 2017 16:57:29 +0200 Subject: Add constants for ExtractStatistic --- dpd/src/const.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/dpd/src/const.py b/dpd/src/const.py index 1011c6c..6cf1537 100644 --- a/dpd/src/const.py +++ b/dpd/src/const.py @@ -36,3 +36,9 @@ class const: # frequency per bin = 1kHz # phase difference per sample offset = delta_t * 2 * pi * delta_freq self.phase_offset_per_sample = 1. / sample_rate * 2 * np.pi * 1000 + + # Constants for ExtractStatistic + self.ES_start = 0.0 + self.ES_end = 1.0 + self.ES_n_bins = 64 + self.ES_n_per_bin = 1024 -- cgit v1.2.3 From a6ab5661fabc51aa1bdfc48c44bdef4b1f30e095 Mon Sep 17 00:00:00 2001 From: andreas128 Date: Wed, 13 Sep 2017 18:30:26 +0200 Subject: Add phase plot --- dpd/src/ExtractStatistic.py | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/dpd/src/ExtractStatistic.py b/dpd/src/ExtractStatistic.py index 8ae48ac..d2aeee4 100644 --- a/dpd/src/ExtractStatistic.py +++ b/dpd/src/ExtractStatistic.py @@ -51,6 +51,7 @@ class ExtractStatistic: self.tx_values_lists.append([]) self.tx_values = self._tx_value_per_bin() + self.rx_values = [] for i in range(c.ES_n_bins): self.rx_values.append(None) @@ -59,9 +60,12 @@ class ExtractStatistic: def _plot_and_log(self): if logging.getLogger().getEffectiveLevel() == logging.DEBUG and self.plot: + phase_diffs_values_lists = self._phase_diff_list_per_bin() + phase_diffs_values = self._phase_diff_value_per_bin(phase_diffs_values_lists) + dt = datetime.datetime.now().isoformat() fig_path = logging_path + "/" + dt + "_ExtractStatistic.png" - sub_rows = 2 + sub_rows = 3 sub_cols = 1 fig = plt.figure(figsize=(sub_cols * 6, sub_rows / 2. * 6)) i_sub = 0 @@ -84,6 +88,23 @@ class ExtractStatistic: ax.set_xlim(0, 1.1) ax.legend(loc=4) + i_sub += 1 + ax = plt.subplot(sub_rows, sub_cols, i_sub) + ax.plot(self.tx_values, np.rad2deg(phase_diffs_values), + label="Estimated Values", + color="red") + for i, tx_value in enumerate(self.tx_values): + phase_diff = phase_diffs_values_lists[i] + ax.scatter(np.ones(len(phase_diff)) * tx_value, + np.rad2deg(phase_diff), + s=0.1, + color="black") + ax.set_xlabel("TX Amplitude") + ax.set_ylabel("Phase Difference") + ax.set_ylim(-60,60) + ax.set_xlim(0, 1.1) + ax.legend(loc=4) + num = [] for i, tx_value in enumerate(self.tx_values): rx_values = self.rx_values_lists[i] @@ -114,6 +135,21 @@ class ExtractStatistic: tx_values.append(np.mean((start, end))) return tx_values + def _phase_diff_list_per_bin(self): + phase_values_lists = [] + for tx_list, rx_list in zip(self.tx_values_lists, self.rx_values_lists): + phase_diffs = [] + for tx, rx in zip(tx_list, rx_list): + phase_diffs.append(np.angle(rx * tx.conjugate())) + phase_values_lists.append(phase_diffs) + return phase_values_lists + + def _phase_diff_value_per_bin(self, phase_diffs_values_lists): + phase_list = [] + for values in phase_diffs_values_lists: + phase_list.append(np.mean(values)) + return phase_list + def extract(self, tx_dpd, rx): _check_input_extract(tx_dpd, rx) @@ -133,7 +169,9 @@ class ExtractStatistic: n_per_bin = [len(values) for values in self.rx_values_lists] - return np.array(self.tx_values, dtype=np.float32), np.array(self.rx_values, dtype=np.float32), n_per_bin + return np.array(self.tx_values, dtype=np.float32), \ + np.array(self.rx_values, dtype=np.float32), \ + n_per_bin # The MIT License (MIT) # -- cgit v1.2.3 From 426671f4af2a5d1d5d01225194eb66b2edb8c059 Mon Sep 17 00:00:00 2001 From: andreas128 Date: Wed, 13 Sep 2017 18:34:05 +0200 Subject: Fix non closed figures --- dpd/src/Agc.py | 2 +- dpd/src/Dab_Util.py | 10 +++++----- dpd/src/ExtractStatistic.py | 2 +- dpd/src/MER.py | 2 +- dpd/src/Model.py | 4 ++-- dpd/src/Model_AM.py | 2 +- dpd/src/phase_align.py | 2 +- dpd/src/subsample_align.py | 2 +- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/dpd/src/Agc.py b/dpd/src/Agc.py index 978b607..b83c91e 100644 --- a/dpd/src/Agc.py +++ b/dpd/src/Agc.py @@ -139,7 +139,7 @@ class Agc: fig.tight_layout() fig.savefig(fig_path) - fig.clf() + plt.close(fig) # The MIT License (MIT) diff --git a/dpd/src/Dab_Util.py b/dpd/src/Dab_Util.py index e3dbfe3..37be5db 100644 --- a/dpd/src/Dab_Util.py +++ b/dpd/src/Dab_Util.py @@ -49,7 +49,7 @@ class Dab_Util: plt.plot(c, label="corr") plt.legend() plt.savefig(corr_path) - plt.clf() + plt.close() return np.argmax(c) - off + 1 @@ -118,7 +118,7 @@ class Dab_Util: fig.tight_layout() fig.savefig(fig_path) - fig.clf() + plt.close(fig) off_meas = self.lag_upsampling(sig_rx, sig_tx, n_up=1) off = int(abs(off_meas)) @@ -161,7 +161,7 @@ class Dab_Util: fig.tight_layout() fig.savefig(fig_path) - fig.clf() + plt.close(fig) sig_rx = sa.subsample_align(sig_rx, sig_tx) @@ -185,7 +185,7 @@ class Dab_Util: fig.tight_layout() fig.savefig(fig_path) - fig.clf() + plt.close(fig) sig_rx = pa.phase_align(sig_rx, sig_tx) @@ -209,7 +209,7 @@ class Dab_Util: fig.tight_layout() fig.savefig(fig_path) - fig.clf() + plt.close(fig) logging.debug("Sig1_cut: %d %s, Sig2_cut: %d %s, off: %d" % (len(sig_tx), sig_tx.dtype, len(sig_rx), sig_rx.dtype, off)) return sig_tx, sig_rx diff --git a/dpd/src/ExtractStatistic.py b/dpd/src/ExtractStatistic.py index d2aeee4..6139e1d 100644 --- a/dpd/src/ExtractStatistic.py +++ b/dpd/src/ExtractStatistic.py @@ -118,7 +118,7 @@ class ExtractStatistic: fig.tight_layout() fig.savefig(fig_path) - fig.clf() + plt.close(fig) pickle.dump(self.rx_values_lists, open("/tmp/rx_values", "wb")) pickle.dump(self.tx_values, open("/tmp/tx_values", "wb")) diff --git a/dpd/src/MER.py b/dpd/src/MER.py index 00fcc23..4f2918e 100644 --- a/dpd/src/MER.py +++ b/dpd/src/MER.py @@ -106,7 +106,7 @@ class MER: plt.tight_layout() plt.savefig(fig_path) plt.show() - plt.clf() + plt.close() MER_res = 20 * np.log10(np.mean([10 ** (MER / 20) for MER in MERs])) return MER_res diff --git a/dpd/src/Model.py b/dpd/src/Model.py index a051927..fe9c56b 100644 --- a/dpd/src/Model.py +++ b/dpd/src/Model.py @@ -139,7 +139,7 @@ class Model: ) plt.hist(phase_diff_choice) plt.savefig('/tmp/hist_' + str(np.random.randint(0, 1000)) + '.svg') - plt.clf() + plt.close() phase_diff_est, phase_A = self.dpd_phase(rx_choice) err_phase = phase_diff_est - phase_diff_choice self.errs_pm.append(np.mean(np.abs(err_phase ** 2))) @@ -326,7 +326,7 @@ class Model: fig.tight_layout() fig.savefig(fig_path) - fig.clf() + plt.close(fig) self.coefs_am = new_coefs_am self.coefs_am_history.append(self.coefs_am) diff --git a/dpd/src/Model_AM.py b/dpd/src/Model_AM.py index 5281dde..bdf55c6 100644 --- a/dpd/src/Model_AM.py +++ b/dpd/src/Model_AM.py @@ -71,7 +71,7 @@ class Model_AM: fig.tight_layout() fig.savefig(fig_path) - fig.clf() + plt.close(fig) def poly(self, sig): return np.array([sig ** i for i in range(1, 6)]).T diff --git a/dpd/src/phase_align.py b/dpd/src/phase_align.py index 7f82392..7317d70 100644 --- a/dpd/src/phase_align.py +++ b/dpd/src/phase_align.py @@ -75,7 +75,7 @@ def phase_align(sig, ref_sig, plot=False): plt.legend(loc=4) plt.tight_layout() plt.savefig(fig_path) - plt.clf() + plt.close() return sig diff --git a/dpd/src/subsample_align.py b/dpd/src/subsample_align.py index 6d1cd2a..b0cbe88 100755 --- a/dpd/src/subsample_align.py +++ b/dpd/src/subsample_align.py @@ -78,7 +78,7 @@ def subsample_align(sig, ref_sig, plot=False): plt.plot(ixs, taus) plt.title("Subsample correlation, minimum is best: {}".format(best_tau)) plt.savefig(tau_path) - plt.clf() + plt.close() # Prepare rotate_vec = fft_sig with rotated phase rotate_vec = np.exp(1j * best_tau * omega) -- cgit v1.2.3 From 0718c0390d9f05ef21ec202be2ce7ea6e2a6a31d Mon Sep 17 00:00:00 2001 From: andreas128 Date: Thu, 14 Sep 2017 11:21:10 +0200 Subject: Add Model_PM --- dpd/main.py | 6 ++- dpd/src/ExtractStatistic.py | 5 ++ dpd/src/Model_AM.py | 2 +- dpd/src/Model_PM.py | 118 ++++++++++++++++++++++++++++++++++++++++++++ dpd/src/const.py | 2 +- 5 files changed, 130 insertions(+), 3 deletions(-) create mode 100644 dpd/src/Model_PM.py diff --git a/dpd/main.py b/dpd/main.py index 99bcf31..320c291 100755 --- a/dpd/main.py +++ b/dpd/main.py @@ -43,6 +43,7 @@ import traceback import src.Measure as Measure import src.Model as Model import src.Model_AM as Model_AM +import src.Model_PM as Model_PM import src.ExtractStatistic as ExtractStatistic import src.Adapt as Adapt import src.Agc as Agc @@ -115,6 +116,7 @@ else: coefs_am, coefs_pm = [[1.0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] model = Model.Model(c, SA, MER, coefs_am, coefs_pm, plot=True) model_am = Model_AM.Model_AM(c, plot=True) +model_pm = Model_PM.Model_PM(c, plot=True) adapt.set_coefs(model.coefs_am, model.coefs_pm) adapt.set_digital_gain(digital_gain) adapt.set_txgain(txgain) @@ -145,10 +147,11 @@ while i < num_iter: # Measure if state == "measure": txframe_aligned, tx_ts, rxframe_aligned, rx_ts, rx_median = meas.get_samples() - tx, rx, n_per_bin = extStat.extract(txframe_aligned, rxframe_aligned) + tx, rx, phase_diff, n_per_bin = extStat.extract(txframe_aligned, rxframe_aligned) n_use = int(len(n_per_bin) * 0.6) tx = tx[:n_use] rx = rx[:n_use] + phase_diff = phase_diff[:n_use] if all(c.ES_n_per_bin == np.array(n_per_bin)[0:n_use]): state = "model" else: @@ -157,6 +160,7 @@ while i < num_iter: # Model elif state == "model": coefs_am = model_am.get_next_coefs(tx, rx, coefs_am) + coefs_pm = model_pm.get_next_coefs(tx, phase_diff, coefs_pm) del extStat extStat = ExtractStatistic.ExtractStatistic(c, plot=True) state = "adapt" diff --git a/dpd/src/ExtractStatistic.py b/dpd/src/ExtractStatistic.py index 6139e1d..897ec0a 100644 --- a/dpd/src/ExtractStatistic.py +++ b/dpd/src/ExtractStatistic.py @@ -169,8 +169,13 @@ class ExtractStatistic: n_per_bin = [len(values) for values in self.rx_values_lists] + # TODO cleanup + phase_diffs_values_lists = self._phase_diff_list_per_bin() + phase_diffs_values = self._phase_diff_value_per_bin(phase_diffs_values_lists) + return np.array(self.tx_values, dtype=np.float32), \ np.array(self.rx_values, dtype=np.float32), \ + np.array(phase_diffs_values, dtype=np.float32), \ n_per_bin # The MIT License (MIT) diff --git a/dpd/src/Model_AM.py b/dpd/src/Model_AM.py index bdf55c6..5c307ef 100644 --- a/dpd/src/Model_AM.py +++ b/dpd/src/Model_AM.py @@ -22,7 +22,7 @@ def check_input_get_next_coefs(tx_dpd, rx_received): x.flags.contiguous) assert is_float32(tx_dpd), \ "tx_dpd is not float32 but {}".format(tx_dpd[0].dtype) - assert is_float32(tx_dpd), \ + assert is_float32(rx_received), \ "rx_received is not float32 but {}".format(tx_dpd[0].dtype) diff --git a/dpd/src/Model_PM.py b/dpd/src/Model_PM.py new file mode 100644 index 0000000..6639382 --- /dev/null +++ b/dpd/src/Model_PM.py @@ -0,0 +1,118 @@ +# -*- coding: utf-8 -*- +# +# DPD Calculation Engine, model implementation for Amplitude and not Phase +# +# http://www.opendigitalradio.org +# Licence: The MIT License, see notice at the end of this file + +import datetime +import os +import logging + +logging_path = os.path.dirname(logging.getLoggerClass().root.handlers[0].baseFilename) + +import numpy as np +import matplotlib.pyplot as plt +from sklearn import linear_model + + +def check_input_get_next_coefs(tx_dpd, phase_diff): + is_float32 = lambda x: (isinstance(x, np.ndarray) and + x.dtype == np.float32 and + x.flags.contiguous) + assert is_float32(tx_dpd), \ + "tx_dpd is not float32 but {}".format(tx_dpd[0].dtype) + assert is_float32(phase_diff), \ + "phase_diff is not float32 but {}".format(tx_dpd[0].dtype) + assert tx_dpd.shape == phase_diff.shape, \ + "tx_dpd.shape {}, phase_diff.shape {}".format( + tx_dpd.shape, phase_diff.shape) + + +class Model_PM: + """Calculates new coefficients using the measurement and the previous + coefficients""" + + def __init__(self, + c, + learning_rate_pm=0.1, + plot=False): + self.c = c + + self.learning_rate_pm = learning_rate_pm + self.plot = plot + + def _plot(self, tx_dpd, phase_diff, coefs_pm, coefs_pm_new): + if logging.getLogger().getEffectiveLevel() == logging.DEBUG and self.plot: + tx_range, phase_diff_est = self.calc_line(coefs_pm, 0, 0.6) + tx_range_new, phase_diff_est_new = self.calc_line(coefs_pm_new, 0, 0.6) + + dt = datetime.datetime.now().isoformat() + fig_path = logging_path + "/" + dt + "_Model_PM.svg" + sub_rows = 1 + sub_cols = 1 + fig = plt.figure(figsize=(sub_cols * 6, sub_rows / 2. * 6)) + i_sub = 0 + + i_sub += 1 + ax = plt.subplot(sub_rows, sub_cols, i_sub) + ax.plot(tx_range, phase_diff_est, + label="Estimated Phase Diff", + alpha=0.3, + color="gray") + ax.plot(tx_range_new, phase_diff_est_new, + label="New Estimated Phase Diff", + color="red") + ax.scatter(tx_dpd, phase_diff, + label="Binned Data", + color="blue", + s=0.1) + ax.set_title("Model_PM") + ax.set_xlabel("TX Amplitude") + ax.set_ylabel("Phase DIff") + ax.legend(loc=4) + + fig.tight_layout() + fig.savefig(fig_path) + plt.close(fig) + + def poly(self, sig): + return np.array([sig ** i for i in range(0, 5)]).T + + def fit_poly(self, tx_abs, phase_diff): + return np.linalg.lstsq(self.poly(tx_abs), phase_diff)[0] + + def calc_line(self, coefs, min_amp, max_amp): + tx_range = np.linspace(min_amp, max_amp) + phase_diff = np.sum(self.poly(tx_range) * coefs, axis=1) + return tx_range, phase_diff + + def get_next_coefs(self, tx_dpd, phase_diff, coefs_pm): + check_input_get_next_coefs(tx_dpd, phase_diff) + + coefs_pm_new = self.fit_poly(tx_dpd, phase_diff) + self._plot(tx_dpd, phase_diff, coefs_pm, coefs_pm_new) + + return coefs_pm_new + +# The MIT License (MIT) +# +# Copyright (c) 2017 Andreas Steger +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. diff --git a/dpd/src/const.py b/dpd/src/const.py index 6cf1537..75ff819 100644 --- a/dpd/src/const.py +++ b/dpd/src/const.py @@ -41,4 +41,4 @@ class const: self.ES_start = 0.0 self.ES_end = 1.0 self.ES_n_bins = 64 - self.ES_n_per_bin = 1024 + self.ES_n_per_bin = 256 -- cgit v1.2.3 From e55b7fc41a1744cf1fd558a4ddfb8698efbd3793 Mon Sep 17 00:00:00 2001 From: andreas128 Date: Thu, 14 Sep 2017 11:22:31 +0200 Subject: Fix plot labels --- dpd/src/Model_AM.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dpd/src/Model_AM.py b/dpd/src/Model_AM.py index 5c307ef..ef6cc6c 100644 --- a/dpd/src/Model_AM.py +++ b/dpd/src/Model_AM.py @@ -65,8 +65,8 @@ class Model_AM: color="blue", s=0.1) ax.set_title("Model_AM") - ax.set_xlabel("RX Amplitude") - ax.set_ylabel("TX Amplitude") + ax.set_xlabel("TX Amplitude") + ax.set_ylabel("RX Amplitude") ax.legend(loc=4) fig.tight_layout() -- cgit v1.2.3 From 4193a475bbdd034d86e2b14ea97300cc999bd994 Mon Sep 17 00:00:00 2001 From: andreas128 Date: Thu, 14 Sep 2017 12:08:25 +0200 Subject: Change Adapt coef return to float32 array --- dpd/src/Adapt.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dpd/src/Adapt.py b/dpd/src/Adapt.py index b4042d6..8f33b70 100644 --- a/dpd/src/Adapt.py +++ b/dpd/src/Adapt.py @@ -147,7 +147,8 @@ class Adapt: .format(path, n_coefs, coefs)) i += 1 f.close() - return (coefs_am_out, coefs_pm_out) + return (np.array(coefs_am_out, dtype=np.float32), + np.array(coefs_pm_out, dtype=np.float32)) def get_coefs(self): return self._read_coef_file(self.coef_path) -- cgit v1.2.3 From e7e7e81730961bba6c8910c21f34616a7548afcb Mon Sep 17 00:00:00 2001 From: andreas128 Date: Thu, 14 Sep 2017 12:09:38 +0200 Subject: Simplify argparse --- dpd/main.py | 10 +++++----- dpd/src/Model_Poly.py | 0 2 files changed, 5 insertions(+), 5 deletions(-) create mode 100644 dpd/src/Model_Poly.py diff --git a/dpd/main.py b/dpd/main.py index 320c291..e17cd5a 100755 --- a/dpd/main.py +++ b/dpd/main.py @@ -55,13 +55,13 @@ import argparse parser = argparse.ArgumentParser( description="DPD Computation Engine for ODR-DabMod") -parser.add_argument('--port', default='50055', +parser.add_argument('--port', default=50055, type=int, help='port of DPD server to connect to (default: 50055)', required=False) -parser.add_argument('--rc-port', default='9400', +parser.add_argument('--rc-port', default=9400, type=int, help='port of ODR-DabMod ZMQ Remote Control to connect to (default: 9400)', required=False) -parser.add_argument('--samplerate', default='8192000', +parser.add_argument('--samplerate', default=8192000, type=int, help='Sample rate', required=False) parser.add_argument('--coefs', default='poly.coef', @@ -79,10 +79,10 @@ parser.add_argument('--digital_gain', default=1, help='Digital Gain', required=False, type=float) -parser.add_argument('--samps', default='81920', +parser.add_argument('--samps', default='81920', type=int, help='Number of samples to request from ODR-DabMod', required=False) -parser.add_argument('-i', '--iterations', default='1', +parser.add_argument('-i', '--iterations', default=1, type=int, help='Number of iterations to run', required=False) parser.add_argument('-l', '--load-poly', diff --git a/dpd/src/Model_Poly.py b/dpd/src/Model_Poly.py new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3 From 594fcefc353fec548ace0b431355121478aa4c1e Mon Sep 17 00:00:00 2001 From: andreas128 Date: Thu, 14 Sep 2017 12:10:16 +0200 Subject: Pack Model_AM and Model_PM into new Model_Poly --- dpd/main.py | 33 +++++++--------- dpd/src/Model_Poly.py | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 20 deletions(-) diff --git a/dpd/main.py b/dpd/main.py index e17cd5a..528f59c 100755 --- a/dpd/main.py +++ b/dpd/main.py @@ -42,9 +42,8 @@ import numpy as np import traceback import src.Measure as Measure import src.Model as Model -import src.Model_AM as Model_AM -import src.Model_PM as Model_PM import src.ExtractStatistic as ExtractStatistic +import src.Model_Poly import src.Adapt as Adapt import src.Agc as Agc import src.TX_Agc as TX_Agc @@ -91,15 +90,15 @@ parser.add_argument('-l', '--load-poly', cli_args = parser.parse_args() -port = int(cli_args.port) -port_rc = int(cli_args.rc_port) +port = cli_args.port +port_rc = cli_args.rc_port coef_path = cli_args.coefs digital_gain = cli_args.digital_gain txgain = cli_args.txgain rxgain = cli_args.rxgain -num_req = int(cli_args.samps) -samplerate = int(cli_args.samplerate) -num_iter = int(cli_args.iterations) +num_req = cli_args.samps +samplerate = cli_args.samplerate +num_iter = cli_args.iterations SA = src.Symbol_align.Symbol_align(samplerate) MER = src.MER.MER(samplerate) @@ -109,19 +108,15 @@ meas = Measure.Measure(samplerate, port, num_req) extStat = ExtractStatistic.ExtractStatistic(c, plot=True) adapt = Adapt.Adapt(port_rc, coef_path) -if cli_args.load_poly: - coefs_am, coefs_pm = adapt.get_coefs() - model = Model.Model(c, SA, MER, coefs_am, coefs_pm, plot=True) -else: - coefs_am, coefs_pm = [[1.0, 0, 0, 0, 0], [0, 0, 0, 0, 0]] - model = Model.Model(c, SA, MER, coefs_am, coefs_pm, plot=True) -model_am = Model_AM.Model_AM(c, plot=True) -model_pm = Model_PM.Model_PM(c, plot=True) -adapt.set_coefs(model.coefs_am, model.coefs_pm) +coefs_am, coefs_pm = adapt.get_coefs() +model_poly = src.Model_Poly.Model_Poly(c, coefs_am, coefs_pm, plot=True) +if not cli_args.load_poly: + coefs_am, coefs_pm = model_poly.get_default_coefs() + +adapt.set_coefs(model_poly.coefs_am, model_poly.coefs_pm) adapt.set_digital_gain(digital_gain) adapt.set_txgain(txgain) adapt.set_rxgain(rxgain) -print(coefs_am) tx_gain = adapt.get_txgain() rx_gain = adapt.get_rxgain() @@ -159,15 +154,13 @@ while i < num_iter: # Model elif state == "model": - coefs_am = model_am.get_next_coefs(tx, rx, coefs_am) - coefs_pm = model_pm.get_next_coefs(tx, phase_diff, coefs_pm) + coefs_am, coefs_pm = model_poly.get_next_coefs(tx, rx, phase_diff) del extStat extStat = ExtractStatistic.ExtractStatistic(c, plot=True) state = "adapt" # Adapt elif state == "adapt": - print(coefs_am) adapt.set_coefs(coefs_am, coefs_pm) state = "measure" i += 1 diff --git a/dpd/src/Model_Poly.py b/dpd/src/Model_Poly.py index e69de29..1faff24 100644 --- a/dpd/src/Model_Poly.py +++ b/dpd/src/Model_Poly.py @@ -0,0 +1,104 @@ +# -*- coding: utf-8 -*- +# +# DPD Calculation Engine, model implementation using polynomial +# +# http://www.opendigitalradio.org +# Licence: The MIT License, see notice at the end of this file + +import datetime +import os +import logging + +logging_path = os.path.dirname(logging.getLoggerClass().root.handlers[0].baseFilename) + +import numpy as np +import matplotlib.pyplot as plt +from sklearn import linear_model + +import src.Model_AM as Model_AM +import src.Model_PM as Model_PM + + +def assert_np_float32(x): + assert isinstance(x, np.ndarray) + assert x.dtype == np.float32 + assert x.flags.contiguous + +def _check_input_get_next_coefs(tx_abs, rx_abs, phase_diff): + assert_np_float32(tx_abs) + assert_np_float32(rx_abs) + assert_np_float32(phase_diff) + + assert tx_abs.shape == rx_abs.shape, \ + "tx_abs.shape {}, rx_abs.shape {}".format( + tx_abs.shape, rx_abs.shape) + assert tx_abs.shape == phase_diff.shape, \ + "tx_abs.shape {}, phase_diff.shape {}".format( + tx_abs.shape, phase_diff.shape) + + +class Model_Poly: + """Calculates new coefficients using the measurement and the previous + coefficients""" + + def __init__(self, + c, + coefs_am, + coefs_pm, + learning_rate_am=1.0, + learning_rate_pm=1.0, + plot=False): + assert_np_float32(coefs_am) + assert_np_float32(coefs_pm) + + self.c = c + + self.learning_rate_am = learning_rate_am + self.learning_rate_pm = learning_rate_pm + + self.coefs_am = coefs_am + self.coefs_pm = coefs_pm + + self.model_am = Model_AM.Model_AM(c, plot=True) + self.model_pm = Model_PM.Model_PM(c, plot=True) + + self.plot = plot + + def get_default_coefs(self): + self.coefs_am[:] = 0 + self.coefs_am[0] = 1 + self.coefs_pm[:] = 0 + return self.coefs_am, self.coefs_pm + + def get_next_coefs(self, tx_abs, rx_abs, phase_diff): + _check_input_get_next_coefs(tx_abs, rx_abs, phase_diff) + + coefs_am_new = self.model_am.get_next_coefs(tx_abs, rx_abs, self.coefs_am) + coefs_pm_new = self.model_pm.get_next_coefs(tx_abs, phase_diff, self.coefs_pm) + + self.coefs_am = self.coefs_am + (coefs_am_new - self.coefs_am) * self.learning_rate_am + self.coefs_pm = self.coefs_pm + (coefs_pm_new - self.coefs_pm) * self.learning_rate_pm + + return self.coefs_am, self.coefs_pm + +# The MIT License (MIT) +# +# Copyright (c) 2017 Andreas Steger +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. -- cgit v1.2.3 From bf32d4e1efb87eb7a51207281f2565ee54e1aee2 Mon Sep 17 00:00:00 2001 From: andreas128 Date: Thu, 14 Sep 2017 14:21:17 +0200 Subject: Add report in main --- dpd/main.py | 18 +++++++++++++++++- dpd/src/Symbol_align.py | 3 ++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/dpd/main.py b/dpd/main.py index 528f59c..208a4ff 100755 --- a/dpd/main.py +++ b/dpd/main.py @@ -162,9 +162,25 @@ while i < num_iter: # Adapt elif state == "adapt": adapt.set_coefs(coefs_am, coefs_pm) - state = "measure" + state = "report" i += 1 + # Report + elif state == "report": + try: + off = SA.calc_offset(txframe_aligned) + tx_mer = MER.calc_mer(txframe_aligned[off:off+c.T_U], debug=True) + rx_mer = MER.calc_mer(rxframe_aligned[off:off+c.T_U], debug=True) + mse = np.mean(np.abs((txframe_aligned - rxframe_aligned)**2)) + logging.info("It {}: TX_MER {}, RX_MER {}," \ + " MSE {}, coefs_am {}, coefs_pm {}". + format(i, tx_mer, rx_mer, mse, coefs_am, coefs_pm)) + state = "measure" + except: + logging.warning("Iteration {}: Report failed.".format(i)) + logging.warning(traceback.format_exc()) + state = "measure" + except Exception as e: logging.warning("Iteration {} failed.".format(i)) logging.warning(traceback.format_exc()) diff --git a/dpd/src/Symbol_align.py b/dpd/src/Symbol_align.py index 05a9049..6c814a8 100644 --- a/dpd/src/Symbol_align.py +++ b/dpd/src/Symbol_align.py @@ -147,7 +147,8 @@ class Symbol_align: delta_sample_int = np.round(delta_sample).astype(int) error = np.abs(delta_sample_int - delta_sample) if error > 0.1: - raise RuntimeError("Could not calculate sample offset") + raise RuntimeError("Could not calculate " \ + "sample offset. Error {}".format(error)) return delta_sample_int def calc_offset(self, tx): -- cgit v1.2.3