Reply by October 1, 20132013-10-01
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.
Reply by glen herrmannsfeldt September 30, 20132013-09-30
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
Reply by Vladimir Vassilevsky September 30, 20132013-09-30
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
Reply by Dave September 30, 20132013-09-30
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
Reply by robert bristow-johnson September 30, 20132013-09-30
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."
Reply by Greg Berchin September 30, 20132013-09-30
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
Reply by Dave September 30, 20132013-09-30
On Sunday, September 29, 2013 12:58:24 PM UTC-4, Greg Berchin wrote:
> On Sun, 29 Sep 2013 10:25:07 -0500, Vladimir Vassilevsky > > <nospam@nowhere.com> wrote: > > > > >On 9/28/2013 9:49 PM, robert bristow-johnson wrote: > > > > > >> i would still be interested in hearing from Vlad what he does going > > >> sorta "MATLAB commando". no tools other than the ones you make > > >> yourself, Vlad? > > > > <...> > > > > >Sorry for being brief, I am currently busy fixing client project. At the > > >beginning, they did common mistake of playing with graphic IDE instead > > >of developing code and algorithms. Now it is finally falling apart, and > > >the timeline is approaching. > > > > > >Graphic tools are sale tools. > > > > My most recent contract stipulated that proof-of-concept was to be > > written in MATLAB, but that the deliverable software was to be > > written in C. I took a lot of heat for "writing a MATLAB script > > that looked like a C program", but after the flashy > > proof-of-concept demonstration, the translation to C was much > > simplified. > > > > The most difficult problem was the off-by-1 indices. > > > > Greg
I was the project scientist on a large post-processor for sonar data. I used Matlab to code all the algorithms. The matlab code was used for: unit testing, included in the software design documentation, and was given to the coders since many of them didn't know much about DSP. 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. As an example I couldn't filter data with the matlab command "firfilt" - because the coders in their libraries don't have that function. 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 Cheers, Dave
Reply by Sebastian Doht September 30, 20132013-09-30
Am 27.09.2013 01:01, schrieb robert bristow-johnson:
> > 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? > > use http://www.mathworks.com/products/matlab-coder/ ? > > 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. >
I know of companies who are generating C code from Simulink models. The prototypes I came across where usually rewritten to the language of choice. If your prototype is a little bit larger and the combination of MATLAB and C/C++ is used, the mex-DLL-interface functionality of MATLAB can help you to do a rewrite step by step and still use your MATLAB environment for testing. Though I would not advise MATLAB for bigger projects because it is hard to keep the code in a maintainable structure.
Reply by mnentwig September 30, 20132013-09-30
>Can you mention some tools you have written as alternative to some
obvious
>public domain tools which you have categorized as non-proven?
I hate to disappoint, but I'm not doing any "categorizing" for you. Please re-read my post. Then decide for yourself, you're the expert on your own work. BTW, don't want to put "proven" stamps anywhere, but gsl has always worked for me, and I've gotten myself into hot waters by using FFTW in code that suddenly had to be licensed to 3rd parties. _____________________________ Posted through www.DSPRelated.com
Reply by bangebuks September 29, 20132013-09-29
> >I've seen too many proprietary software tools that give a very polished >_illusion_ of making problems disappear, when in reality they are just >hidden or postponed.
It sounds like you should be able to mention a lot of software tools then.... I would take a late-adopter attitude, stay clear of
>non-proven tools or methodologies. Spinning your own tools isn't good for >pretty powerpoint slides but usually gets the job done. And you have the >competence (=consultants) in-house. >
Can you mention some tools you have written as alternative to some obvious public domain tools which you have categorized as non-proven? _____________________________ Posted through www.DSPRelated.com