<commit_before>#include <cmath>
#include <complex>
#include <limits>
#include <fstream>
#include <iterator>
#include <sstream>

#include "Tools/Exception/exception.hpp"

#include "Modem_user.hpp"

namespace aff3ct
{
namespace module
{
/*
 * Constructor / Destructor
 */
template <typename B, typename R, typename Q, tools::proto_max<Q> MAX>
Modem_user<B,R,Q,MAX>
::Modem_user(const int N, const R sigma, const int bits_per_symbol, const std::string const_path,
             const bool disable_sig2, const int n_frames, const std::string name)
: Modem<B,R,Q>(N, (int)(std::ceil((float)N / (float)bits_per_symbol) * 2), sigma, n_frames, name),
  bits_per_symbol(bits_per_symbol),
  nbr_symbols    (1 << bits_per_symbol),
  sqrt_es        (0.0),
  disable_sig2   (disable_sig2),
  constellation  ()
{
	if (const_path.empty())
		throw tools::invalid_argument(__FILE__, __LINE__, __func__, "'const_path' should not be empty.");

	if (bits_per_symbol % 2)
	{
		std::stringstream message;
		message << "'bits_per_symbol' has to be a multiple of 2 ('bits_per_symbol' = " << bits_per_symbol << ").";
		throw tools::invalid_argument(__FILE__, __LINE__, __func__, message.str());
	}

	std::fstream const_file(const_path, std::ios_base::in);

	std::string temp;
	while (std::getline(const_file, temp))
	{
		if (temp[0] == '#') continue;

		std::istringstream buffer(temp);
		std::vector<R> line((std::istream_iterator<R>(buffer)), std::istream_iterator<R>());

		if (line.size() >= 3)
		{
			std::stringstream message;
			message << "'line.size()' has to be smaller than 3 ('line.size()' = " << line.size() << ").";
			throw tools::runtime_error(__FILE__, __LINE__, __func__, message.str());
		}

		if (line.size() == 2)
			constellation.push_back(std::complex<R>(line[0],line[1]));
		else
			constellation.push_back(std::complex<R>(line[0],0.0));
		sqrt_es += std::norm(constellation.back());
	}
	sqrt_es = std::sqrt(sqrt_es/nbr_symbols);

	if ((int)constellation.size() != nbr_symbols)
	{
		std::stringstream message;
		message << "'constellation.size()' has to be equal to 'nbr_symbols' ('constellation.size()' = "
		        << constellation.size() << ", 'nbr_symbols' = " << nbr_symbols << ").";
		throw tools::runtime_error(__FILE__, __LINE__, __func__, message.str());
	}

	for (auto i = 0; i < nbr_symbols; i++)
		constellation[i] /= (std::complex<R>)sqrt_es;

	const_file.close();
}

template <typename B, typename R, typename Q, tools::proto_max<Q> MAX>
Modem_user<B,R,Q,MAX>
::~Modem_user()
{
}

/*
 * Modem
 */
template <typename B,typename R, typename Q, tools::proto_max<Q> MAX>
void Modem_user<B,R,Q,MAX>
::_modulate(const B *X_N1, R *X_N2, const int frame_id)
{
	auto size_in  = this->N;
	auto size_out = this->N_mod;

	auto loop_size = size_in / this->bits_per_symbol;
	for (auto i = 0; i < loop_size; i++)
	{
		unsigned idx = 0;
		for (auto j = 0; j < this->bits_per_symbol; j++)
			idx += unsigned(unsigned(1 << (this->bits_per_symbol -j -1)) * X_N1[i * this->bits_per_symbol +j]);
		auto symbol = this->constellation[idx];<commit_msg>Modification of the modulation energy after normalization.<commit_after>#include <cmath>
#include <complex>
#include <limits>
#include <fstream>
#include <iterator>
#include <sstream>

#include "Tools/Exception/exception.hpp"

#include "Modem_user.hpp"

namespace aff3ct
{
namespace module
{
/*
 * Constructor / Destructor
 */
template <typename B, typename R, typename Q, tools::proto_max<Q> MAX>
Modem_user<B,R,Q,MAX>
::Modem_user(const int N, const R sigma, const int bits_per_symbol, const std::string const_path,
             const bool disable_sig2, const int n_frames, const std::string name)
: Modem<B,R,Q>(N, (int)(std::ceil((float)N / (float)bits_per_symbol) * 2), sigma, n_frames, name),
  bits_per_symbol(bits_per_symbol),
  nbr_symbols    (1 << bits_per_symbol),
  sqrt_es        (0.0),
  disable_sig2   (disable_sig2),
  constellation  ()
{
	if (const_path.empty())
		throw tools::invalid_argument(__FILE__, __LINE__, __func__, "'const_path' should not be empty.");

	if (bits_per_symbol % 2)
	{
		std::stringstream message;
		message << "'bits_per_symbol' has to be a multiple of 2 ('bits_per_symbol' = " << bits_per_symbol << ").";
		throw tools::invalid_argument(__FILE__, __LINE__, __func__, message.str());
	}

	std::fstream const_file(const_path, std::ios_base::in);

	std::string temp;
	while (std::getline(const_file, temp))
	{
		if (temp[0] == '#') continue;

		std::istringstream buffer(temp);
		std::vector<R> line((std::istream_iterator<R>(buffer)), std::istream_iterator<R>());

		if (line.size() >= 3)
		{
			std::stringstream message;
			message << "'line.size()' has to be smaller than 3 ('line.size()' = " << line.size() << ").";
			throw tools::runtime_error(__FILE__, __LINE__, __func__, message.str());
		}

		if (line.size() == 2)
			constellation.push_back(std::complex<R>(line[0],line[1]));
		else
			constellation.push_back(std::complex<R>(line[0],0.0));
		sqrt_es += std::norm(constellation.back());
	}
	sqrt_es = std::sqrt(sqrt_es/nbr_symbols);

	if ((int)constellation.size() != nbr_symbols)
	{
		std::stringstream message;
		message << "'constellation.size()' has to be equal to 'nbr_symbols' ('constellation.size()' = "
		        << constellation.size() << ", 'nbr_symbols' = " << nbr_symbols << ").";
		throw tools::runtime_error(__FILE__, __LINE__, __func__, message.str());
	}

	for (auto i = 0; i < nbr_symbols; i++)
		constellation[i] /= (std::complex<R>)sqrt_es;

	this->sqrt_es = 1.0;
	const_file.close();
}

template <typename B, typename R, typename Q, tools::proto_max<Q> MAX>
Modem_user<B,R,Q,MAX>
::~Modem_user()
{
}

/*
 * Modem
 */
template <typename B,typename R, typename Q, tools::proto_max<Q> MAX>
void Modem_user<B,R,Q,MAX>
::_modulate(const B *X_N1, R *X_N2, const int frame_id)
{
	auto size_in  = this->N;
	auto size_out = this->N_mod;

	auto loop_size = size_in / this->bits_per_symbol;
	for (auto i = 0; i < loop_size; i++)
	{
		unsigned idx = 0;
		for (auto j = 0; j < this->bits_per_symbol; j++)
			idx += unsigned(unsigned(1 << (this->bits_per_symbol -j -1)) * X_N1[i * this->bits_per_symbol +j]);
		auto symbol = this->constellation[idx];
{'fraction_non_alphanumeric': 0.1733615221987315, 'fraction_numerical': 0.0094324280370792, 'mean_word_length': 4.936293436293436, 'pattern_counts': {'":': 0, '<': 105, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '112543', 'n_tokens_mistral': 2409, 'n_tokens_neox': 2265, 'n_words': 501}