-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathdp.cpp
More file actions
311 lines (235 loc) · 8.13 KB
/
dp.cpp
File metadata and controls
311 lines (235 loc) · 8.13 KB
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
245
246
247
248
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* Dynamic Phasor Interface Algorithm hook.
*
* Author: Steffen Vogel <post@steffenvogel.de>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
#include <cmath>
#include <complex>
#include <cstring>
#include <villas/dsp/window.hpp>
#include <villas/hook.hpp>
#include <villas/sample.hpp>
#include <villas/utils.hpp>
using namespace std::complex_literals;
using namespace villas::utils;
namespace villas {
namespace node {
class DPHook : public Hook {
protected:
char *signal_name;
unsigned signal_index;
int inverse;
double f0;
double timestep;
double time;
double steps;
std::complex<double> *coeffs;
int *fharmonics;
int fharmonics_len;
dsp::Window<double> window;
void step(double *in, std::complex<float> *out) {
int N = window.size();
std::complex<double> om_k, corr;
double newest = *in;
__attribute__((unused)) double oldest = window.update(newest);
for (int k = 0; k < fharmonics_len; k++) {
om_k = 2.0i * M_PI * (double)fharmonics[k] / (double)N;
// Correction for stationary phasor
corr = std::exp(-om_k * (steps - (N + 1)));
//corr = 1;
#if 0
// Recursive update
coeffs[k] = std::exp(om) * (coeffs[k] + (newest - oldest));
out[k] = (2.0 / N) * (coeffs[i] * corr);
// DC component
if (fharmonics[k] == 0)
out[k] /= 2.0;
#else
// Full DFT
std::complex<double> X_k = 0;
for (int n = 0; n < N; n++) {
double x_n = window[n];
X_k += x_n * std::exp(om_k * (double)n);
}
out[k] = X_k / (corr * (double)N);
#endif
}
}
void istep(std::complex<float> *in, double *out) {
std::complex<double> value = 0;
// Reconstruct the original signal
for (int k = 0; k < fharmonics_len; k++) {
double freq = fharmonics[k];
// cppcheck-suppress objectIndex
std::complex<double> coeff = in[k];
std::complex<double> om = 2.0i * M_PI * freq * time;
value += coeff * std::exp(om);
}
*out = std::real(value);
}
public:
DPHook(Path *p, Node *n, int fl, int prio, bool en = true)
: Hook(p, n, fl, prio, en), signal_name(nullptr), signal_index(0),
inverse(0), f0(50.0), timestep(50e-6), time(), steps(0), coeffs(),
fharmonics(), fharmonics_len(0) {}
~DPHook() override {
// Release memory
if (fharmonics)
delete fharmonics;
if (coeffs)
delete coeffs;
if (signal_name)
free(signal_name);
}
void start() override {
assert(state == State::PREPARED);
time = 0;
steps = 0;
for (int i = 0; i < fharmonics_len; i++)
coeffs[i] = 0;
window = dsp::Window<double>((1.0 / f0) / timestep, 0.0);
if (window.size() == 0) {
throw RuntimeError(
"Windows size is 0: f0 * timestep < 1.0 not satisfied");
}
state = State::STARTED;
}
void parse(json_t *json) override {
int ret;
json_error_t err;
json_t *json_harmonics, *json_harmonic, *json_signal;
size_t i;
Hook::parse(json);
double rate = -1, dt = -1;
ret = json_unpack_ex(json, &err, 0,
"{ s: o, s: F, s?: F, s?: F, s: o, s?: b }", "signal",
&json_signal, "f0", &f0, "dt", &dt, "rate", &rate,
"harmonics", &json_harmonics, "inverse", &inverse);
if (ret)
throw ConfigError(json, err, "node-config-hook-dp");
if (rate > 0)
timestep = 1. / rate;
else if (dt > 0)
timestep = dt;
else
throw ConfigError(
json, "node-config-hook-dp",
"Either on of the settings 'dt' or 'rate' must be given");
if (!json_is_array(json_harmonics))
throw ConfigError(json_harmonics, "node-config-hook-dp-harmonics",
"Setting 'harmonics' must be a list of integers");
switch (json_typeof(json_signal)) {
case JSON_STRING:
signal_name = strdup(json_string_value(json_signal));
break;
case JSON_INTEGER:
signal_name = nullptr;
signal_index = json_integer_value(json_signal);
break;
default:
throw ConfigError(json_signal, "node-config-hook-dp-signal",
"Invalid value for setting 'signal'");
}
fharmonics_len = json_array_size(json_harmonics);
fharmonics = new int[fharmonics_len];
coeffs = new std::complex<double>[fharmonics_len];
if (!fharmonics || !coeffs)
throw MemoryAllocationError();
json_array_foreach (json_harmonics, i, json_harmonic) {
if (!json_is_integer(json_harmonic))
throw ConfigError(json_harmonic, "node-config-hook-dp-harmonics",
"Setting 'harmonics' must be a list of integers");
fharmonics[i] = json_integer_value(json_harmonic);
}
state = State::PARSED;
}
void prepare() override {
assert(state == State::CHECKED);
std::string new_sig_name;
assert(state != State::STARTED);
if (signal_name) {
int si = signals->getIndexByName(signal_name);
if (si < 0) {
throw RuntimeError("Failed to find signal: {}", signal_name);
}
signal_index = si;
}
if (inverse) {
// Remove complex-valued coefficient signals
for (int i = 0; i < fharmonics_len; i++) {
auto orig_sig = signals->getByIndex(signal_index + i);
if (!orig_sig)
throw RuntimeError("Failed to find signal");
if (orig_sig->type != SignalType::COMPLEX)
throw RuntimeError("Signal is not complex");
signals->erase(signals->begin() + signal_index + i);
}
// Add new real-valued reconstructed signals
auto new_sig = std::make_shared<Signal>("dp", "idp", SignalType::FLOAT);
if (!new_sig)
throw RuntimeError("Failed to create signal");
signals->insert(signals->begin() + signal_index, new_sig);
} else {
auto orig_sig = signals->getByIndex(signal_index);
if (!orig_sig)
throw RuntimeError("Failed to find signal");
if (orig_sig->type != SignalType::FLOAT)
throw RuntimeError("Signal is not float");
signals->erase(signals->begin() + signal_index);
for (int i = 0; i < fharmonics_len; i++) {
new_sig_name = fmt::format("{}_harm{}", orig_sig->name, i);
auto new_sig = std::make_shared<Signal>(new_sig_name, orig_sig->unit,
SignalType::COMPLEX);
if (!new_sig)
throw RuntimeError("Failed to create new signal");
signals->insert(signals->begin() + signal_index, new_sig);
}
}
state = State::PREPARED;
}
void check() override {
assert(state == State::PARSED);
if (signal_index < 0)
throw RuntimeError("Signal index not set");
if (fharmonics_len <= 0)
throw RuntimeError("No harmonics given");
if (timestep <= 0)
throw RuntimeError("Invalid timestep");
if (f0 <= 0)
throw RuntimeError("Invalid fundamental frequency");
state = State::CHECKED;
}
Hook::Reason process(struct Sample *smp) override {
if (signal_index >= smp->length)
return Hook::Reason::ERROR;
if (inverse) {
double signal;
std::complex<float> *coeffs =
reinterpret_cast<std::complex<float> *>(&smp->data[signal_index].z);
istep(coeffs, &signal);
sample_data_remove(smp, signal_index, fharmonics_len);
sample_data_insert(smp, reinterpret_cast<union SignalData *>(&signal),
signal_index, 1);
} else {
double signal = smp->data[signal_index].f;
std::complex<float> coeffs[fharmonics_len];
step(&signal, coeffs);
sample_data_remove(smp, signal_index, 1);
sample_data_insert(smp, reinterpret_cast<union SignalData *>(coeffs),
signal_index, fharmonics_len);
}
time += timestep;
steps++;
return Reason::OK;
}
};
// Register hook
static char n[] = "dp";
static char d[] = "Transform to/from dynamic phasor domain";
static HookPlugin<DPHook, n, d,
(int)Hook::Flags::PATH | (int)Hook::Flags::NODE_READ |
(int)Hook::Flags::NODE_WRITE>
p;
} // namespace node
} // namespace villas