summaryrefslogtreecommitdiff
path: root/tests/unit/jtty/test_jtty_spectrum.cpp
blob: e1d1556e122144381262bc8be00119e08ddaa6c4 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <QtTest>
#include <QCryptographicHash>
#include <cmath>
#include <memory>
#include <vector>

#include "commons.h"
#include "widgets/JttySpectrum.hpp"

extern "C" {
  void symspec_ (struct dec_data *, int *, int *, int *, bool *, int *,
                 float *, float *, float *, int *, int *, float *, int *);
}

namespace {
std::vector<qint16> fixture (int count)
{
  std::vector<qint16> result (count);
  quint32 random = 17;
  for (int i = 0; i < count; ++i) {
    random = 1664525u * random + 1013904223u;
    result[i] = 1000 * std::sin (2 * 3.141592653589793 * 1234 * i / 12000)
        + int (random >> 24) - 128;
  }
  return result;
}

QByteArray fingerprint (JttySpectrumFrame const& frame)
{
  QCryptographicHash hash {QCryptographicHash::Sha256};
  for (auto const * values : {&frame.bins, &frame.cumulative, &frame.linearAverage}) {
    hash.addData (reinterpret_cast<char const *> (values->data ()), sizeof (*values));
  }
  return hash.result ();
}

constexpr float spectrumToleranceScale = 5.e-4f;

float spectrumTolerance (float actual, float expected)
{
  auto const magnitude = std::max ({1.f, std::abs (actual), std::abs (expected)});
  return spectrumToleranceScale * magnitude;
}

bool spectrumNearlyEqual (float actual, float expected)
{
  return std::abs (actual - expected) <= spectrumTolerance (actual, expected);
}

QString spectrumMismatch (char const * series, int bin, float actual, float expected)
{
  return QStringLiteral ("%1[%2]: actual=%3 expected=%4 difference=%5 tolerance=%6")
    .arg (QString::fromLatin1 (series)).arg (bin)
    .arg (actual, 0, 'g', 9).arg (expected, 0, 'g', 9)
    .arg (std::abs (actual - expected), 0, 'g', 9)
    .arg (spectrumTolerance (actual, expected), 0, 'g', 9);
}
}

class TestJttySpectrum final : public QObject
{
  Q_OBJECT
private slots:
  void agreesWithTimedSpectrumMath_data ()
  {
    QTest::addColumn<bool> ("windowed");
    QTest::newRow ("rectangular") << false;
    QTest::newRow ("low-sidelobes") << true;
  }

  void agreesWithTimedSpectrumMath ()
  {
    QFETCH (bool, windowed);
    auto shared = std::make_unique<dec_data_t> ();
    auto const pcm = fixture (24 * JttySpectrum::Hop);
    std::copy (pcm.begin (), pcm.end (), shared->d2);
    JttySpectrum spectrum;
    spectrum.begin (1, 0, 0);
    int nsps = 6912;
    int gain = 3;
    int smoothing = 2;
    int symbols = 0;
    int eighths = 0;
    int percentile = 0;
    for (int end = JttySpectrum::Hop; end <= int (pcm.size ()); end += JttySpectrum::Hop) {
      std::array<float, NSMAX> bins {};
      float db = 0, peak = 0, width = 0;
      symspec_ (shared.get (), &end, &nsps, &gain, &windowed, &smoothing,
                &db, bins.data (), &width, &symbols, &eighths, &peak, &percentile);
      bool emitted = false;
      QVERIFY (spectrum.process (end - JttySpectrum::Hop, pcm.data () + end - JttySpectrum::Hop,
          JttySpectrum::Hop, gain, windowed, smoothing, [&] (JttySpectrumFrame const& frame) {
        emitted = true;
        QCOMPARE (frame.row.endSample, qint64 (end));
        QCOMPARE (frame.row.utcEndMs, qint64 (end / 12));
        QVERIFY (std::abs (frame.powerDb - db) < 0.002f);
        QVERIFY (std::abs (frame.maxPowerDb - peak) < 0.002f);
        for (int i = 0; i < NSMAX; ++i) {
          QVERIFY2 (spectrumNearlyEqual (frame.bins[i], bins[i]),
                    qPrintable (spectrumMismatch ("bins", i, frame.bins[i], bins[i])));
          QVERIFY2 (spectrumNearlyEqual (frame.cumulative[i], shared->savg[i]),
                    qPrintable (spectrumMismatch ("cumulative", i, frame.cumulative[i],
                                                  shared->savg[i])));
        }
      }));
      QVERIFY (emitted);
    }
  }

