Repository navigation
Expand file tree
/
Copy pathgui_analysis_source.cpp
More file actions
188 lines (170 loc) · 7.4 KB
/
Copy pathgui_analysis_source.cpp
File metadata and controls
188 lines (170 loc) · 7.4 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
// Shared time selection and synchronized channel snapshots for GUI analyses.
#include "gui_analysis_source.hpp"
#include "gui_processing.hpp"
#include "gui_state.hpp"
#include "gui_frf.hpp"
#include "gui_spectrum.hpp"
#include "gui_state_history.hpp"
#include "recording_overview.hpp"
namespace gui {
std::shared_ptr<const lvm::RecordingSource> current_recording_source() {
if(!g.recording_source) {
if(g.ds.time.empty() || g.source_path.empty() || !g.loaded_source_stamp_valid ||
g.ds.source_columns.size()!=g.ds.channel_count())
throw lvm::RecordingFailure(lvm::RecordingError::InvalidRequest,"Document has no replayable recording source.");
g.recording_source=lvm::RecordingSource::from_file(std::filesystem::path(g.source_path),
{g.ds.time.front(),g.ds.time.back()},g.ds.source_columns,g.source_scan_index,
lvm::RecordingStamp{g.loaded_source_size,g.loaded_source_modified});
}
g.recording_source->check_current();
return g.recording_source;
}
bool has_fft_window() {
return g.fft_window_active && g.fft_window_end > g.fft_window_start;
}
std::size_t analysis_source_rows(double start,double end) {
const auto* timeline=g.recording_source ? g.recording_source->resident_timeline() : nullptr;
const auto& time=timeline ? timeline->time : g.ds.time;
if(time.size()>2 && (timeline || !g.current_file_partial))return static_cast<std::size_t>(std::upper_bound(time.begin(),time.end(),end)-
std::lower_bound(time.begin(),time.end(),start));
if(g.overview.index) {
const auto rows=g.overview.index->sample_range(lvm::AnalysisRange::selected(start,end));
return static_cast<std::size_t>(rows.second-rows.first);
}
const auto bounds=analysis_recording_bounds();
if(start<=bounds.start && end>=bounds.end)return static_cast<std::size_t>(g.recording_rows);
return 0;
}
lvm::TimeRange analysis_recording_bounds() {
if(!g.ds.frequency_axis) {
if(g.recording_source)return g.recording_source->bounds();
if(g.source_scan_index)return {g.source_scan_index->range_start,g.source_scan_index->range_end};
}
return {g.data_t0,g.data_t1};
}
void clear_fft_window() {
invalidate_frf();
g.fft_window_active = false;
g.fft_window_start = 0.0;
g.fft_window_end = 0.0;
clear_spectrum_cache_state();
}
void clamp_time_window(double& start, double& end) {
if (start > end) std::swap(start, end);
const auto bounds=analysis_recording_bounds();
start = std::max(start, bounds.start);
end = std::min(end, bounds.end);
}
void set_fft_window(double start, double end) {
clamp_time_window(start, end);
if (end <= start) {
clear_fft_window();
return;
}
invalidate_frf();
g.fft_window_active = true;
g.fft_window_start = start;
g.fft_window_end = end;
clear_spectrum_cache_state();
}
bool current_fft_source_window(double& start, double& end, bool& from_selection) {
if (!has_data()) return false;
if (has_fft_window()) {
start = g.fft_window_start;
end = g.fft_window_end;
from_selection = true;
return true;
}
const auto bounds=analysis_recording_bounds();
start = bounds.start;
end = bounds.end;
clamp_time_window(start, end);
from_selection = false;
return end > start;
}
bool change_analysis_range(lvm::AnalysisRange range) {
lvm::TimeRange resolved;
if(!has_data() || !range.resolve(analysis_recording_bounds(),resolved) || resolved.end<=resolved.start)return false;
const auto before=capture_settings_snapshot();
if(range.kind==lvm::AnalysisRange::Kind::EntireRecording)clear_fft_window();
else set_fft_window(resolved.start,resolved.end);
record_settings_change(before);
return true;
}
bool fft_window_contains_time(double t) {
return has_fft_window() && t >= g.fft_window_start && t <= g.fft_window_end;
}
bool build_time_window_dataset(const lvm::Dataset& in, double start, double end, lvm::Dataset& out,
const std::vector<std::size_t>* selected_channels, bool apply_processing) {
out = lvm::Dataset{};
out.stats = in.stats;
out.frequency_axis = in.frequency_axis;
out.export_comments = in.export_comments;
out.ok = true;
if (apply_processing) ensure_channel_formula_vectors();
if (in.time.empty()) return true;
std::vector<std::size_t> channel_indices;
if (selected_channels && !selected_channels->empty()) {
channel_indices = *selected_channels;
} else {
channel_indices.resize(in.channels.size());
for (std::size_t i = 0; i < in.channels.size(); ++i) channel_indices[i] = i;
}
out.names.reserve(channel_indices.size());
out.channels.resize(channel_indices.size());
for (std::size_t channel_index : channel_indices) {
if (channel_index < in.names.size()) out.names.push_back(in.names[channel_index]);
else out.names.push_back("Channel_" + std::to_string(channel_index + 1));
}
const std::size_t lo = static_cast<std::size_t>(
std::lower_bound(in.time.begin(), in.time.end(), start) - in.time.begin());
const std::size_t hi = static_cast<std::size_t>(
std::upper_bound(in.time.begin(), in.time.end(), end) - in.time.begin());
if (lo >= hi) return true;
out.time.assign(in.time.begin() + static_cast<std::ptrdiff_t>(lo),
in.time.begin() + static_cast<std::ptrdiff_t>(hi));
for (std::size_t out_index = 0; out_index < channel_indices.size(); ++out_index) {
const std::size_t c = channel_indices[out_index];
auto& dst = out.channels[out_index];
dst.reserve(hi - lo);
const TransformRuntimeKind kind = (c < g.channel_transform_kind.size())
? g.channel_transform_kind[c]
: TransformRuntimeKind::Identity;
if (!apply_processing || kind == TransformRuntimeKind::Identity) {
const std::size_t base = dst.size();
dst.insert(dst.end(),
in.channels[c].begin() + static_cast<std::ptrdiff_t>(lo),
in.channels[c].begin() + static_cast<std::ptrdiff_t>(hi));
if (apply_processing && g.noise_threshold_enabled) {
for (std::size_t i = base; i < dst.size(); ++i) {
dst[i] = rendered_channel_sample(c, lo + (i - base));
}
}
continue;
}
if (kind == TransformRuntimeKind::Affine) {
for (std::size_t r = lo; r < hi; ++r) {
dst.push_back(rendered_channel_sample(c, r));
}
continue;
}
ensure_transformed_channel_cache(c);
const auto& cache = g.transformed_channel_cache[c];
if(c>=g.transformed_channel_cache_valid.size() || !g.transformed_channel_cache_valid[c]) {
for(std::size_t r=lo;r<hi;++r)dst.push_back(apply_processing && g.noise_threshold_enabled
? rendered_channel_sample(c,r) : transform_channel_value(c,in.channels[c][r]));
continue;
}
const std::size_t base = dst.size();
dst.insert(dst.end(),
cache.begin() + static_cast<std::ptrdiff_t>(lo),
cache.begin() + static_cast<std::ptrdiff_t>(hi));
if (g.noise_threshold_enabled) {
for (std::size_t i = base; i < dst.size(); ++i) {
dst[i] = rendered_channel_sample(c, lo + (i - base));
}
}
}
return true;
}
} // namespace gui