DSPRelated.com
Forums

prototyping and migrating code to production: MATLAB, C, C++, asm

Started by robert bristow-johnson September 26, 2013
On Mon, 30 Sep 2013 05:31:34 -0700 (PDT), Dave <dspguy2@netscape.net> wrote:

>The matlab code mimicked the overall software design, so it wasn't how most >people would implement the algorithm in matlab - but it duplicated all the glue >that as used around any particular algorithm.
Precisely. MATLAB does a lot of under-the-hood stuff that had to be done explicitly in C. A *trivial* example is: MATLAB: vector1 = [ a b c ... z ]; vector2 = [ A B C ... Z ]; vector3 = vector1 .* vector2; C: double vector1[26]; double vector2[26]; double vector3[26]; // assume that vector1 and vector2 are somehow filled as above for (int i = 0; i < 26; ++i) { // don't bug me about my {} convention vector3[i] = vector1[i] * vector2[i]; } MATLAB written like C: vector1 = [ a b c ... z ]; vector2 = [ A B C ... Z ]; for i = 1:26 vector3(i) = vector1(i) * vector2(i); end It's not as tidy as the pure MATLAB code, but it gives insight into the under-the-hood stuff that you're going to have to understand anyway when you write the C code. So you might as well debug the structure (as well as the numerics) in the interactive MATLAB environment, if you can.
>I anticipated the off by 1 problem, so in the matlab code I wrote the loop in >the typical matlab way, but created an auxiliary variable which mimics how you'd >do it in C.
I have done this, on occasion, as well -- usually when the index has physical meaning. FFT bin indices is an example. Greg
On 9/30/13 5:31 AM, Dave wrote:
> > I anticipated the off by 1 problem, so in the matlab code I wrote the loop in the typical matlab way, but created an auxiliary variable which mimics how you'd do it in C. As an example: > for ind=1:length(Vec) %This is self documented matlab > c_ind = ind-1; %C version of zero based index > blah , blah blah > > end
wouldn't it be the other way around? for ind = 0:(length(Vec)-1) ... something = Vec(ind+1); ... end if you're writing MATLAB code that will correspond most to the C, wouldn't it be like that? -- r b-j rbj@audioimagination.com "Imagination is more important than knowledge."
On Monday, September 30, 2013 10:38:44 AM UTC-4, robert bristow-johnson wrote:
> On 9/30/13 5:31 AM, Dave wrote: > > > > for ind=1:length(Vec) %This is self documented matlab > > > c_ind = ind-1; %C version of zero based index > > > blah , blah blah > > > > end > > wouldn't it be the other way around? > > for ind = 0:(length(Vec)-1) > ... > something = Vec(ind+1); > ... > end > > > if you're writing MATLAB code that will correspond most to the C, > wouldn't it be like that?
It can depend - the length() operator is clear in what exactly you are doing, so to me it makes it clear to the person reading the code. It can depend on the operations supported by the underlying vector objects. We were using VSIPL to make our code more portable. For us VSIPL was a necessary evil - it seems like a nice idea but the interface is poor. In your example you are going to have (ind+1) everywhere unless you create an auxiliary variable. You want to indicate to the code which variable is used for matlab functions and which one is used for C. In you case the loop variable should have some indicator that it is the C version. The important part was that you pick a coding standard and stick to it. BTW - I apologize for any formatting issues. I'm using Google groups - which is really a POS. Cheers, Dave
On 9/30/2013 9:38 AM, robert bristow-johnson wrote:
> On 9/30/13 5:31 AM, Dave wrote: >> >> I anticipated the off by 1 problem, so in the matlab code I wrote the >> loop in the typical matlab way, but created an auxiliary variable >> which mimics how you'd do it in C. As an example: >> for ind=1:length(Vec) %This is self documented matlab >> c_ind = ind-1; %C version of zero based index >> blah , blah blah >> >> end > > wouldn't it be the other way around? > > for ind = 0:(length(Vec)-1) > ... > something = Vec(ind+1); > ... > end > > > if you're writing MATLAB code that will correspond most to the C, > wouldn't it be like that?
template <class T> T& operator[] (const int) VLV
Greg Berchin <gjberchin@chatter.net.invalid> wrote:
> On Mon, 30 Sep 2013 05:31:34 -0700 (PDT), Dave <dspguy2@netscape.net> wrote:
>>The matlab code mimicked the overall software design, so it >>wasn't how most people would implement the algorithm in >> matlab - but it duplicated all the glue that as used around >>any particular algorithm.
> Precisely. MATLAB does a lot of under-the-hood stuff that had to > be done explicitly in C.
Especially related to memory allocation. Matlab depends on having enough memory available that you don't have to worry about what is allocated and what isn't, at least most of the time. I did once do an image processing probem in Matlab, read in a JPG file, and do some processing on it. Each step allocates a new, very large, matrix. Assigning {} to a matrix you aren't using anymore can help keep from running out, or just slowing down to much as it starts pagin. -- glen
On Thu, 26 Sep 2013 16:01:59 -0700, robert bristow-johnson wrote:

> okay guys, what do you do, if you start with an alg that works pretty > good in MATLAB/Octave/MathCad/Mathematica/whatever, and you wanna make > production code out of it. let's pretend the production code will be C. > what do you do? > > rewrite it yourself, line by line?
Sort of. It's more than rewriting the code line by line. After a first functional reference for an algorithm is established, including tests, we ussually re-factor and restructure that reference to fit into the target eco system, determining already the structure of the final implementation. This reference is then the basis (and documentation) for the implementation which is hand coding, supported by code template and code generation mechanisms. This is also the time to think about quantization / fix point etc. Reference is ussually Simulink and the target ultimately some sort of HDL (involing one more step from a C++ reference) but the procedure would be similar from Matlab to code for an embedded processor. Experience tells that actual coding / code transformation takes at most 10% of the time. Crafting the architecture and testing takes the biggest share.
> use http://www.mathworks.com/products/matlab-coder/ ?
We use it but not for production code but rather for tooling projects (from Matlab) and rapid simulation (from Simulink). If your target platform is well supported and space / efficiency are less important than quick cycle times it could be worth a look. Attention, the price tag is hefty!
> something else? (i guess i would also be interested in hearing from Vlad > about what he does instead of what the silly Matlabies do.) > > thanks for opinions.