]> sigrok.org Git - pulseview.git/blame - pv/data/analogsegment.cpp
Implement autoranging for analog channels
[pulseview.git] / pv / data / analogsegment.cpp
CommitLineData
aba1dd16
JH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
aba1dd16
JH
18 */
19
20#include <extdef.h>
21
22#include <assert.h>
23#include <string.h>
24#include <stdlib.h>
d9e71737 25#include <cmath>
aba1dd16 26
fda5b6e0
JH
27#include <algorithm>
28
f3d66e52 29#include "analogsegment.hpp"
aba1dd16 30
3b68d03d
JH
31using std::lock_guard;
32using std::recursive_mutex;
819f4c25
JH
33using std::max;
34using std::max_element;
35using std::min;
36using std::min_element;
aba1dd16
JH
37
38namespace pv {
1b1ec774 39namespace data {
aba1dd16 40
f3d66e52
JH
41const int AnalogSegment::EnvelopeScalePower = 4;
42const int AnalogSegment::EnvelopeScaleFactor = 1 << EnvelopeScalePower;
43const float AnalogSegment::LogEnvelopeScaleFactor =
fda5b6e0 44 logf(EnvelopeScaleFactor);
f3d66e52 45const uint64_t AnalogSegment::EnvelopeDataUnit = 64*1024; // bytes
fda5b6e0 46
26a883ed 47AnalogSegment::AnalogSegment(uint64_t samplerate) :
73e377fe
SA
48 Segment(samplerate, sizeof(float)),
49 min_value_(0),
50 max_value_(0)
aba1dd16 51{
8dbbc7f0
JH
52 lock_guard<recursive_mutex> lock(mutex_);
53 memset(envelope_levels_, 0, sizeof(envelope_levels_));
aba1dd16
JH
54}
55
f3d66e52 56AnalogSegment::~AnalogSegment()
fda5b6e0 57{
8dbbc7f0
JH
58 lock_guard<recursive_mutex> lock(mutex_);
59 for (Envelope &e : envelope_levels_)
fda5b6e0
JH
60 free(e.samples);
61}
62
f3d66e52 63void AnalogSegment::append_interleaved_samples(const float *data,
bb2cdfff 64 size_t sample_count, size_t stride)
aba1dd16 65{
8dbbc7f0 66 assert(unit_size_ == sizeof(float));
bb2cdfff 67
8dbbc7f0 68 lock_guard<recursive_mutex> lock(mutex_);
bb2cdfff 69
26a883ed
SA
70 for (uint32_t i=0; i < sample_count; i++) {
71 append_single_sample((void*)data);
bb2cdfff
JH
72 data += stride;
73 }
74
fda5b6e0
JH
75 // Generate the first mip-map from the data
76 append_payload_to_envelope_levels();
aba1dd16
JH
77}
78
f3d66e52 79const float* AnalogSegment::get_samples(
d3758367 80 int64_t start_sample, int64_t end_sample) const
a8acb46e 81{
d3758367 82 assert(start_sample >= 0);
8dbbc7f0 83 assert(start_sample < (int64_t)sample_count_);
d3758367 84 assert(end_sample >= 0);
8dbbc7f0 85 assert(end_sample < (int64_t)sample_count_);
d3758367
JH
86 assert(start_sample <= end_sample);
87
8dbbc7f0 88 lock_guard<recursive_mutex> lock(mutex_);
d3758367 89
26a883ed
SA
90 return (float*)get_raw_samples(start_sample, (end_sample - start_sample));
91}
92
73e377fe
SA
93const std::pair<float, float> AnalogSegment::get_min_max() const
94{
95 return std::make_pair(min_value_, max_value_);
96}
97
26a883ed
SA
98SegmentAnalogDataIterator* AnalogSegment::begin_sample_iteration(uint64_t start) const
99{
100 return (SegmentAnalogDataIterator*)begin_raw_sample_iteration(start);
101}
102
103void AnalogSegment::continue_sample_iteration(SegmentAnalogDataIterator* it, uint64_t increase) const
104{
105 Segment::continue_raw_sample_iteration((SegmentRawDataIterator*)it, increase);
106}
107
108void AnalogSegment::end_sample_iteration(SegmentAnalogDataIterator* it) const
109{
110 Segment::end_raw_sample_iteration((SegmentRawDataIterator*)it);
a8acb46e
JH
111}
112
f3d66e52 113void AnalogSegment::get_envelope_section(EnvelopeSection &s,
9320072d
JH
114 uint64_t start, uint64_t end, float min_length) const
115{
116 assert(end <= get_sample_count());
117 assert(start <= end);
118 assert(min_length > 0);
119
8dbbc7f0 120 lock_guard<recursive_mutex> lock(mutex_);
9320072d
JH
121
122 const unsigned int min_level = max((int)floorf(logf(min_length) /
123 LogEnvelopeScaleFactor) - 1, 0);
124 const unsigned int scale_power = (min_level + 1) *
125 EnvelopeScalePower;
126 start >>= scale_power;
127 end >>= scale_power;
128
129 s.start = start << scale_power;
130 s.scale = 1 << scale_power;
131 s.length = end - start;
132 s.samples = new EnvelopeSample[s.length];
8dbbc7f0 133 memcpy(s.samples, envelope_levels_[min_level].samples + start,
9320072d
JH
134 s.length * sizeof(EnvelopeSample));
135}
136
f3d66e52 137void AnalogSegment::reallocate_envelope(Envelope &e)
fda5b6e0
JH
138{
139 const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) /
140 EnvelopeDataUnit) * EnvelopeDataUnit;
2ad82c2e 141 if (new_data_length > e.data_length) {
fda5b6e0
JH
142 e.data_length = new_data_length;
143 e.samples = (EnvelopeSample*)realloc(e.samples,
144 new_data_length * sizeof(EnvelopeSample));
145 }
146}
147
f3d66e52 148void AnalogSegment::append_payload_to_envelope_levels()
fda5b6e0 149{
8dbbc7f0 150 Envelope &e0 = envelope_levels_[0];
fda5b6e0
JH
151 uint64_t prev_length;
152 EnvelopeSample *dest_ptr;
26a883ed 153 SegmentRawDataIterator* it;
fda5b6e0
JH
154
155 // Expand the data buffer to fit the new samples
156 prev_length = e0.length;
8dbbc7f0 157 e0.length = sample_count_ / EnvelopeScaleFactor;
fda5b6e0
JH
158
159 // Break off if there are no new samples to compute
160 if (e0.length == prev_length)
161 return;
162
163 reallocate_envelope(e0);
164
165 dest_ptr = e0.samples + prev_length;
166
167 // Iterate through the samples to populate the first level mipmap
26a883ed
SA
168 uint64_t start_sample = prev_length * EnvelopeScaleFactor;
169 uint64_t end_sample = e0.length * EnvelopeScaleFactor;
170
171 it = begin_raw_sample_iteration(start_sample);
172 for (uint64_t i = start_sample; i < end_sample; i += EnvelopeScaleFactor) {
173 const float* samples = (float*)it->value;
174
fda5b6e0 175 const EnvelopeSample sub_sample = {
26a883ed
SA
176 *min_element(samples, samples + EnvelopeScaleFactor),
177 *max_element(samples, samples + EnvelopeScaleFactor),
fda5b6e0
JH
178 };
179
73e377fe
SA
180 if (sub_sample.min < min_value_) min_value_ = sub_sample.min;
181 if (sub_sample.max > max_value_) max_value_ = sub_sample.max;
182
26a883ed 183 continue_raw_sample_iteration(it, EnvelopeScaleFactor);
fda5b6e0
JH
184 *dest_ptr++ = sub_sample;
185 }
26a883ed 186 end_raw_sample_iteration(it);
fda5b6e0
JH
187
188 // Compute higher level mipmaps
2ad82c2e 189 for (unsigned int level = 1; level < ScaleStepCount; level++) {
8dbbc7f0
JH
190 Envelope &e = envelope_levels_[level];
191 const Envelope &el = envelope_levels_[level-1];
fda5b6e0
JH
192
193 // Expand the data buffer to fit the new samples
194 prev_length = e.length;
195 e.length = el.length / EnvelopeScaleFactor;
196
26a883ed 197 // Break off if there are no more samples to be computed
fda5b6e0
JH
198 if (e.length == prev_length)
199 break;
200
201 reallocate_envelope(e);
202
26a883ed 203 // Subsample the lower level
fda5b6e0
JH
204 const EnvelopeSample *src_ptr =
205 el.samples + prev_length * EnvelopeScaleFactor;
206 const EnvelopeSample *const end_dest_ptr = e.samples + e.length;
26a883ed 207
fda5b6e0 208 for (dest_ptr = e.samples + prev_length;
2ad82c2e 209 dest_ptr < end_dest_ptr; dest_ptr++) {
fda5b6e0
JH
210 const EnvelopeSample *const end_src_ptr =
211 src_ptr + EnvelopeScaleFactor;
212
213 EnvelopeSample sub_sample = *src_ptr++;
2ad82c2e 214 while (src_ptr < end_src_ptr) {
73e377fe 215 sub_sample.min = min(sub_sample.min, src_ptr->min);;
fda5b6e0
JH
216 sub_sample.max = max(sub_sample.max, src_ptr->max);
217 src_ptr++;
218 }
219
220 *dest_ptr = sub_sample;
221 }
222 }
223}
224
1b1ec774 225} // namespace data
aba1dd16 226} // namespace pv