summaryrefslogtreecommitdiffstats
path: root/dpd/src/Symbol_align.py
blob: 2f871bccdab2cdd004660d074d396846932a7bb0 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# -*- coding: utf-8 -*-
#
# Modulation Error Rate
#
# http://www.opendigitalradio.org
# Licence: The MIT License, see notice at the end of this file

import datetime
import os
import logging
import time
try:
    logging_path = os.path.dirname(logging.getLoggerClass().root.handlers[0].baseFilename)
except:
    logging_path = "/tmp/"

import numpy as np
import src.const
import scipy
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt

class Symbol_align:
    """
    Find the phase offset to the start of the DAB symbols in an
    unaligned dab signal.
    """
    def __init__(self, sample_rate, plot=False):
        self.c = src.const.const(sample_rate)
        self.plot = plot
        pass

    def _calc_offset_to_first_symbol_without_prefix(self, tx):
        tx_orig = tx[0:-self.c.T_U]
        tx_cut_prefix = tx[self.c.T_U:]

        tx_product = np.abs(tx_orig - tx_cut_prefix)
        tx_product_avg = np.correlate(
            tx_product,
            np.ones(self.c.T_C),
            mode='valid')
        tx_product_avg_min_filt = \
            scipy.ndimage.filters.minimum_filter1d(
                tx_product_avg,
                int(1.5 * self.c.T_S)
            )
        peaks = np.ravel(np.where(tx_product_avg == tx_product_avg_min_filt))

        offset = peaks[np.argmin([tx_product_avg[peak] for peak in peaks])]

        if logging.getLogger().getEffectiveLevel() == logging.DEBUG and self.plot:
            dt = datetime.datetime.now().isoformat()
            fig_path = logging_path + "/" + dt + "_Symbol_align.svg"

            fig = plt.figure(figsize=(9, 9))

            ax = fig.add_subplot(4, 1, 1)
            ax.plot(tx_product)
            ylim = ax.get_ylim()
            for peak in peaks:
                ax.plot((peak, peak), (ylim[0], ylim[1]))
                if peak == offset:
                    ax.text(peak, ylim[0] + 0.3 * np.diff(ylim), "offset", rotation=90)
                else:
                    ax.text(peak, ylim[0] + 0.2 * np.diff(ylim), "peak", rotation=90)
            ax.set_xlabel("Sample")
            ax.set_ylabel("Conj. Product")
            ax.set_title("Difference with shifted self")

            ax = fig.add_subplot(4, 1, 2)
            ax.plot(tx_product_avg)
            ylim = ax.get_ylim()
            for peak in peaks:
                ax.plot((peak, peak), (ylim[0], ylim[1]))
                if peak == offset:
                    ax.text(peak, ylim[0] + 0.3 * np.diff(ylim), "offset", rotation=90)
                else:
                    ax.text(peak, ylim[0] + 0.2 * np.diff(ylim), "peak", rotation=90)
            ax.set_xlabel("Sample")
            ax.set_ylabel("Conj. Product")
            ax.set_title("Moving Average")

            ax = fig.add_subplot(4, 1, 3)
            ax.plot(tx_product_avg_min_filt)
            ylim = ax.get_ylim()
            for peak in peaks:
                ax.plot((peak, peak), (ylim[0], ylim[1]))
                if peak == offset:
                    ax.text(peak, ylim[0] + 0.3 * np.diff(ylim), "offset", rotation=90)
                else:
                    ax.text(peak, ylim[0] + 0.2 * np.diff(ylim), "peak", rotation=90)
            ax.set_xlabel("Sample")
            ax.set_ylabel("Conj. Product")
            ax.set_title("Min Filter")

            ax = fig.add_subplot(4, 1, 4)
            tx_product_crop = tx_product[peaks[0]-50:peaks[0]+50]
            x = range(tx_product.shape[0])[peaks[0]-50:peaks[0]+50]
            ax.plot(x, tx_product_crop)
            ylim = ax.get_ylim()
            ax.plot((peaks[0], peaks[0]), (ylim[0], ylim[1]))
            ax.set_xlabel("Sample")
            ax.set_ylabel("Conj. Product")
            ax.set_title("Difference with shifted self")

            fig.tight_layout()
            fig.savefig(fig_path)
            plt.close(fig)

        # "offset" measures where the shifted signal matches the
        # original signal. Therefore we have to subtract the size
        # of the shift to find the offset of the symbol start.
        return (offset + self.c.T_C) % self.c.T_S

    def _remove_outliers(self, x, stds=5):
        deviation_from_mean = np.abs(x - np.mean(x))
        inlier_idxs = deviation_from_mean < stds * np.std(x)
        x = x[inlier_idxs]
        return x

    def _calc_delta_angle(self, fft):
        # Introduce invariance against carrier
        angles = np.angle(fft) % (np.pi / 2.)

        # Calculate Angle difference and compensate jumps
        deltas_angle = np.diff(angles)
        deltas_angle[deltas_angle > np.pi/4.] =\
            deltas_angle[deltas_angle > np.pi/4.] - np.pi/2.
        deltas_angle[-deltas_angle > np.pi/4.] = \
            deltas_angle[-deltas_angle > np.pi/4.] + np.pi/2.
        deltas_angle = self._remove_outliers(deltas_angle)

        delta_angle = np.mean(deltas_angle)

        return delta_angle

    def _delta_angle_to_samples(self, angle):
        return - angle / self.c.phase_offset_per_sample

    def _calc_sample_offset(self, sig, debug=False):
        assert sig.shape[0] == self.c.T_U,\
            "Input length is not a Symbol without cyclic prefix"

        fft = np.fft.fftshift(np.fft.fft(sig))
        fft_crop = np.delete(fft[self.c.FFT_start:self.c.FFT_end], self.c.FFT_delete)
        delta_angle = self._calc_delta_angle(fft_crop)
        delta_sample = self._delta_angle_to_samples(delta_angle)
        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. Error {}".format(error))
        return delta_sample_int

    def calc_offset(self, tx):
        off_sym = self._calc_offset_to_first_symbol_without_prefix(
            tx)
        off_sam = self._calc_sample_offset(
            tx[off_sym:off_sym + self.c.T_U])
        off = (off_sym + off_sam) % self.c.T_S

        assert self._calc_sample_offset(tx[off:off + self.c.T_U]) == 0, \
            "Failed to calculate offset"
        return off

    def crop_symbol_without_cyclic_prefix(self, tx):
        off = self.calc_offset(tx)
        return tx[
               off:
               off+self.c.T_U
               ]

# 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.