我有一个包含数据块的二维数组,我创建了一个函数来计算每个值过零的次数。
我正在使用 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;
}