\chapter{Impulse Response Classes}
\label{chap.affine}
\section{\optvec\: Class}
The \optvec\: class is the most fundamental in the toolbox, as it
provides the basic representation of a quantity to be optimized. An
\optvec\: is an extension of the basic {\sc Matlab} vector, where each
element of the vector is an affine (linear plus a constant) combination
of the underlying optimization variables.

As per {\sc Matlab} convention, a length-$N$ \optvec\: {\tt a} has first
element {\tt a(1)} and last element {\tt a(N)}. A new \optvec\: is
created by calling
\begin{code}
\optvar{a} & \optfn{optVector}(\ml{N}, \optvar{ov})
\end{code}
which allocates {\tt N} new optimization variables from the pool {\tt
ov} and then assigns one variable to each element of {\tt a}. When
called with a single constant vector argument, \optfn{optVector}
returns a constant \optvec. This is rarely needed, since operators and
functions treat a constant \optvec\: the same as a standard {\sc Matlab}
vector. Most of the usual {\sc Matlab} operators are overloaded to
accept and return variables of class \optvec, as listed in Table
\ref{ovfcns}. In addition to the usual {\sc Matlab} requirements on size
compatibility, the multiply and divide operators have the restriction
that one of the two arguments must be constant, so that the result is
still affine in the optimization variables.

\section{\optseq\: Class}

The \optseq\: class builds on the \optvec\: class, adding a time index
$n$. This is used to represent FIR filters, as well as constant input
signals and non-optimized filters. Additional operators defined for this
class are shown in Table \ref{osfcns}. In addition, the function
\optfn{fourier}(\optvar{h}, \ml{f}) is defined, returning an \ogs\: in
which the elements hold the frequence response of {\tt h} at the
normalized frequencies {\tt f}.


\subsection{Examples}



\opt\ supports a finite-length-sequence data type incorporating both
values and times of samples.  \opt\ code
\begin{code}
\optvar{w} & \optfn{optSequence( \ml{vect} )} ;\\
%\plot{w}{a}
\end{code}
associates times $0,\ldots,\mathsf{length(vect)}\sm{-}1$ with the elements of \matlab\
vector \ml{vect}, effectively making \optvar{w} a fixed,
nonoptimizable impulse response.  
Alternate form
\begin{code}
\optvar{u} & \optfn{optSequence( \ml{N}, \optvar{X} )} ;\\
%\plot{u}{b}
\end{code}
adds $N$ new variables to the set \optvar{X} of real optimization
variables and creates a sequence in which samples $0, \ldots, N\sm{-}1$
are associated with the new variables.

  Here \opt\ variable \optvar{u} is the {\em optimizable}
impulse response of a real nonlinear-phase filter.  Such sequences also
represent uniformly-spaced
trains in continuous time; other
\optfn{optSequence} forms place impulses nonuniformly and in multiple
dimensions.

Overloaded \matlab\ operators allow construction of structured impulse
reponses. 
The prime operator ($^\prime$) yields the time-reversed conjugate
(match) and is used to impose a linear-phase response:
\begin{code}
\optvar{u} & \optfn{optSequence( \ml{N}, \optvar{X} )} ;\\
\optvar{v} & \optvar{u} $+$ \optvar{u} \ml{$^\prime$} ;\\
%\plot{v}{c}
\end{code}
Here \optvar{v} is a real impulse response with linear phase and support
on $-(N\sm{-}1), \ldots, N\sm{-}1$. The creation instead of a complex
linear-phase impulse response is straightforward using the delay
operator ($\:|\:$) and simple arithmetic operations:
\begin{code}
\optvar{a} & \optfn{optSequence( \ml{N}, \optvar{X} )} ;\\
\optvar{b} & \optfn{optSequence( \ml{N}, \optvar{X} )} ;\\
\optvar{r} & (\optvar{a} + \ml{j} * \optvar{b})$\:|$ \ml{1} ;\\
\optvar{p} & \optvar{r} + \optfn{optSequence}(\ml{1}, \optvar{X}) +
             \optvar{r} \ml{$^\prime$} ; \\
%\plot{v}{c}
\end{code}
Here \optvar{p} depends on $2N+1$ distinct optimization variables. The
center sample is real by construction, as required.

