aboutsummaryrefslogtreecommitdiffstats
path: root/AlignSample.hpp
blob: 95297925571c92ce73c5f9b5bc559a99c82624f3 (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
/*
   Copyright (C) 2017
   Matthias P. Braendli, matthias.braendli@mpb.li

    http://opendigitalradio.org
 */
/*
   This file is part of ODR-DPD.

   ODR-DPD is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as
   published by the Free Software Foundation, either version 3 of the
   License, or (at your option) any later version.

   ODR-DPD is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with ODR-DPD.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include "utils.hpp"
#include <thread>
#include <vector>
#include <deque>
#include <mutex>
#include <complex>
#include "fftw3.h"
#define FFT_TYPE fftwf_complex


using complexf = std::complex<float>;
using vec_cf = std::vector<complexf>;

struct CorrelationResult {
    CorrelationResult(size_t len) :
        correlation(len),
        rx_power(0),
        tx_power(0),
        rx_timestamp(0),
        tx_timestamp(0) {}

    vec_cf correlation;
    float rx_power;
    float tx_power;

    double rx_timestamp;
    double tx_timestamp;
};

class AlignSample {
    public:
        AlignSample() {
            m_first_rx_sample_time = 0;
            m_first_tx_sample_time = 0;
            m_num_rx_samples_dropped = 0;
            m_num_tx_samples_dropped = 0;
        }

        void push_tx_samples(complexf* samps, size_t len, double first_sample_time);

        void push_rx_samples(complexf* samps, size_t len, double first_sample_time);

        void reset_rx(void);

        bool ready(size_t min_samples);

        void debug() {
            std::lock_guard<std::mutex> lock(m_mutex);
            MDEBUG("Aligner\n");
            MDEBUG("  RX: %f--%f %zu\n",
                    m_rx_sample_time(),
                    m_rx_sample_time() +
                    (double)m_rxsamples.size() / (double)samplerate,
                    m_rxsamples.size());
            MDEBUG("  TX: %f--%f %zu\n",
                    m_tx_sample_time(),
                    m_tx_sample_time() +
                    (double)m_txsamples.size() / (double)samplerate,
                    m_txsamples.size());
        }

        CorrelationResult crosscorrelate(size_t len);

        /* Drop delay samples from the rx_samples */
        void delay_rx_samples(size_t delay);

        /* Calculate and compensate for subsample delay, assuming that the
         * RX and TX samples are already aligned
         */
        std::pair<vec_cf, vec_cf> get_samples(
                size_t len);

        void consume(size_t samples);

    private:
        bool align();

        double m_rx_sample_time(size_t offset = 0) const {
            return m_first_rx_sample_time +
                (double)(m_num_rx_samples_dropped + offset) / (double)samplerate;
        }

        double m_tx_sample_time(size_t offset = 0) const {
            return m_first_tx_sample_time +
                (double)(m_num_tx_samples_dropped + offset) / (double)samplerate;
        }

        void m_drop_tx_samples(size_t samples) {
            m_txsamples.erase(m_txsamples.begin(), m_txsamples.begin() + samples);
            m_num_tx_samples_dropped += samples;
        }

        void m_drop_rx_samples(size_t samples) {
            m_rxsamples.erase(m_rxsamples.begin(), m_rxsamples.begin() + samples);
            m_num_rx_samples_dropped += samples;
        }

        std::mutex m_mutex;
        double m_first_rx_sample_time;
        size_t m_num_rx_samples_dropped;
        std::deque<complexf> m_rxsamples;

        double m_first_tx_sample_time;
        size_t m_num_tx_samples_dropped;
        std::deque<complexf> m_txsamples;
};