sy_lib.hpp
Go to the documentation of this file.
1 /**********************************************************************
2  * sy_lib.hpp -- a library of useful processes in the SY MoC *
3  * *
4  * Authors: Hosein Attarzadeh (shan2@kth.se) *
5  * *
6  * Purpose: Enriching the SY library. *
7  * *
8  * Usage: *
9  * *
10  * License: BSD3 *
11  *******************************************************************/
12 
13 #ifndef SYLIB_H
14 #define SYLIB_H
15 
23 #include "sy_moc.hpp"
24 
25 namespace ForSyDe
26 {
27 
28 namespace SY
29 {
30 
32 
35 class gaussian : public source<double>
36 {
37 public:
38  gaussian(sc_module_name name_,
39  const double& gaussVar,
40  const double& gaussMean
41  ) : source(name_, [=](abst_ext<double>& out1, const abst_ext<double>& inp)
42  {
43  double rnd1,rnd2,G,Q,Q1,Q2;
44  do
45  {
46  rnd1 = ((double)my_rand()) / ((double)2147483647) ;
47  rnd2 = ((double)my_rand()) / ((double)2147483647) ;
48 
49  Q1 = 2.0 * rnd1 - 1.0 ;
50  Q2 = 2.0 * rnd2 - 1.0 ;
51 
52  Q = Q1 * Q1 + Q2 * Q2 ;
53  } while (Q > 1.0) ;
54 
55  G = gaussMean+sqrt(gaussVar)*(sqrt(-2.0*log(Q)/Q)*Q1);
56  out1 = abst_ext<double>(G);
57  }, abst_ext<double>(0)) {}
58 private:
59  // state variable:
60  bool shiftreg[64]; // boolean array for the LFSR random number generator
61  void initialize()
62  {
63  long int seed = 11206341;
64  for(int i=63; i>=0; i--) { // the LFSR shiftregister is initialized with 0
65  if(seed>=pow(2.,(double)i)) {
66  shiftreg[i]=true;
67  seed-=(long int)pow(2.,(double)i);
68  }
69  else shiftreg[i]=false;
70  }
71  }
72 
73  int my_rand() {
74  bool zw = (((shiftreg[59]==shiftreg[60])==shiftreg[62])==shiftreg[63]); // computing feedback
75  for(int i=63; i>0; i--) {
76  shiftreg[i] = shiftreg[i-1]; // shifting
77  }
78  shiftreg[0] = zw; // writing the feedback bit
79  double val = 0.;
80  for(int i=0; i<31; i++) {
81  if(shiftreg[2*i]) val+=pow(2.,(double)i); // extracting random number
82  }
83  return((int)floor(val));
84  }
85 };
86 
88 
94 template <template <class> class OIf>
95 inline gaussian* make_gaussian(const std::string& pName,
96  const double& gaussVar,
97  const double& gaussMean,
98  OIf<double>& outS
99  )
100 {
101  auto p = new gaussian(pName.c_str(), gaussVar, gaussMean);
102 
103  (*p).oport1(outS);
104 
105  return p;
106 }
107 
108 }
109 }
110 #endif
gaussian * make_gaussian(const std::string &pName, const double &gaussVar, const double &gaussMean, OIf< double > &outS)
Helper function to construct a Gaussian randome wave generator.
Definition: sy_lib.hpp:95
Implements the synchronous Model of Computation.
The namespace for ForSyDe.
Definition: abssemantics.hpp:30
gaussian(sc_module_name name_, const double &gaussVar, const double &gaussMean)
Definition: sy_lib.hpp:38
Process constructor for a source process.
Definition: sy_process_constructors.hpp:1092
Absent-extended data types.
Definition: abst_ext.hpp:32
Process constructor for a Gaussian randome wave generator.
Definition: sy_lib.hpp:35