]> sigrok.org Git - pulseview.git/blame - pv/data/analogsnapshot.cpp
Check if unportable sample pack/unpack works and in that case use it.
[pulseview.git] / pv / data / analogsnapshot.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
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <extdef.h>
22
23#include <assert.h>
24#include <string.h>
25#include <stdlib.h>
26#include <math.h>
27
fda5b6e0
JH
28#include <algorithm>
29
aba1dd16
JH
30#include <boost/foreach.hpp>
31
1b1ec774 32#include "analogsnapshot.h"
aba1dd16 33
819f4c25
JH
34using boost::lock_guard;
35using boost::recursive_mutex;
36using std::max;
37using std::max_element;
38using std::min;
39using std::min_element;
aba1dd16
JH
40
41namespace pv {
1b1ec774 42namespace data {
aba1dd16 43
fda5b6e0
JH
44const int AnalogSnapshot::EnvelopeScalePower = 4;
45const int AnalogSnapshot::EnvelopeScaleFactor = 1 << EnvelopeScalePower;
46const float AnalogSnapshot::LogEnvelopeScaleFactor =
47 logf(EnvelopeScaleFactor);
48const uint64_t AnalogSnapshot::EnvelopeDataUnit = 64*1024; // bytes
49
27d7c96b 50AnalogSnapshot::AnalogSnapshot(const uint64_t expected_num_samples) :
1b1ec774 51 Snapshot(sizeof(float))
aba1dd16 52{
27d7c96b
DK
53 set_capacity(expected_num_samples);
54
aba1dd16 55 lock_guard<recursive_mutex> lock(_mutex);
fda5b6e0 56 memset(_envelope_levels, 0, sizeof(_envelope_levels));
aba1dd16
JH
57}
58
fda5b6e0
JH
59AnalogSnapshot::~AnalogSnapshot()
60{
61 lock_guard<recursive_mutex> lock(_mutex);
62 BOOST_FOREACH(Envelope &e, _envelope_levels)
63 free(e.samples);
64}
65
bb2cdfff
JH
66void AnalogSnapshot::append_interleaved_samples(const float *data,
67 size_t sample_count, size_t stride)
aba1dd16 68{
bb2cdfff
JH
69 assert(_unit_size == sizeof(float));
70
aba1dd16 71 lock_guard<recursive_mutex> lock(_mutex);
bb2cdfff
JH
72
73 _data = realloc(_data, (_sample_count + sample_count) * sizeof(float));
74
75 float *dst = (float*)_data + _sample_count;
76 const float *dst_end = dst + sample_count;
77 while (dst != dst_end)
78 {
79 *dst++ = *data;
80 data += stride;
81 }
82
83 _sample_count += sample_count;
fda5b6e0
JH
84
85 // Generate the first mip-map from the data
86 append_payload_to_envelope_levels();
aba1dd16
JH
87}
88
d3758367
JH
89const float* AnalogSnapshot::get_samples(
90 int64_t start_sample, int64_t end_sample) const
a8acb46e 91{
d3758367
JH
92 assert(start_sample >= 0);
93 assert(start_sample < (int64_t)_sample_count);
94 assert(end_sample >= 0);
95 assert(end_sample < (int64_t)_sample_count);
96 assert(start_sample <= end_sample);
97
98 lock_guard<recursive_mutex> lock(_mutex);
99
100 float *const data = new float[end_sample - start_sample];
101 memcpy(data, (float*)_data + start_sample, sizeof(float) *
102 (end_sample - start_sample));
103 return data;
a8acb46e
JH
104}
105
9320072d
JH
106void AnalogSnapshot::get_envelope_section(EnvelopeSection &s,
107 uint64_t start, uint64_t end, float min_length) const
108{
109 assert(end <= get_sample_count());
110 assert(start <= end);
111 assert(min_length > 0);
112
113 lock_guard<recursive_mutex> lock(_mutex);
114
115 const unsigned int min_level = max((int)floorf(logf(min_length) /
116 LogEnvelopeScaleFactor) - 1, 0);
117 const unsigned int scale_power = (min_level + 1) *
118 EnvelopeScalePower;
119 start >>= scale_power;
120 end >>= scale_power;
121
122 s.start = start << scale_power;
123 s.scale = 1 << scale_power;
124 s.length = end - start;
125 s.samples = new EnvelopeSample[s.length];
126 memcpy(s.samples, _envelope_levels[min_level].samples + start,
127 s.length * sizeof(EnvelopeSample));
128}
129
fda5b6e0
JH
130void AnalogSnapshot::reallocate_envelope(Envelope &e)
131{
132 const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) /
133 EnvelopeDataUnit) * EnvelopeDataUnit;
134 if (new_data_length > e.data_length)
135 {
136 e.data_length = new_data_length;
137 e.samples = (EnvelopeSample*)realloc(e.samples,
138 new_data_length * sizeof(EnvelopeSample));
139 }
140}
141
142void AnalogSnapshot::append_payload_to_envelope_levels()
143{
144 Envelope &e0 = _envelope_levels[0];
145 uint64_t prev_length;
146 EnvelopeSample *dest_ptr;
147
148 // Expand the data buffer to fit the new samples
149 prev_length = e0.length;
150 e0.length = _sample_count / EnvelopeScaleFactor;
151
152 // Break off if there are no new samples to compute
153 if (e0.length == prev_length)
154 return;
155
156 reallocate_envelope(e0);
157
158 dest_ptr = e0.samples + prev_length;
159
160 // Iterate through the samples to populate the first level mipmap
161 const float *const end_src_ptr = (float*)_data +
162 e0.length * EnvelopeScaleFactor;
163 for (const float *src_ptr = (float*)_data +
164 prev_length * EnvelopeScaleFactor;
165 src_ptr < end_src_ptr; src_ptr += EnvelopeScaleFactor)
166 {
167 const EnvelopeSample sub_sample = {
168 *min_element(src_ptr, src_ptr + EnvelopeScaleFactor),
169 *max_element(src_ptr, src_ptr + EnvelopeScaleFactor),
170 };
171
172 *dest_ptr++ = sub_sample;
173 }
174
175 // Compute higher level mipmaps
176 for (unsigned int level = 1; level < ScaleStepCount; level++)
177 {
178 Envelope &e = _envelope_levels[level];
179 const Envelope &el = _envelope_levels[level-1];
180
181 // Expand the data buffer to fit the new samples
182 prev_length = e.length;
183 e.length = el.length / EnvelopeScaleFactor;
184
185 // Break off if there are no more samples to computed
186 if (e.length == prev_length)
187 break;
188
189 reallocate_envelope(e);
190
191 // Subsample the level lower level
192 const EnvelopeSample *src_ptr =
193 el.samples + prev_length * EnvelopeScaleFactor;
194 const EnvelopeSample *const end_dest_ptr = e.samples + e.length;
195 for (dest_ptr = e.samples + prev_length;
196 dest_ptr < end_dest_ptr; dest_ptr++)
197 {
198 const EnvelopeSample *const end_src_ptr =
199 src_ptr + EnvelopeScaleFactor;
200
201 EnvelopeSample sub_sample = *src_ptr++;
202 while (src_ptr < end_src_ptr)
203 {
204 sub_sample.min = min(sub_sample.min, src_ptr->min);
205 sub_sample.max = max(sub_sample.max, src_ptr->max);
206 src_ptr++;
207 }
208
209 *dest_ptr = sub_sample;
210 }
211 }
212}
213
1b1ec774 214} // namespace data
aba1dd16 215} // namespace pv