<?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="m10658">
  <name>Spectrum Analyzer: PSD Estimator (55x)</name>
  <metadata>
  <md:version>1.1</md:version>
  <md:created>2008/02/21 15:43:21.534 US/Central</md:created>
  <md:revised>2008/02/21 15:54:02.945 US/Central</md:revised>
  <md:authorlist>
      <md:author id="tbshen">
      <md:firstname>Thomas</md:firstname>
      
      <md:surname>Shen</md:surname>
      <md:email>tbshen@uiuc.edu</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="tbshen">
      <md:firstname>Thomas</md:firstname>
      
      <md:surname>Shen</md:surname>
      <md:email>tbshen@uiuc.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>autocorrelation</md:keyword>
    <md:keyword>block processing</md:keyword>
    <md:keyword>C language</md:keyword>
    <md:keyword>DFT</md:keyword>
    <md:keyword>digital signal processing</md:keyword>
    <md:keyword>discrete fourier transform</md:keyword>
    <md:keyword>discrete time fourier transform</md:keyword>
    <md:keyword>DTFT</md:keyword>
    <md:keyword>fast algorithms</md:keyword>
    <md:keyword>fast fourier transform</md:keyword>
    <md:keyword>FFT</md:keyword>
    <md:keyword>frequency domain</md:keyword>
    <md:keyword>IIR filter</md:keyword>
    <md:keyword>PN generator</md:keyword>
    <md:keyword>power spectral density</md:keyword>
    <md:keyword>power spectral density estimation</md:keyword>
    <md:keyword>PSD</md:keyword>
    <md:keyword>pseudo-noise generator</md:keyword>
    <md:keyword>spectral analysis</md:keyword>
    <md:keyword>spectrum</md:keyword>
    <md:keyword>windowing</md:keyword>
  </md:keywordlist>

  <md:abstract>This is an implementation of an autocorrelation-based power spectral density (PSD) estimator. This implementation estimates the PSD of an IIR-filtered pseudo-noise generator.</md:abstract>
</metadata>


  <content>

    <section id="sec2">
      <name>Reference Implementation of a PSD estimator</name>
      <para id="sec2para1">We provide for you in Appendix <cnxn target="sec_appendix_d">D</cnxn>
        and <cnxn target="sec_appendix_e">E</cnxn> a complete C implementation
        of a PSD estimator. The input is an IIR-filtered pseudo-noise (PN)
        sequence generator and the PSD estimate is based on windowing the
        autocorrelation with a rectangular window.
        The code consists of the files <code>lab4b.c</code>,
        <code>lab4b.h</code>, <code>intrinsics.h</code>, <code>pn.c</code>,
        <code>iirfilt.c</code>, <code>autocorr.c</code>,
        <code>c_fft_given_iirc.asm</code>, and the previously-given TI FFT
        routine. The assembly file <code>c_fft_given_iirc.asm</code> differs
        from <code>c_fft_given.asm</code> in that the window array has been
        removed and variables and arrays associated with IIR filtering have
        been added. Note that the multiply functions in the functions are
        actually compiler directives contained in <code>intrinsics.h</code>.
        Make sure you know which ones are used and why; note that
        <code>VPO</code> is not defined by the TI compiler, therefore the
        corresponding section of the <code>#ifdef</code> statement is not used.
        Open up the lab4b.pjt project and <code>Rebuild All</code>.
Load <code>lab4b.out</code> onto the DSP and
        run the code. Make sure that an
        IIR-filtered PN sequence appears on channel 1 and its PSD estimate
        appears on channel 2.
      </para>

      <para id="sec2para2">
        Does the output match your expectations based on the theory? Does this
        application illustrate any limitations of the FFT implementation?
        (Hint: note that most of the values in the FFT input are zero.)
        The previously-given C implementation uses a similar algorithm as the
        TI FFT; take a look at the C code for help.
        What are the limitation(s) of the FFT that show up in this application?
      </para>

      <para id="sec2para3">In <code>lab4b.h</code>
	<code>M</code> sets the number of
        autocorrelation points that are calculated. What is the maximum value
        of <code>M</code> that allows the reference routines to run in real
        time? In determining this value you may find it useful to connect a
        wave-function generator to the input and copy input on that channel
        into channel 1 of output. You may limit <code>M</code> to
        powers of <code>2</code> minus <code>1</code>.
      </para>
    </section>

   

    <section id="sec_appendix_a">
      <name>Appendix A: Main routine, header files for PSD estimator</name>

      <para id="sdp1">
        <link src="lab4b.h">lab4b.h</link>
      </para>

      <para id="sdp2">
        <link src="intrinsics.h">intrinsics.h</link>
      </para>

      <para id="sdp3"><link src="lab4b.c">lab4b.c</link>
