Logo coherent WaveBurst  
Library Reference Guide
Logo
LineFilter.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 "Pipe.hh"
26 #include "Time.hh"
27 #include "Interval.hh"
28 #include "TSeries.hh"
29 #include "wseries.hh"
30 
31 #include <complex>
32 
33 typedef std::complex<float> f_complex;
34 typedef double wavereal;
36 
37 struct lineData {
38  Time T_current;
39  float frequency;
40  float intensity;
41  unsigned int first;
42  std::vector<f_complex> amplitude;
43  std::vector<float> line;
44  std::vector<float> noise;
45  std::vector<float> filter;
46 };
47 
48 /** The LineFilter class containes methods to track and remove quasi-
49  * monochromatic lines. a TSeries by 2^N. The TSeries
50  * data are filtered before decimation to remove aliasing.
51  * @memo Line Removal.
52  * @version 1.2 ; Modified November 01, 2000
53  * @version 1.3 ; Modified November 17, 2000
54  * @author Sergey Klimenko
55  */
56 class LineFilter : public Pipe {
57 public:
58  /** Build an empty LineFilter.
59  * @memo Default constructor.
60  */
61  LineFilter(void);
62 
63  /** @memo LineFilter constructor
64  * filter type (default fid = 1) and time interval T to estimate and
65  * remove interference (T=0 - the whole input TS is used).
66  * @memo Constructor.
67  *@memo Set parameters of the line filter
68  *@param f - line base frequency
69  *@param T - time interval to estimate interference
70  * (T=0 - the whole TS is used)
71  *@param fid - filter ID:
72  * 1 - use resampling and FFT with noise floor estimation
73  * 0 - use resampling and FFT, no noise floor estimation
74  * -1 - heterodyne estimation of amplitude and phase, frequency=const
75  * -2 - -1 + Hann window
76  * -3 - the same as -2, but sin(), cos() and Hann() are tabulated
77  *@param nT - number of interval T sub0divisions
78  */
79  LineFilter(double f, double T = 0., int fid = 1, int nT = 1);
80 
81  /** Build a LineFilter identical to an existing filter.
82  * @memo Copy constructor.
83  */
84  LineFilter(const LineFilter& x);
85 
86  /** Destroy the LineFilter object and release the function storage.
87  * @memo Virtual destructor.
88  */
89  ~LineFilter(void);
90 
91 
92  /** Clone a LineFilter
93  */
94  LineFilter* clone(void) const;
95 
96  /** The argument time series is filtered to remove lines, and
97  * the argument TSeries ts is left unchanged.
98  * @memo Return cleaned TSeries.
99  */
100  TSeries apply(const TSeries& ts);
101  TSeries operator()(const TSeries& ts);
102  FilterIO& operator()(const FilterIO& in) {
103  return Pipe::operator() (in); }
104 
105  /** Operate on wavearray object
106  */
107  void apply(WaveData& ts);
108  FilterIO& apply(const FilterIO& in) {
109  return Pipe::apply (in); }
110 
111  /** Check the data for validity. If the data are not applicable for
112  * line removal, an exception is thrown.
113  */
114  void dataCheck(const TSeries& ts) const;
115  void dataCheck(const FilterIO& in) const {
116  Pipe::dataCheck (in); }
117 
118  /** Check the data for validity. Performs the same data checks as
119  * dataCheck() but returns a boolean status instead f throwing an
120  * exception.
121  */
122  bool isDataValid(const TSeries& ts) const;
123  bool isDataValid(const FilterIO& in) const {
124  return Pipe::isDataValid (in); }
125 
126 
127  /***setFilter*********************************************************
128  *@memo Set parameters of the line filter
129  *@param nF - first harmonic
130  *@param nL - last harmonic
131  *@param nS - skip nS-1 harmonics (take nF, nF+nS, nF+2nS,....)
132  *@param nD - wavelet decimation factor
133  *@param nB - nB/T is a frequency band to estimate noise
134  *@param nR - order of Resample Interpolating Filter
135  *@param nW - order of the decimating lifting wavelet
136  *********************************************************************/
137  void setFilter(int nF = 1,
138  int nL = 0,
139  int nS = 1,
140  int nD = -1,
141  int nB = 5,
142  int nR = 6,
143  int nW = 8);
144 
145  /***setFScan*********************************************************
146  *@memo Set parameters for getFrequency()
147  *@param f - base frequency: if f<=0 - don't scan frequency,
148  *@ f=0 - don't change frequency specified (by LineFilter)
149  *@param sn - limit on signal to noise ratio
150  *@param fS - initial range of frequency scan in units of fft bin
151  *@param nS - number of steps during frequency scan
152  *********************************************************************/
153  void setFScan(double f = 0.,
154  double sn = 2.,
155  double fS = 0.45,
156  int nS = 20);
157 
158 
159  /** Clear/release the internal History vector and reset the current
160  * time.
161  */
162  void reset();
163  void resize(size_t=0);
164 
165  inline Time getStartTime(void) const;
166  inline Time getCurrentTime(void) const;
167  inline bool inUse(void) const;
168 
169 //private:
170 
171  int FilterID;
172  double Frequency; // fundamental line frequency
173  double Window;
174  double Stride;
175  unsigned int nFirst; // first line harmonic
176  unsigned int nLast; // last line harmonic
177  int nStep; // skip harmonics (take nF, nF+nS, nF+2nS,....)
178  int nScan; // # of frequency steps to scan frequency
179  unsigned int nBand; // frequency band in fft bins to average noise
180  int nSubs; // number of data subsets to estimate signal PSD
181  double fBand; // frequency step in fft bins to scan frequency
182  int nLPF; // decimation factor
183  int nWave; // order of the interpolating wavelet
184  bool clean; // true if to clean data
185  bool badData; // false if valid data
186  bool noScan; // true if Frequency is fixed
187  int nRIF; // order of Resample Interpolating Filter
188  double SNR; // limit on SNR used by makeFilter
189  bool reFine; // refine frequency if true (set by SNR<0)
190  size_t dumpStart; // first lineList index used to dump data
193 
195  Time StartTime;
196  Interval Sample;
197 
198  wavearray<double> ct; // tabulated cos()
199  wavearray<double> st; // tabulated cos()
200  wavearray<double> wt; // tabulated window
201 
202  std::list<lineData> lineList;
203 
207 
208  WaveData getPSD(const WaveData &, int = 1);
209  double makeFilter(const WaveData &, int = 0);
210  lineData getLine(WaveData &);
211 
212  /***getHeteroLine****************************************************
213  *@memo reconstruct line amplitude and phase using heterodyne method
214  *@param input time series
215  *********************************************************************/
216  lineData getHeteroLine(WaveData &);
217 
218  double getOmega(const WaveData &, int = 2);
219  double fScan(const WaveData &);
220  double Interference(WaveData &, double);
221 
222  wavearray<float> getTrend(int, char);
223  bool DumpTrend(const char*, int = 0);
224  bool LoadTrend(const char*);
225 
226  inline double newRate(double);
227  unsigned int maxLine(int);
228  inline double axb(double, double);
229  inline double wrap(double);
230  inline long intw(double);
231 
232  // used by THtml doc
233  ClassDef(linefilter,1)
234 };
235 
236 inline double LineFilter::newRate(double rate)
237 {
238  double f = rate/Frequency;
239  f *= (nLPF >= 0) ? 1 : 2;
240  return (int(f)+1)*Frequency;
241 }
242 
243 inline double LineFilter::axb(double a, double b)
244 {return (a-long(a))*long(b) + (b-long(b))*long(a) + (a-long(a))*(b-long(b));}
245 
246 inline long LineFilter::intw(double a)
247 {return (a>0) ? long(a+0.5) : long(a-0.5);}
248 
249 inline double LineFilter::wrap(double a)
250 {
251  long l = a>0 ? long(a/PI/2. + 0.5) : long(a/PI/2. - 0.5);
252  return a - 2*PI*l;
253 }
254 
255 #ifndef __CINT__
256 
257 inline Time LineFilter::getStartTime(void) const {
258  return StartTime;
259 }
260 
261 inline Time LineFilter::getCurrentTime(void) const {
262  return CurrentTime;
263 }
264 
265 inline bool LineFilter::inUse(void) const {
266  return (StartTime != Time(0));
267 }
268 
269 #endif
270 
271 #endif // LineFilter_HH
int FilterState
Definition: LineFilter.hh:191
bool inUse(void) const
Definition: LineFilter.hh:265
unsigned int nBand
Definition: LineFilter.hh:179
wavearray< double > st
Definition: LineFilter.hh:199
void dataCheck(const FilterIO &in) const
Definition: LineFilter.hh:115
double wrap(double)
Definition: LineFilter.hh:249
r apply(0.2)
The linefilter class containes methods to track and remove quasi- monochromatic lines.
double axb(double, double)
Definition: LineFilter.hh:243
wavearray< double > a(hp.size())
bool badData
Definition: LineFilter.hh:185
WaveData Filter
Definition: LineFilter.hh:206
Time StartTime
Definition: LineFilter.hh:195
std::vector< f_complex > amplitude
Definition: LineFilter.hh:42
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
Time getStartTime(void) const
Definition: LineFilter.hh:257
float frequency
Definition: LineFilter.hh:39
double Frequency
Definition: LineFilter.hh:172
wavearray< wavereal > WaveData
Definition: LineFilter.hh:35
long intw(double)
Definition: LineFilter.hh:246
Time T_current
Definition: LineFilter.hh:38
bool isDataValid(const FilterIO &in) const
Definition: LineFilter.hh:123
Time getCurrentTime(void) const
Definition: LineFilter.hh:261
double newRate(double)
Definition: LineFilter.hh:236
cout<< "SNR "<< xsnr<< endl;wavearray< double > f
Definition: ComputeSNR.C:75
std::list< lineData > lineList
Definition: LineFilter.hh:202
#define PI
Definition: watfun.hh:32
r setFilter(10)
x resize(N)
wavearray< double > ct
Definition: LineFilter.hh:198
std::vector< float > filter
Definition: LineFilter.hh:45
int l
unsigned int nLast
Definition: LineFilter.hh:176
double Stride
Definition: LineFilter.hh:174
FilterIO & apply(const FilterIO &in)
Definition: LineFilter.hh:108
double Window
Definition: LineFilter.hh:173
float intensity
Definition: LineFilter.hh:40
WaveData LineSD
Definition: LineFilter.hh:205
double T
Definition: testWDM_4.C:11
Interval Sample
Definition: LineFilter.hh:196
ifstream in
wavearray< double > ts(N)
double SNR
Definition: LineFilter.hh:188
double wavereal
Definition: LineFilter.hh:34
WaveData NoiseSD
Definition: LineFilter.hh:204
unsigned int nFirst
Definition: LineFilter.hh:175
The LineFilter class containes methods to track and remove quasi- monochromatic lines.
Definition: LineFilter.hh:56
size_t dumpStart
Definition: LineFilter.hh:190
unsigned int first
Definition: LineFilter.hh:41
std::complex< float > f_complex
Definition: LineFilter.hh:33
double fBand
Definition: LineFilter.hh:181
Time CurrentTime
Definition: LineFilter.hh:194
std::vector< float > noise
Definition: LineFilter.hh:44
std::vector< float > line
Definition: LineFilter.hh:43
wavearray< double > wt
Definition: LineFilter.hh:200
FilterIO & operator()(const FilterIO &in)
Definition: LineFilter.hh:102
double SeedFrequency
Definition: LineFilter.hh:192