summaryrefslogtreecommitdiffstats
path: root/dpd/src/Model_Poly.py
blob: 16881adad87feb4d33d5808193fb022ca291a800 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# -*- 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 os
import logging

logging_path = os.path.dirname(logging.getLoggerClass().root.handlers[0].baseFilename)

import numpy as np

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 Poly:
    """Calculates new coefficients using the measurement and the previous
    coefficients"""

    def __init__(self,
                 c,
                 learning_rate_am=1.0,
                 learning_rate_pm=1.0):
        self.c = c
        self.plot = c.MDL_plot

        self.learning_rate_am = learning_rate_am
        self.learning_rate_pm = learning_rate_pm

        self.reset_coefs()

        self.model_am = Model_AM.Model_AM(c, plot=self.plot)
        self.model_pm = Model_PM.Model_PM(c, plot=self.plot)

        self.plot = c.MDL_plot

    def reset_coefs(self):
        self.coefs_am = np.zeros(5, dtype=np.float32)
        self.coefs_am[0] = 1
        self.coefs_pm = np.zeros(5, dtype=np.float32)
        return self.coefs_am, self.coefs_pm

    def train(self, tx_abs, rx_abs, phase_diff, lr=None):
        """
        :type tx_abs: np.ndarray
        :type rx_abs: np.ndarray
        :type phase_diff: np.ndarray
        :type lr: float
        """
        _check_input_get_next_coefs(tx_abs, rx_abs, phase_diff)

        if not lr is None:
            self.model_am.learning_rate_am = lr
            self.model_pm.learning_rate_pm = lr

        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

    def get_dpd_data(self):
        return "poly", 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.