summaryrefslogtreecommitdiffhomepage
path: root/compat/correlation-calibrator.cpp
blob: 01f3b14ff992085158893042269289aa89d2eec5 (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
#include "correlation-calibrator.hpp"
#include "variance.hpp"
#include "compat/math.hpp"
#include "compat/meta.hpp"

#include <cmath>
#include <iterator>

#include <QDebug>

#define DEBUG_PRINT
#ifdef DEBUG_PRINT
#   include <cstdio>
#endif

namespace correlation_calibrator_impl {

static constexpr unsigned nbuckets[6] =
{
    x_nbuckets,
    y_nbuckets,
    z_nbuckets,

    yaw_nbuckets,
    pitch_nbuckets,
    roll_nbuckets,
};

static constexpr double spacing[6] =
{
    translation_spacing,
    translation_spacing,
    translation_spacing,

    yaw_spacing_in_degrees,
    pitch_spacing_in_degrees,
    roll_spacing_in_degrees,
};

static constexpr char const* const names[6] {
    "x", "y", "z",
    "yaw", "pitch", "roll",
};

bool correlation_calibrator::check_buckets(const vec6& data)
{
    bool ret = false;
    unsigned pos[6];

    for (unsigned k = 0; k < 6; k++)
    {
        const double val = clamp(data[k], min[k], max[k]);
        pos[k] = unsigned((val-min[k])/spacing[k]);

        if (pos[k] >= nbuckets[k])
        {
            eval_once(qDebug() << "idx" << k
                               << "bucket" << (int)pos[k]
                               << "outside bounds" << nbuckets[k]);

            return false;
        }

        if (!buckets[k][pos[k]])
        {
            ret = true;
            buckets[k][pos[k]] = true;
        }
    }

    return ret;
}

void correlation_calibrator::input(const vec6& data_)
{
    if (!check_buckets(data_))
        return;

    data.push_back(data_);
}

mat66 correlation_calibrator::get_coefficients() const
{
    if (data.size() < min_samples)
    {
        qDebug() << "correlation-calibrator: not enough data";

        mat66 ret;
        for (unsigned k = 0; k < 6; k++)
            ret(k, k) = 1;
        return ret;
    }

    variance vs[6];
    vec6 devs, means;

    for (const vec6& x : data)
        for (unsigned i = 0; i < 6; i++)
            vs[i].input(x(i));

    for (unsigned i = 0; i < 6; i++)
    {
        means(i) = vs[i].avg();
        devs(i) = vs[i].stddev();

        constexpr double EPS = 1e-4;

        if (devs(i) < EPS)
            devs(i) = EPS;
    }

    mat66 cs;

    for (const vec6& x : data)
        for (unsigned k = 0; k < 6; k++)
        {
            for (unsigned idx = 0; idx < 6; idx++)
            {
                const double zi = (x(idx) - means(idx)),
                             zk = (x(k) - means(k));

                cs(idx, k) += zi * zk / (devs(k)*devs(k));
            }
        }

    cs = cs * (1./(data.size() - 1));

#if defined DEBUG_PRINT
    fprintf(stderr, "v:change-of h:due-to\n");
    fprintf(stderr, "%10s ", "");
    for (char const* k : names)
        fprintf(stderr, "%10s", k);
    fprintf(stderr, "\n");

    for (unsigned i = 0; i < 6; i++)
    {
        fprintf(stderr, "%10s ", names[i]);
        for (unsigned k = 0; k < 6; k++)
            fprintf(stderr, "%10.3f", cs(i, k));
        fprintf(stderr, "\n");
    }
    fflush(stderr);
#endif

    for (unsigned k = 0; k < 6; k++)
        cs(k, k) = 1;

    // derivations from
    // https://www.thoughtco.com/how-to-calculate-the-correlation-coefficient-3126228

    return cs;
}

unsigned correlation_calibrator::sample_count() const
{
    return data.size();
}

} // ns correlation_calibrator_impl