Phorce
Phorce

Reputation: 2674

Converting MatLab code - Confused

Basically, I have this final piece of code to convert from MatLab to C++.

The function takes in a 2D vector and then checks the elements of the 2D vector against 2 criteria and if not matched, it removes the blocks. But I'm confused to what the code in MatLab wants to be returned, a 2D or a 1D vector? Here is the code:

function f = strip(blocks, sumthresh, zerocrossthresh)

% This function removes leading and trailing blocks that do 
% not contain sufficient energy or frequency to warrent consideration.
% Total energy is measured by summing the entire vector.
% Frequency is measured by counting the number of times 0 is crossed.
% The parameters sumthresh and zerocrossthrech are the thresholds,
% averaged across each sample, above which consideration is warrented.

% A good sumthresh would be 0.035
% A good zerocrossthresh would be 0.060

len = length(blocks);
n = sum(size(blocks)) - len;
min = n+1;
max = 0;
sumthreshtotal = len * sumthresh;
zerocrossthreshtotal = len * zerocrossthresh;
for i = 1:n
currsum = sum(abs(blocks(i,1:len)));
currzerocross = zerocross(blocks(i,1:len));
if or((currsum > sumthreshtotal),(currzerocross > zerocrossthreshtotal))
if i < min
  min = i;
end
if i > max;
  max = i;
  end
end
end

% Uncomment these lines to see the min and max selected
% max
% min

 if max > min
   f = blocks(min:max,1:len);
 else
 f = zeros(0,0);
end

Alternatively, instead of returning another vector (whether it be 1D or 2D) might it be better to actually send the memory location of the vector and remove the blocks from it? So for example..

for(unsigned i=0; (i < theBlocks.size()); i++)
{
  for(unsigned j=0; (j < theBlocks[i].size()); j++)
  {
      // handle theBlocks[i][kj] .... 
  }
}

Also, I do not understand this line:

currsum = sum(abs(blocks(i,1:len)));

Basically the: (i,1:len)

Any ideas? Thanks :)

Upvotes: 0

Views: 284

Answers (1)

user1401864
user1401864

Reputation:

blocks(i,1:len) is telling the array that it wants to go from blocks[i][1 to the end]. So if it was a 3x3 array it's doing something like:

blocks[i][1]
blocks[i][2]
blocks[i][3]
.
.
.
blocks[i][end]

Then it's taking the absolute value of the contents of the matrix and adding them together. It's returning a [x][x] matrix but the length is either going to be a 0x0 or of (max)X(len).

Upvotes: 2

Related Questions