1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-29 19:28:47 -05:00
sdrangel/modemm17/SymbolEvm.h

85 lines
1.7 KiB
C
Raw Normal View History

2022-07-20 03:07:00 -04:00
// Copyright 2020 Mobilinkd LLC.
#pragma once
#include "IirFilter.h"
#include <array>
#include <algorithm>
#include <numeric>
#include <tuple>
2022-07-04 17:03:07 -04:00
namespace modemm17
{
template <size_t N>
struct SymbolEvm
{
2022-07-20 00:11:14 -04:00
using filter_type = BaseIirFilter<N>;
using symbol_t = int;
using result_type = std::tuple<symbol_t, float>;
filter_type filter_;
float erasure_limit_;
float evm_ = 0.0;
SymbolEvm(filter_type&& filter, float erasure_limit = 0.0) :
filter_(std::forward<filter_type>(filter)),
erasure_limit_(erasure_limit)
{}
float evm() const { return evm_; }
/**
* Decode a normalized sample into a symbol. Symbols
* are decoded into +3, +1, -1, -3. If an erasure limit
* is set, symbols outside this limit are 'erased' and
* returned as 0.
*/
result_type operator()(float sample)
{
symbol_t symbol;
float evm;
2022-07-20 00:11:14 -04:00
sample = std::min(3.0f, std::max(-3.0f, sample));
if (sample > 2)
{
symbol = 3;
evm = (sample - 3) * 0.333333;
}
else if (sample > 0)
{
symbol = 1;
evm = sample - 1;
}
else if (sample >= -2)
{
symbol = -1;
evm = sample + 1;
}
else
{
symbol = -3;
evm = (sample + 3) * 0.333333;
}
2022-07-20 00:11:14 -04:00
if (erasure_limit_ and (std::abs(evm) > erasure_limit_)) symbol = 0;
evm_ = filter_(evm);
return std::make_tuple(symbol, evm);
}
};
template <size_t N>
SymbolEvm<N> makeSymbolEvm(
BaseIirFilter<N>&& filter,
float erasure_limit = 0.0f
)
{
2022-07-20 00:11:14 -04:00
return std::move(SymbolEvm<N>(std::move(filter), erasure_limit));
}
2022-07-04 17:03:07 -04:00
} // modemm17