<?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:m="http://www.w3.org/1998/Math/MathML" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="m10027">
  
  <name> Spectrum Analyzer: Introduction to Fast Fourier Transform and Power Spectra (ECE 420 specific)</name>
  <metadata>
  <md:version>1.1</md:version>
  <md:created>2004/08/19 16:51:27 GMT-5</md:created>
  <md:revised>2004/09/20 22:04:59.121 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="kleffner">
      <md:firstname>Matt</md:firstname>
      
      <md:surname>Kleffner</md:surname>
      <md:email>kleffner@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:authorlist>

  <md:maintainerlist>
    <md:maintainer id="kleffner">
      <md:firstname>Matt</md:firstname>
      
      <md:surname>Kleffner</md:surname>
      <md:email>kleffner@uiuc.edu</md:email>
    </md:maintainer>
    <md:maintainer 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:maintainer>
    <md:maintainer id="laska">
      <md:firstname>Jason</md:firstname>
      <md:othername>Noah</md:othername>
      <md:surname>Laska</md:surname>
      <md:email>laska@uiuc.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  

  <md:abstract>In this lab you will explore DSP FFT implementations and power spectral density (PSD) estimators. You will also learn how to write DSP code in C and the trade-offs between writing in C versus assembly.</md:abstract>
</metadata>

  <content>
    <section id="s0">
      <name>Introduction</name>
      <para id="s0p1">
        In this lab you are going to apply the <term>Fast Fourier
	Transform</term> (<term>FFT</term>) to analyze the spectral
	content of an input signal in real time. You will also 
        explore algorithms that estimate a stationary random signal's
        <term>Power Spectral Density</term> (<term>PSD</term>). Finally,
        you will be introduced to using the C environment and code
        optimization in a practical application. This knowledge will be
        applied in optimizing a reference implementation of a PSD estimator.
      </para>
    </section>

    <section id="s1s1">
      <name>Fast Fourier Transform</name>
      <para id="s1p1">
        First, samples of the
        <term>power spectrum</term> of a deterministic signal will be
        calculated via the magnitude squared of the FFT of the windowed
        signal. You will transform a 1024-sample block of input data
        and send the power spectrum to the output for display on the
        oscilloscope. After computing the
	FFT of a 1024-sample block of input data, you will then
	compute the squared magnitude of the sampled spectrum and send
	it to the output for display on the oscilloscope.  In contrast
	to the systems you have implemented in the previous labs, the
	FFT is an algorithm that operates on blocks of samples at a
	time.  In order to operate on blocks of samples, you will need
	to use interrupts to halt processing so that samples can be
	transferred.
      </para>   
      <para id="s1p2">
	The FFT can be used to analyze the spectral content of a
	signal.  Recall that the FFT is an efficient algorithm for
	computing the <term>Discrete Fourier Transform</term>
	(<term>DFT</term>), a frequency-sampled version of the
	<term>DTFT</term>.
      </para>
      <para id="s1p3">
	DFT:
	<equation id="eqn1">
	  <m:math>
	    <m:apply>
	      <m:eq/>
	      <m:apply>
		<m:ci type="fn" class="discrete">X</m:ci>
		<m:ci>k</m:ci>
	      </m:apply>
	      <m:apply>
		<m:sum/>
		<m:bvar>
		  <m:ci>n</m:ci>
		</m:bvar>
		<m:lowlimit>
		  <m:cn>0</m:cn>
		</m:lowlimit>
		<m:uplimit>
		  <m:apply>
		    <m:minus/>
		    <m:ci>N</m:ci>
		    <m:cn>1</m:cn>
		  </m:apply>
		</m:uplimit>
		<m:apply>
		  <m:times/>
		  <m:apply>
		    <m:ci type="fn" class="discrete">x</m:ci>
		    <m:ci>n</m:ci>
		  </m:apply>
		  <m:apply>
		    <m:exp/>
		    <m:apply>
		      <m:minus/>
		      <m:apply>	
			<m:times/>
			<m:imaginaryi/>
			<m:apply>
			  <m:divide/>
			  <m:apply>
			    <m:times/>
			    <m:cn>2</m:cn>
			    <m:pi/>
			  </m:apply>
			  <m:ci>N</m:ci>
			</m:apply>
			<m:ci>n</m:ci>
			<m:ci>k</m:ci>
		      </m:apply>
		    </m:apply>
		  </m:apply>
		</m:apply>
	      </m:apply>
	    </m:apply>
	  </m:math>
	</equation> where
	<m:math>
	  <m:apply>
	    <m:in/>
	    <m:apply>
	      <m:and/>
	      <m:ci>n</m:ci>
	      <m:ci>k</m:ci>
	    </m:apply>
	    <m:set>
	      <m:cn>0</m:cn>
	      <m:cn>1</m:cn>
	      <m:ci>…</m:ci>
	      <m:apply>
		<m:minus/>
		<m:ci>N</m:ci>
		<m:cn>1</m:cn>
	      </m:apply>
	    </m:set>
	  </m:apply>
	</m:math>
      </para>
      <para id="s1p4">
        Your implementation will include windowing of the input data
        prior to the FFT computation.  This is simple a point-by-point
        multiplication of the input with an analysis window.  As you
        will explore in the prelab exercises, the choice of window
        affects the shape of the resulting spectrum.
      </para>

      
      <para id="s1p5">
        A block diagram of the spectrum analyzer you
        will implement in the lab, including the required input and
        ouput locations, is depicted in <cnxn target="spectrum_system"/>.
      </para>

      <figure id="spectrum_system">
        <media type="image/png" src="spectrum_system.png"/>
        <caption>
          FFT-based spectrum analyzer
        </caption>
      </figure>
    </section>

    <section id="s2">
      <name>Pseudo-Noise Sequence Generator</name>
      <para id="s2p1">
        Second, you will generate a colored, psuedo-noise (PN) sequence as
        input to the power spectrum algorithm. The noise sequence will be
        generated with a linear feedback shift register, whose operation is
        as shown in <cnxn target="fig2"/>. This PN generator is simply a
        shift-register and an XOR gate. Bits 0, 2, and 15 of the shift-register
        are XORed together and the result is shifted into the lowest bit of
        the register. This lowest bit is the output of the PN generator,
        and the highest bit is discarded in the shift process.
        The LSB is used to generate a
        value of <m:math><m:mo>±</m:mo><m:ci>M</m:ci></m:math> and this
        sequence will repeat with a period of
        <m:math>
          <m:apply>
            <m:minus/>
            <m:apply>
              <m:power/>
              <m:cn>2</m:cn>
              <m:ci>B</m:ci>
            </m:apply>
            <m:cn>1</m:cn>
          </m:apply>
        </m:math>, where <m:math><m:ci>B</m:ci></m:math> is the width in
        bits of the shift register and <m:math><m:ci>M</m:ci></m:math> is
        a constant. The power spectral density of this sequence is flat
        (white) and
        it will be "colored" via a fourth-order IIR filter. PN generators of
        this type are a useful source of random data bits for system testing.
        They are especially useful as a data model in simulating communication
        systems as these systems tend to randomize the bits seen by the
        transmission scheme so that bandwidth can be efficiently utilized.
        However, this method will not produce very "random" numbers.
        For more on this, see
        <cite src="http://en.wikipedia.org/wiki/Pseudo-random_number_generator">
        Pseudorandom number generator</cite>,
        <cite src="http://en.wikipedia.org/wiki/Linear_feedback_shift_register">
        Linear feedback shift register</cite>,
        and chapter 7, section 4 of
        <cite src="http://www.library.cornell.edu/nr/cbookcpdf.html">
        Numerical Recipes</cite>.
      </para>

      <figure id="fig2">
        <name>Pseudo-Noise Generator</name>
        <media type="image/png" src="pn-gen.png"/>
      </figure>

    </section>
    <section id="s3">
    <name>Power Spectral Density Estimation</name>
      <para id="s3p1">
        The direct-power-spectrum (DPS) algorithm outlined above is insufficient
        for estimating the PSD of a stationary noise
        signal because the variance of the estimated PSD is proportional to the
        value of the actual PSD. For the third part of this lab you will try to
        reduce the variance of the PSD estimate by windowing the
        autocorrelation of the noise signal and computing the fft.
      </para>

      <para id="s3p2">
        The autocorrelation of a sequence is the correlation of the
        sequence with itself:
      </para>
      <para id="s3p3">
	<equation id="eqn2">
	  <m:math>
	    <m:apply>
	      <m:eq/>
	      <m:apply>
		<m:ci type="fn" class="discrete">R</m:ci>
		<m:ci>m</m:ci>
	      </m:apply>
	      <m:apply>
		<m:sum/>
		<m:bvar>
		  <m:ci>i</m:ci>
		</m:bvar>
		<m:lowlimit>
		  <m:cn>0</m:cn>
		</m:lowlimit>
		<m:uplimit>
		  <m:apply>
		    <m:minus/>
                    <m:apply>
		      <m:minus/>
		        <m:ci>N</m:ci>
		        <m:cn>1</m:cn>
                    </m:apply>
                    <m:apply>
                      <m:abs/><m:ci>m</m:ci>
                    </m:apply>
		  </m:apply>
		</m:uplimit>
		<m:apply>
		  <m:times/>
		  <m:apply>
		    <m:ci type="fn" class="discrete">x</m:ci>
		    <m:ci>i</m:ci>
		  </m:apply>
		  <m:apply>
		    <m:ci type="fn" class="discrete">x</m:ci>
                    <m:apply>
                      <m:plus/>
		      <m:ci>i</m:ci>
                      <m:apply>
                        <m:abs/><m:ci>m</m:ci>
                      </m:apply>
                    </m:apply>
		  </m:apply>
		</m:apply>
	      </m:apply>
	    </m:apply>
	  </m:math>
	</equation> where
	<m:math>
	  <m:apply>
	    <m:in/>
	      <m:ci>m</m:ci>
	    <m:set>
              <m:apply><m:minus/><m:apply>
                <m:minus/>
	        <m:ci>N</m:ci>
	        <m:cn>1</m:cn>
              </m:apply></m:apply>
              <m:apply><m:minus/><m:apply>
                <m:minus/>
	        <m:ci>N</m:ci>
	        <m:cn>2</m:cn>
              </m:apply></m:apply>
	      <m:ci>…</m:ci>
              <m:apply>
                <m:minus/>
	        <m:ci>N</m:ci>
	        <m:cn>1</m:cn>
              </m:apply>
	    </m:set>
	  </m:apply>
	</m:math>

      </para>

      <para id="s3p4">
        For random signals, the autocorrelation here is an estimate
        of the actual autocorrelation.
        As <m:math><m:apply><m:abs/><m:ci>m</m:ci></m:apply></m:math> is
        increased, the number of samples used in the autocorrelation decreases. 
        The windowed-DPS algorithm is equivalent to taking the FFT of the
        autocorrelation of the windowed data. Windowing of the data
        adds even more noise to the autocorrelation estimate, since the
        autocorrelation is performed on a distorted version of the original
        signal. An improvement can be made by constructing an accurate estimate
        of the autocorrelation (using a rectangular window),
        applying a window and computing the FFT. The motivation for applying
        the window at the latter stage is that emphasis should be given to
        accurate autocorrelation values while less accurate values should be
        de-emphasized or discarded.
      </para>

      <para id="s3p5">
        A good empirical characterization of a random process requires
        sufficient data, and both of the PSD-estimation algorithms defined
        above can be extended to accomodate more data. There is one caveat,
        however: many
        real-world processes are modeled as <emphasis>short-time</emphasis>
        stationary processes (non-stationary models are hard to deal with),
        so there is a practical limit to how much data is available for a PSD
        estimate.
        Additional data is added to the direct-PSD estimation algorithm by
        adding multiple spectra together, thereby smoothing the PSD estimate.
        Additional data is added to the windowed-autocorrelation method
        by computing the autocorrelation of the total data set before
        windowing. You will explore the windowed-autocorrelation method on
        the DSP. 
      </para>

    </section>

    <section id="s4">
      <name>Compiling and Optimization</name>
      <para id="s4p1">
        A second objective of this lab exercise is to introduce the
        TMS320C549 C environment in a practical DSP application. The C
        environment provides a fast and convenient way to implement
        a DSP system using C and assembly modules. You will also learn how
        to optimize a program and when to use C or assembly. In future labs,
        the benefits of using the C environment will become clear as larger
        systems are developed.
      </para>
    </section>
  </content>
  </document>
