9 std::vector<T>
flatten(
const std::vector<std::vector<T>> &vec)
16 std::size_t total_size = 0;
17 for (
const auto &sub : vec)
18 total_size += sub.size();
19 std::vector<T> result;
20 result.reserve(total_size);
21 for (
const auto &sub : vec)
22 result.insert(result.end(), sub.begin(), sub.end());
26 std::vector<double>
angle_arr(
const fftw_complex *z,
35 constexpr
int REAL = 0;
36 constexpr
int IMAG = 1;
37 constexpr
double coeff = 180.0 / M_PI;
39 std::vector<double> angle_z(n);
40 for (
int i = 0; i < n; i++)
42 std::complex<double> z0(z[i][REAL], z[i][IMAG]);
44 angle_z[i] = std::arg(z0) * coeff;
46 angle_z[i] = std::arg(z0);
53 inline double average(
const std::vector<T> &vec,
63 return accumulate(vec.begin() + index,
69 fftw_complex *out_array)
79 constexpr
int REAL = 0;
80 constexpr
int IMAG = 1;
83 int N = in_vec.size();
84 for (
int i = 0; i < N; i++)
86 out_array[i][REAL] = in_vec[i];
87 out_array[i][IMAG] = 0;
90 fftw_plan plan = fftw_plan_dft_1d(N,
96 fftw_destroy_plan(plan);
102 for (
int i = 1; i < hN; i++)
104 out_array[i][REAL] *= 2;
105 out_array[i][IMAG] *= 2;
115 out_array[hN][REAL] *= 2;
116 out_array[hN][IMAG] *= 2;
120 memset(&out_array[hN + 1][REAL], 0, numRem *
sizeof(fftw_complex));
123 plan = fftw_plan_dft_1d(N, out_array, out_array, FFTW_BACKWARD, FFTW_ESTIMATE);
126 fftw_destroy_plan(plan);
130 for (
int i = 0; i < N; i++)
132 out_array[i][REAL] /= N;
133 out_array[i][IMAG] /= N;
137 std::vector<double>
unwrap(
const std::vector<double> &in_vec)
149 int len = in_vec.size();
150 std::vector<double> out_vec(len);
151 out_vec[0] = in_vec[0];
152 for (
size_t i = 1; i < len; i++)
154 double d = in_vec[i] - in_vec[i - 1];
155 d = d > M_PI ? d - 2 * M_PI : (d < -M_PI ? d + 2 * M_PI : d);
156 out_vec[i] = out_vec[i - 1] + d;
165 for (
int i = 1; i < n; i++)
167 sum += abs(x[i] - x[0]);
169 double result = sum / (n - 1.0);
175 int n = voltages.size();
176 int nstep = voltages[0].size();
177 std::vector<double> delta_phi(nstep);
178 std::vector<std::vector<double>> inst_phase(n, std::vector<double>(nstep));
180 for (
int i = 0; i < n; i++)
182 fftw_complex z[nstep];
184 std::vector<double> angle_z =
angle_arr(z, nstep);
186 for (
int j = 0; j < nstep; j++)
187 inst_phase[i][j] = fmod(angle_z[j], (2.0 * M_PI));
190 for (
int j = 0; j < nstep; j++)
192 std::vector<double> x(n);
193 for (
int i = 0; i < n; i++)
194 x[i] = inst_phase[i][j];
197 double result =
average(delta_phi, 0);
203 #endif // !STATISTICS
std::vector< T > flatten(const std::vector< std::vector< T >> &vec)
Definition: statistics.hpp:9
void hilbert_transform(const std::vector< double > &in_vec, fftw_complex *out_array)
Definition: statistics.hpp:68
double average(const std::vector< T > &vec, const int index=0)
Definition: statistics.hpp:53
std::vector< double > unwrap(const std::vector< double > &in_vec)
Definition: statistics.hpp:137
std::vector< double > angle_arr(const fftw_complex *z, const int n, bool deg=false)
Definition: statistics.hpp:26
double calculate_phase_synchrony(const std::vector< std::vector< double >> &voltages)
Definition: statistics.hpp:173
double calculate_phase_difference(const std::vector< double > &x)
Definition: statistics.hpp:161