]>
Commit | Line | Data |
---|---|---|
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 |
34 | using boost::lock_guard; |
35 | using boost::recursive_mutex; | |
36 | using std::max; | |
37 | using std::max_element; | |
38 | using std::min; | |
39 | using std::min_element; | |
aba1dd16 JH |
40 | |
41 | namespace pv { | |
1b1ec774 | 42 | namespace data { |
aba1dd16 | 43 | |
fda5b6e0 JH |
44 | const int AnalogSnapshot::EnvelopeScalePower = 4; |
45 | const int AnalogSnapshot::EnvelopeScaleFactor = 1 << EnvelopeScalePower; | |
46 | const float AnalogSnapshot::LogEnvelopeScaleFactor = | |
47 | logf(EnvelopeScaleFactor); | |
48 | const uint64_t AnalogSnapshot::EnvelopeDataUnit = 64*1024; // bytes | |
49 | ||
1b1ec774 JH |
50 | AnalogSnapshot::AnalogSnapshot(const sr_datafeed_analog &analog) : |
51 | Snapshot(sizeof(float)) | |
aba1dd16 JH |
52 | { |
53 | lock_guard<recursive_mutex> lock(_mutex); | |
fda5b6e0 | 54 | memset(_envelope_levels, 0, sizeof(_envelope_levels)); |
aba1dd16 JH |
55 | append_payload(analog); |
56 | } | |
57 | ||
fda5b6e0 JH |
58 | AnalogSnapshot::~AnalogSnapshot() |
59 | { | |
60 | lock_guard<recursive_mutex> lock(_mutex); | |
61 | BOOST_FOREACH(Envelope &e, _envelope_levels) | |
62 | free(e.samples); | |
63 | } | |
64 | ||
1b1ec774 | 65 | void AnalogSnapshot::append_payload( |
aba1dd16 JH |
66 | const sr_datafeed_analog &analog) |
67 | { | |
68 | lock_guard<recursive_mutex> lock(_mutex); | |
aba1dd16 | 69 | append_data(analog.data, analog.num_samples); |
fda5b6e0 JH |
70 | |
71 | // Generate the first mip-map from the data | |
72 | append_payload_to_envelope_levels(); | |
aba1dd16 JH |
73 | } |
74 | ||
d3758367 JH |
75 | const float* AnalogSnapshot::get_samples( |
76 | int64_t start_sample, int64_t end_sample) const | |
a8acb46e | 77 | { |
d3758367 JH |
78 | assert(start_sample >= 0); |
79 | assert(start_sample < (int64_t)_sample_count); | |
80 | assert(end_sample >= 0); | |
81 | assert(end_sample < (int64_t)_sample_count); | |
82 | assert(start_sample <= end_sample); | |
83 | ||
84 | lock_guard<recursive_mutex> lock(_mutex); | |
85 | ||
86 | float *const data = new float[end_sample - start_sample]; | |
87 | memcpy(data, (float*)_data + start_sample, sizeof(float) * | |
88 | (end_sample - start_sample)); | |
89 | return data; | |
a8acb46e JH |
90 | } |
91 | ||
9320072d JH |
92 | void AnalogSnapshot::get_envelope_section(EnvelopeSection &s, |
93 | uint64_t start, uint64_t end, float min_length) const | |
94 | { | |
95 | assert(end <= get_sample_count()); | |
96 | assert(start <= end); | |
97 | assert(min_length > 0); | |
98 | ||
99 | lock_guard<recursive_mutex> lock(_mutex); | |
100 | ||
101 | const unsigned int min_level = max((int)floorf(logf(min_length) / | |
102 | LogEnvelopeScaleFactor) - 1, 0); | |
103 | const unsigned int scale_power = (min_level + 1) * | |
104 | EnvelopeScalePower; | |
105 | start >>= scale_power; | |
106 | end >>= scale_power; | |
107 | ||
108 | s.start = start << scale_power; | |
109 | s.scale = 1 << scale_power; | |
110 | s.length = end - start; | |
111 | s.samples = new EnvelopeSample[s.length]; | |
112 | memcpy(s.samples, _envelope_levels[min_level].samples + start, | |
113 | s.length * sizeof(EnvelopeSample)); | |
114 | } | |
115 | ||
fda5b6e0 JH |
116 | void AnalogSnapshot::reallocate_envelope(Envelope &e) |
117 | { | |
118 | const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) / | |
119 | EnvelopeDataUnit) * EnvelopeDataUnit; | |
120 | if (new_data_length > e.data_length) | |
121 | { | |
122 | e.data_length = new_data_length; | |
123 | e.samples = (EnvelopeSample*)realloc(e.samples, | |
124 | new_data_length * sizeof(EnvelopeSample)); | |
125 | } | |
126 | } | |
127 | ||
128 | void AnalogSnapshot::append_payload_to_envelope_levels() | |
129 | { | |
130 | Envelope &e0 = _envelope_levels[0]; | |
131 | uint64_t prev_length; | |
132 | EnvelopeSample *dest_ptr; | |
133 | ||
134 | // Expand the data buffer to fit the new samples | |
135 | prev_length = e0.length; | |
136 | e0.length = _sample_count / EnvelopeScaleFactor; | |
137 | ||
138 | // Break off if there are no new samples to compute | |
139 | if (e0.length == prev_length) | |
140 | return; | |
141 | ||
142 | reallocate_envelope(e0); | |
143 | ||
144 | dest_ptr = e0.samples + prev_length; | |
145 | ||
146 | // Iterate through the samples to populate the first level mipmap | |
147 | const float *const end_src_ptr = (float*)_data + | |
148 | e0.length * EnvelopeScaleFactor; | |
149 | for (const float *src_ptr = (float*)_data + | |
150 | prev_length * EnvelopeScaleFactor; | |
151 | src_ptr < end_src_ptr; src_ptr += EnvelopeScaleFactor) | |
152 | { | |
153 | const EnvelopeSample sub_sample = { | |
154 | *min_element(src_ptr, src_ptr + EnvelopeScaleFactor), | |
155 | *max_element(src_ptr, src_ptr + EnvelopeScaleFactor), | |
156 | }; | |
157 | ||
158 | *dest_ptr++ = sub_sample; | |
159 | } | |
160 | ||
161 | // Compute higher level mipmaps | |
162 | for (unsigned int level = 1; level < ScaleStepCount; level++) | |
163 | { | |
164 | Envelope &e = _envelope_levels[level]; | |
165 | const Envelope &el = _envelope_levels[level-1]; | |
166 | ||
167 | // Expand the data buffer to fit the new samples | |
168 | prev_length = e.length; | |
169 | e.length = el.length / EnvelopeScaleFactor; | |
170 | ||
171 | // Break off if there are no more samples to computed | |
172 | if (e.length == prev_length) | |
173 | break; | |
174 | ||
175 | reallocate_envelope(e); | |
176 | ||
177 | // Subsample the level lower level | |
178 | const EnvelopeSample *src_ptr = | |
179 | el.samples + prev_length * EnvelopeScaleFactor; | |
180 | const EnvelopeSample *const end_dest_ptr = e.samples + e.length; | |
181 | for (dest_ptr = e.samples + prev_length; | |
182 | dest_ptr < end_dest_ptr; dest_ptr++) | |
183 | { | |
184 | const EnvelopeSample *const end_src_ptr = | |
185 | src_ptr + EnvelopeScaleFactor; | |
186 | ||
187 | EnvelopeSample sub_sample = *src_ptr++; | |
188 | while (src_ptr < end_src_ptr) | |
189 | { | |
190 | sub_sample.min = min(sub_sample.min, src_ptr->min); | |
191 | sub_sample.max = max(sub_sample.max, src_ptr->max); | |
192 | src_ptr++; | |
193 | } | |
194 | ||
195 | *dest_ptr = sub_sample; | |
196 | } | |
197 | } | |
198 | } | |
199 | ||
1b1ec774 | 200 | } // namespace data |
aba1dd16 | 201 | } // namespace pv |