/*
* quantizer.c: A7id3858
*
* I certify that this program is my sole work and I did not collaborate with
* any other students from the class, nor did I copy any material from a
* student who attended the class in a previous year.
*
*/
#include "quantizer.h"
/****************************************************************************/
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
/****************************************************************************/
#define L1_REC calc_rec (1)
#define L2_REC calc_rec (2)
/****************************************************************************/
int calc_rec (int level)
{
if (level == 0)
return 0;
if ((QUANT % 2) == 0)
return QUANT * (2 * level + 1) - 1;
else
return QUANT * (2 * level + 1);
}
/****************************************************************************/
double quantize (double value)
{
int curr_level = 1;
int curr_rec;
int is_negative = FALSE;
int level_lim = (NUM_LEVELS - 1) / 2;
int slope = (L2_REC - L1_REC) / 2;
/* zero case */
if (value > (L1_REC - slope)*(-1) && value < (L1_REC - slope))
return 0.0;
if (value < 0.0)
{
is_negative = TRUE;
value = value * -1;
}
while (curr_level < level_lim)
{
curr_rec = calc_rec (curr_level);
if (value >= (curr_rec - slope) && value < (curr_rec + slope))
return !is_negative ? (curr_rec - slope) : (curr_rec - slope) * -1;
curr_level++;
}
/* if we got here, curr_level >= level_lim */
return !is_negative ? (calc_rec (level_lim) - slope)
: (calc_rec (level_lim) - slope) * -1;
}
/****************************************************************************/