Noise.cxx
Go to the documentation of this file.
1 // This was chopped out of NoiseSource, originally by Xin.
2 
3 #include "Noise.h"
4 
5 using namespace WireCell;
6 
7 Waveform::realseq_t Gen::Noise::generate_waveform(const std::vector<float>& spec,
9  double replace)
10 {
11  // reuse randomes a bit to optimize speed.
12  static std::vector<double> random_real_part;
13  static std::vector<double> random_imag_part;
14 
15  if (random_real_part.size()!=spec.size()){
16  random_real_part.resize(spec.size(),0);
17  random_imag_part.resize(spec.size(),0);
18  for (unsigned int i=0;i<spec.size();i++){
19  random_real_part.at(i) = rng->normal(0,1);
20  random_imag_part.at(i) = rng->normal(0,1);
21  }
22  }
23  else {
24  const int shift1 = rng->uniform(0,random_real_part.size());
25  // replace certain percentage of the random number
26  const int step = 1./ replace;
27  for (int i =shift1; i<shift1 + int(spec.size()); i+=step){
28  if (i<int(spec.size())){
29  random_real_part.at(i) = rng->normal(0,1);
30  random_imag_part.at(i) = rng->normal(0,1);
31  }else{
32  random_real_part.at(i-spec.size()) = rng->normal(0,1);
33  random_imag_part.at(i-spec.size()) = rng->normal(0,1);
34  }
35  }
36  }
37 
38  const int shift = rng->uniform(0,random_real_part.size());
39 
40  WireCell::Waveform::compseq_t noise_freq(spec.size(),0);
41 
42  for (int i=shift;i<int(spec.size());i++){
43  const double amplitude = spec.at(i-shift) * sqrt(2./3.1415926);// / units::mV;
44  noise_freq.at(i-shift).real(random_real_part.at(i) * amplitude);
45  noise_freq.at(i-shift).imag(random_imag_part.at(i) * amplitude);//= complex_t(real_part,imag_part);
46  }
47  for (int i=0;i<shift;i++){
48  const double amplitude = spec.at(i+int(spec.size())-shift) * sqrt(2./3.1415926);
49  noise_freq.at(i+int(spec.size())-shift).real(random_real_part.at(i) * amplitude);
50  noise_freq.at(i+int(spec.size())-shift).imag(random_imag_part.at(i) * amplitude);
51  }
52 
53  Waveform::realseq_t noise_time = WireCell::Waveform::idft(noise_freq);
54  return noise_time;
55 }
Sequence< real_t > realseq_t
Definition: Waveform.h:31
std::shared_ptr< Interface > pointer
Definition: Interface.h:16
WireCell::Waveform::realseq_t generate_waveform(const std::vector< float > &spec, IRandom::pointer rng, double replace=0.02)
Definition: Noise.cxx:7
Definition: Main.h:22
realseq_t idft(compseq_t spec)
Definition: Waveform.cxx:149
Sequence< complex_t > compseq_t
A complex-valued sequence, eg for discrete spectrum powers.
Definition: Waveform.h:34