File: instrument2SpectralCoherence.cpp

package info (click to toggle)
groops 0%2Bgit20250907%2Bds-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 11,140 kB
  • sloc: cpp: 135,607; fortran: 1,603; makefile: 20
file content (244 lines) | stat: -rw-r--r-- 8,847 bytes parent folder | download | duplicates (2)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/***********************************************/
/**
* @file instrument2SpectralCoherence.cpp
*
* @brief Empirical computation of the spectral coherence between two instrument files.
*
* @author Andreas Kvas
* @date 2018-01-01
*
*/
/***********************************************/

// Latex documentation
#define DOCSTRING docstring
static const char *docstring = R"(
This program computes the spectral coherence between two \file{instrument files}{instrument}.

The (magnitude-squared) coherence is defined as
\begin{equation}
  C_{xy}(f) = \frac{|P_{xy}(f)|^2}{P_{xx}(f)P_{yy}(f)}
\end{equation}
and is a measure in the range [0, 1] for the similarity of the signals $x$ and $y$ in frequency domain.
$P_{xy}$ is the cross-spectral density between $x$ and $y$ and $P_{xx}$, $P_{yy}$ are auto-spectral densities.
Auto- and cross-spectral densities are computed using Lomb's method (see \program{Instrument2PowerSpectralDensity} for details).

The resulting PSD is the average over all arcs. For regularly sampled time series,
this method yields the same results as FFT based PSD estimates.

A regular frequency grid based on the longest arc and the median sampling is computed.
The maximum number of epochs per arc is determined by
\begin{equation}
  N = \frac{t_{\text{end}} - t_{\text{start}}}{\Delta t_{\text{median}} } + 1,
\end{equation}
the Nyquist frequency is given by
\begin{equation}
  f_{\text{nyq}} = \frac{1}{2\Delta t_{\text{median}}}.
\end{equation}

If it is suspected that \configFile{inputfileInstrument}{instrument} contains secular variations,
the input should be detrended using \program{InstrumentDetrend}.

The \configFile{outputfileCoherence}{matrix} contains a matrix with the frequency vector as first column,
the coherence for each instrument channel is saved in the following columns.
)";

/***********************************************/

#include "programs/program.h"
#include "base/fourier.h"
#include "files/fileMatrix.h"
#include "files/fileInstrument.h"

/***** CLASS ***********************************/

/** @brief Empirical computation of the spectral coherence between two instrument files.
* @ingroup programsGroup */
class Instrument2SpectralCoherence
{
  Matrix designMatrix(const Vector &t, Double f, Bool isNyquist = FALSE);
  std::vector<std::vector<std::complex<Double>>> leastSquaresFourier(const Vector &freqs, const_MatrixSliceRef arcMatrix, Bool countEven);

public:
  void run(Config &config, Parallel::CommunicatorPtr comm);
};

GROOPS_REGISTER_PROGRAM(Instrument2SpectralCoherence, PARALLEL, "Empirical computation of the spectral coherence between two instrument files.", Instrument, Statistics)
GROOPS_RENAMED_PROGRAM(InstrumentComputeSpectralCoherence, Instrument2SpectralCoherence, date2time(2020, 7, 7))

/***********************************************/

