#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include "awgn.h"
using namespace std;
typedef signed short fx_t;
#define M 3
#define F 2
#define F_MAX ( ( 1 << (M+F) ) - 1 )
#define F_MIN ( -1 * ( 1<<(M+F) ))
#define Fl2Fx(x) (fx_t) (x >= 0) ? ( (x * (1 << F)) + 0.5 ) : ( (x * (1 << F)) - 0.5 )
#define Fx2Fl(x) (((float)x)/(1 << F))
fx_t sat_check(fx_t x)
{
if (x > F_MAX)
return F_MAX;
else if (x < F_MIN)
return F_MIN;
else
return x;
}
int main(void){
float R = 0.5;
double snr = 3.0;
double sigma;
double max = 0;
const int N = 100;
float sample[N] = { 0 };
fx_t f_sample[N] = { 0 };
fx_t q_sample[N] = { 0 };
int q_bit = 4;
int q_max = pow(2, q_bit) - 1;
cout << "Max. Fixed-Point :" << F_MAX << endl;
cout << "Min. Fixed-Point :" << F_MIN << endl;
sigma = pow(10, -0.05*snr) / sqrt(2 * R);
for (int i = 0; i < N; i++)
{
float temp;
sample[i] = 1 + gaussian(0, sigma);
f_sample[i] = Fl2Fx(sample[i]);
}
cout << "F_samp\t\tOrig.\t\tSat" << endl;
fx_t temp = 0;
for (int i = 0; i < N; i++)
{
temp += f_sample[i];
cout << f_sample[i]<<"\t\t"<<temp << "\t\t" << sat_check(temp) << endl;
getchar();
}
cout << endl;
cout << "maximum: " << max << endl;
return 0;
}
fixed_point_proto.md