</para>

      <para id="element-445"><link src="lab4bmain.c">lab4bmain.c</link>
</para><code type="block" id="sdb1">
        <![CDATA[
1    #define N 1024    /* Length of output buffers */
2    #define L N       /* Length of input data     */
3    #define logL 10   /* log base 2 of L          */
4    #define M 31      /* Compute 2*M+1 autocorrelation points */
5
6    /* #define M (L/2-1) */ /* Be sure to use ()'s in this case */
7                            /* or algebraic substitution bugs   */
8                            /* can be introduced                */
        ]]>
      </code>

      <code type="block" id="sdb2"><![CDATA[
/* Compiler intrinsics for the TI compiler        */
/* and the Very Portable Optimizer (VPO) port     */
/* to TMS320C54X series DSPs                      */
/*                                                */
/* Use compile option -DVPO when using VPO        */
/*                                                */
/* Copyright September 2005 by Matt Kleffner      */
/* under the Creative Commons Attribution License */
/* Works with TMS320C55X series                   */

#ifndef INTRINSICS_H

#define INTRINSICS_H

#ifdef VPO

long int vpo_l_mul_ii(int w0, int w1);

/* fractional multiply without fractional mode (long result) */
#define _l_mul_fract_fb0_ii(w0,w1) \
         (vpo_l_mul_ii(w0,w1) << 1)

/* fractional multiply with fractional mode already on (long result) */
#define _l_mul_fract_fb1_ii(w0,w1) \
         (vpo_l_mul_ii(w0,w1))

/* fractional multiply without fractional mode (int result) */
#define _i_mul_fract_fb0_ii(w0,w1) \
         (vpo_l_mul_ii(w0,w1) >> 15)

/* fractional multiply with fractional mode already on (int result) */
#define _i_mul_fract_fb1_ii(w0,w1) \
         (vpo_l_mul_ii(w0,w1) >> 16)

#define _set_fract_bit() vpo_set_fract()
#define _reset_fract_bit() vpo_reset_fract()
#define _set_ovm_bit() vpo_set_ovm()
#define _reset_ovm_bit() vpo_reset_ovm()

#define _l_add_shiftl_li(w0,w1) (((int32)(w0))+(((int32)(int16)(w1))<<16) )
#define _l_sub_shiftl_li(w0,w1) (((int32)(w0))-(((int32)(int16)(w1))<<16) )

#else

/* fractional multiply without fractional mode (long result) */
#define _l_mul_fract_fb0_ii(w0,w1) \
         (((long int)w0 * (long int)w1) << 1)

/* fractional multiply with fractional mode already on (long result) */
#define _l_mul_fract_fb1_ii(w0,w1) \
         (((long int)w0 * (long int)w1))

/* fractional multiply without fractional mode (int result) */
#define _i_mul_fract_fb0_ii(w0,w1) \
         (((long int)w0 * (long int)w1) >> 15)

/* fractional multiply with fractional mode already on (int result) */
#define _i_mul_fract_fb1_ii(w0,w1) \
         (((long int)w0 * (long int)w1) >> 16)

#define _set_fract_bit() asm("	ssbx frct")
#define _reset_fract_bit() asm("	rsbx frct")
#define _set_ovm_bit() asm("	ssbx ovm")
#define _reset_ovm_bit() asm("	rsbx ovm")

#endif /* VPO */

#endif /* INTRINSICS_H */

        ]]>
