Extract data from peak regions using peak detection.
118{
119 std::vector<std::vector<double>> result;
120 result.reserve(data.size());
121
122 for (const auto& channel : data) {
123 if (channel.size() < 3) {
124 result.emplace_back();
125 continue;
126 }
127
128 try {
129 auto analyzer = std::make_shared<EnergyAnalyzer<std::vector<Kakshya::DataVariant>, Eigen::VectorXd>>(
130 512, 256);
131 analyzer->set_parameter("method", "peak");
132 analyzer->set_parameter("threshold", threshold);
133
134 std::vector<Kakshya::DataVariant> data_variant {
136 };
137 EnergyAnalysis energy_result = analyzer->analyze_energy(data_variant);
138
139 if (energy_result.channels.empty()) {
140 result.emplace_back();
141 continue;
142 }
143
144 const auto& peak_positions = energy_result.channels[0].event_positions;
145
146 if (peak_positions.empty()) {
147 result.emplace_back();
148 continue;
149 }
150
151 std::vector<size_t> filtered_positions;
152 size_t last_position = 0;
153
154 for (size_t pos : peak_positions) {
155 if (filtered_positions.empty() || (pos - last_position) >= static_cast<size_t>(min_distance)) {
156 filtered_positions.push_back(pos);
157 last_position = pos;
158 }
159 }
160
161 std::vector<double> extracted_data;
162 for (size_t peak_pos : filtered_positions) {
163 const size_t half_region = region_size / 2;
164 const size_t start_idx = (peak_pos >= half_region) ? peak_pos - half_region : 0;
165 const size_t end_idx = std::min(peak_pos + half_region, channel.size());
166
167 if (start_idx < channel.size() && end_idx <= channel.size() && start_idx < end_idx) {
168 std::ranges::copy(channel.subspan(start_idx, end_idx - start_idx),
169 std::back_inserter(extracted_data));
170 }
171 }
172
173 result.push_back(std::move(extracted_data));
174 } catch (const std::exception&) {
175 result.emplace_back();
176 }
177 }
178
179 return result;
180}
std::variant< std::vector< double >, std::vector< float >, std::vector< uint8_t >, std::vector< uint16_t >, std::vector< uint32_t >, std::vector< std::complex< float > >, std::vector< std::complex< double > >, std::vector< glm::vec2 >, std::vector< glm::vec3 >, std::vector< glm::vec4 >, std::vector< glm::mat4 > > DataVariant
Multi-type data storage for different precision needs.