Embedded Systems Design - June 2008 - (Page 36) feature Listing 7 Look up table (C, static allocation). int lut_static(int ind) { int N=1025; int n; static int lut[N]; // static allocation Listing 8 Find and variable-size array. function ind=findrand() % % fill x with random values % between 0 and 1.0 x=rand(10,1); % % length of ind unknown a priori ind=find(x>0.5); Listing 9 C Translation of findrand with fixed-size arrays. void _findrand(void) { double x[10]; int32 x_dim1; int32 ind[10]; int32 ind_dim1; // track actual size of ind int32 n; // fill x with 10 random numbers my_rand(x, &x_dim1); // ind=find(x>0.5) ind_dim1=0; for (n=0;n 0.5) { ind[ind_dim1]=n; // store 0-based index ind_dim1++; } } print_vector("ind", ind, NULL, 9, 2, 1, ind_dim1); return; } function: given a vector x, the MATLAB statement “ind=find(x)” returns a vector ind of indices corresponding to nonzero entries of x. The exact length of ind cannot be determined until run time. Find is a commonly used function in MATLAB, but the variable length of the output makes translation to C problematic, especially when dynamic memory allocation is prohibited. The example in Listing 8 initializes x with ten random numbers between 0 and 1.0, then returns the indices of elements in x that are greater than 0.5. The length of ind isn’t known until after the random numbers are generated. To avoid dynamic memory allocation, you can recognize that the length of ind will never exceed the length of x (=10). This gives you the maximum possible size for ind, which you allocate on the stack. You can also introduce a separate variable, ind_dim1, to track the actual size of ind. The resulting C translation of findrand is shown in Listing 9. MATLAB MIGHT SET YOU FREE The MATLAB language frees you from having to think about memory allocation and actively encourages the use of variable-size arrays. On the other hand, many algorithms developed in MATLAB are eventually ported to C on an embedded system, where memory, time, and hardware constraints limit how the C implementation allocates memory. In some cases, embedded projects prohibit the use of dynamic memory allocation entirely. It’s no wonder that translating from MATLAB to embedded C is a complicated task. ■ Robert Yu is an applications engineer at Agility Design Solutions involved in the development and use of the company’s Agility MCS product for automatic MATLAB-to-C translation tool in embedded applications. Before joining Agility, he held software development and management positions at a variety of Silicon Valley companies. During this time, his work focused on signal processing and telecommunications applications. Robert holds BS and MEng in electrical engineering from Cornell. He can be reached at robert.yu@agilityds.com. several solutions for handling the variable-size array: • Rewrite the algorithm to eliminate all variable-size arrays: you can emulate variable-size arrays by pre-allocating a maximum, fixed-size array, then use extra “bookkeeping” variables and logic to track the actual array size during runtime. This method can be done in C or even MATLAB, although this goes against • the grain of normal MATLAB coding style. Use a custom memory allocator in C, such as a pool manager, to allow dynamic memory allocation without the associated problems: this solution sidesteps the problems with malloc and free but introduces extra complexity. As an example of variable-size arrays in MATLAB, consider the MATLAB find 36 JUNE 2008 | embedded systems design | www.embedded.com http://www.embedded.com
Table of Contents Feed for the Digital Edition of Embedded Systems Design - June 2008 Embedded Systems Design - June 2008 Contents #Include Party Bit Programmer's Toolbox Cover Feature: Virtual Hardware Platforms for Embedded Software Validation Allocating Memory in MATLAB-to-C Code MDD and IDEs: Making the Twain Meet in Embedded Systems Design Avoid a Thrashing Guest Editor Advertising Index Break Points Marketplace Embedded Systems Design - June 2008 Embedded Systems Design - June 2008 - Embedded Systems Design - June 2008 (Page Cover1) Embedded Systems Design - June 2008 - Embedded Systems Design - June 2008 (Page Cover2) Embedded Systems Design - June 2008 - Embedded Systems Design - June 2008 (Page 1) Embedded Systems Design - June 2008 - Embedded Systems Design - June 2008 (Page 2) Embedded Systems Design - June 2008 - Contents (Page 3) Embedded Systems Design - June 2008 - Contents (Page 4) Embedded Systems Design - June 2008 - Contents (Page 5) Embedded Systems Design - June 2008 - Contents (Page 6) Embedded Systems Design - June 2008 - #Include (Page 7) Embedded Systems Design - June 2008 - #Include (Page 8) Embedded Systems Design - June 2008 - #Include (Page 9) Embedded Systems Design - June 2008 - Party Bit (Page 10) Embedded Systems Design - June 2008 - Party Bit (Page 11) Embedded Systems Design - June 2008 - Party Bit (Page 12) Embedded Systems Design - June 2008 - Party Bit (Page 13) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 14) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 15) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 16) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 17) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 18) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 19) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 20) Embedded Systems Design - June 2008 - Programmer's Toolbox (Page 21) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 22) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 23) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 24) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 25) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 26) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 27) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 28) Embedded Systems Design - June 2008 - Cover Feature: Virtual Hardware Platforms for Embedded Software Validation (Page 29) Embedded Systems Design - June 2008 - Allocating Memory in MATLAB-to-C Code (Page 30) Embedded Systems Design - June 2008 - Allocating Memory in MATLAB-to-C Code (Page 31) Embedded Systems Design - June 2008 - Allocating Memory in MATLAB-to-C Code (Page 32) Embedded Systems Design - June 2008 - Allocating Memory in MATLAB-to-C Code (Page 33) Embedded Systems Design - June 2008 - Allocating Memory in MATLAB-to-C Code (Page 34) Embedded Systems Design - June 2008 - Allocating Memory in MATLAB-to-C Code (Page 35) Embedded Systems Design - June 2008 - Allocating Memory in MATLAB-to-C Code (Page 36) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 37) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 38) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 39) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 40) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 41) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 42) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 43) Embedded Systems Design - June 2008 - MDD and IDEs: Making the Twain Meet in Embedded Systems Design (Page 44) Embedded Systems Design - June 2008 - Avoid a Thrashing (Page 45) Embedded Systems Design - June 2008 - Avoid a Thrashing (Page 46) Embedded Systems Design - June 2008 - Avoid a Thrashing (Page 47) Embedded Systems Design - June 2008 - Guest Editor (Page 48) Embedded Systems Design - June 2008 - Guest Editor (Page 49) Embedded Systems Design - June 2008 - Guest Editor (Page 50) Embedded Systems Design - June 2008 - Guest Editor (Page 51) Embedded Systems Design - June 2008 - Advertising Index (Page 52) Embedded Systems Design - June 2008 - Break Points (Page 53) Embedded Systems Design - June 2008 - Break Points (Page 54) Embedded Systems Design - June 2008 - Marketplace (Page 55) Embedded Systems Design - June 2008 - Marketplace (Page 56) Embedded Systems Design - June 2008 - Marketplace (Page Cover3) Embedded Systems Design - June 2008 - Marketplace (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.