% Copyright INRIA \chapter[FIR Filters]{Design of Finite Impulse Response Filters} \label{fir} \index{FIR filters} \section{Windowing Techniques} \index{windowing!FIR filters} \index{FIR filter design!windowing technique} \label{s1} In theory, the design of FIR filters is straightforward. One takes the inverse Fourier transform of the desired frequency response and obtains the discrete time impulse response of the filter according to (\ref{e1}) % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{equation} \begin{array}{cc} h(n)=\displaystyle{\frac{1}{2\pi}\int_{-\pi}^{\pi} H(\omega)e^{j\omega n}d\omega} & -\infty [wft,wfm,fr]=wfir() \end{verbatim} where the parentheses are a required part of the name. This format of the function is interactive and will prompt the user for required input parameters such as the filter type (lp='low pass', hp='high pass', bp='band pass', sb='stop band'), filter length (an integer $n>2$), window type (re='rectangular', tr='triangular', hm='hamming', kr='kaiser', ch='chebyshev') and other special parameters such as $\alpha$ for the the generalized Hamming window ($0<\alpha<1$) and $\beta$ for the Kaiser window ($\beta>0$). The three returned arguments are: % \begin{itemize} \item{wft:} A vector containing the windowed filter coefficients for a filter of length n. \item{wfm:} A vector of length 256 containing the frequency response of the windowed filter. \item{fr:} A vector of length 256 containing the frequency axis values ($0\le$ fr$\le .5$) associated to the values contained in wfm. \end{itemize} % The second format of the function is as follows: \begin{verbatim} --> [wft,wfm,fr]=wfir(ftype,forder,cfreq,wtype,fpar) \end{verbatim} This format of the function is not interactive and, consequently, all the input parameters must be passed as arguments to the function. The first argument {\tt ftype} indicates the type of filter to be constructed and can take the values {\tt 'lp'}, {\tt 'hp'}, {\tt 'bp'}, and {\tt sb'} representing, respectively the filters low-pass, high-pass, band-pass, and stop-band. The argument {\tt forder} is a positive integer giving the order of the desired filter. The argument {\tt cfreq} is a two-vector for which only the first element is used in the case of low-pass and high-pass filters. Under these circumstances {\tt cfreq(1)} is the cut-off frequency (in normalized Hertz) of the desired filter. For band-pass and stop-band filters both elements of {\tt cfreq} are used, the first being the low frequency cut-off and the second being the high frequency cut-off of the filter. Both values of {\tt cfreq} must be in the range $[0,.5)$ corresponding to the possible values of a discrete frequency response. The argument {\tt wtype} indicates the type of window desired and can take the values {\tt 're'}, {\tt 'tr'}, {\tt 'hm'}, {\tt 'hn'}, {\tt 'kr'}, and {\tt 'ch'} representing, respectively, the windows rectangular, triangular, Hamming, Hanning, Kaiser, and Chebyshev. Finally, the argument {\tt fpar} is a two-vector for which only the first element is used in the case of Kaiser window and for which both elements are used in the case of a Chebyshev window. In the case of a Kaiser window the first element of {\tt fpar} indicates the relative trade-off between the main lobe of the window frequency response and the side-lobe height and must be a positive integer. For more on this parameter see \cite{rabiner}. For the case of the Chebyshev window one can specify either the width of the window's main lobe or the height of the window sidelobes. The first element of {\tt fpar} indicates the side-lobe height and must take a value in the range $[0,1)$ and the second element gives the main-lobe width and must take a value in the range $[0,.5)$. The unspecified element of the {\tt fpar}-vector is indicated by assigning it a negative value. Thus, {\tt fpar=[.01,-1]} means that the Chebyshev window will have side-lobes of height $.01$ and the main-lobe width is left unspecified. Note: Because of the properties of FIR linear phase filters it is not possible to design an even length high pass or stop band filter. \subsection{Examples} \index{FIR filter design!examples} \label{s5} This section gives several examples of windowed filter design. In the first example we choose a low pass filter of length $n=33$ using a Kaiser window with parameter $\beta=5.6$. The resulting magnitude of the windowed filter is plotted in Figure~\ref{f7} where the magnitude axis is given on a log scale. % \input{\Figdir fir7.tex} \dessin{{\tt exec('fir7.code')} Low pass filter with Kaiser window, $n=33$, $\beta=5.6$} {f7} % The second example is a stop band filter of length 127 using a Hamming window with parameter $\alpha=.54$. The resulting magnitude of the windowed filter is plotted in Figure~\ref{f8} where the magnitude is given on a log scale. % \input{\Figdir fir8.tex} \dessin{{\tt exec('fir8.code')} Stop band filter with Hamming window, $n=127$, $\alpha=.54$} {f8} % The third example is a band pass filter of length 55 using a Chebyshev window with parameter $dp=.001$ and $df=.0446622$. The resulting magnitude of the windowed filter is plotted in Figure~\ref{f9} where the magnitude is given on a log scale. % \input{\Figdir fir9.tex} \dessin{{\tt exec('fir9.code')} Band pass filter with Chebyshev window, $n=55$, $dp=.001$, $df=.0446622$} {f9} % \section{Frequency Sampling Technique} \index{FIR filter design!frequency sampling} This technique is based on specification of a set of samples of the desired frequency response at $N$ uniformly spaced points around the unit circle, where $N$ is the filter length. The z-transform of an FIR filter is easily shown to be : %%%%%%%%%%%%%%%% \begin{eqnarray} H(z)=\frac{1-z^{-N}}{N}\sum_{k=0}^{N-1}\frac{H(k)}{(1-z^{-1}e^{j(2\pi/N)k})} \label{e.fs.1} \end{eqnarray} %%%%%%%%%%%%%%%% This means that one way of approximating any continuous frequency response is to {\em sample in frequency}, at $N$ equi-spaced points around the unit circle (the frequency samples), and interpolate between them to obtain the continuous frequency response. Thus, the approximation error will be exactly zero at the sampling frequencies and finite between them. This fact has to be related to the reconstruction of a continuous function from its samples, as exposed in section~\ref{c2.sampling} for the case of a continuous-time signal. The interpolation formula for an FIR filter, that is its frequency response, is obtained by evaluating (\ref{e.fs.1}) on the unit circle: %%%%%%%%%%%%%%% \begin{eqnarray} H(e^{j\omega}) &=& \frac{e^{-j\omega(N-1)/2}}{N}\sum_{k=0}^{N-1}\frac{H(k)e^{-jk\pi/N}\sin(N\omega/2)}{\sin(\omega/2-k\pi/N)}\nonumber\\ &=& \frac{e^{-j\omega(N-1)/2}}{N}\sum_{k=0}^{N-1}H(k)S(\omega,k) \end{eqnarray} %%%%%%%%%%%%%%% where %%%%%%%%%%%%%%% \begin{eqnarray} S(\omega,k) &=& e^{-jk\pi/N}\frac{\sin(N\omega/2)}{\sin(\omega/2-k\pi/N)}\nonumber\\ &=&\pm e^{-jk\pi/N}\frac{\sin(N(\omega/2)-k\pi/N)}{\sin(\omega/2-k\pi/N)} \end{eqnarray} %%%%%%%%%%%%%% are the interpolating functions. Thus, the contribution of every frequency sample to the continuous frequency response is proportional to the interpolating function $\sin(N\omega/2)/\sin(\omega/2)$ shifted by \(k\pi/N\) in frequency. The main drawback of this technique is the lack of flexibility in specifying the transition band width, which is equal to the number of samples the user decides to put in times \(\pi/N\), and thus is strongly related to $N$. Moreover, the specification of frequency samples in transition bands, giving minimum ripple near the band edges, is not immediate. Nevertheless, it will be seen, in a later chapter on filter optimization techniques, that simple linear programming techniques can be used to drastically reduce the error approximation by optimizing only those samples located in the transition bands. To illustrate this point, Figure~\ref{f.fs1} shows the response obtained for a type 1 band pass filter with length 65 : first with no sample in the transition bands and second (dashed curve) with one sample of magnitude .5 in each of these bands. It is worth noting at this point that the linear-FIR design problem with arbitrary frequency response specification is more efficiently solved using a minmax approximation approach, which is exposed in the next section. \\ %%%%%%%%%%% \input{\Figdir fstyp121.tex} \dessin{{\tt exec('fstyp121.code')}Type 1 band pass filter with no sample or one sample in each transition band}{f.fs1} %%%%%%%%%% Finally, depending on where the initial frequency sample occurs, two distinct sets of frequency samples can be given, corresponding to the so-called type 1 and type 2 FIR filters :\\ \begin{eqnarray} f_k&=&\frac{k}{N} \; \; k=0, \ldots ,N-1 \mbox{ for type 1 filters}\nonumber\\ f_k&=&\frac{k+1/2}{N}\; \; k=0,\ldots ,N-1 \mbox{ for type 2 filters}\nonumber \end{eqnarray} The type of design is at user's will and depends on the application: for example, a band edge may be closer to a type 1 than to a type 2 frequency sampling point. This point is illustrated in Figure~\ref{f.fs2} for the case of a low pass filter with length 64 and no sample in the transition band. %%%%%%%%%%% \input{\Figdir fstyp122.tex} \dessin{{\tt exec('fstyp122.code')}Type 1 and type 2 low pass filter}{f.fs2} %%%%%%%%%% The full line (resp. the dashed line) gives the approximated response for the type 1 (resp. type 2) FIR linear filter. We give now the way the two previous examples have been generated and the code of the function {\tt fsfir} which calculates the approximated response. Figure~\ref{f.fs1} was obtained with the following set of instructions :\\ \verbatok{\Diary fstyp121.dia} \end{verbatim} and Figure~\ref{f.fs2} with : \verbatok{\Diary fstyp122.dia} \end{verbatim} \section{Optimal filters} \index{FIR filter design!minimax optimization} The design of FIR linear phase filters is a topic addressed in some detail in the section on windowed filter design. The essential idea behind the techniques of windowed filter design is to obtain a filter which is close to a minimum squared error approximation to the desired filter. This section is devoted to the description of a filter design function which seeks to optimize such an alternative criterion : the minimax or Chebyshev error approximation. \subsection{Minimax Approximation} \index{minimax approximation} \index{Chebyshev approximation} \index{optimal FIR filter design} To illustrate the problem of minimax approximation we propose an overspecified system of $N$ linear equations in $M$ unknowns where $N>M$. If $x$ represents the unknown $M$-vector then the system of equations can be written as % \begin{equation} Ax=b \label{e5.01} \end{equation} % where $A$ is an $N\times M$ matrix and $b$ is an $N$-vector. In general, no solution will exist for (\ref{e5.01}) and, consequently, it is reasonable to seek an approximation to $x$ such that the error vector % \begin{equation} r(x)=Ax-b \label{e5.02} \end{equation} % is in some way minimized with respect to $x$. Representing the $N$ components of $r(x)$ as $r_k(x)$, $k=1,2,\ldots,N$ the minimax approximation problem for the system of linear equations in (\ref{e5.01}) can be posed as follows. The minimax approximation, $\hat{x}_{\infty}$, is obtained by finding the solution to % \begin{equation} \hat{x}_{\infty}=\arg\min_x ||r_k(x)||_{\infty} \label{e5.03} \end{equation} % where % \begin{equation} ||r_k(x)||_{\infty}=\max_k |r_k(x)|. \label{e5.04} \end{equation} % Equation (\ref{e5.04}) defines the supremum norm of the vector $r(x)$. The supremum norm of $r(x)$ for a particular value of $x$ is the component of $r(x)$ (i.e., the $r_k(x)$) which is the largest. The minimax approximation in (\ref{e5.03}) is the value of $x$ which, out of all possible values for $x$, makes (\ref{e5.04}) the smallest. The minimax approximation can be contrasted by the minimum squared error approximation, $\hat{x}_2$, as defined by % \begin{equation} \hat{x}_2=\arg\min_x ||r(x)||_2 \label{e5.05} \end{equation} % where % \begin{equation} ||r(x)||_2=[\sum_{k=1}^{N}{r_k}^2(x)]^{1/2}. \label{e5.06} \end{equation} % There is a relationship between (\ref{e5.04}) and (\ref{e5.06}) which can be seen by examining the class of norms defined on $r(x)$ by % \begin{equation} ||r(x)||_n=[\sum_{k=1}^{N}{r_k}^n(x)]^{1/n}. \label{e5.07} \end{equation} % For $n=2$ the expression in (\ref{e5.07}) is the squared error norm in (\ref{e5.06}) and for $n\rightarrow \infty$ the norm in (\ref{e5.07}) becomes the supremum norm in (\ref{e5.04}) (which explains the notation $||\cdot||_{\infty}$). If $r(x)$ was a continuous function instead of a discrete component vector then the sum in (\ref{e5.06}) would become an integral and the interpretation of the approximation in (\ref{e5.05}) would be that the best approximation was the one which minimized the area under the magnitude of the error function $r(x)$. By contrast the interpretation of the approximation in (\ref{e5.03}) would be that the best approximation is the one which minimizes the maximum magnitude of $r(x)$. As an example, consider the system of four linear equations in one unknown: % \begin{eqnarray} x&=&2\nonumber\\ \frac{1}{3}x&=&1\nonumber\\ x&=&4\nonumber\\ \frac{6}{15}x&=&3 \label{e5.08} \end{eqnarray} % The plot of the magnitude of the four error functions $|r_k(x)|$, $k=1,2,3,4$ is shown in Figure~\ref{f5.01}. % \input{\Figdir remez1.tex} \dessin{{\tt exec('remez1.code')} Minimax Approximation for Linear Equations} {f5.01} % Also shown in Figure~\ref{f5.01} is a piece-wise continuous function denoted by the cross-hatched segments of the $r_k(x)$. This is the function which represents $||r(x)||_{\infty}$ as a function of $x$. Consequently, it is easy to see which value of $x$ minimizes $||r(x)||_{\infty}$ for this problem. It is the value of $x$ which lies at the cross-hatched intersection of the functions $|x-2|$ and $|\frac{6}{15}x-3|$, that is $\hat{x}_{\infty}=3.571$. The maximum error at this value is $1.571$. By comparison the mean squared error approximation for a system of linear equations as in (\ref{e5.01}) is % \begin{equation} \hat{x}_2=(A^TA)^{-1}A^Tb \label{e5.09} \end{equation} % where $T$ denotes the transpose (and assuming that $A^TA$ is invertible). Consequently, for the example in (\ref{e5.08}) we have that $A=[1, \frac{1}{3}, 1, \frac{6}{15}]^T$ and that $b=[2, 1, 4, 3]^T$ and thus $\hat{x}_2=3.317$. The maximum error here is $1.673$. As expected the maximum error for the approximation $\hat{x}_2$ is bigger than that for the approximation $\hat{x}_{\infty}$. \subsection{The Remez Algorithm} \index{Remez algorithm} The Remez algorithm seeks to uniformly minimize the magnitude of an error function $E(f)$ on an interval $[f_0,f_1]$. In the following discussion the function $E(f)$ takes the form of a weighted difference of two functions % \begin{equation} E(f)=W(f)(D(f)-H(f)) \label{e5.1} \end{equation} % where $D(f)$ is a single-valued function which is to be approximated by $H(f)$, and $W(f)$ is a positive weighting function. The Remez algorithm iteratively searches for the $H^*(f)$ such that % \begin{equation} H^*(f)=\arg \min_{H(f)}\|E(f)\|_{\infty} \label{e5.2} \end{equation} % where % \begin{equation} \|E(f)\|_{\infty}=\max_{f_0\le f\le f_1}|E(f)| \label{e5.3} \end{equation} % is known as both the Chebyshev and the minimax norm of $E(f)$. The details of the Remez algorithm can be found in \cite{cheney}. The function $H(f)$ is constrained, for our purposes, to the class of functions % \begin{equation} H(f)=\sum_{n=0}^{N}a_n\cos(2\pi fn). \label{e5.4} \end{equation} % Furthermore, we take the interval of approximation to be $[0,.5]$. Under these conditions the posed problem corresponds to digital filter design where the functions $H(f)$ represent the discrete Fourier transform of an FIR, linear phase filter of odd length and even symmetry. Consequently, the function $H(f)$ can be written % \begin{equation} H(f)=\sum_{n=-N}^{N}h_n e^{-j2\pi fn} \label{e5.5} \end{equation} % The relationship between the coefficients in (\ref{e5.4}) and (\ref{e5.5}) is $a_n=2h_n$ for $n=1,2,\ldots,N$ and $a_0=h_0$. With respect to the discussion in the previous section the problem posed here can be viewed as an overspecified system of linear equations in the $N+1$ unknowns, $a_n$, where the number of equations is uncountably infinite. The Remez algorithm seeks to solve this overspecified system of linear equations in the minimax sense. The next section describes the Scilab function {\tt remezb} and how it can be used to design FIR filters. \subsection{Function {\tt remezb}} \index{function syntax!remezb@{\tt remezb}} \index{optimal FIR filter design!function syntax} The syntax for the function {\tt remezb} is as follows: {\tt \begin{verbatim} --> an=remezb(nc,fg,df,wf) \end{verbatim}} \noindent where {\tt df} and {\tt wf} are vectors which are sampled values of the functions $D(f)$ and $W(f)$ (see the previous section for definition of notation), respectively. The sampled values of $D(f)$ and $W(f)$ are taken on a grid of points along the $f$-axis in the interval $[0,.5]$. The values of the frequency grid are in the vector {\tt fg}. The values of {\tt fg} are not obliged to be equi-spaced in the interval $[0,.5]$. In fact, it is very useful, for certain problems, to specify an {\tt fg} which has elements which are equi-spaced in only certain sub-intervals of $[0,.5]$ (see the examples in the following section). The value of {\tt nc} is the number of cosine functions to be used in the evaluation of the approximating function $H(f)$ (see (\ref{e5.4})). The value of the variable {\tt nc} must be a positive, odd integer if the problem is to correspond to an FIR filter. The {\tt an} are the values of the coefficients in (\ref{e5.4}) which correspond to the optimal $H(f)$. To obtain the coefficients of the corresponding FIR filter it suffices to create a vector {\tt hn} using the Scilab commands: \verbatok{\Diary remez8.code} \end{verbatim} Even length filters can be implemented as follows. For an even length filter to have linear phase the filter must have even symmetry about the origin. Consequently, it follows that the filter must take values at the points $n=\pm\frac{1}{2},\pm\frac{3}{2},\ldots,\pm\frac{N-1}{2}$ and that the frequency response of the filter has the form % \begin{equation} H(f)=\sum_{n=-N-\frac{1}{2}}^{N+\frac{1}{2}}h_ne^{-j2\pi fn}. \label{e5.001} \end{equation} % Due to the even symmetry of the frequency response, $H(f)$, (\ref{e5.001}) can be put into the form % \begin{equation} H(f)=\sum_{n=1}^{N}b_n\cos[2\pi(n-\frac{1}{2})f] \label{e5.002} \end{equation} % where the relationship between the $h_n$ in (\ref{e5.001}) and the $b_n$ in (\ref{e5.002}) is $h(n)=\frac{1}{2}b(N-n)$ for $n=1,2,\ldots,N$. The expression for $H(f)$ in (\ref{e5.002}) can be rewritten so that % \begin{equation} H(f)=\cos(\pi f)\sum_{n=0}^{N-1}\tilde{b}_n\cos(2\pi nf). \label{e5.003} \end{equation} % where $b(n)=\frac{1}{2}[\tilde{b}(n-1)+\tilde{b}(n)]$ for $n=2,3,\ldots,N-1$ and $b(1)=\tilde{b}(0)+\frac{1}{2}\tilde{b}(1)$ and $b(N)=\frac{1}{2}\tilde{b}(N-1)$. Since the expression in (\ref{e5.003}) is identical to that in (\ref{e5.4}) all that is required to make the function {\tt remezb} work is a change in the values of the desired and weight vectors by the factor $\cos^{-1}(\pi f)$. That is, the arguments given to the function {\tt remezb} are {\tt ddf} and {\tt wwf} where $\mtt{ ddf}=\mtt{ df}/\cos(\pi f)$ and $\mtt{ wwf}=\mtt{ wf}\cos(\pi f)$. Caution must be used in choosing the values of {\tt fg} since for $f=.5$ the division of {\tt df} by $\cos(\pi f)=0$ is not acceptable. The output, {\tt an}, of the function can be converted to the filter coefficients {\tt hn} by using the Scilab commands \verbatok{\Diary remez2.code} \end{verbatim} Noting that the form of (\ref{e5.003}) has the term $\cos(\pi f)$ as a factor, it can be seen that $H(.5)=0$ regardless of the choice of filter coefficients. Consequently, the user should not attempt to design filters which are of even length and which have non-zero magnitude at $f=.5$. \subsection{Examples Using the function {\tt remezb}} \index{optimal FIR filter design!examples} Several examples are presented in this section. These examples show the capabilities and properties of the function {\tt remezb}. The first example is that of a low-pass filter with cut-off frequency .25. The number of cosine functions used is 21. The input data to the function are first created and then passed to the function {\tt remezb}. The subsequent output of cosine coefficients is displayed below. Notice that the frequency grid {\tt fg} is a vector of frequency values which are equally spaced in the interval $[0,.5]$. The desired function {\tt ds} is a vector of the same length as {\tt fg} and which takes the value $1$ in the interval $[0,.25]$ and the value $0$ in $(.25,.5]$. The weight function {\tt wt} is unity for all values in the interval. \verbatok{\Diary remez4.dia} \end{verbatim} As described in the previous section the cosine coefficients {\tt an} are converted into the coefficients for a even symmetry FIR filter which has frequency response as illustrated in Figure~\ref{f5.1}. % \input{\Figdir remez2.tex} \dessin{{\tt exec('remez2\_4.code')} Low Pass Filter with No Transition Band} {f5.1} % The error of the solution illustrated in Figure 3.13 is very large; it can become reasonable by leaving a transition band when giving the specification of the frequency grid. The following example shows how this is done; {\tt remezb} is specified as follows : \verbatok{\Diary remez5.dia} \end{verbatim} Here the frequency grid {\tt fg} is specified in the intervals $[0,.24]$ and $[.26,.5]$ leaving the interval $[.24,.26]$ as an unconstrained transition band. The frequency magnitude response of the resulting filter is illustrated in Figure~\ref{f5.2}. As can be seen the response in Figure~\ref{f5.2} is much more acceptable than that in Figure~\ref{f5.1}. % \input{\Figdir remez3.tex} \dessin{{\tt exec('remez2\_4.code')} Low Pass Filter with Transition Band $[.24,.26]$} {f5.2} % A third and final example using the function {\tt remezb} is illustrated below. In this example the desired function is triangular in shape. The input data was created using the following Scilab commands \verbatok{\Diary remez6.dia} \end{verbatim} The resulting frequency magnitude response is illustrated in Figure~\ref{f5.3}.This example illustrates the strength of the function {\tt remezb}. The function is not constrained to standard filter design problems such as the class of band pass filters. The function is capable of designing linear phase FIR filters of any desired magnitude response. % \input{\Figdir remez4.tex} \dessin{{\tt exec('remez2\_4.code')} Triangular Shaped Filter} {f5.3} % \subsection{Scilab function {\tt eqfir}} \index{function syntax!eqfir@{\tt eqfir}} \index{optimal FIR filter design!function syntax} For the design of piece-wise constant filters (such as band pass, low pass, high pass, and stop band filters) with even or odd length the user may use the function {\tt eqfir} which is of simpler manipulation. Three examples are presented here. The first two examples are designs for a stopband filter. The third example is for a design of a high pass filter. The first design for the stop band filter uses the following Scilab commands to create the input to the function {\tt eqfir}: \verbatok{\Diary eqfir1.dia} \end{verbatim} \noindent The resulting magnitude response of the filter coefficients is shown in Figure~\ref{f5.5}. As can be seen the design is very bad. This is due to the fact that the design is made with an even length filter and at the same time requires that the frequency response at $f=.5$ be non-zero. % \input{\Figdir remez5.tex} \dessin{{\tt exec('remez5\_7.code')} Stop Band Filter of Even Length} {f5.5} % The same example with $\mtt{nf}=33$ is run with the result shown in Figure~\ref{f5.6}. % \input{\Figdir remez6.tex} \dessin{{\tt exec('remez5\_7.code')} Stop Band Filter of Odd Length} {f5.6} % The final example is that of a high pass filter whose input parameters were created as follows: \verbatok{\Diary eqfir2.dia} \end{verbatim} The result is displayed in Figure~\ref{f5.7}. % \input{\Figdir remez7.tex} \dessin{{\tt exec('remez5\_7.code')} High Pass Filter Design} {f5.7} % %\section{Linear Programming Design of FIR filters} %\section{Hilbert Transform} %\end{document}