过零返回高值

时间:2012-11-14 16:09:20

标签: c++ matlab signal-processing

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

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

这是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;
}

1 个答案:

答案 0 :(得分:0)

在Matlab中大量使用for循环传统上通常是个坏主意,因为Matlab的JIT编译器不是很快(或者不是很快)(至少没有比较)到本机C代码)。我猜你写了C ++代码来加速计算,但也许另一种方法是用matlab编写代码&#39;:

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