void Instrument2SpectralCoherence::run(Config &config, Parallel::CommunicatorPtr comm)
{
  try
  {
    FileName outputName;
    FileName inputName, inputNameReference;

    readConfig(config, "outputfileCoherence",           outputName,            Config::MUSTSET, "", "column 1: frequency, column 2-n coherence");
    readConfig(config, "inputfileInstrument",           inputName,             Config::MUSTSET,  "", "");
    readConfig(config, "inputfileInstrumentReference",  inputNameReference,    Config::MUSTSET,  "", "");
    if(isCreateSchema(config)) return;

    // check input
    // -----------
    InstrumentFile instrumentFile(inputName);
    InstrumentFile instrumentFileReference(inputNameReference);
    InstrumentFile::checkArcCount({instrumentFile, instrumentFileReference});

    // determine arc length and data fields
    // ------------------------------------
    Vector freqs;
    UInt arcEpochCount, dataCount, arcCount;
    Double sampling = 1.0;
    if(Parallel::isMaster(comm))
    {
      arcCount = instrumentFile.arcCount();
      std::vector<Time> times;
      Time maxArcLen = seconds2time(0.0);

      dataCount = 0;
      for(UInt arcNo = 0; arcNo<arcCount; arcNo++)
      {
        Arc arc = instrumentFile.readArc(arcNo);
        if(arc.size() == 0)
          continue;
        auto arcTimes = arc.times();

        dataCount = std::max(dataCount, arc.at(0).data().rows());
        maxArcLen = std::max(arcTimes.back() - arcTimes.front(), maxArcLen);
        times.insert(times.end(), arcTimes.begin(), arcTimes.end());
      }
      sampling = medianSampling(times).seconds();

      arcEpochCount = static_cast<UInt>(std::round(maxArcLen.seconds()/sampling)+1);

      freqs = Fourier::frequencies(arcEpochCount, sampling);
      logInfo<<"  maximum arc length: "<<arcEpochCount<<" epochs"<<Log::endl;
      logInfo<<"  median sampling:    "<<sampling<<" seconds"<<Log::endl;
    }
    Parallel::broadCast(freqs,         0, comm);
    Parallel::broadCast(arcEpochCount, 0, comm);
    Parallel::broadCast(dataCount,     0, comm);
    Parallel::broadCast(arcCount,      0, comm);
    Bool countEven = (arcEpochCount%2) == 0; // flag that determines how to handle the nyquist frequency, see fourier.h for details

    // estimate the covariance matrix for each arc, then reduce
    // --------------------------------------------------------
    logStatus<<"Estimate spectral coherence for each arc"<<Log::endl;
    Matrix Gxx(freqs.rows(), dataCount);
    Matrix Gyy(freqs.rows(), dataCount);

    Matrix GxyReal(freqs.rows(), dataCount);
    Matrix GxyImag(freqs.rows(), dataCount);

    Parallel::forEach(arcCount, [&](UInt arcNo)
    {
      Arc arc = instrumentFile.readArc(arcNo);
      Arc arcRef = instrumentFileReference.readArc(arcNo);

      Matrix X = arc.matrix();
      Matrix Y = arcRef.matrix();

      std::vector<std::vector<std::complex<Double>>> F = leastSquaresFourier(freqs, X, countEven);
      std::vector<std::vector<std::complex<Double>>> G = leastSquaresFourier(freqs, Y, countEven);

      // accumulate estimates
      for(UInt k = 0; k<std::min(F.size(), G.size()); k++)
      {
        for(UInt n = 0; n<freqs.rows(); n++)
        {
          auto c = F.at(k).at(n)*std::conj(G.at(k).at(n)); // cross PSD
          GxyReal(n, k) += c.real();
          GxyImag(n, k) += c.imag();
          Gxx(n, k) += std::abs(F.at(k).at(n)*std::conj(F.at(k).at(n))); // auto PSDs
          Gyy(n, k) += std::abs(G.at(k).at(n)*std::conj(G.at(k).at(n)));
        }
      }
    }, comm);

    Parallel::reduceSum(GxyReal, 0, comm);
    Parallel::reduceSum(GxyImag, 0, comm);
    Parallel::reduceSum(Gxx, 0, comm);
    Parallel::reduceSum(Gyy, 0, comm);

    if(Parallel::isMaster(comm))
    {
      Matrix coherence(freqs.rows(), dataCount+1); // first row is frequency
      copy(freqs, coherence.column(0));

      for(UInt n = 0; n<freqs.rows(); n++)
        for(UInt k=0; k<dataCount; k++)
          coherence(n, k+1) = (GxyReal(n,k)*GxyReal(n,k) + GxyImag(n,k)*GxyImag(n,k))/(Gxx(n, k)*Gyy(n,k)); // C =|Gxy|^2/(Gxx*Gyy)

      logStatus<<"write coherence to <"<<outputName<<">"<<Log::endl;
      writeFileMatrix(outputName, coherence);
    }
  }
  catch(std::exception &e)
  {
    GROOPS_RETHROW(e)
  }
}

/***********************************************/

Matrix Instrument2SpectralCoherence::designMatrix(const Vector &t, Double f, Bool isNyquist)
{
  Matrix A;
  if(isNyquist)
  {
    A = Matrix(t.rows(), 1);
    for(UInt i = 0; i<A.rows(); i++)
      A(i, 0) = (i%2) == 0 ? 1.0 : -1.0;
  }
  else
  {
    A = Matrix(t.rows(), 2);
    for(UInt i = 0; i<A.rows(); i++)
    {
      A(i, 0) = std::sin(2*PI*f*t(i));
      A(i, 1) = std::cos(2*PI*f*t(i));
    }
  }

  return A;
}

/***********************************************/

std::vector<std::vector<std::complex<Double>>> Instrument2SpectralCoherence::leastSquaresFourier(const Vector &freqs, const_MatrixSliceRef arcMatrix, Bool countEven)
{
  Vector t(arcMatrix.column(0));
  t-=t(0);
  t*=86400.0; // mjd -> seconds

  std::vector< std::vector< std::complex<Double> > > F(arcMatrix.columns()-1);

  for(UInt i = 0; i<F.size(); i++) // zero frequency: mean
    F.at(i).push_back(std::complex<Double>(mean(arcMatrix.column(i+1)), 0.0));

  UInt loopCount = countEven ? freqs.size()-2 : freqs.size()-1;
  for(UInt k = 0; k<loopCount; k++)
  {
    Matrix A = designMatrix(t, freqs[k+1]);
    Matrix l = Matrix(arcMatrix.column(1, arcMatrix.columns()-1));

    Matrix x_hat = leastSquares(A, l);
    for(UInt i = 0; i<arcMatrix.columns()-1; i++)
      F.at(i).push_back(std::complex<Double>(0.5*x_hat(1, i), -0.5*x_hat(0, i)));
  }
  if(countEven) // special case nyquist frequency
  {
    Matrix A = designMatrix(t, 0.5, TRUE);
    Matrix l = Matrix(arcMatrix.column(1, arcMatrix.columns()-1));

    Matrix x_hat = leastSquares(A, l);
    for(UInt i = 0; i<arcMatrix.columns()-1; i++)
      F.at(i).push_back(std::complex<Double>(x_hat(0, i), 0.0));
  }

  return F;
}

/***********************************************/