\opt's time-axis scale-up operator (\ml{./}) used here makes a complex
linear-phase impulse response be halfband:
\begin{code}
\optvar{u} & \optfn{optSequence( \ml{N}, \optvar{X} )} ;\\
\optvar{v} & \optfn{optSequence( \ml{N}, \optvar{X} )} ;\\
\optvar{c} & \optfn{optSequence( \ml{N}, \ml{1} )} ;\\
\optvar{r} & ((\optvar{u} + \ml{j} * \optvar{v}) ./ 2 ) $\:|$ \ml{1} ;\\
\optvar{h} & \optvar{r} \ml{$^\prime$} + \optvar{c} + \optvar{r}; \\
%\plot{v}{c}
\end{code}
(Operator $.\backslash$ would perform decimation.)

A convolution operator allows certain cascades to be defined. If two
filters have impulse responses \optvar{p} and \optvar{w}, then
\begin{code}
\optvar{q} & \optvar{p} .* \optvar{w} ; \\
\end{code}
makes \optvar{q} the impulse response of their cascade. There is an
important restriction, however. Every sample in an \opt\ sequence must
depend affinely (linearly plus a constant) on optimization variables. To
avoid quadratic dependencies, which are not allowed, convolution and
multiplication must have at least one ``fixed'' or nonoptimizable
argument, like the fixed sequence \optvar{w} defined earlier. These
optimizable-fixed cascades are used when designing a filter to meet
specifications on a cascade of which it is a member

\section{\optarr\: Class}
The \optarr\: class similarly is an extension of
the \optvec\: class, with the addition of location information, used for
the representation of multi-dimensional sequences. Location information
is stored in a matrix, each row of which contains the coordinates of an
individual location. For example, for a three-dimensional \optarr, the
locations matrix {\tt locs} is
\[ \mathtt{locs} = \left[
\begin{array}{ccc}
x_1 & y_1 & z_1 \\
\vdots & \vdots & \vdots \\
x_n & y_n & z_n
\end{array}
\right]
\] 
The locations matrix is stored in sorted form; sorted by the first
dimension first, then the second, and so on. A location can be any
vector $\mathbf{x} \in \field{R}^n$.

All of the operators defined for the one-dimension \optseq\: class are
present in the \optarr\: class, with some slight modifications, as well
as the addition of a few others. The transpose ({\tt .'}) and
conjugate-transpose ({\tt '}) operators flip a sequence about the
origin; that is, each element is moved to the inverse of its
location. The \func{flip} function inverts the sequence in a single
dimension. The \func{rot}{\tt (seq, ang, dims)} function rotates the
sequence in a 2-dimensional subspace by applying a rotation matrix to
the two columns of {\tt locs} indicated by {\tt dims}. A vector
$\mathbf{x}$ is rotated by an angle $\theta$ by premultiplying it by a
rotation matrix
\[ \mathbf{Q}_{\theta} = \left[
\begin{array}{rr}
\cos\theta & -\sin\theta \\
\sin\theta & \cos\theta
\end{array}
\right] \]
After the rotation matrix is applied the locations are resorted. For
example, 
\\ 
\func{rot}{\tt (seq, pi/4, [1 3])}
\\
will rotate {\tt seq} 45 degrees in the $xz$-plane (about the $y$-axis).

The \func{or} function, with its equivalent operator {\tt |} is used as
before as a shift operator, with the requirement that the offset operand
be a vector of the same dimension as the sequence.

Convolution in multiple dimensions  is expressed as
\begin{eqnarray*}
 y(\mathbf{n}) &=& x(\mathbf{n}) \ast h(\mathbf{n}) \\
 &=& (x \ast h)(\mathbf{n}) \\ 
 &=& \sum_{\mathbf{k}} x(\mathbf{k})h(\mathbf{n} -
\mathbf{k})
\end{eqnarray*}
where $\mathbf{n}, \mathbf{k} \in \field{R}^n$.

The Fourier transform of a multi-dimensional sequence $h(\mathbf{n})$ is
\[ H(\mathbf{f}) = \sum_{\mathbf{n}} h(\mathbf{n})
e^{-j2\pi <\mathbf{f}, \mathbf{n}> } \]

If \optvar{soln} contains a solution obtained by \optfn{minimize},
\optfn{optimal}(\optvar{h}, \optvar{soln}) returns a constant \optarr\ of
identical structure which is the optimal impulse response.
