\chapter{Design Examples}

\section{A Simple Notch Filter}
\subsection{Gridded Formulation}

An example using $L_\infty$ error measure is a simple linear-phase
FIR notch filter. To ensure linear phase, the impulse response is
constrained to be symmetrical about the origin. Therefore, a length-$N$
\optfn{optSequence} is allocated and shifted to the right, along with
a center tap at the origin.
\begin{quote}
\begin{verbatim}
rt = optSequence(N, ov) | 1;
ct = optSequence(1, ov);
\end{verbatim}
\end{quote}
The filter impulse response is then constructed by
\begin{quote}
\begin{verbatim}
h = rt' + ct + rt;
\end{verbatim}
\end{quote}
The stopband and its Fourier transform are specified by
\begin{quote}
\begin{verbatim}
df = 1/(50*N);
fsb = [0.21:df:0.29];
Hsb = real(fourier(h, fsb));
\end{verbatim}
\end{quote}
where \ml{df} is the discretization step $\Delta f$ described
above. Since $H(f)$ is real by construction, \optfn{real} is used to
eliminate computational noise in the imaginary components.
The passbands and their Fourier transforms are specified similarly by
\begin{quote}
\begin{verbatim}
fpb = [0:df:0.15 0.35:df:0.5];
Hpb = real(fourier(h, fpb));
\end{verbatim}
\end{quote}
The constraints are then assembled into a cell array, constraining the
stopband by an auxiliary optimization variable \optvar{delta} and the
passbands by a constant.
\begin{quote}
\begin{verbatim}
delta = optVar(ov);
lev = 0.1/20;
constr = {-delta<Hsb, Hsb<delta, ...
          Hpb<10^lev, 10^-lev<Hpb};
\end{verbatim}
\end{quote}
Finally \optfn{minimize} is called by
\begin{quote}
\begin{verbatim}
soln = minimize(delta, constr, ov, 'sedumi');
\end{verbatim}
\end{quote}
The impulse and frequency responses of the optimized filter, with
$N=15$, are shown in
\rfig{art.notchresp}.

\begin{figure}[h]
%\centerline{\epsfxsize=4in\epsfbox{art/circArray.eps}} 
\begin{center}
\includegraphics[width=4.0in]{art/notchresp.eps} 
\end{center}
\caption{Notch Filter Impulse and Frequency Responses.}  
\label{art.notchresp}
\end{figure}

The optimal stopband supression can be found by examining the optimal
value of \optvar{delta}:

\begin{quote}
\begin{verbatim}
db(double(optimal(delta, soln)))
\end{verbatim}
\end{quote}
which gives -31.6 dB, which is verified by a glance at \rfig{art.notchresp}.

\subsection{Random Process Formulation}

The notch filter response can also be expressed using a random process
formulation. This entails specifying a new objective and set of
constraints, while keeping the same impulse response specification as
above.

The following creates two random processes; \optvar{pp} for the passband
and \optvar{ps} for the stopband. Recall that a \optfn{Process} is
created using the call \optfn{Process}(\ml{basis}, \ml{offset},
\ml{Coeff}).
\begin{quote}
\begin{verbatim}
pp = Process('Triangle',0.5,[1 1 1 0 0 0 0 1 1 1 zeros(1,10)]);
ps = Process('Box',0.5,[0 0 0 0 1 1 0 0 0 0 zeros(1,10)]);
\end{verbatim}
\end{quote}
The coefficient vectors are of length 20, so a basis function
corresponding to entry $k$ of the coefficient vector is
centered at frequency $k/20$. The offset of 0.5 means that the entire
PSD is shifted by $0.5 \times 1/20$. The PSDs are shown in \rfig{art.notchpsd}.

\begin{figure}[h]
%\centerline{\epsfxsize=4in\epsfbox{art/circArray.eps}} 
\begin{center}
\includegraphics[width=2.5in]{art/pp.eps} \quad
\includegraphics[width=2.5in]{art/ps.eps}
\end{center}
\caption{Random Process PSDs.}  
\label{art.notchpsd}
\end{figure}

