diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index d423749..20af7b7 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -28,4 +28,4 @@ add_example(multinary_ex OFF) add_example(text_io_ex OFF) add_example(getoption_ex OFF) add_example(process_ex OFF) -add_example(array_ex ON) \ No newline at end of file +add_example(array_ex OFF) \ No newline at end of file diff --git a/lib/algorithm/space_filter.cpp b/lib/algorithm/space_filter.cpp index d728940..fb6610d 100644 --- a/lib/algorithm/space_filter.cpp +++ b/lib/algorithm/space_filter.cpp @@ -260,14 +260,24 @@ void gctl::gauss3d_filter(const array &in_data, array &out_data, void gctl::moving_window_1d(int data_len, int win_len, int cur_data_idx, array &win_data_idx) { + if (win_len < 3 || win_len/2 == 0) + { + throw std::runtime_error("[gctl::moving_average] Invalid window size (must be >= 3 and odd)."); + } + + if (data_len <= win_len) + { + throw std::runtime_error("[gctl::moving_average] The window size must be smaller than the data size)."); + } + win_data_idx.resize(win_len); int ht = win_len/2; - for (size_t j = 0; j < win_len; j++) + for (int j = 0; j < win_len; j++) { win_data_idx[j] = cur_data_idx + j - ht; if (cur_data_idx <= ht) win_data_idx[j] = j; - if (data_len - cur_data_idx <= ht) win_data_idx[j] = data_len - win_len + j; + if (data_len - cur_data_idx <= ht) win_data_idx[j] = data_len - win_len + j; } return; } @@ -276,29 +286,35 @@ void gctl::moving_average_1d(const array &in_data, array &out_da { if (win_size < 3 || win_size/2 == 0) { - throw std::runtime_error("Invalid window size (must be >= 3 and odd). From gctl::moving_average(...)"); + throw std::runtime_error("[gctl::moving_average] Invalid window size (must be >= 3 and odd)."); + } + + int d_size = in_data.size(); + if (d_size <= win_size) + { + throw std::runtime_error("[gctl::moving_average] The window size must be smaller than the data size)."); } // Copy the input data to output ones out_data = in_data; + int id; double d_sum, c_sum; int half_k = win_size/2; - int id, d_size = in_data.size(); for (int i = 0; i < d_size; i++) { - d_sum = c_sum = 0.0; - for (int p = 0; p < win_size; p++) - { - id = i + p - half_k; - if (id >= 0 && id < d_size) - { - d_sum += in_data[id]; - c_sum += 1.0; - } - } + d_sum = c_sum = 0.0; + for (int p = 0; p < win_size; p++) + { + id = i + p - half_k; + if (id >= 0 && id < d_size) + { + d_sum += in_data[id]; + c_sum += 1.0; + } + } - out_data[i] = d_sum/c_sum; + out_data[i] = d_sum/c_sum; } return; } diff --git a/lib/algorithm/space_filter.h b/lib/algorithm/space_filter.h index 992fb66..6d5b0f1 100644 --- a/lib/algorithm/space_filter.h +++ b/lib/algorithm/space_filter.h @@ -110,7 +110,7 @@ namespace gctl double sigma = 0.5, int k_size = 3); /** - * @brief 一维数组各个计算点对应的滑动窗口内的数据索引 + * @brief 计算一个一维数组中各个索引位置对应的滑动窗口内的数据索引列表 * * @param data_len 一维数据总个数 * @param win_len 窗口大小 @@ -120,11 +120,11 @@ namespace gctl void moving_window_1d(int data_len, int win_len, int cur_data_idx, array &win_data_idx); /** - * @brief 规则网格滑动平均 + * @brief 一位数组滑动平均 * - * @param in_data 输入数据 - * @param out_data 输出数据 - * @param k_size 窗口的大小 + * @param in_data 输入数据,数组个数大于等于窗口大小 + * @param out_data 输出数据,数组大小等于输入数据 + * @param k_size 窗口的大小,大于等于3且为奇数 */ void moving_average_1d(const array &in_data, array &out_data, int win_size = 3);