You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
902 lines
33 KiB
902 lines
33 KiB
/////////////////////////////////////////////////////////////////////////////////// |
|
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> // |
|
// Some code by AI // |
|
// // |
|
// 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 as 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 V3 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/>. // |
|
/////////////////////////////////////////////////////////////////////////////////// |
|
|
|
#include <algorithm> |
|
#include <cmath> |
|
#include <limits> |
|
#include <random> |
|
|
|
#include <QDebug> |
|
#include <QElapsedTimer> |
|
#include <QVector> |
|
|
|
#include "dsp/raisedcosine.h" |
|
#include "mainbench.h" |
|
#include "psk31demodulator.h" |
|
#include "util/psk31.h" |
|
|
|
namespace { |
|
|
|
constexpr float sampleRate = 1000.0f; |
|
constexpr int samplesPerSymbol = 32; |
|
constexpr float measurementBandwidth = 100.0f; |
|
constexpr int noiseDurationSeconds = 3600; |
|
constexpr float noiseLevelDbFS = -40.0f; |
|
|
|
enum class PulseShape |
|
{ |
|
RaisedCosine, // As generated by SDRangel's PSK31 modulator |
|
CosineEnvelope // Classic PSK31: cosine amplitude envelope on each reversal |
|
}; |
|
|
|
struct TrialOptions |
|
{ |
|
float snrDb100Hz = std::numeric_limits<float>::infinity(); |
|
float frequencyOffset = 0.0f; |
|
float clockErrorPpm = 0.0f; |
|
int timingOffset = 0; |
|
float amplitude = 0.2f; |
|
float initialPhase = std::numeric_limits<float>::quiet_NaN(); |
|
float interfererOffset = 0.0f; |
|
float interfererRelativeDb = -std::numeric_limits<float>::infinity(); |
|
bool interfererPSK31 = false; // PSK31 station rather than a CW carrier |
|
float rfBandwidth = measurementBandwidth; |
|
int phaseGlitchStart = -1; |
|
int phaseGlitchLength = 0; |
|
int fadeStart = -1; |
|
int fadeLength = 0; |
|
float fadeDb = -40.0f; |
|
quint32 seed = 1; |
|
}; |
|
|
|
struct TrialResult |
|
{ |
|
QString received; |
|
float snr = 0.0f; |
|
bool locked = false; |
|
float frequencyOffset = 0.0f; |
|
}; |
|
|
|
struct NoiseResult |
|
{ |
|
int characters = 0; |
|
qint64 lockedSamples = 0; |
|
int lockAcquisitions = 0; |
|
}; |
|
|
|
QVector<bool> encodeBits(const QString& text, int carrierTailSymbols = 0, int preambleSymbols = 256) |
|
{ |
|
PSK31Encoder encoder; |
|
QVector<bool> bits; |
|
|
|
// Reversing phase on every idle zero gives the Costas loop and symbol clock |
|
// time to acquire before any scored text arrives. |
|
bits.fill(false, preambleSymbols); |
|
|
|
for (QChar character : text) |
|
{ |
|
unsigned encoded; |
|
unsigned int bitCount; |
|
encoder.encode(character, encoded, bitCount); |
|
|
|
for (unsigned int i = 0; i < bitCount; ++i) { |
|
bits.append((encoded >> i) & 1U); |
|
} |
|
} |
|
|
|
for (int i = 0; i < 32; ++i) { |
|
bits.append(false); |
|
} |
|
|
|
// Unmodulated carrier, as sent at the end of a transmission |
|
for (int i = 0; i < carrierTailSymbols; ++i) { |
|
bits.append(true); |
|
} |
|
|
|
return bits; |
|
} |
|
|
|
QVector<float> modulate(const QVector<bool>& bits, PulseShape shape = PulseShape::RaisedCosine) |
|
{ |
|
QVector<float> waveform; |
|
waveform.reserve((bits.size() + 4) * samplesPerSymbol); |
|
|
|
RaisedCosine<Real> pulseShape; |
|
pulseShape.create(1.0, 2, samplesPerSymbol, true); |
|
std::vector<float> cosinePulse(2 * samplesPerSymbol); |
|
for (int i = 0; i < 2 * samplesPerSymbol; ++i) { |
|
cosinePulse[i] = 0.5f - 0.5f * std::cos(2.0f * M_PI * i / (2 * samplesPerSymbol)); |
|
} |
|
std::vector<float> symbols; |
|
|
|
bool symbol = false; |
|
|
|
for (bool bit : bits) |
|
{ |
|
if (!bit) { |
|
symbol = !symbol; |
|
} |
|
|
|
if (shape == PulseShape::RaisedCosine) |
|
{ |
|
for (int sample = 0; sample < samplesPerSymbol; ++sample) { |
|
waveform.append(pulseShape.filter(sample == 1 ? (symbol ? 1.0f : -1.0f) : 0.0f)); |
|
} |
|
} |
|
else |
|
{ |
|
symbols.push_back(symbol ? 1.0f : -1.0f); |
|
} |
|
} |
|
|
|
if (shape == PulseShape::RaisedCosine) |
|
{ |
|
// Flush the pulse-shaping filter. |
|
for (int i = 0; i < 3 * samplesPerSymbol; ++i) { |
|
waveform.append(pulseShape.filter(0.0f)); |
|
} |
|
} |
|
else |
|
{ |
|
waveform.fill(0.0f, (symbols.size() + 2) * samplesPerSymbol); |
|
for (int k = 0; k < (int) symbols.size(); ++k) |
|
{ |
|
for (int i = 0; i < 2 * samplesPerSymbol; ++i) { |
|
waveform[k * samplesPerSymbol + i] += symbols[k] * cosinePulse[i]; |
|
} |
|
} |
|
} |
|
|
|
return waveform; |
|
} |
|
|
|
QVector<float> appendSilence(QVector<float> waveform, int samples) |
|
{ |
|
for (int i = 0; i < samples; ++i) { |
|
waveform.append(0.0f); |
|
} |
|
return waveform; |
|
} |
|
|
|
QVector<float> prependSilence(const QVector<float>& waveform, int samples) |
|
{ |
|
QVector<float> result(samples, 0.0f); |
|
result.append(waveform); |
|
return result; |
|
} |
|
|
|
// An unrelated PSK31 transmission used as an adjacent channel interferer |
|
const QVector<float>& interfererWaveform() |
|
{ |
|
static const QVector<float> waveform = modulate(encodeBits( |
|
"THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG 1234567890 the quick brown fox jumps over the lazy dog " |
|
"THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG 1234567890 the quick brown fox jumps over the lazy dog ")); |
|
return waveform; |
|
} |
|
|
|
double meanPower(const QVector<float>& waveform) |
|
{ |
|
double power = 0.0; |
|
for (float sample : waveform) { |
|
power += sample * sample; |
|
} |
|
return power / waveform.size(); |
|
} |
|
|
|
QVector<float> resample(const QVector<float>& source, double scale, double offset) |
|
{ |
|
QVector<float> resampled; |
|
resampled.reserve(static_cast<int>(source.size() * scale) + 1); |
|
|
|
for (double position = offset; position < source.size() - 1; position += 1.0 / scale) |
|
{ |
|
const int index = static_cast<int>(position); |
|
const float fraction = static_cast<float>(position - index); |
|
resampled.append(source[index] + fraction * (source[index + 1] - source[index])); |
|
} |
|
|
|
return resampled; |
|
} |
|
|
|
TrialResult runTrial(const QVector<float>& source, const TrialOptions& options) |
|
{ |
|
const QVector<float> resampled = resample(source, 1.0 + options.clockErrorPpm * 1.0e-6, options.timingOffset); |
|
const double meanSignalPower = options.amplitude * options.amplitude * meanPower(resampled); |
|
|
|
// SNR is quoted in the demodulator's 100 Hz RF bandwidth. The generated |
|
// complex AWGN spans the 1000 sample/s simulation bandwidth. |
|
double noisePower = 0.0; |
|
if (std::isfinite(options.snrDb100Hz)) |
|
{ |
|
const double ratio = std::pow(10.0, options.snrDb100Hz / 10.0); |
|
noisePower = meanSignalPower * (sampleRate / measurementBandwidth) / ratio; |
|
} |
|
|
|
std::mt19937 generator(options.seed); |
|
std::normal_distribution<float> gaussian(0.0f, std::sqrt(noisePower / 2.0)); |
|
std::uniform_real_distribution<float> phaseDistribution(-M_PI, M_PI); |
|
const float initialPhase = std::isfinite(options.initialPhase) |
|
? options.initialPhase : phaseDistribution(generator); |
|
const QVector<float>& psk31Interferer = interfererWaveform(); |
|
const float interfererScale = options.interfererPSK31 ? 1.0f / std::sqrt(meanPower(psk31Interferer)) : 1.0f; |
|
const float interfererAmplitude = std::isfinite(options.interfererRelativeDb) |
|
? std::sqrt(meanSignalPower) * std::pow(10.0f, options.interfererRelativeDb / 20.0f) |
|
: 0.0f; |
|
const float fadeAmplitude = std::pow(10.0f, options.fadeDb / 20.0f); |
|
|
|
PSK31Demodulator demod; |
|
demod.setRFBandwidth(options.rfBandwidth); |
|
|
|
TrialResult result; |
|
demod.setCharacterCallback([&result](QChar character) { result.received.append(character); }); |
|
|
|
for (int i = 0; i < resampled.size(); ++i) |
|
{ |
|
const float carrierPhase = initialPhase + 2.0f * M_PI * options.frequencyOffset * i / sampleRate; |
|
Complex sample = options.amplitude * resampled[i] * Complex(std::cos(carrierPhase), std::sin(carrierPhase)); |
|
|
|
if ((i >= options.phaseGlitchStart) && (i < options.phaseGlitchStart + options.phaseGlitchLength)) { |
|
sample = -sample; |
|
} |
|
if ((i >= options.fadeStart) && (i < options.fadeStart + options.fadeLength)) { |
|
sample *= fadeAmplitude; |
|
} |
|
|
|
if (interfererAmplitude > 0.0f) |
|
{ |
|
const float interfererPhase = 0.37f + 2.0f * M_PI * options.interfererOffset * i / sampleRate; |
|
const float modulation = options.interfererPSK31 |
|
? interfererScale * psk31Interferer[(i + 1234) % psk31Interferer.size()] |
|
: 1.0f; |
|
sample += interfererAmplitude * modulation * Complex(std::cos(interfererPhase), std::sin(interfererPhase)); |
|
} |
|
|
|
sample += Complex(gaussian(generator), gaussian(generator)); |
|
demod.processSample(sample); |
|
} |
|
|
|
result.snr = demod.getSNR(); |
|
result.locked = demod.isLocked(); |
|
result.frequencyOffset = demod.getFrequencyOffset(); |
|
return result; |
|
} |
|
|
|
struct ChannelInterferer |
|
{ |
|
float offset; // Hz relative to the wanted signal |
|
float relativeDb; // Power relative to the wanted signal |
|
bool psk31; |
|
}; |
|
|
|
// Run the full channel path: NCO, resampling to the internal rate and |
|
// fixed-point samples, as used by the plugin. |
|
QString runChannelTrial( |
|
const QVector<float>& source, |
|
int channelSampleRate, |
|
int channelFrequencyOffset, |
|
float residualOffset, |
|
const std::vector<ChannelInterferer>& interferers, |
|
float snrDb100Hz, |
|
quint32 seed) |
|
{ |
|
const double scale = channelSampleRate / sampleRate; |
|
const QVector<float> resampled = resample(source, scale, 0.0); |
|
const float amplitude = 0.01f; // -40 dBFS, leaving headroom for strong interferers |
|
const double meanSignalPower = amplitude * amplitude * meanPower(resampled); |
|
double noisePower = 0.0; |
|
if (std::isfinite(snrDb100Hz)) { |
|
noisePower = meanSignalPower * (channelSampleRate / measurementBandwidth) / std::pow(10.0, snrDb100Hz / 10.0); |
|
} |
|
|
|
std::mt19937 generator(seed); |
|
std::normal_distribution<float> gaussian(0.0f, std::sqrt(noisePower / 2.0)); |
|
const QVector<float>& psk31Interferer = interfererWaveform(); |
|
const float interfererScale = 1.0f / std::sqrt(meanPower(psk31Interferer)); |
|
|
|
PSK31Demodulator demod; |
|
demod.setChannel(channelSampleRate, channelFrequencyOffset); |
|
QString received; |
|
demod.setCharacterCallback([&received](QChar character) { received.append(character); }); |
|
|
|
SampleVector samples; |
|
samples.reserve(resampled.size()); |
|
const float signalFrequency = channelFrequencyOffset + residualOffset; |
|
|
|
for (int i = 0; i < resampled.size(); ++i) |
|
{ |
|
const double t = i / static_cast<double>(channelSampleRate); |
|
const double phase = 0.5 + 2.0 * M_PI * signalFrequency * t; |
|
Complex sample = amplitude * resampled[i] * Complex(std::cos(phase), std::sin(phase)); |
|
|
|
for (const auto& interferer : interferers) |
|
{ |
|
const double interfererPhase = 1.1 + 2.0 * M_PI * (signalFrequency + interferer.offset) * t; |
|
const float interfererAmplitude = std::sqrt(meanSignalPower) * std::pow(10.0f, interferer.relativeDb / 20.0f); |
|
const float modulation = interferer.psk31 |
|
? interfererScale * psk31Interferer[static_cast<int>(t * sampleRate + 777) % psk31Interferer.size()] |
|
: 1.0f; |
|
sample += interfererAmplitude * modulation * Complex(std::cos(interfererPhase), std::sin(interfererPhase)); |
|
} |
|
|
|
sample += Complex(gaussian(generator), gaussian(generator)); |
|
samples.push_back(Sample(sample.real() * SDR_RX_SCALEF, sample.imag() * SDR_RX_SCALEF)); |
|
} |
|
|
|
constexpr int blockSize = 1000; |
|
for (std::size_t i = 0; i < samples.size(); i += blockSize) { |
|
demod.feed(samples.begin() + i, samples.begin() + std::min<int>(i + blockSize, samples.size())); |
|
} |
|
|
|
return received; |
|
} |
|
|
|
NoiseResult runNoiseTrial(int durationSeconds, float levelDbFS, quint32 seed) |
|
{ |
|
const float noisePower = std::pow(10.0f, levelDbFS / 10.0f); |
|
std::mt19937 generator(seed); |
|
std::normal_distribution<float> gaussian(0.0f, std::sqrt(noisePower / 2.0f)); |
|
|
|
PSK31Demodulator demod; |
|
demod.setRFBandwidth(measurementBandwidth); |
|
|
|
NoiseResult result; |
|
demod.setCharacterCallback([&result](QChar) { ++result.characters; }); |
|
bool wasLocked = false; |
|
const int sampleCount = static_cast<int>(durationSeconds * sampleRate); |
|
|
|
for (int i = 0; i < sampleCount; ++i) |
|
{ |
|
demod.processSample(Complex(gaussian(generator), gaussian(generator))); |
|
const bool locked = demod.isLocked(); |
|
result.lockedSamples += locked; |
|
result.lockAcquisitions += locked && !wasLocked; |
|
wasLocked = locked; |
|
} |
|
|
|
return result; |
|
} |
|
|
|
QString escaped(const QString& text) |
|
{ |
|
QString result; |
|
for (QChar character : text) |
|
{ |
|
if ((character.unicode() < 0x20) || (character.unicode() >= 0x7f)) { |
|
result.append(QString("<%1>").arg(static_cast<int>(character.unicode()), 2, 16, QChar('0'))); |
|
} else { |
|
result.append(character); |
|
} |
|
} |
|
return result; |
|
} |
|
|
|
int editDistance(const QString& a, const QString& b) |
|
{ |
|
std::vector<int> previous(b.size() + 1); |
|
std::vector<int> current(b.size() + 1); |
|
|
|
for (int j = 0; j <= b.size(); ++j) { |
|
previous[j] = j; |
|
} |
|
|
|
for (int i = 1; i <= a.size(); ++i) |
|
{ |
|
current[0] = i; |
|
for (int j = 1; j <= b.size(); ++j) |
|
{ |
|
current[j] = std::min({ |
|
previous[j] + 1, |
|
current[j - 1] + 1, |
|
previous[j - 1] + (a[i - 1] != b[j - 1] ? 1 : 0)}); |
|
} |
|
std::swap(previous, current); |
|
} |
|
|
|
return previous[b.size()]; |
|
} |
|
|
|
// Number of trials where characters are decoded after a transmission that |
|
// ends without a carrier tail, followed by noise |
|
int abruptEndTrials(const QVector<float>& waveform, const QString& payload, float snrDb, int trials) |
|
{ |
|
const QVector<float> abrupt = appendSilence(waveform, 3000); |
|
int extra = 0; |
|
|
|
for (int trial = 0; trial < trials; ++trial) |
|
{ |
|
TrialOptions options; |
|
options.snrDb100Hz = snrDb; |
|
options.seed = 9000 + trial; |
|
const QString received = runTrial(abrupt, options).received; |
|
const int index = received.indexOf(payload); |
|
extra += (index >= 0) && (received.size() > index + payload.size()); |
|
} |
|
|
|
return extra; |
|
} |
|
|
|
void measureNoiseRejection() |
|
{ |
|
const NoiseResult noise = runNoiseTrial(noiseDurationSeconds, noiseLevelDbFS, 6000); |
|
qInfo().noquote() << "Noise_only_duration_s,noise_level_dBFS,locked_samples,lock_acquisitions,false_characters"; |
|
qInfo().noquote() << QString("%1,%2,%3,%4,%5") |
|
.arg(noiseDurationSeconds) |
|
.arg(noiseLevelDbFS, 0, 'f', 0) |
|
.arg(noise.lockedSamples) |
|
.arg(noise.lockAcquisitions) |
|
.arg(noise.characters); |
|
} |
|
|
|
void measureCPUPerformance() |
|
{ |
|
constexpr int blockSize = 4096; |
|
constexpr qint64 minimumElapsedMilliseconds = 1000; |
|
|
|
QVector<Complex> block(blockSize); |
|
for (int i = 0; i < blockSize; ++i) |
|
{ |
|
const Real inPhase = ((i / samplesPerSymbol) & 1) ? 0.2f : -0.2f; |
|
const Real quadrature = 0.002f * std::sin(2.0f * M_PI * i / 97.0f); |
|
block[i] = Complex(inPhase, quadrature); |
|
} |
|
|
|
PSK31Demodulator demod; |
|
demod.setRFBandwidth(measurementBandwidth); |
|
|
|
qint64 processedSamples = 0; |
|
QElapsedTimer timer; |
|
timer.start(); |
|
|
|
do |
|
{ |
|
for (const Complex& sample : block) { |
|
demod.processSample(sample); |
|
} |
|
processedSamples += blockSize; |
|
} while (timer.elapsed() < minimumElapsedMilliseconds); |
|
|
|
const double elapsedSeconds = timer.nsecsElapsed() * 1.0e-9; |
|
const double samplesPerSecond = processedSamples / elapsedSeconds; |
|
const double realTimeFactor = samplesPerSecond / sampleRate; |
|
|
|
qInfo().noquote() << "CPU_internal_samples_per_second,real_time_factor,elapsed_seconds"; |
|
qInfo().noquote() << QString("%1,%2,%3") |
|
.arg(samplesPerSecond, 0, 'f', 0) |
|
.arg(realTimeFactor, 0, 'f', 0) |
|
.arg(elapsedSeconds, 0, 'f', 3); |
|
} |
|
|
|
bool framePassed(const QVector<float>& waveform, const QString& payload, const TrialOptions& options) |
|
{ |
|
return runTrial(waveform, options).received.contains(payload); |
|
} |
|
|
|
int trailingCharacterErrors(const QString& received, const QString& payload) |
|
{ |
|
const QString scored = received.right(payload.size()); |
|
int errors = std::abs(scored.size() - payload.size()); |
|
const int compared = std::min(scored.size(), payload.size()); |
|
|
|
for (int i = 0; i < compared; ++i) { |
|
errors += scored[i] != payload[payload.size() - compared + i]; |
|
} |
|
|
|
return errors; |
|
} |
|
|
|
void measurePerformance(const QVector<float>& waveform, const QVector<float>& cosineWaveform, const QString& payload, const QString& argsStr) |
|
{ |
|
qInfo().noquote() << "PSK31 RX performance benchmark"; |
|
qInfo().noquote() << "SNR definition: average signal power / AWGN power in 100 Hz; acquired carrier; exact-frame criterion"; |
|
qInfo().noquote() << "SNR_dB,raised_cosine_frames_passed,cosine_envelope_frames_passed,frames_total"; |
|
|
|
for (int snrDb = -2; snrDb <= 12; ++snrDb) |
|
{ |
|
int passed = 0; |
|
int cosinePassed = 0; |
|
constexpr int trials = 50; |
|
|
|
for (int trial = 0; trial < trials; ++trial) |
|
{ |
|
TrialOptions options; |
|
options.snrDb100Hz = snrDb; |
|
options.initialPhase = 0.0f; // Measure AWGN sensitivity after carrier acquisition. |
|
options.seed = 1000 + trial; |
|
passed += framePassed(waveform, payload, options); |
|
cosinePassed += framePassed(cosineWaveform, payload, options); |
|
} |
|
|
|
qInfo().noquote() << QString("%1,%2,%3,%4").arg(snrDb).arg(passed).arg(cosinePassed).arg(trials); |
|
} |
|
|
|
qInfo().noquote() << "Frequency_offset_Hz,frames_passed_10dB,frames_passed_5dB,frames_total (random starting phase)"; |
|
for (int offset = -45; offset <= 45; offset += 3) |
|
{ |
|
int passed = 0; |
|
int weakPassed = 0; |
|
constexpr int trials = 10; |
|
|
|
for (int trial = 0; trial < trials; ++trial) |
|
{ |
|
TrialOptions options; |
|
options.frequencyOffset = offset + 0.37f * trial; |
|
options.snrDb100Hz = 10.0f; |
|
options.seed = 2000 + trial; |
|
const TrialResult result = runTrial(waveform, options); |
|
passed += result.received.contains(payload); |
|
if (argsStr.contains("trace", Qt::CaseInsensitive) && !result.received.contains(payload)) { |
|
qInfo().noquote() << " " << options.frequencyOffset << result.frequencyOffset << result.locked << escaped(result.received); |
|
} |
|
options.snrDb100Hz = 5.0f; |
|
weakPassed += framePassed(waveform, payload, options); |
|
} |
|
|
|
qInfo().noquote() << QString("%1,%2,%3,%4").arg(offset).arg(passed).arg(weakPassed).arg(trials); |
|
} |
|
|
|
qInfo().noquote() << "Sample_clock_error_ppm,frames_passed,frames_total,total_character_errors"; |
|
for (int ppm = -5000; ppm <= 5000; ppm += 500) |
|
{ |
|
int passed = 0; |
|
int characterErrors = 0; |
|
constexpr int trials = 5; |
|
|
|
for (int trial = 0; trial < trials; ++trial) |
|
{ |
|
TrialOptions options; |
|
options.clockErrorPpm = ppm; |
|
options.timingOffset = trial * 6; |
|
options.seed = 3000 + trial; |
|
const QString received = runTrial(waveform, options).received; |
|
passed += received.contains(payload); |
|
characterErrors += trailingCharacterErrors(received, payload); |
|
} |
|
|
|
qInfo().noquote() << QString("%1,%2,%3,%4").arg(ppm).arg(passed).arg(trials).arg(characterErrors); |
|
} |
|
|
|
qInfo().noquote() << "Signal_level_dBFS,timing_phases_passed,timing_phases_total"; |
|
for (int levelDb = -10; levelDb >= -100; levelDb -= 10) |
|
{ |
|
int passed = 0; |
|
constexpr int trials = 8; |
|
|
|
for (int trial = 0; trial < trials; ++trial) |
|
{ |
|
TrialOptions options; |
|
options.amplitude = std::pow(10.0f, levelDb / 20.0f); |
|
options.timingOffset = trial * 4; |
|
options.seed = 4000 + trial; |
|
passed += framePassed(waveform, payload, options); |
|
} |
|
|
|
qInfo().noquote() << QString("%1,%2,%3").arg(levelDb).arg(passed).arg(trials); |
|
} |
|
|
|
for (bool psk31 : {false, true}) |
|
{ |
|
qInfo().noquote() << QString("%1_interferer_offset_Hz,interferer_over_signal_dB,frames_passed,frames_total (15 dB SNR)") |
|
.arg(psk31 ? "PSK31" : "CW"); |
|
for (int offset : {25, 50, 75, 100, 150}) |
|
{ |
|
for (int relativeDb : {-20, -10, 0, 10, 20, 30, 40}) |
|
{ |
|
int passed = 0; |
|
constexpr int trials = 5; |
|
|
|
for (int trial = 0; trial < trials; ++trial) |
|
{ |
|
TrialOptions options; |
|
options.interfererOffset = (trial & 1) ? -offset : offset; |
|
options.interfererRelativeDb = relativeDb; |
|
options.interfererPSK31 = psk31; |
|
options.snrDb100Hz = 15.0f; |
|
options.seed = 5000 + trial; |
|
passed += framePassed(waveform, payload, options); |
|
} |
|
|
|
qInfo().noquote() << QString("%1,%2,%3,%4") |
|
.arg(offset).arg(relativeDb).arg(passed).arg(trials); |
|
} |
|
} |
|
} |
|
|
|
qInfo().noquote() << "Preamble_symbols,frames_passed,frames_total (noise before signal, random phase, offset within +/-15 Hz, 10 dB SNR)"; |
|
for (int preamble : {8, 16, 24, 32, 48, 64}) |
|
{ |
|
const QVector<float> shortWaveform = prependSilence(modulate(encodeBits(payload, 0, preamble)), 2000); |
|
int passed = 0; |
|
constexpr int trials = 20; |
|
|
|
for (int trial = 0; trial < trials; ++trial) |
|
{ |
|
TrialOptions options; |
|
options.snrDb100Hz = 10.0f; |
|
options.frequencyOffset = -15.0f + 1.5f * (trial % 21); |
|
options.seed = 8000 + trial; |
|
passed += framePassed(shortWaveform, payload, options); |
|
} |
|
|
|
qInfo().noquote() << QString("%1,%2,%3").arg(preamble).arg(passed).arg(trials); |
|
} |
|
|
|
qInfo().noquote() << "Abrupt_end_SNR_dB,trials_with_characters_after_end,trials_total (no carrier tail, then noise)"; |
|
for (int snrDb : {20, 10, 5}) |
|
{ |
|
constexpr int trials = 100; |
|
qInfo().noquote() << QString("%1,%2,%3").arg(snrDb).arg(abruptEndTrials(waveform, payload, snrDb, trials)).arg(trials); |
|
} |
|
|
|
qInfo().noquote() << "Fade_ms,average_character_errors (fade to -40 dB inside text, 15 dB SNR)"; |
|
for (int fadeMs : {0, 50, 100, 200, 300, 500}) |
|
{ |
|
int errors = 0; |
|
constexpr int trials = 10; |
|
|
|
for (int trial = 0; trial < trials; ++trial) |
|
{ |
|
TrialOptions options; |
|
options.snrDb100Hz = 15.0f; |
|
options.fadeStart = 256 * samplesPerSymbol + 40 * samplesPerSymbol + trial * 7; |
|
options.fadeLength = fadeMs; |
|
options.seed = 6000 + trial; |
|
const QString received = runTrial(waveform, options).received; |
|
errors += editDistance(received, payload); |
|
if (argsStr.contains("trace", Qt::CaseInsensitive)) { |
|
qInfo().noquote() << " " << escaped(received); |
|
} |
|
} |
|
|
|
qInfo().noquote() << QString("%1,%2").arg(fadeMs).arg(errors / static_cast<float>(trials), 0, 'f', 1); |
|
} |
|
|
|
qInfo().noquote() << "Channel_path_interferers,frames_passed,frames_total (1536 S/s channel, 10 dB SNR)"; |
|
const std::vector<std::pair<QString, std::vector<ChannelInterferer>>> channelCases = { |
|
{"none", {}}, |
|
{"PSK31 +20 dB at +/-75 Hz", {{75.0f, 20.0f, true}, {-75.0f, 20.0f, true}}}, |
|
{"PSK31 +30 dB at 100 Hz", {{100.0f, 30.0f, true}}}, |
|
{"CW +40 dB at 300 Hz", {{300.0f, 40.0f, false}}}, |
|
{"CW +40 dB at -600 Hz (aliases)", {{-600.0f, 40.0f, false}}}, |
|
}; |
|
for (const auto& channelCase : channelCases) |
|
{ |
|
int passed = 0; |
|
constexpr int trials = 5; |
|
for (int trial = 0; trial < trials; ++trial) { |
|
passed += runChannelTrial(waveform, 1536, 200, 7.0f, channelCase.second, 10.0f, 7000 + trial).contains(payload); |
|
} |
|
qInfo().noquote() << QString("%1,%2,%3").arg(channelCase.first).arg(passed).arg(trials); |
|
} |
|
|
|
measureNoiseRejection(); |
|
measureCPUPerformance(); |
|
} |
|
|
|
bool testDecoder() |
|
{ |
|
PSK31Encoder encoder; |
|
PSK31Decoder decoder; |
|
QVector<int> decoded; |
|
bool success = true; |
|
|
|
// Exercise every entry in the shared extended-ASCII Varicode table as one |
|
// continuous stream. This also checks character-boundary synchronization. |
|
for (int value = 0; value < 256; ++value) |
|
{ |
|
unsigned bits; |
|
unsigned int bitCount; |
|
encoder.encode(QChar::fromLatin1(static_cast<char>(value)), bits, bitCount); |
|
|
|
for (unsigned int bit = 0; bit < bitCount; ++bit) |
|
{ |
|
QChar character; |
|
if (decoder.decode((bits >> bit) & 1U, character)) { |
|
decoded.append(character.toLatin1() & 0xff); |
|
} |
|
} |
|
} |
|
|
|
QChar finalCharacter; |
|
if (decoder.decode(false, finalCharacter)) { |
|
decoded.append(finalCharacter.toLatin1() & 0xff); |
|
} |
|
|
|
success = success && (decoded.size() == 256); |
|
for (int i = 0; success && (i < decoded.size()); ++i) { |
|
success = decoded[i] == i; |
|
} |
|
|
|
decoder.reset(); |
|
QChar character; |
|
for (int i = 0; i < 8; ++i) { |
|
success = success && !decoder.decode(false, character); |
|
} |
|
// Overlong runs of ones, such as unmodulated carrier, must not decode. |
|
// A 14-bit run would leave "1" (space) after a naive overflow reset. |
|
for (int ones : {13, 14, 15, 17, 40}) |
|
{ |
|
for (int i = 0; i < ones; ++i) { |
|
success = success && !decoder.decode(true, character); |
|
} |
|
success = success && !decoder.decode(false, character); |
|
success = success && !decoder.decode(false, character); |
|
} |
|
|
|
unsigned bits; |
|
unsigned int bitCount; |
|
encoder.encode('A', bits, bitCount); |
|
bool gotA = false; |
|
for (unsigned int bit = 0; bit < bitCount; ++bit) { |
|
gotA = decoder.decode((bits >> bit) & 1U, character) || gotA; |
|
} |
|
gotA = decoder.decode(false, character) || gotA; |
|
success = success && gotA && (character == 'A'); |
|
|
|
const QString message("CQ TEST\r\n"); |
|
QString received; |
|
bool symbol = false; |
|
decoder.reset(); |
|
decoder.decodeSymbol(symbol, character); |
|
|
|
for (QChar sourceCharacter : message) |
|
{ |
|
encoder.encode(sourceCharacter, bits, bitCount); |
|
|
|
for (unsigned int bit = 0; bit < bitCount; ++bit) |
|
{ |
|
if (((bits >> bit) & 1U) == 0U) { |
|
symbol = !symbol; |
|
} |
|
|
|
if (decoder.decodeSymbol(symbol, character)) { |
|
received.append(character); |
|
} |
|
} |
|
} |
|
|
|
symbol = !symbol; |
|
if (decoder.decodeSymbol(symbol, character)) { |
|
received.append(character); |
|
} |
|
|
|
return success && (received == message); |
|
} |
|
|
|
} |
|
|
|
void MainBench::testPSK31(const QString& argsStr) |
|
{ |
|
const bool decoderPassed = testDecoder(); |
|
bool success = decoderPassed; |
|
|
|
const QString payload("CQ CQ DE SDRANGEL 0123456789\r\n"); |
|
const QVector<float> waveform = modulate(encodeBits(payload)); |
|
const QVector<float> cosineWaveform = modulate(encodeBits(payload), PulseShape::CosineEnvelope); |
|
|
|
TrialOptions nominal; |
|
nominal.snrDb100Hz = 20.0f; |
|
const TrialResult nominalResult = runTrial(waveform, nominal); |
|
const bool nominalPassed = nominalResult.received.contains(payload); |
|
const bool snrPassed = std::abs(nominalResult.snr - nominal.snrDb100Hz) < 2.0f; |
|
const bool cosinePassed = framePassed(cosineWaveform, payload, nominal); |
|
|
|
TrialOptions wideBandwidth = nominal; |
|
wideBandwidth.rfBandwidth = 200.0f; |
|
const TrialResult wideResult = runTrial(waveform, wideBandwidth); |
|
const bool wideBandwidthPassed = wideResult.received.contains(payload); |
|
const bool bandwidthNormalized = std::abs(wideResult.snr - nominalResult.snr) < 1.0f; |
|
|
|
TrialOptions glitch; |
|
glitch.phaseGlitchStart = 250 * samplesPerSymbol + 2; |
|
glitch.phaseGlitchLength = 12; |
|
const bool glitchPassed = framePassed(waveform, payload, glitch); |
|
|
|
// Carrier acquisition beyond the Costas loop's own pull-in range |
|
bool offsetPassed = true; |
|
for (float offset : {-30.0f, -20.0f, 20.0f, 30.0f}) |
|
{ |
|
TrialOptions options = nominal; |
|
options.frequencyOffset = offset; |
|
offsetPassed = offsetPassed && framePassed(waveform, payload, options); |
|
} |
|
|
|
// Unmodulated carrier at the end of a transmission must not produce a |
|
// character. Afterwards, lock and SNR must not be left stale. |
|
TrialOptions tail = nominal; |
|
const TrialResult tailResult = runTrial(appendSilence(modulate(encodeBits(payload, 40)), 3000), tail); |
|
const bool tailPassed = tailResult.received.endsWith(payload); |
|
const bool unlockPassed = !tailResult.locked && (tailResult.snr == 0.0f); |
|
|
|
// Transmission ending without a carrier tail must not have noise decoded |
|
// after it |
|
const int abruptEndExtra = abruptEndTrials(waveform, payload, 20.0f, 40); |
|
const bool abruptEndPassed = abruptEndExtra == 0; |
|
|
|
// Transmission starting with a typical one second of idle |
|
TrialOptions shortPreamble = nominal; |
|
shortPreamble.frequencyOffset = 12.0f; |
|
const bool shortPreamblePassed = framePassed( |
|
prependSilence(modulate(encodeBits(payload, 0, 32)), 2000), payload, shortPreamble); |
|
|
|
// Short fade inside text must not lose lock |
|
TrialOptions fade = nominal; |
|
fade.fadeStart = 256 * samplesPerSymbol + 40 * samplesPerSymbol; |
|
fade.fadeLength = 100; |
|
const int fadeErrors = editDistance(runTrial(waveform, fade).received, payload); |
|
const bool fadePassed = fadeErrors <= 2; |
|
|
|
// Full channel path with strong adjacent signals |
|
const QString channelReceived = runChannelTrial( |
|
waveform, 1536, 200, 7.0f, |
|
{{100.0f, 30.0f, true}, {-75.0f, 20.0f, true}, {-600.0f, 40.0f, false}}, |
|
15.0f, 7000); |
|
const bool channelPassed = channelReceived.contains(payload); |
|
|
|
if (!tailPassed) { |
|
qWarning().noquote() << "PSK31 carrier tail received:" << escaped(tailResult.received); |
|
} |
|
if (!channelPassed) { |
|
qWarning().noquote() << "PSK31 channel path received:" << escaped(channelReceived); |
|
} |
|
|
|
success = success && nominalPassed && snrPassed && cosinePassed && wideBandwidthPassed && bandwidthNormalized |
|
&& glitchPassed && offsetPassed && tailPassed && unlockPassed && abruptEndPassed && shortPreamblePassed |
|
&& fadePassed && channelPassed; |
|
qInfo() << "PSK31 regressions: decoder" << decoderPassed |
|
<< "nominal" << nominalPassed |
|
<< "reported SNR" << nominalResult.snr << "valid" << snrPassed |
|
<< "cosine envelope" << cosinePassed |
|
<< "200 Hz SNR" << wideResult.snr << "normalized" << bandwidthNormalized |
|
<< "phase glitch" << glitchPassed |
|
<< "frequency offset" << offsetPassed |
|
<< "carrier tail" << tailPassed << "unlock" << unlockPassed |
|
<< "abrupt end extra" << abruptEndExtra |
|
<< "short preamble" << shortPreamblePassed |
|
<< "fade errors" << fadeErrors |
|
<< "channel path" << channelPassed; |
|
|
|
const NoiseResult noise = runNoiseTrial(noiseDurationSeconds, noiseLevelDbFS, 6000); |
|
success = success && (noise.characters == 0); |
|
|
|
if (argsStr.contains("perf", Qt::CaseInsensitive)) { |
|
measurePerformance(waveform, cosineWaveform, payload, argsStr); |
|
} |
|
else if (argsStr.contains("noise", Qt::CaseInsensitive)) { |
|
measureNoiseRejection(); |
|
} |
|
else if (argsStr.contains("cpu", Qt::CaseInsensitive)) { |
|
measureCPUPerformance(); |
|
} |
|
|
|
if (argsStr.contains("trace", Qt::CaseInsensitive)) |
|
{ |
|
for (int ppm : {-5000, 5000}) |
|
{ |
|
TrialOptions options; |
|
options.clockErrorPpm = ppm; |
|
const QString trace = runTrial(waveform, options).received; |
|
qInfo().noquote() << QString("clock %1 ppm: %2").arg(ppm, 6).arg(QString(trace.toLatin1().toHex())); |
|
} |
|
} |
|
|
|
if (success) { |
|
qInfo() << "MainBench::testPSK31: success"; |
|
} else { |
|
qCritical() << "MainBench::testPSK31: failed"; |
|
} |
|
}
|
|
|