Logo coherent WaveBurst  
Library Reference Guide
Logo
wavelinefilter.hh
Go to the documentation of this file.
1 /*
2 # Copyright (C) 2019 Sergey Klimenko
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 
19 #ifndef linefilter_HH
20 #define linefilter_HH
21 
22 #include <iosfwd>
23 #include <list>
24 #include <vector>
25 #include "wseries.hh"
26 
27 #include <complex>
28 
29 typedef std::complex<float> f_complex;
30 typedef double wavereal;
32 
33 struct linedata {
34  double T_current;
35  float frequency;
36  float intensity;
37  unsigned int first;
38  std::vector<f_complex> amplitude;
39  std::vector<float> line;
40  std::vector<float> noise;
41  std::vector<float> filter;
42 };
43 
44 /** The linefilter class containes methods to track and remove quasi-
45  * monochromatic lines. a TSeries by 2^N. The TSeries
46  * data are filtered before decimation to remove aliasing.
47  * @memo Line Removal.
48  * @version 1.2 ; Modified November 01, 2000
49  * @version 1.3 ; Modified November 17, 2000
50  * @author Sergey Klimenko
51  */
52 class linefilter {
53 public:
54  /** Build an empty linefilter.
55  * @memo Default constructor.
56  */
57  linefilter(void);
58 
59  /** @memo linefilter constructor
60  * filter type (default fid = 1) and time interval T to estimate and
61  * remove interference (T=0 - the whole input TS is used).
62  * @memo Constructor.
63  *@memo Set parameters of the line filter
64  *@param f - line base frequency
65  *@param T - time interval to estimate interference
66  * (T=0 - the whole TS is used)
67  *@param fid - filter ID:
68  * 1 - use resampling and FFT with noise floor estimation
69  * 0 - use resampling and FFT, no noise floor estimation
70  * -1 - heterodyne estimation of amplitude and phase, frequency=const
71  * -2 - -1 + Hann window
72  * -3 - the same as -2, but sin(), cos() and Hann() are tabulated
73  *@param nT - number of interval T sub0divisions
74  */
75  linefilter(double f, double T = 0., int fid = 1, int nT = 1);
76 
77  /** Build a linefilter identical to an existing filter.
78  * @memo Copy constructor.
79  */
80  linefilter(const linefilter& x);
81 
82  /** Destroy the linefilter object and release the function storage.
83  * @memo Virtual destructor.
84  */
85  ~linefilter(void);
86 
87 
88  /** Clone a linefilter
89  */
90  linefilter* clone(void) const;
91 
92  /** Operate on wavearray object
93  */
94  void apply(WaveData& ts);
95 
96  /***setFilter*********************************************************
97  *@memo Set parameters of the line filter
98  *@param nF - first harmonic
99  *@param nL - last harmonic
100  *@param nS - skip nS-1 harmonics (take nF, nF+nS, nF+2nS,....)
101  *@param nD - wavelet decimation factor
102  *@param nB - nB/T is a frequency band to estimate noise
103  *@param nR - order of Resample Interpolating Filter
104  *@param nW - order of the decimating lifting wavelet
105  *********************************************************************/
106  void setFilter(int nF = 1,
107  int nL = 0,
108  int nS = 1,
109  int nD = -1,
110  int nB = 5,
111  int nR = 6,
112  int nW = 8);
113 
114  /***setFScan*********************************************************
115  *@memo Set parameters for getFrequency()
116  *@param f - base frequency: if f<=0 - don't scan frequency,
117  *@ f=0 - don't change frequency specified (by linefilter)
118  *@param sn - limit on signal to noise ratio
119  *@param fS - initial range of frequency scan in units of fft bin
120  *@param nS - number of steps during frequency scan
121  *********************************************************************/
122  void setFScan(double f = 0.,
123  double sn = 2.,
124  double fS = 0.45,
125  int nS = 20);
126 
127 
128  /** Clear/release the internal History vector and reset the current
129  * time.
130  */
131  void reset();
132  void resize(size_t=0);
133 
134  inline double getStartTime(void) const;
135  inline double getCurrentTime(void) const;
136  inline bool inUse(void) const;
137 
138 //private:
139 
140  int FilterID;
141  double Frequency; // fundamental line frequency
142  double Window;
143  double Stride;
144  unsigned int nFirst; // first line harmonic
145  unsigned int nLast; // last line harmonic
146  int nStep; // skip harmonics (take nF, nF+nS, nF+2nS,....)
147  int nScan; // # of frequency steps to scan frequency
148  unsigned int nBand; // frequency band in fft bins to average noise
149  int nSubs; // number of data subsets to estimate signal PSD
150  double fBand; // frequency step in fft bins to scan frequency
151  int nLPF; // decimation factor
152  int nWave; // order of the interpolating wavelet
153  bool clean; // true if to clean data
154  bool badData; // false if valid data
155  bool noScan; // true if Frequency is fixed
156  int nRIF; // order of Resample Interpolating Filter
157  double SNR; // limit on SNR used by makeFilter
158  bool reFine; // refine frequency if true (set by SNR<0)
159  size_t dumpStart; // first lineList index used to dump data
162 
163  double CurrentTime;
164  double StartTime;
165  double Sample;
166 
167  wavearray<double> ct; // tabulated cos()
168  wavearray<double> st; // tabulated cos()
169  wavearray<double> wt; // tabulated window
170 
171  std::list<linedata> lineList;
172 
176 
177  WaveData getPSD(const WaveData &, int = 1);
178  double makeFilter(const WaveData &, int = 0);
179  linedata getLine(WaveData &);
180 
181  /***getHeteroLine****************************************************
182  *@memo reconstruct line amplitude and phase using heterodyne method
183  *@param input time series
184  *********************************************************************/
185  linedata getHeteroLine(WaveData &);
186 
187  double getOmega(const WaveData &, int = 2);
188  double fScan(const WaveData &);
189  double Interference(WaveData &, double);
190 
191  wavearray<float> getTrend(int, char);
192  bool DumpTrend(const char*, int = 0);
193  bool LoadTrend(const char*);
194 
195  inline double newRate(double);
196  unsigned int maxLine(int);
197  inline double axb(double, double);
198  inline double wrap(double);
199  inline long intw(double);
200 
201 };
202 
203 inline double linefilter::newRate(double rate)
204 {
205  double f = rate/Frequency;
206  f *= (nLPF >= 0) ? 1 : 2;
207  return (int(f)+1)*Frequency;
208 }
209 
210 inline double linefilter::axb(double a, double b)
211 {return (a-long(a))*long(b) + (b-long(b))*long(a) + (a-long(a))*(b-long(b));}
212 
213 inline long linefilter::intw(double a)
214 {return (a>0) ? long(a+0.5) : long(a-0.5);}
215 
216 inline double linefilter::wrap(double a)
217 {
218  long l = a>0 ? long(a/PI/2. + 0.5) : long(a/PI/2. - 0.5);
219  return a - 2*PI*l;
220 }
221 
222 inline double linefilter::getStartTime(void) const {
223  return StartTime;
224 }
225 
226 inline double linefilter::getCurrentTime(void) const {
227  return CurrentTime;
228 }
229 
230 inline bool linefilter::inUse(void) const {
231  return (StartTime != 0.);
232 }
233 
234 #endif // linefilter_HH
wavearray< double > ct
unsigned int nBand
std::list< linedata > lineList
double newRate(double)
r apply(0.2)
The linefilter class containes methods to track and remove quasi- monochromatic lines.
wavearray< double > a(hp.size())
double StartTime
WaveData Filter
unsigned int nLast
wavearray< double > wt
std::vector< float > line
cout<< endl;cout<< "ts size = "<< ts.size()<< " ts rate = "<< ts.rate()<< endl;tf.Forward(ts, wdm);int levels=tf.getLevel();cout<< "tf size = "<< tf.size()<< endl;double dF=tf.resolution();double dT=1./(2 *dF);cout<< "rate(hz) : "<< RATE<< "\ layers : "<< nLAYERS<< "\ dF(hz) : "<< dF<< "\ dT(ms) : "<< dT *1000.<< endl;int itime=TIME_PIXEL_INDEX;int ifreq=FREQ_PIXEL_INDEX;int index=(levels+1) *itime+ifreq;double time=itime *dT;double freq=(ifreq >0) ? ifreq *dF :dF/4;cout<< endl;cout<< "PIXEL TIME = "<< time<< " sec "<< endl;cout<< "PIXEL FREQ = "<< freq<< " Hz "<< endl;cout<< endl;wavearray< double > x
WaveData LineSD
float frequency
WaveData NoiseSD
double SeedFrequency
cout<< "SNR "<< xsnr<< endl;wavearray< double > f
Definition: ComputeSNR.C:75
double wrap(double)
#define PI
Definition: watfun.hh:32
std::vector< f_complex > amplitude
double Frequency
unsigned int nFirst
double getStartTime(void) const
r setFilter(10)
float intensity
x resize(N)
unsigned int first
double CurrentTime
double wavereal
int l
std::vector< float > filter
long intw(double)
wavearray< double > st
double axb(double, double)
double T
Definition: testWDM_4.C:11
wavearray< double > ts(N)
std::complex< float > f_complex
bool inUse(void) const
double T_current
wavearray< wavereal > WaveData
double getCurrentTime(void) const
size_t dumpStart
std::vector< float > noise