#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include "awgn.h"

using namespace std;
// fixed-point header file
// Qm.f
// m: the number of bits for integer
// f: the number of bits for fraction 

typedef signed short fx_t;

#define M 3  // m
#define F 2  // f



#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 )    //floating to fixed-point (using integer)
#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; //length

    float sample[N] = { 0 };
    fx_t f_sample[N] = { 0 };  //fixed point
    fx_t q_sample[N] = { 0 };  // quantized

    int q_bit = 4; // the number of bit for quatization
    int q_max = pow(2, q_bit) - 1;

    //fixed-point 에서의 maximum 값, overflow 처리 필요.

    cout << "Max. Fixed-Point :" << F_MAX << endl;
    cout << "Min. Fixed-Point :" << F_MIN << endl;
    //getchar();
    sigma = pow(10, -0.05*snr) / sqrt(2 * R);

    //cout << "Orig.\t\tFixed\t\tFx_int\t\tFx_fl" << endl;
    for (int i = 0; i < N; i++)
    {
        float temp;
        sample[i] = 1 + gaussian(0, sigma);

        f_sample[i] = Fl2Fx(sample[i]);
        //temp = (int)(sample[i] * (1 << F));
        //temp = (float)temp / (1 << F);
        //cout << setw(8) << sample[i] << "\t" << temp << "\t\t";

        //printf("%d\t\t", f_sample[i]);
        //printf("%f\n", Fx2Fl(f_sample[i]));
        //putchar('\n');
    }
    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


'C and C++' 카테고리의 다른 글

Fixed-point in C/C++  (0) 2016.04.05
main function arguments  (0) 2016.03.11
OpenMP in Macbook  (0) 2016.03.10
OpenMP  (0) 2016.03.09
C언어 최적화 기법  (0) 2016.02.05

+ Recent posts