<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="m10813">
  <name>IIR Filtering: Filter-Coefficient Quantization Exercise in MATLAB</name>
  <metadata>
  <md:version>2.5</md:version>
  <md:created>2002/08/14</md:created>
  <md:revised>2003/07/31 10:38:20.716 GMT-5</md:revised>
  <md:authorlist>
    <md:author id="appadwed">
      <md:firstname>Swaroop</md:firstname>
      
      <md:surname>Appadwedula</md:surname>
      <md:email>appadwed@uiuc.edu</md:email>
    </md:author>
    <md:author id="dljones">
      <md:firstname>Douglas</md:firstname>
      <md:othername>L.</md:othername>
      <md:surname>Jones</md:surname>
      <md:email>dl-jones@uiuc.edu</md:email>
    </md:author>
    <md:author id="kramer">
      <md:firstname>Michael</md:firstname>
      <md:othername>L.</md:othername>
      <md:surname>Kramer</md:surname>
      <md:email>kramer@ifp.uiuc.edu</md:email>
    </md:author>
    <md:author id="moussa">
      <md:firstname>Dima</md:firstname>
      
      <md:surname>Moussa</md:surname>
      <md:email>dmoussa@uiuc.edu</md:email>
    </md:author>
    <md:author id="bwade">
      <md:firstname>Brian</md:firstname>
      
      <md:surname>Wade</md:surname>
      <md:email>bwade@uiuc.edu</md:email>
    </md:author>
    <md:author id="jake">
      <md:firstname>Jake</md:firstname>
      
      <md:surname>Janevitz</md:surname>
      <md:email>jake@janevitz.com</md:email>
    </md:author>
    <md:author id="dsachs">
      <md:firstname>Daniel</md:firstname>
      <md:othername>Grobe</md:othername>
      <md:surname>Sachs</md:surname>
      <md:email>sachs@uiuc.edu</md:email>
    </md:author>
    <md:author id="mjberry">
      <md:firstname>Matthew</md:firstname>
      <md:othername>J.</md:othername>
      <md:surname>Berry</md:surname>
      <md:email>mjberry@uiuc.edu</md:email>
    </md:author>
    <md:author id="markhaun">
      <md:firstname>Mark</md:firstname>
      <md:othername>A.</md:othername>
      <md:surname>Haun</md:surname>
      <md:email>markhaun@uiuc.edu</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="mjberry">
      <md:firstname>Matthew</md:firstname>
      <md:othername>J.</md:othername>
      <md:surname>Berry</md:surname>
      <md:email>mjberry@uiuc.edu</md:email>
    </md:maintainer>
    <md:maintainer id="butala">
      <md:firstname>Mark</md:firstname>
      <md:othername>D.</md:othername>
      <md:surname>Butala</md:surname>
      <md:email>butala@uiuc.edu</md:email>
    </md:maintainer>
    <md:maintainer id="rlmorris">
      <md:firstname>Robert</md:firstname>
      <md:othername>L.</md:othername>
      <md:surname>Morrison</md:surname>
      <md:email>rlmorris@uiuc.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>IIR filter</md:keyword>
    <md:keyword>coefficient quantization</md:keyword>
    <md:keyword>stability</md:keyword>
    <md:keyword>fixed-point</md:keyword>
    <md:keyword>integer</md:keyword>
    <md:keyword>DSP</md:keyword>
  </md:keywordlist>

  <md:abstract>You will design a fourth-order notch filter and investigate the effects of filter-coefficient quantization.  You will compare the response of the filter having unquantized coefficients with that of a filter having coefficients quantized as a single, fourth-order stage and with that of a filter having coefficients quantized as a cascade of two, second-order stages.  