</code>

      <code type="block" id="sdb3"><![CDATA[
// lab4b.c
// Uses PN generation, IIR filtering, and autocorrelation
// code by Matt Kleffner -9/2004
// Based on swi_process.c by Educational DSP

#include "dsk5510_dual3006cfg.h"
#include "dsk5510.h"
#include "swi_process.h"
#include "dsplib.h"

#include "lab4b.h"              /* Define N here in header file */

/* function defined in pn.c */
void rand_fillbuffer(void);

/* IIR values and buffers (declared in c_fft_given_iirc.asm) */
#define IIR_order 4
extern int scale;
extern int coef[IIR_order];
extern int state[IIR_order];

/* Pointer to state buffer location */
int iirptr;
extern unsigned int *iseed;            /* seed for rand_fillbuffer() and randbit() */

/* function defined in iirfilt.c */
void iirfilt(void);

/* function defined in autocorr.c */
void autocorr(void);

/* Function defined by c_fft_given_iirc.asm */
//void bit_rev_fft(void);

/* FFT data buffers (declared in c_fft_given_iirc.asm) */
extern int bit_rev_data[N*2];   /* Data output for bit-reverse function */
extern int fft_data[N*2];       /* In-place FFT input & Output array */

/* Our input/output buffers */
int autocorr_in[N];
int autocorr_out[N];

// all data processing should be done in SWI_ProcessBuffer

void SWI_ProcessBuffer()
{
	static unsigned int mbox_value = 0;
	short *psrc, *pdest;
	unsigned int i;

	mbox_value |= SWI_getmbox();

	// buffers are only processed when both transmit and receive are ready
	if((mbox_value & DMA_RECEIVE_DONE) && (mbox_value & DMA_TRANSMIT_DONE)) {
 		mbox_value = 0;

		// get buffer pointers
		psrc = receive_buffer[receive_buffer_to_process_index];
		pdest = transmit_buffer[transmit_buffer_to_fill_index];
		
		    /* First, transfer inputs and outputs */

	    for (i = 0; i < N; i++) 
	    {
    	    pdest[4*i] = autocorr_in[i];
        	pdest[4*i+1] = bit_rev_data[i*2] << 8;
		
        /* Some statements useful in debugging */
        /* pdest[4*i] = psrc[4*i+2]; */
        /* Be sure to comment out PN-sequence generation */
        /* when using the next two lines */
        //autocorr_in[i] = psrc[4i+2];
		
	    }
    	
    	/* Last, set the DC coefficient to -1 for a trigger pulse */
    	pdest[0] = -32768;

		/* Generate PN input */
    	rand_fillbuffer();
		
    	/* Filter input */
    	iirfilt();

    	/* Calculate autocorrelation */
    	autocorr();
	
	 	/* Transfer autocorr output to FFT input buffer */
    	for (i = 0; i < N; i++) {
        	fft_data[i*2] = autocorr_out[i];
        	fft_data[i*2+1] = 0;
    	}
 
		
		// Bit-reverse and compute FFT 
		cfft((DATA *)fft_data,N, SCALE);
		cbrev((DATA *)fft_data,(DATA *)bit_rev_data,N);
		
		
		 /* Done, wait for next time around! */
		receive_buffer_processed = 1; // flag receive buffer as processed
		transmit_buffer_filled = 1; // flag output buffer as full
	}
}


        ]]>
</code><code type="block"><![CDATA[
// lab4bmain.c
// based on main.c by Educational DSP
// Initializes autocorr_out, state, and iseed.
// Can be modified for optimization. 

#include "dsk5510_dual3006cfg.h"
#include "dsk5510.h"

#include <csl.h>
#include "lab4b.h"
#define IIR_order 4

extern int iirptr;
unsigned int *iseed;
extern int autocorr_out[N];
extern int state[IIR_order];

void init_DMA();

void main()
{
    int i;
 
	
    DSK5510_init();						// init BSL
    
    DSK5510_rset(DSK5510_MISC, 0x03);	// route McBSP0/1 to J3


	// Initialize autocorr_out to zero since some values will remain zero
	for (i = 0; i < N; ++i)
	{
		autocorr_out[i] = 0;
	}

	for ( i = 0; i < IIR_order; ++i)
		state[i] = 0;
    
    // Start McBSP0 I2S slave
    MCBSP_start(hMcbsp0, MCBSP_XMIT_START | MCBSP_RCV_START |
	MCBSP_SRGR_START | MCBSP_SRGR_FRAMESYNC, 220);
    
    // Start McBSP1 I2S master
    MCBSP_start(hMcbsp1, MCBSP_XMIT_START | MCBSP_RCV_START |
	MCBSP_SRGR_START | MCBSP_SRGR_FRAMESYNC, 220);
   
	init_DMA();							// configure DMA and interrupts

	*iseed = 1;	
	iirptr = 0;
   
    return;								// let DSP/BIOS scheduler take over
}

]]></code>

    </section>

    <section id="sec_appendix_e">                                                                     
      <name>Appendix E: Additional routines for PSD estimator</name>
                                                                                  
      <para id="sep1">
        <link src="pn.c">pn.c</link>
      </para>

      <para id="sep2">
        <link src="iirfilt.c">iirfilt.c</link>
      </para>

      <para id="sep3">
        <link src="autocorr.c">autocorr.c</link>
      </para>

      <para id="sep4">
        <link src="c_fft_given_iirc.asm">c_fft_given_iirc.asm</link>
      </para>

      <code type="block" id="seb1"><![CDATA[
/* ECE420, Lab 4, Reference PN Generator Implementation (Non-Optimized) */
/* Matt Kleffner 08/04                                                  */
/* Original by Michael Frutiger 02/24/04                                */
/* Use governed by the Creative Commons Attribution License             */

#include "lab4b.h"

extern unsigned int *iseed;
extern int autocorr_in[N];

/* Returns as an integer a random bit, based on the 15 lowest significant
   bits in iseed (which is modified for the next call). */
int randbit()
{
   int newbit;
   /* XOR bits 15, 1 and 0 of iseed */
   newbit =  (*iseed >> 15) & 1 ^ (*iseed >> 1) & 1 ^ *iseed & 1;
   /* Leftshift the seed and put the result of the XOR's in bit 1. */
   *iseed=(*iseed << 1) | newbit;  
   return(newbit);
}

void rand_fillbuffer(void)
{
   int i;

   for (i = 0; i < N; ++i)
   {
      if (randbit()) autocorr_in[i] =  32767;
      else           autocorr_in[i] = -32767;
   }
}

        ]]>
