0

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 :)

10
  • 1
    Looking at all of your last questions I get the feeling the users of SO are doing most of your work regarding your matlab to c++ conversion. Commented Aug 16, 2012 at 17:11
  • I am not asking for code, I am asking for peoples opinions. Commented Aug 16, 2012 at 17:25
  • 1
    You want to know what the return is? Last if statement makes it obvious and if you still don't get it why don't you just execute the code. You want to know what sum(abs(blocks())) is doing? why don't you just try help sum, help abs and check what's stored inside of blocks(x,y). This is not about asking other people about their opinion that's just being lazy. Commented Aug 16, 2012 at 17:37
  • 3
    That means I should apologies for saying you should run the code but for everything else there is a very useful documentation. mathworks.de/help/techdoc Commented Aug 16, 2012 at 17:41
  • 1
    Would gnu.org/software/octave help you? Commented Aug 16, 2012 at 17:58

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.