aboutsummaryrefslogtreecommitdiffstats
path: root/AlignSample.hpp
blob: f61d0bb5f089403b99d11ee83493fa89debfe3fc (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
/*
   Copyright (C) 2015
   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/>.
 */

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


typedef std::complex<float> complexf;

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

    std::vector<complexf> 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);

        std::pair<std::vector<complexf>, std::vector<complexf> > get_samples(
                size_t len, size_t rx_delay);

        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;
};