stfinvfourier.cc 11.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*! \file stfinvfourier.cc
 * \brief a base class for all engines which operate in the Fourier domain (implementation)
 * 
 * ----------------------------------------------------------------------------
 * 
 * $Id$
 * \author Thomas Forbriger
 * \date 08/05/2011
 * 
 * a base class for all engines which operate in the Fourier domain (implementation)
 * 
 * Copyright (c) 2011 by Thomas Forbriger (BFO Schiltach) 
 *
 * ----
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * ----
 *
 * 
 * REVISIONS and CHANGES 
 *  - 08/05/2011   V1.0   Thomas Forbriger
 * 
 * ============================================================================
 */
#define STFINV_STFINVFOURIER_CC_VERSION \
  "STFINV_STFINVFOURIER_CC   V1.0   "
#define STFINV_STFINVFOURIER_CC_CVSID \
  "$Id$"

41
#include <sstream>
42
#include <stfinv/stfinvfourier.h>
43
44
#include <aff/subarray.h>
#include <aff/slice.h>
45
46
47

namespace stfinv {

thomas.forbriger's avatar
thomas.forbriger committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  /*! 
   * - Refernces to signal storage are available
   * - Parameters are parsed and available through the base class
   * What has to be done here:
   * -# The FFT engine must be initialized appropriately
   *    -# Time domain padding has to be read from the parameters
   *    -# Number of sample modulo has to be read from parameters
   *    -# number of samples must be calculated from these values
   *    -# based on these values workspace arrays must be constructed
   *    -# The FFT engine must be constructed from the workspace arrays.
   *       This requires a copy constructor for the FFT engine, which
   *       currently is not present.
   *       Alternatively, we could only use a pointer to the engine.
   */
  STFFourierDomainEngine::STFFourierDomainEngine(const stfinv::Tvectoroftriples& triples,
                                                 const stfinv::Waveform& stf,
                                                 const std::string& parameters)
thomas.forbriger's avatar
thomas.forbriger committed
65
    :Tbase(triples, stf, parameters)
thomas.forbriger's avatar
thomas.forbriger committed
66
  { 
thomas.forbriger's avatar
thomas.forbriger committed
67
    this->initialize();
thomas.forbriger's avatar
thomas.forbriger committed
68
69
  } // STFFourierDomainEngine::STFFourierDomainEngine

70
71
72
73
74
75
76
77
  /*----------------------------------------------------------------------*/

  void STFFourierDomainEngine::help(std::ostream& os) const
  {
    STFFourierDomainEngine::classhelp(os);
  } // void STFFourierDomainEngine::help(std::ostream& os) const


78
79
80
81
82
83
  /*----------------------------------------------------------------------*/

  const char* STFFourierDomainEngine::name() const
  {
    return("STFFourierDomainEngine");
  } //  const char const* STFFourierDomainEngine::name() const
84
85
86
87
88
89
90
91
92
  /*----------------------------------------------------------------------*/