</md:abstract>
</metadata>





  <content>
    <section id="sec3">
      <name>Filter-Coefficient Quantization</name>
      <para id="p10">
	One important issue that must be considered when IIR filters
	are implemented on a fixed-point processor is that the filter
	coefficients that are actually used are quantized from the
	"exact" (high-precision floating point) values computed by
	MATLAB.  Although quantization was not a concern when we
	worked with FIR filters, it can cause significant deviations
	from the expected response of an IIR filter.
      </para>
      <para id="p11">
	By default, MATLAB uses 64-bit floating point numbers in all
	of its computation. These floating point numbers can typically
	represent 15-16 digits of precision, far more than the DSP can
	represent internally.  For this reason, when creating filters
	in MATLAB, we can generally regard the precision as
	"infinite," because it is high enough for any reasonable task.
	<note type="Note">
	  Not all IIR filters are necessarily "reasonable"!
	</note>
	The DSP, on the other hand, operates using 16-bit fixed-point
	numbers in the range of -1.0 to
	<m:math>
	  <m:apply>
	    <m:minus/>
	    <m:cn>1.0</m:cn>
	    <m:apply>
	      <m:power/>
	      <m:cn>2</m:cn>
	      <m:cn>-15</m:cn>
	    </m:apply>
	  </m:apply>
	</m:math>.  This gives the DSP only 4-5 digits of precision
	and only if the input is properly scaled to occupy the full
	range from -1 to 1.
      </para>
      <para id="p12">
	For this section exercise, you will examine how this
	difference in precision affects a <term>notch filter</term>
	generated using the <term><code>butter</code></term> command:
	<code>[B,A] = butter(2,[0.07 0.10],'stop')</code>.
      </para>

      <section id="sec3a">
	<name>Quantizing coefficients in MATLAB</name>
	<para id="p13">
	  It is not difficult to use MATLAB to <term>quantize</term>
	  the filter coefficients to the 16-bit precision used on the
	  DSP.  To do this, first take each vector of filter
	  coefficients (that is, the 
	  <m:math>
	    <m:ci type="vector">A</m:ci>
	  </m:math> and 
	  <m:math>
	    <m:ci type="vector">B</m:ci> 
	  </m:math> vectors) and divide by the smallest power of two
	  such that the resulting absolute value of the largest filter
	  coefficient is less than or equal to one.  This is an easy
	  but fairly reasonable approximation of how numbers outside
	  the range of -1 to 1 are actually handled on the DSP.
	</para>
	<para id="p13a">
	  Next, quantize the resulting vectors to 16 bits of precision
	  by first multiplying them by
	  <m:math>
	    <m:apply>
	      <m:eq/>
	      <m:apply>
		<m:power/>
		<m:cn>2</m:cn>
		<m:cn>15</m:cn>
	      </m:apply>
	      <m:cn>32768</m:cn>
	    </m:apply>
	  </m:math>, rounding to the nearest integer (use
	  <code>round</code>), and then dividing the resulting vectors
	  by 32768. Then multiply the resulting numbers, which will be
	  in the range of -1 to 1, back by the power of two that you
	  divided out.
	</para>
      </section>

      <section id="sec3b">
	<name>Effects of quantization</name>
	<para id="p14">
	  Explore the effects of quantization by quantizing the filter
	  coefficients for the notch filter.  Use the
	  <code>freqz</code> command to compare the response of the
	  unquantized filter with two quantized versions: first,
	  quantize the entire fourth-order filter at once, and second,
	  quantize the second-order ("bi-quad") sections separately
	  and recombine the resulting quantized sections using the
	  <term><code>conv</code></term> function.  Compare the
	  response of the unquantized filter and the two quantized
	  versions.  Which one is "better?"  Why do we always
	  implement IIR filters using second-order sections instead of
	  implementing fourth (or higher) order filters directly?
	</para>
	<para id="p15">
	  Be sure to create graphs showing the difference between the
	  filter responses of the unquantized notch filter, the notch
	  filter quantized as a single fourth-order section, and the
	  notch filter quantized as two second-order sections.  Save
	  the MATLAB code you use to generate these graphs, and be
	  prepared to reproduce and explain the graphs as part of your
	  quiz.  Make sure that in your comparisons, you rescale the
	  resulting filters to ensure that the response is unity (one)
	  at frequencies far outside the notch.
	</para>
      </section>
    </section>

  </content>
  
</document>
