c++ - 优化直方图更新

标签 c++ optimization signal-processing

我正在更新一个直方图,它使用具有16个bin的简单整数数组来表示,如下所示。

const int binSize = 4096;
int histogram[16];

unsigned short inData[1024];  // This is my input data. Short is 16 bits
for(int i = 0; i < 1024; ++i)
{
   ++histogram[inData[i] / binSize];
}

我经常运行此操作,因此成为瓶颈,因为DSP无法并行处理此循环,因为无法同时更新多个bin。我该如何优化呢?

我正在TI DSP C6000系列上运行此代码。

最佳答案

举例说明注释的含义:

#include <array>
#include <algorithm>
#include <boost/range/adaptor/transformed.hpp>
using Histogram = std::array<int, 16>;

Histogram from_short(short num)
{
    Histogram result;
    result[num / 4096] = 1;
    return result;
}

Histogram add(const Histogram & lhs, const Histogram & rhs)
{
    Histogram result;
    for (size_t i = 0; i < 16; ++i) { result[i] = lhs[i] + rhs[i]; }
    return result;
}

auto singles = indata | boost::adaptors::transformed(from_short);
Histogram histogram = std::reduce(begin(singles), end(singles), Histogram{}, add);

另外一个选项:
std::sort(begin(indata), end(indata));
short * previous = begin(indata);
for (size_t i = 0; i < 15; ++i)
{
    short * current = std::lower_bound(indata, 4096 * (i + 1));
    histogram[i] = std::distance(previous, current);
    previous = current;
}
histogram[16] = std::distance(previous, end(indata));

关于c++ - 优化直方图更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43663096/

相关文章:

c++ - 链接规范无效

c++ - 错误 : Initial value of reference to non-const must be an lvalue

c++ - 在成员构造函数之后调用基类构造函数

c - 关于优化这个 C 程序的建议?

performance - 许多相同键的最有效排序算法?

java - 以 %.7f 格式创建时间戳字符串的更快方法

matlab - 高频的 FFT 错误结果?

matlab - 如何在给定 b,a 系统系数的情况下生成频率响应?

c++ - 奈奎斯特频率限制 - 如何仅混合低于奈奎斯特限制的谐波

c++ - 我想根据自己的参数使用 count_if