2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
30 #include <boost/foreach.hpp>
32 #include "analogsnapshot.h"
34 using namespace boost;
40 const int AnalogSnapshot::EnvelopeScalePower = 4;
41 const int AnalogSnapshot::EnvelopeScaleFactor = 1 << EnvelopeScalePower;
42 const float AnalogSnapshot::LogEnvelopeScaleFactor =
43 logf(EnvelopeScaleFactor);
44 const uint64_t AnalogSnapshot::EnvelopeDataUnit = 64*1024; // bytes
46 AnalogSnapshot::AnalogSnapshot(const sr_datafeed_analog &analog) :
47 Snapshot(sizeof(float))
49 lock_guard<recursive_mutex> lock(_mutex);
50 memset(_envelope_levels, 0, sizeof(_envelope_levels));
51 append_payload(analog);
54 AnalogSnapshot::~AnalogSnapshot()
56 lock_guard<recursive_mutex> lock(_mutex);
57 BOOST_FOREACH(Envelope &e, _envelope_levels)
61 void AnalogSnapshot::append_payload(
62 const sr_datafeed_analog &analog)
64 lock_guard<recursive_mutex> lock(_mutex);
65 append_data(analog.data, analog.num_samples);
67 // Generate the first mip-map from the data
68 append_payload_to_envelope_levels();
71 const float* AnalogSnapshot::get_samples(
72 int64_t start_sample, int64_t end_sample) const
74 assert(start_sample >= 0);
75 assert(start_sample < (int64_t)_sample_count);
76 assert(end_sample >= 0);
77 assert(end_sample < (int64_t)_sample_count);
78 assert(start_sample <= end_sample);
80 lock_guard<recursive_mutex> lock(_mutex);
82 float *const data = new float[end_sample - start_sample];
83 memcpy(data, (float*)_data + start_sample, sizeof(float) *
84 (end_sample - start_sample));
88 void AnalogSnapshot::get_envelope_section(EnvelopeSection &s,
89 uint64_t start, uint64_t end, float min_length) const
91 assert(end <= get_sample_count());
93 assert(min_length > 0);
95 lock_guard<recursive_mutex> lock(_mutex);
97 const unsigned int min_level = max((int)floorf(logf(min_length) /
98 LogEnvelopeScaleFactor) - 1, 0);
99 const unsigned int scale_power = (min_level + 1) *
101 start >>= scale_power;
104 s.start = start << scale_power;
105 s.scale = 1 << scale_power;
106 s.length = end - start;
107 s.samples = new EnvelopeSample[s.length];
108 memcpy(s.samples, _envelope_levels[min_level].samples + start,
109 s.length * sizeof(EnvelopeSample));
112 void AnalogSnapshot::reallocate_envelope(Envelope &e)
114 const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) /
115 EnvelopeDataUnit) * EnvelopeDataUnit;
116 if (new_data_length > e.data_length)
118 e.data_length = new_data_length;
119 e.samples = (EnvelopeSample*)realloc(e.samples,
120 new_data_length * sizeof(EnvelopeSample));
124 void AnalogSnapshot::append_payload_to_envelope_levels()
126 Envelope &e0 = _envelope_levels[0];
127 uint64_t prev_length;
128 EnvelopeSample *dest_ptr;
130 // Expand the data buffer to fit the new samples
131 prev_length = e0.length;
132 e0.length = _sample_count / EnvelopeScaleFactor;
134 // Break off if there are no new samples to compute
135 if (e0.length == prev_length)
138 reallocate_envelope(e0);
140 dest_ptr = e0.samples + prev_length;
142 // Iterate through the samples to populate the first level mipmap
143 const float *const end_src_ptr = (float*)_data +
144 e0.length * EnvelopeScaleFactor;
145 for (const float *src_ptr = (float*)_data +
146 prev_length * EnvelopeScaleFactor;
147 src_ptr < end_src_ptr; src_ptr += EnvelopeScaleFactor)
149 const EnvelopeSample sub_sample = {
150 *min_element(src_ptr, src_ptr + EnvelopeScaleFactor),
151 *max_element(src_ptr, src_ptr + EnvelopeScaleFactor),
154 *dest_ptr++ = sub_sample;
157 // Compute higher level mipmaps
158 for (unsigned int level = 1; level < ScaleStepCount; level++)
160 Envelope &e = _envelope_levels[level];
161 const Envelope &el = _envelope_levels[level-1];
163 // Expand the data buffer to fit the new samples
164 prev_length = e.length;
165 e.length = el.length / EnvelopeScaleFactor;
167 // Break off if there are no more samples to computed
168 if (e.length == prev_length)
171 reallocate_envelope(e);
173 // Subsample the level lower level
174 const EnvelopeSample *src_ptr =
175 el.samples + prev_length * EnvelopeScaleFactor;
176 const EnvelopeSample *const end_dest_ptr = e.samples + e.length;
177 for (dest_ptr = e.samples + prev_length;
178 dest_ptr < end_dest_ptr; dest_ptr++)
180 const EnvelopeSample *const end_src_ptr =
181 src_ptr + EnvelopeScaleFactor;
183 EnvelopeSample sub_sample = *src_ptr++;
184 while (src_ptr < end_src_ptr)
186 sub_sample.min = min(sub_sample.min, src_ptr->min);
187 sub_sample.max = max(sub_sample.max, src_ptr->max);
191 *dest_ptr = sub_sample;