3

我有一个包含数据块的二维数组,我创建了一个函数来计算每个值过零的次数。

我正在使用 MatLab 并尝试转换代码,MatLab 为过零返回 287 个值,在 C++ 代码中,这些值非常高,我似乎无法弄清楚原因。

这是 Matlab 代码:

function f = zerocross(vector)

% This function simply reports the number of times
% that the input vector crosses the zero boundary

len = length(vector);
currsum = 0;
prevsign = 0;

for i = 1:len
currsign = sign(vector(i));
if (currsign * prevsign) == -1 
  currsum = currsum + 1;
end
if currsign ~= 0
  prevsign = currsign;
end
end

f = currsum;

还有我的 C++ 代码:

vector<iniMatrix> Audio::filter(vector<iniMatrix>&blocks, double sumThres, double ZeroThres)
{
 double totalSum = this->width * sumThres;
 double totalZero = this->width * ZeroThres;

int currZero = 0;
int currsum = 0;

int prevsign = 0;


for(unsigned i=0; (i < 287); i++)
{
    for(int j=0; (j < blocks.size()); j++)
    {
        currZero = sign<double>(blocks[j][i]);

        if(currZero * prevsign == -1)
        {
            currsum++;
        }

        if(currZero != 0)
        {
            prevsign = currZero;
        }
    }
    cout << currsum << endl;
}

return blocks;

签到功能:

int sign(T n)
{
  if(n < 0) return -1;
  if(n > 0) return 1;
  return n;
}

我应该(和 matlab 给出)的值是:

6, 6, 7, 9, 9, 10 .., 11, ..., 9, ...

我得到的值:

212, 337, 118, 84, ...., 348, ..., 92

有人有想法么?

编辑:

这就是我现在的循环方式:

for(int q=0; (q < 287); q++)
{
    for(unsigned i=0; (i < blocks.size()); i++)
    {
        for(unsigned j=0; (j < blocks[0].size()); j++)
        {
            currZero = sign<double>(blocks[i][j]);
            cout << currZero << endl;
        }
        cout << endl << endl << endl;

    }
    //cout << currZero << endl;
}
4

1 回答 1

0

传统上,在 Matlab 中大量使用 for 循环通常是一个坏主意,因为 Matlab 的 JIT 编译器不是(或不是)非常快(至少与本机 C 代码相比没有)。我猜您编写 C++ 代码是为了加快计算速度,但也许另一种方法是使用“matlab 方式”编写代码:

vector = rand(1e7,1)-0.1;
zero_crossings = sum(diff(array<0)~=0);

虽然我刚刚在 Matlab 2015b 上进行了测试,但在矢量化代码中的速度只有大约两倍:

function test()

    function currsum = zerocross(vector)

        % This function simply reports the number of times
        % that the input vector crosses the zero boundary

        len = length(vector);
        currsum = 0;
        prevsign = 0;

        for i = 1:len
            currsign = sign(vector(i));
            if (currsign * prevsign) == -1
                currsum = currsum + 1;
            end
            if currsign ~= 0
                prevsign = currsign;
            end
        end
    end

    function f = zerocross_vectorized(array)
        f = sum(diff(array<0)~=0);
    end

    array = rand(1e5,1e3)-0.1;

    % test for loop
    t = tic;
    crossings = nan(1,size(array,2));
    for column = 1:size(array,2)
        vector = array(:,column);
        crossings(column) = zerocross(vector);
    end
    disp(crossings(1:5))
    fprintf(1,'For loop: Calculated in %0.4f seconds\n',toc(t));

    % test vectorized
    t = tic;
    crossings = zerocross_vectorized(array);
    disp(crossings(1:5))
    fprintf(1,'Vectorized: Calculated in %0.4f seconds\n',toc(t));

end

结果是:

>> zerocross
       18208       17884       17902       17734       17988

For loop: Calculated in 1.7186 seconds
       18208       17884       17902       17734       17988

Vectorized: Calculated in 0.9695 seconds
于 2015-11-10T18:54:18.023 回答