# Connexions

You are here: Home » Content » Digital Signal Processing Laboratory (ECE 420) » Optimization Theory

### Lenses

What is a lens?

#### Definition of a lens

##### Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

##### What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

##### Who can create a lens?

Any individual member, a community, or a respected organization.

##### What are tags?

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

#### Affiliated with (What does "Affiliated with" mean?)

This content is either by members of the organizations listed or about topics related to the organizations listed. Click each link to see a list of all content affiliated with the organization.
• TI DSP

This collection is included inLens: Texas Instruments DSP Lens
By: Texas Instruments

"Doug course at UIUC using the TI C54x DSP has been adopted by many EE, CE and CS depts Worldwide "

Click the "TI DSP" link to see all content affiliated with them.

Click the tag icon to display tags associated with this content.

#### Also in these lenses

• Lens for Engineering

This module and collection are included inLens: Lens for Engineering
By: Sidney Burrus

Click the "Lens for Engineering" link to see all content selected in this lens.

• Real-Time DSP with MATLAB

This module is included inLens: DSP with MATLAB lens
By: Bhaskar BhattacharyaAs a part of collection: "Digital Signal Processing Laboratory (ECE 420 55x)"

"Real-Time DSP with MATLAB"

Click the "Real-Time DSP with MATLAB" link to see all content selected in this lens.

Click the tag icon to display tags associated with this content.

### Recently Viewed

This feature requires Javascript to be enabled.

### Tags

(What is a tag?)

These tags come from the endorsement, affiliation, and other lenses that include this content.

Inside Collection (Course):

# Optimization Theory

Module by: Matt Kleffner, Douglas L. Jones. E-mail the authors

Summary: An overview of DSP optimization techniques, from the design stage, through high-level implementations, to assembly implementations.

## Introduction to Code Optimization

Most practical DSP applications have a clock-cycle and/or memory budget. Initial implementations typically don't meet these budgets; therefore, the code must be optimized. Code development usually follows six steps:

1. Develop algorithm on paper
2. Simulate in MATLAB
3. Develop and simulate more efficient implementations
4. Implement algorithm in C
5. Use library routines when available
6. Use optimizing compiler
7. Manually write assembly routines for key routines

## Develop algorithm on paper

The algorithm to be implemented should first be designed on paper. In addition to equations describing the algorithm, its design should include inputs and outputs of each routine and a flow chart of the operation of the complete program. Never design an algorithm or a program at a computer. While it may be possible to implement some basic algorithms and programs this way, compilers cannot overcome any design flaws that are likely to be introduced.

## Simulate in Matlab

Before any C code is written, the algorithm should be developed and simulated in MATLAB since problems with the algorithm design can be found more easily. This is done by applying the algorithm to test vectors and inspecting and/or plotting the results. Once the algorithm is completely defined, C implementation can begin. Recall that in each of the previous labs a MATLAB simulation step was given before assembly implementation.

## Develop and simulate more efficient implementations

An efficient algorithm used as few multiplications and additions as possible. Applying DSP theory to "simplify" the algorithm is a common way of doing this. The autocorrelation function is a simple example: a simple-minded way of implementing this is to compute sums for each lag, but careful inspection of the autocorrelation function reveals that it is actually a function of the absolute value of the lag. Therefore each value at a negative lag is identical to the value at the corresponding positive lag. Approximately half the apparent multiplications and additions are therefore required. If the entire autocorrelation sequence is needed, the autocorrelation can be optimized even further if it is treated as a convolution of two sequences. An FFT and an inverse FFT can then be used to compute the autocorrelation.

## C Implementation

After the algorithm has been designed and optimized, an initial implentation in C is done. Tips on writing code with efficiency in mind can be found here [link]. It is important, however, to get a working implementation of the algorithm in a reasonable amount of time as some optimizations cannot be anticipated by the programmer. Some of these coding techniques in the URL reference can be applied later on when it is clear which routines need the most optimization. This implementation can serve as a reference implementation to compare the correctness and speed of optimized versions.

## Library routines

If the algorithm uses common mathematical operations, such as the cosine and FFT operations, it is usually wise to use existing library routines instead of "reinventing the wheel." As many library routines are readily available from DSP manufacturers and over the internet, the first factor to consider in using a library is its license: do you have permission to use it in your application? Libraries can often be used freely for educational and research purposes, but any other use requires inspection of the library license.

The second factor to consider is the design goal of the library: was it designed for speed or low memory usage? Typically speed can be bought with more memory and vice-versa, so when selecting a library it is important to decide which budget (speed or memory) is more important with respect to the routine.

## Compiler Optimization

Recall that the basic operation of a C compiler is to translate C source code into assembly instructions and then into an executable.

"Compiler optimization is used to improve the efficiency (in terms of running time or resource usage) of the executables output by a compiler. These techniques allow programmers to write source code in a straightforward manner, expressing their intentions clearly, while allowing the computer to make choices about implementation details that lead to efficient execution. Contrary to what the term might imply, this rarely results in executables that are perfectly "optimal" by any measure, only executables that are much improved compared to direct translation of the programmer's original source." [1]

An optimizing compiler traditionally groups optimizations into phases. Each phase contains a series of optimizations (or transformations) that are performed in a fixed order. These phases are usually turned on with command-line flags such as -O1, -O2, etc. Each flag indicates an optimization "level" where the level includes all of the lower levels. At higher optimization levels bugs in the code are sometimes introduced, so it is important to check the behavior of a compiler-optimized program against the reference implementation. Keep the highest optimization level that produces accurate code.

At this point the compiled code should be checked against the budgetary constraints. Is it fast enough? Does it fit in available memory? Total memory usage is placed in a file produced by the compiler (sometimes a command-line flag is needed for this). Speed can be measured in a couple of ways. The most common method is the use of a profiler. A profiler tracks the performance of the program, providing data on how many times each function is called, as well as how much time each function takes in terms of cycles and percentages of total program cycles. A simulator also allows clock cycles to be measured, typically by allowing the user to place breakpoints around sections of code to be measured. If the speed and memory properties of the compiled code fit the budget, optimization is finished. If not, some of the routines must be hand-written in assembly.

## Write key assembly routines manually

Finally, if the budget cannot be met with the other optimization techniques some routines must be written in assembly. Manually-written assembly code is usually the most efficient, but it is labor-intensive and it is not portable to other architectures. Therefore it is done as a last resort and only on routines that absolutely require it for budget constraints to be met. This is done by rewriting the routine that consumes the largest portion of the budget, followed by the next largest budget-consuming routine and so on until the budget is met. It should be noted that this step is almost always required in embedded applications as current state-of-the-art C compilers and optimizers do not produce sufficiently fast and/or small code.

If meeting the budget is unexpectedly difficult, remember that no compiler optimization or assembler can effectively overcome a poor algorithm design or implementation. If you are confident that your implentation is fast and accurate, then the budget may be too tight for the application. Either some parts of the application must be removed (extra "features", for example) or an architecture with more resources must be used.

## References

1. Various authors. (2002-Present). Compiler optimization. [http://en.wikipedia.org/wiki/Compiler_optimization]. Wikipedia.

## Content actions

PDF | EPUB (?)

### What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

PDF | EPUB (?)

### What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

#### Collection to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

#### Definition of a lens

##### Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

##### What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

##### Who can create a lens?

Any individual member, a community, or a respected organization.

##### What are tags?

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks

#### Module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

#### Definition of a lens

##### Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

##### What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

##### Who can create a lens?

Any individual member, a community, or a respected organization.

##### What are tags?

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks