aboutsummaryrefslogtreecommitdiffstats
path: root/repetitionrate.cpp
blob: 45dd495d3a67804a0e4de25bd2cdc3af5a11e0d1 (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
/*
    Copyright (C) 2016 Matthias P. Braendli (http://www.opendigitalradio.org)

    This program 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.

    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.

    Authors:
         Matthias P. Braendli <matthias@mpb.li>

*/

#include "repetitionrate.hpp"
#include <vector>
#include <map>
#include <unordered_set>

using namespace std;

const double frame_duration = 24e-3;

struct FIGTypeExt {
    int figtype;
    int figextension;

    bool operator<(const FIGTypeExt& other) const {
        if (this->figtype == other.figtype) {
            return this->figextension < other.figextension;
        }
        else {
            return this->figtype < other.figtype ;
        }
    }
};

struct FIGRateInfo {
    // List of frame numbers in which the FIG is present
    vector<int> frames_present;

    // List of frame numbers in which a complete DB for that FIG has been sent
    vector<int> frames_complete;

    // Which FIBs this FIG was seen in
    unordered_set<int> in_fib;
};


static map<FIGTypeExt, FIGRateInfo> fig_rates;

static int current_frame_number = 0;
static int current_fib = 0;

void rate_announce_fig(int figtype, int figextension, bool complete)
{
    FIGTypeExt f = {.figtype = figtype, .figextension = figextension};

    if (fig_rates.count(f) == 0) {
        FIGRateInfo rate;
        fig_rates[f] = rate;
    }

    FIGRateInfo& rate = fig_rates[f];

    rate.frames_present.push_back(current_frame_number);
    if (complete) {
        rate.frames_complete.push_back(current_frame_number);
    }
    rate.in_fib.insert(current_fib);
}

void rate_disply_analysis()
{
    printf("FIG carousel analysis. Format:\n"
         "  average FIGs per second (total count, delta variance in frames)\n"
         "  average complete FIGs per second (total count, delta variance in frames)\n");
    for (auto& fig_rate : fig_rates) {
        auto& frames_present = fig_rate.second.frames_present;
        auto& frames_complete = fig_rate.second.frames_complete;

        const size_t n_present = frames_present.size();
        const size_t n_complete = frames_complete.size();

        if (n_present and n_complete) {
            double average_present_interval =
                (double)(frames_present.back() - frames_present.front()) /
                (double)(frames_present.size() - 1);

            double average_complete_interval =
                (double)(frames_complete.back() - frames_complete.front()) /
                (double)(frames_complete.size() - 1);

            double variance_of_delta_present = 0;
            for (size_t i = 1; i < frames_present.size(); i++) {
                double s =
                    (double)(frames_present[i] - frames_present[i-1]) -
                    average_present_interval;

                variance_of_delta_present += s * s;
            }
            variance_of_delta_present /= frames_present.size();

            double variance_of_delta_complete = 0;
            for (size_t i = 1; i < frames_complete.size(); i++) {
                double s =
                    (double)(frames_complete[i] - frames_complete[i-1]) -
                    average_complete_interval;

                variance_of_delta_complete += s * s;
            }
            variance_of_delta_complete /= frames_complete.size();

            const double n_present_per_second =
                (double)n_present / (double)current_frame_number / frame_duration;

            const double n_complete_per_second =
                (double)n_complete / (double)current_frame_number / frame_duration;

            printf("FIG%2d/%2d %2.2f (%6zu %2.2f) %2.2f (%6zu %2.2f)\n",
                    fig_rate.first.figtype, fig_rate.first.figextension,
                    n_present_per_second, n_present, variance_of_delta_present,
                    n_complete_per_second, n_complete, variance_of_delta_complete);
        }
        else {
            printf("FIG%2d/%2d 0\n",
                    fig_rate.first.figtype, fig_rate.first.figextension);
        }
    }
}

void rate_new_fib(int fib)
{
    if (fib == 0) {
        current_frame_number++;
    }

    current_fib = fib;
}