  /*! \brief online help text giving information on options
   *
   * This must be kept synchronous with the options used by
   * STFFourierDomainEngine::initialize()
   */
  void STFFourierDomainEngine::classhelp(std::ostream& os)
  {
93
94
95
96
97
98
99
100
101
102
    os << "Options and parameters in common for Fourier engines:\n"
      << "fpad=f       padding factor (default: 1.5)\n"
      << "fpow2        use power of two\n"
      << "fdiv=d       use integer multiple of d\n"
      << "These options define the number of samples N used for the FFT.\n"
      << "This should be larger than the number of samples M in the\n"
      << "original series to avoid wrap-around. N=M*f at least. If fpow2\n"
      << "is set, N will be the next power of 2 larger than M*f. Else if\n"
      << "fdiv is set, N will be the next integer multiple of d larger\n"
      << "than M*f." << std::endl;
103
    STFBaseEngine::classhelp(os);
104
  } // void STFFourierDomainEngine::classhelp(std::ostream& os)
105

thomas.forbriger's avatar
thomas.forbriger committed
106
107
  /*----------------------------------------------------------------------*/

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  /*! \brief Create FFT engines
   *
   * \par Number of samples
   * For the FFT we usually use a number of samples larger than that of the
   * underlying time series. 
   * This way we at least partially avoid the undesired effect of the cyclic
   * discrete Fourier transform, which can lead to wrap-around.
   * Currently there are three ways to modify the number of samples actually
   * used:
   * -# Option \c fpad specifies an padding factor. 
   *    The number of samples simply is larger than the number of samples of
   *    the underlying time series by this factor.
   * -# Option \c fpow2:
   *    If set, the next power of two larger than the fpad*nsamples is used.
   * -# Option \c fdiv specifies a divisor.
   *    If set and if \c fpow2 is not set the number of samples will be the
   *    next integer multiple of the divisor larger than fpad*nsamples.
   *
   * \par Workspace
   * Two FFT engines will be created:
128
129
130
131
132
133
   * -# One engine (\c STFFourierDomainEngine::Mfftengineinput) being shared
   *  by recorded data and synthetic data, because both have to be transformed
   *  to Fourier domain at once.
   * -# One engine (\c STFFourierDomainEngine::Mfftengineoutput) being shared
   *  by the stf and the convolved synthetics, because both have to be
   *  transformed to the time domain at once.
134
   */
thomas.forbriger's avatar
thomas.forbriger committed
135
136
  void STFFourierDomainEngine::initialize()
  {
137
    // extract parameter values
138
139
    // ------------------------
    // padding factor
140
141
142
143
144
145
146
    double padfactor;
    {
      std::istringstream is(this->parameter("fpad","1.5"));
      is >> padfactor;
    }
    STFINV_assert(padfactor >= 1.,
                  "ERROR: parameter for option \"fpad\" not larger or equal 1");
147
    // flag: use power of two
148
    bool poweroftwo=(this->parameter("fpow2","false")=="true");
149
    // flag: use integer multiples of divisor
150
    bool divisorset=this->parameterisset("fdiv");
151
    // number of samples shall be integer power of divisor
152
    unsigned int divisor=1;
153
154
155
    if (divisorset)
    {
      std::istringstream is (this->parameter("fdiv","100")); 
156
      is >> divisor;
157
158
      STFINV_assert(divisor > 0,
                  "ERROR: parameter for option \"fdiv\" not larger than 0");
159
160
161
    }

    // define number of samples to be used by Fourier engine
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
    // -----------------------------------------------------
    // use at least the number of samples sepcified by the padfactor
    unsigned int nsamples=padfactor*this->nsamples();
    if (nsamples<this->nsamples()) { nsamples=this->nsamples(); }
    // power of two has precendence
    if (poweroftwo)
    {
      unsigned int n=2;
      while (n<nsamples) { n *= 2; }
      nsamples=n;
    }
    else if (divisorset)
    {
      unsigned int rest=nsamples % divisor;
      nsamples -= rest;
      nsamples += divisor;
    } // if (poweroftwo) or (divisorset)

    // allocate workspace by creating engines
    // --------------------------------------
    Mfftengineinput=fourier::fft::DRFFTWAFFArrayEngine(2*this->nreceivers(),
                                                       nsamples);
    Mfftengineoutput=fourier::fft::DRFFTWAFFArrayEngine(1+this->nreceivers(),
                                                        nsamples);
thomas.forbriger's avatar
thomas.forbriger committed
186
187
188
189
190
191
  } // void STFFourierDomainEngine::initialize() 

  /*----------------------------------------------------------------------*/

  void STFFourierDomainEngine::fftinput()
  {
192
193
194
195
196
197
198
199
200
201
202
203
204
205
    // clear workspace
    TAseries sarray=Mfftengineinput.series();
    sarray=0.;

    // cycle through receivers
    for (unsigned int i=0; i<this->nreceivers(); ++i)
    {
      // get references to time series in workspace
      TAseries recording=Mfftengineinput.series(i);
      TAseries synthetic=Mfftengineinput.series(i+this->nreceivers());
      // copyin function copies as many elements as possible
      recording.copyin(this->recording(i));
      synthetic.copyin(this->synthetic(i));
    } // for (unsigned int i=0; i<this->nreceivers(); ++i)
thomas.forbriger's avatar
thomas.forbriger committed
206
207
208
209
210
211
  } // void STFFourierDomainEngine::fftinput() 

  /*----------------------------------------------------------------------*/

