summaryrefslogtreecommitdiffstats
path: root/dpd/src/Test_data.py
blob: 9dd0913c3c933ed933e8423f31a2440326dc5602 (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
# -*- 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 src.const
import src.Dab_Util
import numpy as np
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt


class Test_data:
    def __init__(self, sample_rate, type):
        """
        Standardized access to complex64 test data files.
        
        :param sample_rate: 
        :param type: 
        
        Testing:
        TD = src.Test_data.Test_data(8192000, 'file')
        tx_orig = TD.get_symbol(0,0)
        fig = plt.figure(figsize=(9,6))
        ax = fig.add_subplot(2,1,1)
        ax.plot(tx_orig)
        ax = fig.add_subplot(2,1,2)
        plt.plot(np.angle(np.fft.fftshift(np.fft.fft(tx_orig))), 'p')
        """

        self.c = src.const.const(sample_rate)
        self.du = src.Dab_Util.Dab_Util(sample_rate)

        self.file_paths = {
            (2048000, 'file'):
                ("./test_data/odr-dabmod_to_file_2048_NoFir_noDPD.iq",
                 (
                    self.c.T_F +    # Pipelineing
                    self.c.T_NULL + # NULL Symbol
                    self.c.T_S +    # Synchronization Symbol
                    self.c.T_C      # Cyclic Prefix
                 )),
            (8192000, 'file'):
                ("./test_data/odr-dabmod_to_file_8192_NoFir_noDPD.iq",
                 (
                     self.c.T_F +    # Pipelining
                     self.c.T_U +    # Pipelining Resampler TODO(?)
                     self.c.T_NULL + # NULL Symbol
                     self.c.T_S +    # Synchronization Symbol
                     self.c.T_C      # Cyclic Prefix
                 )),
            (8192000, 'rec_noFir'):
                ("./test_data/odr-dabmod_reconding_8192_NoFir_DPD_2104.iq",
                 ( 64 )),
            (8192000, 'rec_fir'):
                ("./test_data/odr-dabmod_reconding_8192_Fir_DPD_2104.iq",
                 ( 232 )),
        }

        config = (sample_rate, type)
        if not config in self.file_paths.keys():
            raise RuntimeError("Configuration not found, possible are:\n {}".
                               format(self.file_paths))

        self.path, self.file_offset = self.file_paths[(sample_rate, type)]

    def _load_from_file(self, offset, length):
        print(offset, length, self.file_offset)
        return self.du.fromfile(
            self.path,
            length=length,
            offset=offset + self.file_offset)

    def get_symbol_without_prefix(self,
                                  frame_idx=0,
                                  symbol_idx=0,
                                  off=0):
        return self._load_from_file(
            frame_idx*self.c.T_F +
            symbol_idx*self.c.T_S +
            off,
            self.c.T_U)

    def get_symbol_with_prefix(self,
                               frame_idx=0,
                               symbol_idx=0,
                               n_symbols=1,
                               off=0):
        offset = (
            frame_idx*self.c.T_F +
            symbol_idx*self.c.T_S -
            self.c.T_C +
            off)
        return self._load_from_file( offset, self.c.T_S * n_symbols)

    def get_file_length_in_symbols(self):
        symbol_size = float(
                        64/8 * # complex 64
                        self.c.T_S # Symbol Size
                    )
        return os.path.getsize(self.path) / symbol_size


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