]> sigrok.org Git - pulseview.git/blob - pv/data/analogsegment.cpp
886bd4f58f9a531d5aff113e692352fa7460f742
[pulseview.git] / pv / data / analogsegment.cpp
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
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <extdef.h>
21
22 #include <assert.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <cmath>
26
27 #include <algorithm>
28
29 #include "analogsegment.hpp"
30
31 using std::lock_guard;
32 using std::recursive_mutex;
33 using std::max;
34 using std::max_element;
35 using std::min;
36 using std::min_element;
37
38 namespace pv {
39 namespace data {
40
41 const int AnalogSegment::EnvelopeScalePower = 4;
42 const int AnalogSegment::EnvelopeScaleFactor = 1 << EnvelopeScalePower;
43 const float AnalogSegment::LogEnvelopeScaleFactor =
44         logf(EnvelopeScaleFactor);
45 const uint64_t AnalogSegment::EnvelopeDataUnit = 64*1024;       // bytes
46
47 AnalogSegment::AnalogSegment(uint64_t samplerate) :
48         Segment(samplerate, sizeof(float)),
49         min_value_(0),
50         max_value_(0)
51 {
52         lock_guard<recursive_mutex> lock(mutex_);
53         memset(envelope_levels_, 0, sizeof(envelope_levels_));
54 }
55
56 AnalogSegment::~AnalogSegment()
57 {
58         lock_guard<recursive_mutex> lock(mutex_);
59         for (Envelope &e : envelope_levels_)
60                 free(e.samples);
61 }
62
63 void AnalogSegment::append_interleaved_samples(const float *data,
64         size_t sample_count, size_t stride)
65 {
66         assert(unit_size_ == sizeof(float));
67
68         lock_guard<recursive_mutex> lock(mutex_);
69
70         for (uint32_t i=0; i < sample_count; i++) {
71                 append_single_sample((void*)data);
72                 data += stride;
73         }
74
75         // Generate the first mip-map from the data
76         append_payload_to_envelope_levels();
77 }
78
79 const float* AnalogSegment::get_samples(
80         int64_t start_sample, int64_t end_sample) const
81 {
82         assert(start_sample >= 0);
83         assert(start_sample < (int64_t)sample_count_);
84         assert(end_sample >= 0);
85         assert(end_sample < (int64_t)sample_count_);
86         assert(start_sample <= end_sample);
87
88         lock_guard<recursive_mutex> lock(mutex_);
89
90         return (float*)get_raw_samples(start_sample, (end_sample - start_sample));
91 }
92
93 const std::pair<float, float> AnalogSegment::get_min_max() const
94 {
95         return std::make_pair(min_value_, max_value_);
96 }
97
98 SegmentAnalogDataIterator* AnalogSegment::begin_sample_iteration(uint64_t start) const
99 {
100         return (SegmentAnalogDataIterator*)begin_raw_sample_iteration(start);
101 }
102
103 void AnalogSegment::continue_sample_iteration(SegmentAnalogDataIterator* it, uint64_t increase) const
104 {
105         Segment::continue_raw_sample_iteration((SegmentRawDataIterator*)it, increase);
106 }
107
108 void AnalogSegment::end_sample_iteration(SegmentAnalogDataIterator* it) const
109 {
110         Segment::end_raw_sample_iteration((SegmentRawDataIterator*)it);
111 }
112
113 void AnalogSegment::get_envelope_section(EnvelopeSection &s,
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
120         lock_guard<recursive_mutex> lock(mutex_);
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];
133         memcpy(s.samples, envelope_levels_[min_level].samples + start,
134                 s.length * sizeof(EnvelopeSample));
135 }
136
137 void AnalogSegment::reallocate_envelope(Envelope &e)
138 {
139         const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) /
140                 EnvelopeDataUnit) * EnvelopeDataUnit;
141         if (new_data_length > e.data_length) {
142                 e.data_length = new_data_length;
143                 e.samples = (EnvelopeSample*)realloc(e.samples,
144                         new_data_length * sizeof(EnvelopeSample));
145         }
146 }
147
148 void AnalogSegment::append_payload_to_envelope_levels()
149 {
150         Envelope &e0 = envelope_levels_[0];
151         uint64_t prev_length;
152         EnvelopeSample *dest_ptr;
153         SegmentRawDataIterator* it;
154
155         // Expand the data buffer to fit the new samples
156         prev_length = e0.length;
157         e0.length = sample_count_ / EnvelopeScaleFactor;
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
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
175                 const EnvelopeSample sub_sample = {
176                         *min_element(samples, samples + EnvelopeScaleFactor),
177                         *max_element(samples, samples + EnvelopeScaleFactor),
178                 };
179
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
183                 continue_raw_sample_iteration(it, EnvelopeScaleFactor);
184                 *dest_ptr++ = sub_sample;
185         }
186         end_raw_sample_iteration(it);
187
188         // Compute higher level mipmaps
189         for (unsigned int level = 1; level < ScaleStepCount; level++) {
190                 Envelope &e = envelope_levels_[level];
191                 const Envelope &el = envelope_levels_[level-1];
192
193                 // Expand the data buffer to fit the new samples
194                 prev_length = e.length;
195                 e.length = el.length / EnvelopeScaleFactor;
196
197                 // Break off if there are no more samples to be computed
198                 if (e.length == prev_length)
199                         break;
200
201                 reallocate_envelope(e);
202
203                 // Subsample the lower level
204                 const EnvelopeSample *src_ptr =
205                         el.samples + prev_length * EnvelopeScaleFactor;
206                 const EnvelopeSample *const end_dest_ptr = e.samples + e.length;
207
208                 for (dest_ptr = e.samples + prev_length;
209                                 dest_ptr < end_dest_ptr; dest_ptr++) {
210                         const EnvelopeSample *const end_src_ptr =
211                                 src_ptr + EnvelopeScaleFactor;
212
213                         EnvelopeSample sub_sample = *src_ptr++;
214                         while (src_ptr < end_src_ptr) {
215                                 sub_sample.min = min(sub_sample.min, src_ptr->min);;
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
225 } // namespace data
226 } // namespace pv