#include "Tracker.hpp"

#include <iostream>

using namespace std;

namespace Radio
{
namespace Metrics
{

Tracker::Tracker(RealScalar rho, RealScalar smoothness)
: m_mags(NUM_QUADRRANTS)
, m_rho(rho)
, m_smoothness(smoothness)
{
	m_mags = 0;
}

Tracker::~Tracker()
{
}

ComplexScalar const* Tracker::trackers() const
{
	return m_trackers;
}

void Tracker::process(ComplexScalar const &x)
{
	size_t qu = getQuadrant(x);
	m_mags *= m_rho;

	RealScalar mag = abs(x);
	if (mag > m_mags[qu])
	{
		m_mags[qu] = mag;
		m_trackers[qu] = m_smoothness*x + (RealScalar(1.0)-m_smoothness)*m_trackers[qu];
	}
}

size_t Tracker::getQuadrant(ComplexScalar const &x)
{
	size_t result = 0;

	if (x.real() < 0)
	{
		result |= 0x02;
	}
	if (x.imag() < 0)
	{
		result |= 0x01;
	}
	return result;
}

} // Metrics
} // Radio