c++ - 过零返回高值

标签 c++ matlab signal-processing

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

我正在使用 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;
}

最佳答案

传统上,在 Matlab 中大量使用 for 循环通常不是一个好主意,因为 Matlab 的 JIT 编译器不是(或不是)非常快(至少与 native 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

关于c++ - 过零返回高值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13382564/

相关文章:

matlab - 不同采样频率的不同长度的声音文件

C++ 弹跳球跳出屏幕

c++ - 微软视觉 C++ 2010

matlab - 如何在 MATLAB 中查找集合的中心点

python - Spyder 绘图 block 控制台命令

matlab - 从相位和幅度获取傅里叶变换 - Matlab

c++ - 编译器错误: not declared in the scope

c++ - Eigen 和 glm 产品产生不同的结果

matlab - 不仅仅根据 z 定义曲面

ios - 音量快速变化会导致 AVAudioPlayer 出现伪影