For both the stopband and passband, we wish to express a quantity that
is to be bounded.  In the case of the stopband, this is
simply the average output power. For the passband, we instead use the
power of the difference between the filtered process
\optvar{pp} .* \optvar{h} and the unfiltered process \optvar{pp}.
The powers are computed using the following:
\begin{quote}
\begin{verbatim}
Ppwr = pwr(pp.*h-pp)/pwr(pp);
Spwr = pwr(ps.*h)/pwr(ps);
\end{verbatim}
\end{quote}
These powers, being quadratic quantities, may only be bounded from
above by a constant or another quadratic,
 to ensure that the optimization problem is convex.
We bound the passband power by an auxiliary optimization variable, and
we bound the stopband power by a constant:
\begin{quote}
\begin{verbatim}
delta = optVar(ov);
constr = {Ppwr < delta.^2, Spwr < 10^(-4) };
\end{verbatim}
\end{quote}

\optfn{minimize} is then called by
\begin{quote}
\begin{verbatim}
soln = minimize(delta, constr, ov, 'sedumi');
\end{verbatim}
\end{quote}

The impulse and frequency responses of the optimized filter are shown in
\rfig{art.notchpsdresp}.

\begin{figure}[h]
%\centerline{\epsfxsize=4in\epsfbox{art/circArray.eps}} 
\begin{center}
\includegraphics[width=4.0in]{art/notchpsdresp.eps} 
\end{center}
\caption{Notch Filter Impulse and Frequency Responses.}  
\label{art.notchpsdresp}
\end{figure}

\section{Annular Filter}

Here we design a two-dimensional filter with an annular frequency
response. Since the desired frequency response is circlarly symmetric,
we can use the impulse response of the example in section
\ref{sect.circimpulse} on page \pageref{sect.circimpulse}.

Using the same random process strategy as for the first example, we
define passband and stopband processes:
\begin{quote}
\begin{verbatim}
pp = NDProcess('Circle', [.255;.245], [0 0; 0 0], [1; -1]);
ps = NDProcess({'Box'; 'Circle'; 'Circle'}, {[1 1]; .32; .18}, ...
	       [0 0; 0 0; 0 0], [1; -1; 1]);
\end{verbatim}
\end{quote}
 The desired passband is a thin annulus between radii .245 and
 .255. This is accomplished by subtracting a circle of the smaller
 radius from a circle of the larger radius, and hence the
 coefficient vector is [1; -1]. Both circles are centered at the
 origin.

 The stopband is more complicated; starting with a box to indicate
 the entire fundamental period of the frequency response, we
 subtract a circle slightly larger than the outer ring of the
 annulus, and add back to the stopband a circle slightly smaller
 than the inner ring of the annulus. 

 Since this process is composed of different types of basis
 functions, the function call to \optfn{NDProcess} is a bit more
 complicated. The basis function types must now be specified for
 each instance of a basis function in a cell array column
 vector. A cell array is created using the {} and the entries are
 separated by semicolons.

 Similarly, the parameters for each basis function instance must
 be given in separate entries of a cell array column vector. {\tt 'Box'}
 requires a width vector [1 1] which indicates a width of 1 in the
 x-direction and a width of 1 in the y-direction. {\tt 'Circle'}
 requires only a scalar for the radius.

 The frequency shift matrix and coefficient vector are given as usual.

 The various powers are computed as before. The power of the stopband
 process through the filter is to be constrained to be
 small. The passband is treated differently; in that case, we
 minimize the difference in power between the unfiltered and
 filtered passband process.
\begin{quote}
\begin{verbatim}
Ppwr = pwr(pp.*aa-pp);
Spwr = pwr(ps.*aa)/pwr(ps);
\end{verbatim}
\end{quote}

We bound the passband power by an auxiliary optimization variable, and
we bound the stopband power by a constant:
\begin{quote}
\begin{verbatim}
delta = optVar(ov);
constr = {Ppwr < delta.^2, Spwr < 1e-3 };
\end{verbatim}
\end{quote}

\optfn{minimize} is then called by
\begin{quote}
\begin{verbatim}
soln = minimize(delta, constr, ov, 'sedumi');
\end{verbatim}
\end{quote}

The frequency response of the optimized filter is shown in
\rfig{art.circresp}.

\begin{figure}[h]
%\centerline{\epsfxsize=4in\epsfbox{art/circArray.eps}} 
\begin{center}
\includegraphics[width=4.0in,height=3in]{art/circresp.eps} 
\end{center}
\caption{Annular Filter Frequency Response.}  
\label{art.circresp}
\end{figure}