  void STFFourierDomainEngine::fftoutput()
  {
212
213
214
215
216
217
218
219
220
221
222
223
    // cycle through receivers
    for (unsigned int i=0; i<this->nreceivers(); ++i)
    {
      // get references to time series 
      stfinv::Tseries convolvedsynthetics=this->convolvedsynthetic(i);
      // copyin function copies as many elements as possible
      convolvedsynthetics.copyin(Mfftengineoutput.series(i));
    } // for (unsigned int i=0; i<this->nreceivers(); ++i)
      
    // copy stf too
    stfinv::Tseries stf=this->stf();
    stf.copyin(Mfftengineoutput.series(this->nreceivers()));
thomas.forbriger's avatar
thomas.forbriger committed
224
225
226
227
  } // void STFFourierDomainEngine::fftoutput() 

  /*----------------------------------------------------------------------*/

228
  STFFourierDomainEngine::TAspectrum STFFourierDomainEngine::recordingspec() const
thomas.forbriger's avatar
thomas.forbriger committed
229
  {
230
231
    TAspectrum inspecarray=Mfftengineinput.spectrum();
    return(aff::subarray(inspecarray)()(0,this->nreceivers()-1));
232
  } // STFFourierDomainEngine::TAspectrum STFFourierDomainEngine::recordingspec() const
thomas.forbriger's avatar
thomas.forbriger committed
233
234
235

  /*----------------------------------------------------------------------*/

236
  STFFourierDomainEngine::TAspectrum STFFourierDomainEngine::syntheticspec() const
thomas.forbriger's avatar
thomas.forbriger committed
237
  {
238
239
240
    TAspectrum inspecarray=Mfftengineinput.spectrum();
    return(aff::subarray(inspecarray)()(this->nreceivers(),
                                       (2*this->nreceivers())-1));
241
  } // STFFourierDomainEngine::TAspectrum STFFourierDomainEngine::syntheticspec() const
thomas.forbriger's avatar
thomas.forbriger committed
242
243
244

  /*----------------------------------------------------------------------*/

245
  STFFourierDomainEngine::TAspectrum STFFourierDomainEngine::stfspec() const
thomas.forbriger's avatar
thomas.forbriger committed
246
  {
247
    return(Mfftengineoutput.spectrum(this->nreceivers()));
248
  } // STFFourierDomainEngine::Tspectrum STFFourierDomainEngine::stfspec() const
thomas.forbriger's avatar
thomas.forbriger committed
249

250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
  /*----------------------------------------------------------------------*/

  STFFourierDomainEngine::TAspectrum 
    STFFourierDomainEngine::recordingcoeff(const unsigned int& i) const
  {
    STFFourierDomainEngine::TAspectrum rec=this->recordingspec();
    return(aff::slice(rec)(1,i));
  } // STFFourierDomainEngine::recordingcoeff(const unsigned int& i) const

  /*----------------------------------------------------------------------*/

  STFFourierDomainEngine::TAspectrum 
    STFFourierDomainEngine::syntheticcoeff(const unsigned int& i) const
  {
    STFFourierDomainEngine::TAspectrum syn=this->syntheticspec();
    return(aff::slice(syn)(1,i));
  } // STFFourierDomainEngine::syntheticcoeff(const unsigned int& i) const

  /*----------------------------------------------------------------------*/

  STFFourierDomainEngine::TAspectrum::Tvalue& 
    STFFourierDomainEngine::stfcoeff(const unsigned int& i) const
  {
    STFFourierDomainEngine::TAspectrum stffft=this->stfspec();
    return(stffft(i));
  } // STFFourierDomainEngine::stfcoeff(const unsigned int& i) const

  /*----------------------------------------------------------------------*/

  double STFFourierDomainEngine::frequency(const unsigned int& i) const
  {
    return(static_cast<double>(i)/(this->dt()*Mfftengineinput.nsamples()));
  } // double STFFourierDomainEngine::frequency(const unsigned int& i) const

  /*----------------------------------------------------------------------*/

  unsigned int STFFourierDomainEngine::nfreq() const
  {
    return(Mfftengineinput.nfrequencies());
  } // unsigned int STFFourierDomainEngine::nfreq() const

291
292
293
} // namespace stfinv

/* ----- END OF stfinvfourier.cc ----- */