17 #ifndef SV_MOVING_MEDIAN_H 18 #define SV_MOVING_MEDIAN_H 20 #include <bqvec/Allocators.h> 21 #include <bqvec/VectorOps.h> 47 if (size < 1)
throw std::logic_error(
"size must be >= 1");
48 m_frame = breakfastquay::allocate_and_zero<T>(
size);
54 breakfastquay::deallocate(
m_frame);
68 std::cerr <<
"WARNING: MovingMedian: NaN encountered" << std::endl;
91 if (target ==
m_size)
return;
92 int diff = std::abs(target -
m_size);
98 breakfastquay::v_set(
m_frame, fillValue, diff);
100 for (
int sz =
m_size + 1; sz <= target; ++sz) {
104 for (
int i = 0; i < diff; ++i) {
128 m_index = int((m_size * m_percentile) / 100.f);
129 if (m_index >= m_size) m_index = m_size-1;
130 if (m_index < 0) m_index = 0;
134 put(m_sorted, m_size, value);
137 static void put(T *
const sorted,
int size, T value) {
145 T *ptr = std::lower_bound(sorted, sorted + size - 1, value);
146 breakfastquay::v_move(ptr + 1, ptr,
int(sorted + size - ptr) - 1);
151 drop(m_sorted, m_size, value);
154 static void drop(T *
const sorted,
int size, T value) {
164 T *ptr = std::lower_bound(sorted, sorted + size, value);
166 throw std::logic_error
167 (
"MovingMedian::drop: value being dropped is not in array");
169 breakfastquay::v_move(ptr, ptr + 1,
int(sorted + size - ptr) - 1);
170 sorted[size-1] = T();
175 for (
int i = 1; i <
m_size; ++i) {
176 if (m_sorted[i] < m_sorted[i-1]) {
177 std::cerr <<
"ERROR: MovingMedian::checkIntegrity: " 178 <<
"mis-ordered elements in sorted array starting " 179 <<
"at index " << i << std::endl;
184 for (
int i = 0; i <
m_size; ++i) {
186 for (
int j = 0; j <
m_size; ++j) {
187 if (m_sorted[j] == m_frame[i]) {
193 std::cerr <<
"ERROR: MovingMedian::checkIntegrity: " 194 <<
"element in frame at index " << i
195 <<
" not found in sorted array" << std::endl;
200 for (
int i = 0; i <
m_size; ++i) {
202 for (
int j = 0; j <
m_size; ++j) {
203 if (m_sorted[i] == m_frame[j]) {
209 std::cerr <<
"ERROR: MovingMedian::checkIntegrity: " 210 <<
"element in sorted array at index " << i
211 <<
" not found in source frame" << std::endl;
217 std::cerr <<
"Frame contains:" << std::endl;
219 for (
int j = 0; j <
m_size; ++j) {
220 std::cerr << m_frame[j] <<
" ";
222 std::cerr <<
"]" << std::endl;
223 std::cerr <<
"Sorted array contains:" << std::endl;
225 for (
int j = 0; j <
m_size; ++j) {
226 std::cerr << m_sorted[j] <<
" ";
228 std::cerr <<
"]" << std::endl;
229 throw std::logic_error(
"MovingMedian failed integrity check");