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::reallocate_envelope(Envelope &e)
90 const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) /
91 EnvelopeDataUnit) * EnvelopeDataUnit;
92 if (new_data_length > e.data_length)
94 e.data_length = new_data_length;
95 e.samples = (EnvelopeSample*)realloc(e.samples,
96 new_data_length * sizeof(EnvelopeSample));
100 void AnalogSnapshot::append_payload_to_envelope_levels()
102 Envelope &e0 = _envelope_levels[0];
103 uint64_t prev_length;
104 EnvelopeSample *dest_ptr;
106 // Expand the data buffer to fit the new samples
107 prev_length = e0.length;
108 e0.length = _sample_count / EnvelopeScaleFactor;
110 // Break off if there are no new samples to compute
111 if (e0.length == prev_length)
114 reallocate_envelope(e0);
116 dest_ptr = e0.samples + prev_length;
118 // Iterate through the samples to populate the first level mipmap
119 const float *const end_src_ptr = (float*)_data +
120 e0.length * EnvelopeScaleFactor;
121 for (const float *src_ptr = (float*)_data +
122 prev_length * EnvelopeScaleFactor;
123 src_ptr < end_src_ptr; src_ptr += EnvelopeScaleFactor)
125 const EnvelopeSample sub_sample = {
126 *min_element(src_ptr, src_ptr + EnvelopeScaleFactor),
127 *max_element(src_ptr, src_ptr + EnvelopeScaleFactor),
130 *dest_ptr++ = sub_sample;
133 // Compute higher level mipmaps
134 for (unsigned int level = 1; level < ScaleStepCount; level++)
136 Envelope &e = _envelope_levels[level];
137 const Envelope &el = _envelope_levels[level-1];
139 // Expand the data buffer to fit the new samples
140 prev_length = e.length;
141 e.length = el.length / EnvelopeScaleFactor;
143 // Break off if there are no more samples to computed
144 if (e.length == prev_length)
147 reallocate_envelope(e);
149 // Subsample the level lower level
150 const EnvelopeSample *src_ptr =
151 el.samples + prev_length * EnvelopeScaleFactor;
152 const EnvelopeSample *const end_dest_ptr = e.samples + e.length;
153 for (dest_ptr = e.samples + prev_length;
154 dest_ptr < end_dest_ptr; dest_ptr++)
156 const EnvelopeSample *const end_src_ptr =
157 src_ptr + EnvelopeScaleFactor;
159 EnvelopeSample sub_sample = *src_ptr++;
160 while (src_ptr < end_src_ptr)
162 sub_sample.min = min(sub_sample.min, src_ptr->min);
163 sub_sample.max = max(sub_sample.max, src_ptr->max);
167 *dest_ptr = sub_sample;