]>
Commit | Line | Data |
---|---|---|
b0773a8a SA |
1 | /* |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2020 Soeren Apel <soeren@apelpie.net> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <limits> | |
21 | ||
22 | #include <QDebug> | |
23 | ||
24 | #include "mathsignal.hpp" | |
25 | ||
26 | #include <pv/globalsettings.hpp> | |
27 | #include <pv/session.hpp> | |
28 | #include <pv/data/analogsegment.hpp> | |
29 | #include <pv/data/signalbase.hpp> | |
30 | ||
4640a84e | 31 | using std::dynamic_pointer_cast; |
b0773a8a | 32 | using std::make_shared; |
4640a84e SA |
33 | using std::min; |
34 | using std::unique_lock; | |
b0773a8a SA |
35 | |
36 | namespace pv { | |
37 | namespace data { | |
38 | ||
39 | const int64_t MathSignal::ChunkLength = 256 * 1024; | |
40 | ||
41 | ||
3f1f6295 SA |
42 | template<typename T> |
43 | struct sig_sample : public exprtk::igeneric_function<T> | |
44 | { | |
45 | typedef typename exprtk::igeneric_function<T>::parameter_list_t parameter_list_t; | |
46 | typedef typename exprtk::igeneric_function<T>::generic_type generic_type; | |
47 | typedef typename generic_type::scalar_view scalar_t; | |
48 | typedef typename generic_type::string_view string_t; | |
49 | ||
50 | sig_sample(MathSignal& owner) : | |
51 | exprtk::igeneric_function<T>("ST"), // Require channel name and sample number | |
52 | owner_(owner), | |
53 | sig_data(nullptr) | |
54 | { | |
55 | } | |
56 | ||
57 | T operator()(parameter_list_t parameters) | |
58 | { | |
59 | const string_t exprtk_sig_name = string_t(parameters[0]); | |
60 | const scalar_t exprtk_sample_num = scalar_t(parameters[1]); | |
61 | ||
62 | const std::string str_sig_name = to_str(exprtk_sig_name); | |
63 | const double sample_num = exprtk_sample_num(); | |
64 | ||
65 | if (!sig_data) | |
66 | sig_data = owner_.signal_from_name(str_sig_name); | |
67 | ||
68 | assert(sig_data); | |
69 | owner_.update_signal_sample(sig_data, current_segment, sample_num); | |
70 | ||
71 | return T(sig_data->sample_value); | |
72 | } | |
73 | ||
74 | MathSignal& owner_; | |
75 | uint32_t current_segment; | |
76 | signal_data* sig_data; | |
77 | }; | |
78 | ||
79 | ||
b0773a8a SA |
80 | MathSignal::MathSignal(pv::Session &session) : |
81 | SignalBase(nullptr, SignalBase::MathChannel), | |
4640a84e SA |
82 | session_(session), |
83 | use_custom_sample_rate_(false), | |
84 | use_custom_sample_count_(false), | |
85 | expression_(""), | |
86 | error_message_(""), | |
3f1f6295 | 87 | exprtk_unknown_symbol_table_(nullptr), |
4640a84e SA |
88 | exprtk_symbol_table_(nullptr), |
89 | exprtk_expression_(nullptr), | |
3f1f6295 SA |
90 | exprtk_parser_(nullptr), |
91 | fnc_sig_sample_(nullptr) | |
b0773a8a | 92 | { |
4640a84e SA |
93 | set_name(QString(tr("Math%1")).arg(session_.get_next_signal_index(MathChannel))); |
94 | ||
95 | shared_ptr<Analog> data(new data::Analog()); | |
b0773a8a SA |
96 | set_data(data); |
97 | ||
4640a84e SA |
98 | connect(&session_, SIGNAL(capture_state_changed(int)), |
99 | this, SLOT(on_capture_state_changed(int))); | |
100 | connect(&session_, SIGNAL(data_received()), | |
101 | this, SLOT(on_data_received())); | |
b0773a8a | 102 | |
4640a84e | 103 | expression_ = "sin(2 * pi * t) + cos(t / 2 * pi)"; |
b0773a8a SA |
104 | } |
105 | ||
106 | MathSignal::~MathSignal() | |
107 | { | |
4640a84e | 108 | reset_generation(); |
3f1f6295 SA |
109 | |
110 | if (fnc_sig_sample_) | |
111 | delete fnc_sig_sample_; | |
b0773a8a SA |
112 | } |
113 | ||
114 | void MathSignal::save_settings(QSettings &settings) const | |
115 | { | |
4640a84e SA |
116 | settings.setValue("expression", expression_); |
117 | ||
118 | settings.setValue("custom_sample_rate", (qulonglong)custom_sample_rate_); | |
119 | settings.setValue("custom_sample_count", (qulonglong)custom_sample_count_); | |
120 | settings.setValue("use_custom_sample_rate", use_custom_sample_rate_); | |
121 | settings.setValue("use_custom_sample_count", use_custom_sample_count_); | |
b0773a8a SA |
122 | } |
123 | ||
124 | void MathSignal::restore_settings(QSettings &settings) | |
125 | { | |
4640a84e SA |
126 | if (settings.contains("expression")) |
127 | expression_ = settings.value("expression").toString(); | |
128 | ||
129 | if (settings.contains("custom_sample_rate")) | |
130 | custom_sample_rate_ = settings.value("custom_sample_rate").toULongLong(); | |
131 | ||
132 | if (settings.contains("custom_sample_count")) | |
133 | custom_sample_count_ = settings.value("custom_sample_count").toULongLong(); | |
134 | ||
135 | if (settings.contains("use_custom_sample_rate")) | |
136 | use_custom_sample_rate_ = settings.value("use_custom_sample_rate").toBool(); | |
137 | ||
138 | if (settings.contains("use_custom_sample_count")) | |
139 | use_custom_sample_count_ = settings.value("use_custom_sample_count").toBool(); | |
140 | } | |
141 | ||
142 | QString MathSignal::error_message() const | |
143 | { | |
144 | return error_message_; | |
145 | } | |
146 | ||
147 | QString MathSignal::get_expression() const | |
148 | { | |
149 | return expression_; | |
150 | } | |
151 | ||
152 | void MathSignal::set_expression(QString expression) | |
153 | { | |
154 | expression_ = expression; | |
155 | ||
156 | begin_generation(); | |
157 | } | |
158 | ||
159 | void MathSignal::set_error_message(QString msg) | |
160 | { | |
161 | error_message_ = msg; | |
162 | // TODO Emulate noquote() | |
d203009f | 163 | qDebug().nospace() << name() << ": " << msg << "(Expression: '" << expression_ << "')"; |
4640a84e SA |
164 | } |
165 | ||
166 | uint64_t MathSignal::get_working_sample_count(uint32_t segment_id) const | |
167 | { | |
168 | // The working sample count is the highest sample number for | |
169 | // which all used signals have data available, so go through all | |
170 | // channels and use the lowest overall sample count of the segment | |
171 | ||
172 | int64_t result = std::numeric_limits<int64_t>::max(); | |
173 | ||
174 | if (use_custom_sample_count_) | |
175 | // A custom sample count implies that only one segment will be created | |
176 | result = (segment_id == 0) ? custom_sample_count_ : 0; | |
177 | else { | |
178 | if (input_signals_.size() > 0) { | |
3f1f6295 SA |
179 | for (auto input_signal : input_signals_) { |
180 | const shared_ptr<SignalBase>& sb = input_signal.second.sb; | |
181 | ||
4640a84e SA |
182 | shared_ptr<Analog> a = sb->analog_data(); |
183 | const uint32_t last_segment = (a->analog_segments().size() - 1); | |
184 | if (segment_id > last_segment) | |
185 | continue; | |
186 | const shared_ptr<AnalogSegment> segment = a->analog_segments()[segment_id]; | |
187 | result = min(result, (int64_t)segment->get_sample_count()); | |
188 | } | |
189 | } else | |
190 | result = session_.get_segment_sample_count(segment_id); | |
191 | } | |
192 | ||
193 | return result; | |
194 | } | |
195 | ||
196 | void MathSignal::reset_generation() | |
197 | { | |
198 | if (gen_thread_.joinable()) { | |
199 | gen_interrupt_ = true; | |
200 | gen_input_cond_.notify_one(); | |
201 | gen_thread_.join(); | |
202 | } | |
203 | ||
204 | data_->clear(); | |
3f1f6295 | 205 | input_signals_.clear(); |
4640a84e | 206 | |
3f1f6295 SA |
207 | if (exprtk_parser_) { |
208 | delete exprtk_parser_; | |
209 | exprtk_parser_ = nullptr; | |
4640a84e SA |
210 | } |
211 | ||
212 | if (exprtk_expression_) { | |
213 | delete exprtk_expression_; | |
214 | exprtk_expression_ = nullptr; | |
215 | } | |
216 | ||
3f1f6295 SA |
217 | if (exprtk_symbol_table_) { |
218 | delete exprtk_symbol_table_; | |
219 | exprtk_symbol_table_ = nullptr; | |
220 | } | |
221 | ||
222 | if (exprtk_unknown_symbol_table_) { | |
223 | delete exprtk_unknown_symbol_table_; | |
224 | exprtk_unknown_symbol_table_ = nullptr; | |
225 | } | |
226 | ||
227 | if (fnc_sig_sample_) { | |
228 | delete fnc_sig_sample_; | |
229 | fnc_sig_sample_ = nullptr; | |
4640a84e SA |
230 | } |
231 | ||
232 | if (!error_message_.isEmpty()) { | |
233 | error_message_ = QString(); | |
234 | // TODO Emulate noquote() | |
235 | qDebug().nospace() << name() << ": Error cleared"; | |
236 | } | |
237 | } | |
238 | ||
239 | void MathSignal::begin_generation() | |
240 | { | |
241 | reset_generation(); | |
242 | ||
243 | if (expression_.isEmpty()) { | |
244 | set_error_message(tr("No expression defined, nothing to do")); | |
245 | return; | |
246 | } | |
247 | ||
3f1f6295 SA |
248 | fnc_sig_sample_ = new sig_sample<double>(*this); |
249 | ||
250 | exprtk_unknown_symbol_table_ = new exprtk::symbol_table<double>(); | |
251 | ||
4640a84e | 252 | exprtk_symbol_table_ = new exprtk::symbol_table<double>(); |
3f1f6295 | 253 | exprtk_symbol_table_->add_function("sig_sample", *fnc_sig_sample_); |
4640a84e SA |
254 | exprtk_symbol_table_->add_variable("t", exprtk_current_time_); |
255 | exprtk_symbol_table_->add_variable("s", exprtk_current_sample_); | |
256 | exprtk_symbol_table_->add_constants(); | |
257 | ||
258 | exprtk_expression_ = new exprtk::expression<double>(); | |
3f1f6295 | 259 | exprtk_expression_->register_symbol_table(*exprtk_unknown_symbol_table_); |
4640a84e SA |
260 | exprtk_expression_->register_symbol_table(*exprtk_symbol_table_); |
261 | ||
262 | exprtk_parser_ = new exprtk::parser<double>(); | |
3f1f6295 SA |
263 | exprtk_parser_->enable_unknown_symbol_resolver(); |
264 | ||
d203009f | 265 | if (!exprtk_parser_->compile(expression_.toStdString(), *exprtk_expression_)) { |
3f1f6295 | 266 | set_error_message(tr("Error in expression")); |
d203009f | 267 | } else { |
3f1f6295 SA |
268 | // Resolve unknown scalars to signals and add them to the input signal list |
269 | vector<string> unknowns; | |
270 | exprtk_unknown_symbol_table_->get_variable_list(unknowns); | |
271 | for (string& unknown : unknowns) { | |
272 | signal_data* sig_data = signal_from_name(unknown); | |
273 | const shared_ptr<SignalBase> signal = (sig_data) ? (sig_data->sb) : nullptr; | |
274 | if (!signal || (!signal->analog_data())) { | |
275 | set_error_message(QString(tr("%1 isn't a valid signal")).arg( | |
276 | QString::fromStdString(unknown))); | |
277 | } else | |
278 | sig_data->ref = &(exprtk_unknown_symbol_table_->variable_ref(unknown)); | |
279 | } | |
280 | } | |
281 | ||
282 | if (error_message_.isEmpty()) { | |
d203009f SA |
283 | gen_interrupt_ = false; |
284 | gen_thread_ = std::thread(&MathSignal::generation_proc, this); | |
285 | } | |
4640a84e SA |
286 | } |
287 | ||
288 | void MathSignal::generate_samples(uint32_t segment_id, const uint64_t start_sample, | |
289 | const int64_t sample_count) | |
290 | { | |
291 | shared_ptr<Analog> analog = dynamic_pointer_cast<Analog>(data_); | |
292 | shared_ptr<AnalogSegment> segment = analog->analog_segments().at(segment_id); | |
293 | ||
3f1f6295 SA |
294 | // Keep the math functions segment IDs in sync |
295 | fnc_sig_sample_->current_segment = segment_id; | |
296 | ||
4640a84e SA |
297 | const double sample_rate = data_->get_samplerate(); |
298 | ||
299 | exprtk_current_sample_ = start_sample; | |
300 | ||
301 | float *sample_data = new float[sample_count]; | |
302 | ||
303 | for (int64_t i = 0; i < sample_count; i++) { | |
4640a84e | 304 | exprtk_current_time_ = exprtk_current_sample_ / sample_rate; |
3f1f6295 SA |
305 | |
306 | for (auto& entry : input_signals_) { | |
307 | signal_data* sig_data = &(entry.second); | |
308 | update_signal_sample(sig_data, segment_id, exprtk_current_sample_); | |
309 | } | |
310 | ||
4640a84e SA |
311 | double value = exprtk_expression_->value(); |
312 | sample_data[i] = value; | |
3f1f6295 | 313 | exprtk_current_sample_ += 1; |
4640a84e SA |
314 | } |
315 | ||
316 | segment->append_interleaved_samples(sample_data, sample_count, 1); | |
317 | ||
318 | delete[] sample_data; | |
319 | } | |
320 | ||
321 | void MathSignal::generation_proc() | |
322 | { | |
323 | uint32_t segment_id = 0; | |
324 | ||
325 | // Don't do anything until we have a valid sample rate | |
326 | do { | |
327 | if (use_custom_sample_rate_) | |
328 | data_->set_samplerate(custom_sample_rate_); | |
329 | else | |
330 | data_->set_samplerate(session_.get_samplerate()); | |
331 | ||
332 | if (data_->get_samplerate() == 1) { | |
333 | unique_lock<mutex> gen_input_lock(input_mutex_); | |
334 | gen_input_cond_.wait(gen_input_lock); | |
335 | } | |
336 | } while ((!gen_interrupt_) && (data_->get_samplerate() == 1)); | |
337 | ||
338 | if (gen_interrupt_) | |
339 | return; | |
340 | ||
341 | shared_ptr<Analog> analog = dynamic_pointer_cast<Analog>(data_); | |
342 | ||
343 | // Create initial analog segment | |
344 | shared_ptr<AnalogSegment> output_segment = | |
345 | make_shared<AnalogSegment>(*analog.get(), segment_id, analog->get_samplerate()); | |
346 | analog->push_segment(output_segment); | |
347 | ||
348 | // Create analog samples | |
349 | do { | |
350 | const uint64_t input_sample_count = get_working_sample_count(segment_id); | |
351 | const uint64_t output_sample_count = output_segment->get_sample_count(); | |
352 | ||
353 | const uint64_t samples_to_process = | |
354 | (input_sample_count > output_sample_count) ? | |
355 | (input_sample_count - output_sample_count) : 0; | |
356 | ||
357 | // Process the samples if necessary... | |
358 | if (samples_to_process > 0) { | |
359 | const uint64_t chunk_sample_count = ChunkLength; | |
360 | ||
361 | uint64_t processed_samples = 0; | |
362 | do { | |
363 | const uint64_t start_sample = output_sample_count + processed_samples; | |
364 | const uint64_t sample_count = | |
365 | min(samples_to_process - processed_samples, chunk_sample_count); | |
366 | ||
367 | generate_samples(segment_id, start_sample, sample_count); | |
368 | processed_samples += sample_count; | |
369 | ||
370 | // Notify consumers of this signal's data | |
371 | // TODO Does this work when a conversion is active? | |
372 | samples_added(segment_id, start_sample, start_sample + processed_samples); | |
373 | } while (!gen_interrupt_ && (processed_samples < samples_to_process)); | |
374 | } | |
375 | ||
376 | if (samples_to_process == 0) { | |
377 | if (segment_id < session_.get_highest_segment_id()) { | |
378 | // Process next segment | |
379 | segment_id++; | |
380 | ||
381 | output_segment = | |
382 | make_shared<AnalogSegment>(*analog.get(), segment_id, analog->get_samplerate()); | |
383 | analog->push_segment(output_segment); | |
384 | } else { | |
385 | // All segments have been processed, wait for more input | |
386 | unique_lock<mutex> gen_input_lock(input_mutex_); | |
387 | gen_input_cond_.wait(gen_input_lock); | |
388 | } | |
389 | } | |
390 | ||
391 | } while (!gen_interrupt_); | |
392 | } | |
393 | ||
3f1f6295 SA |
394 | signal_data* MathSignal::signal_from_name(const std::string& name) |
395 | { | |
396 | // Look up signal in the map and if it doesn't exist yet, add it for future use | |
397 | ||
398 | auto element = input_signals_.find(name); | |
399 | ||
400 | if (element != input_signals_.end()) { | |
401 | return &(element->second); | |
402 | } else { | |
403 | const vector< shared_ptr<SignalBase> > signalbases = session_.signalbases(); | |
404 | const QString sig_name = QString::fromStdString(name); | |
405 | ||
406 | for (const shared_ptr<SignalBase>& sb : signalbases) | |
407 | if (sb->name() == sig_name) | |
408 | return &(input_signals_.insert({name, signal_data(sb)}).first->second); | |
409 | } | |
410 | ||
411 | return nullptr; | |
412 | } | |
413 | ||
414 | void MathSignal::update_signal_sample(signal_data* sig_data, uint32_t segment_id, uint64_t sample_num) | |
415 | { | |
416 | assert(sig_data); | |
417 | ||
418 | // Update the value only if a different sample is requested | |
419 | if (sig_data->sample_num == sample_num) | |
420 | return; | |
421 | ||
422 | assert(sig_data->sb); | |
423 | const shared_ptr<pv::data::Analog> analog = sig_data->sb->analog_data(); | |
424 | assert(analog); | |
425 | ||
426 | assert(segment_id < analog->analog_segments().size()); | |
427 | ||
428 | const shared_ptr<AnalogSegment> segment = analog->analog_segments().at(segment_id); | |
429 | ||
430 | sig_data->sample_num = sample_num; | |
431 | sig_data->sample_value = segment->get_sample(sample_num); | |
432 | ||
433 | // We only have a reference if this signal is used as a scalar, | |
434 | // if it's used by a function, it's null | |
435 | if (sig_data->ref) | |
436 | *(sig_data->ref) = sig_data->sample_value; | |
437 | } | |
438 | ||
4640a84e SA |
439 | void MathSignal::on_capture_state_changed(int state) |
440 | { | |
441 | if (state == Session::Running) | |
442 | begin_generation(); | |
443 | } | |
444 | ||
445 | void MathSignal::on_data_received() | |
446 | { | |
447 | gen_input_cond_.notify_one(); | |
b0773a8a SA |
448 | } |
449 | ||
450 | } // namespace data | |
451 | } // namespace pv |