</code>

      <code type="block" id="seb2"><![CDATA[
/* Simple, unoptimized IIR filter (feedback only) */
/* for TMS320C54X series DSPs                     */
/* Copyright September 2005 by Matt Kleffner      */
/* under the Creative Commons Attribution License */
/* Works for TMS320C55X series as well			  */

#include "lab4b.h"
#include "intrinsics.h"

/* IIR values and buffers (declared in c_fft_given_iirc.asm) */
#define IIR_order 4
extern int scale;
extern int coef[IIR_order];
extern int state[IIR_order];

/* Arrays declared in main routine */
extern int autocorr_in[N];
extern int autocorr_out[N];

/* Pointer to state buffer location */
extern int iirptr;

void iirfilt()
{
    int i, j;

    _set_fract_bit();
    /* Filter PN input */
    for (i = 0; i < N; ++i)
    {
       int sum = 0;
       /* Calculate and sum all feedback terms except the "oldest" one */
       for (j = 0; j < (IIR_order-1); ++j)
       {
          sum += _i_mul_fract_fb1_ii(coef[j],state[iirptr]);
          /* Avoid usage of "modulo" routine */
          iirptr++;
          if (iirptr == IIR_order) iirptr = 0;
       }
       /* Calculate and sum oldest feedback term without incrementing iirptr */
       sum += _i_mul_fract_fb1_ii(coef[IIR_order-1],state[iirptr]);

       /* Calculate direct input contribution */
       sum += _i_mul_fract_fb1_ii(scale,autocorr_in[i]);
       autocorr_in[i] = sum;
       state[iirptr] = autocorr_in[i];
    }
    _reset_fract_bit();
}

       ]]>
</code>

      <code type="block" id="seb3"><![CDATA[
/***********************************************************/
/* autocorr.c                                              */
/* Copyright August 2004 by Matt Kleffner                  */
/* under the Creative Commons Attribution License          */
/*                                                         */
/* Simple, unoptimized autocorrelation function            */
/* for ECE 420 (TMS320C54X series)                         */
/*                                                         */
/* #defines expected in lab4b.h                            */
/*    L: length of data in autocorr_in buffer              */
/*    N: length of data in autocorr_out buffer             */
/* logL: log base 2 of L (used for scaling output)         */
/*    M: Largest positive lag of autocorrelation desired   */
/*       (must be < L and < N/2)                           */
/*                                                         */
/* 16-bit-limited input/output (must be defined elsewhere) */
/*  autocorr_in: buffer for input data  (L pts)            */
/* autocorr_out: buffer for output data (N pts)            */
/*               N must be >= 2*M+1                        */
/*               assumed to be full of zeros at start      */
/*               output in zero-phase form                 */
/***********************************************************/
/* Works for TMS320C55X series 							   */

#include "lab4b.h"
#include "intrinsics.h"

extern int autocorr_in[L];
extern int autocorr_out[N];

void autocorr(void)
{
   int i,j,temp;

   _set_fract_bit();
   for(i=0;i<=M;++i)
   {
      long int sum=0;
      for(j=0;j<(L-i);++j)
      {
         temp = _i_mul_fract_fb1_ii(autocorr_in[j],autocorr_in[j+i]);
         sum += temp;
      }
      autocorr_out[i]=(int)(sum >> logL);
   }
   _reset_fract_bit();

   /* Copy values for negative indeces at end of buffer */
   for(i=1,j=N-1;i<=M;++i,--j)
   {  autocorr_out[j]=autocorr_out[i]; }
}

        ]]>
</code>

    </section><code type="block"><![CDATA[
; c_fft_given_iirc.asm
; Designed for use in lab4b for ECE420



      .ARMS_off                     ;enable assembler for ARMS=0
      .CPL_on                       ;enable assembler for CPL=1
      .mmregs                       ;enable mem mapped register names

	.global _bit_rev_data
	.global _fft_data
	.global _state
	.global _scale
	.global _coef

	.copy "macro.asm"

	.sect ".data"

N	.set 1024


	.align 4*N
_bit_rev_data .space 16*2*N

	.align 4*N
_fft_data .space 16*2*N


; IIR filter
	.align 4
_coef
	.word	0
	.word	0
	.word	0
	.word	-13421

_state
	.space 16*4

_scale
	.word	19345

	.sect ".text"


]]></code>

  </content>

</document>