  void packetizationAndLargeCoordinatesPreserveSpectrum ()
  {
    auto const pcm = fixture (181 * 12000);
    qint64 const origin = (qint64 (1) << 33) + 117;
    qint64 const utc = 179000;
    JttySpectrum whole;
    JttySpectrum packets;
    whole.begin (7, origin, utc);
    packets.begin (7, origin, utc);
    QVector<QByteArray> expected;
    QVERIFY (whole.process (origin, pcm.data (), int (pcm.size ()), 0, true, 1,
        [&] (JttySpectrumFrame const& frame) { expected.append (fingerprint (frame)); }));
    int frameIndex = 0;
    int offset = 0;
    while (offset < int (pcm.size ())) {
      int const size = std::min (1 + (offset * 17LL % 4001), qint64 (pcm.size () - offset));
      QVERIFY (packets.process (origin + offset, pcm.data () + offset, size, 0, true, 1,
          [&] (JttySpectrumFrame const& frame) {
        QCOMPARE (fingerprint (frame), expected.at (frameIndex));
        QCOMPARE (frame.row.reception, quint64 (7));
        QCOMPARE (frame.row.beginSample, origin + qint64 (frameIndex) * JttySpectrum::Hop);
        QCOMPARE (frame.row.utcEndMs, utc + qint64 (frameIndex + 1) * JttySpectrum::Hop / 12);
        ++frameIndex;
      }));
      offset += size;
    }
    QCOMPARE (frameIndex, expected.size ());
    QVERIFY (!packets.process (origin, pcm.data (), 100, 0, true, 1, [] (JttySpectrumFrame const&) {}));
  }

  void explicitReceptionResetsTailAndAverage ()
  {
    auto const pcm = fixture (12 * JttySpectrum::Hop);
    JttySpectrum reused;
    reused.begin (1, 0, 0);
    QVERIFY (reused.process (0, pcm.data (), int (pcm.size ()) - 13, 0, true, 1,
                            [] (JttySpectrumFrame const&) {}));
    reused.begin (2, 999, 15000);
    JttySpectrum fresh;
    fresh.begin (2, 999, 15000);
    QVector<QByteArray> expected;
    QVERIFY (fresh.process (999, pcm.data (), int (pcm.size ()), 0, true, 1,
        [&] (JttySpectrumFrame const& frame) { expected.append (fingerprint (frame)); }));
    int index = 0;
    QVERIFY (reused.process (999, pcm.data (), int (pcm.size ()), 0, true, 1,
        [&] (JttySpectrumFrame const& frame) { QCOMPARE (fingerprint (frame), expected.at (index++)); }));
    QCOMPARE (index, expected.size ());
  }

  void averagesFollowNewSignalsAfterLongReception ()
  {
    JttySpectrum spectrum;
    spectrum.begin (3, 0, 0);
    std::array<qint16, JttySpectrum::Hop> pcm;
    JttySpectrumFrame latest;
    qint64 nextSample = 0;
    auto receiveTone = [&] (int frequency, int frames) {
      for (int i = 0; i < int (pcm.size ()); ++i) {
        pcm[i] = 1000 * std::sin (2 * 3.141592653589793 * frequency * i / 12000);
      }
      for (int i = 0; i < frames; ++i) {
        QVERIFY (spectrum.process (nextSample, pcm.data (), int (pcm.size ()), 0, true, 0,
            [&] (JttySpectrumFrame const& frame) { latest = frame; }));
        nextSample += pcm.size ();
      }
    };
    int const oldBin = int (1500 / JttySpectrumFrame::BinWidth);
    int const newBin = int (2250 / JttySpectrumFrame::BinWidth);
    receiveTone (1500, 720 * 12000 / JttySpectrum::Hop);
    float const oldPower = latest.cumulative[oldBin];
    QVERIFY (oldPower > 0);
    receiveTone (2250, 180 * 12000 / JttySpectrum::Hop);
    QVERIFY (latest.cumulative[oldBin] < 0.5f * oldPower);
    QVERIFY (latest.cumulative[newBin] > 1.5f * latest.cumulative[oldBin]);
    QVERIFY (latest.linearAverage[newBin] > latest.linearAverage[oldBin]);
    QCOMPARE (latest.row.reception, quint64 (3));
    QCOMPARE (latest.row.endSample, nextSample);
    QCOMPARE (latest.row.utcEndMs, nextSample / 12);
  }
};

QTEST_APPLESS_MAIN (TestJttySpectrum)
#include "test_jtty_spectrum.moc"