DSPRelated.com

IIR Bandstop Filter

Ron April 1, 2011 Coded in C for the TI C67x
// Include the filter coefficients and corresponding variables
#include "IIRBSF.h"

// The intermediate values in the Direct Form II filter
float delay_w[MWSPT_NSEC][3];	// delay_w[i][j] <=> w_i(n-j), j=0,1,2	
				// i is the section, j the delay
						
float sectionOut;		// yk[n] <=> sectionout

interrupt void isr()	 	//Interrupt function t=125us, f = 8kHz
{	
  short i; 			// i loops through the MWSPT_NSEC number of sections

  //  Do the filtering if DIP switch 1 up
  if (get_DIP1() == 1) {
			
	// In the first section, read in the x-value, apply the first stage gain
	sectionOut = NUM[0][0] * get_sample();

	for (i=1; i<MWSPT_NSEC; i++) { // Loop through all the sections
	
		// Get the new delay_w[0];
		delay_w[i][0] = sectionOut - DEN[i][1]*delay_w[i][1] - DEN[i][2]*delay_w[i][2];

		// Get the output of this section		
		sectionOut = NUM[i][0]*delay_w[i][0] + NUM[i][1]*delay_w[i][1] + NUM[i][2]*delay_w[i][2];
	
		// Delay the w's for the next interrupt
		delay_w[i][2] = delay_w[i][1];
		delay_w[i][1] = delay_w[i][0];

	}

	// Apply the gain, convert to short and send out
	send_output((short)(2 * sectionOut));
	// Gain of 2 chosen heuristically for speech from PC

  } else { // If DIP switch 1 down, == 0, then just pass through signal.

	send_output(get_sample());
  }
 	return;	//interrupt done
}

void main()
{
	short i,j;
 	for (i=0; i<MWSPT_NSEC; i++)
 		for (j=0; j<3; j++) 
			delay_w[i][j] = 0;      // init intermediate array			
	init_all();                  	// init all
	while(1);    		   	// infinite loop
}

IIR FIlter and add tone

Ron April 1, 2011 Coded in C for the TI C67x
//iirFilter.c  

// Include the filter coefficients and corresponding variables
#include "IIRLPF.h"
#define A 1		//The amplitude of added wave
#define FREQ 1000	//The frequency of the sine wave in Hz

// The intermediate values in the Direct Form II filter
float delay_w[MWSPT_NSEC][3];		
						// delay_w[i][j] <=> w_i(n-j), j=0,1,2
						// i is the section, j the delay
						
float sectionOut;		// yk[n] <=> sectionout

interrupt void isr()	 //Interrupt function t=125us, f = 8kHz
{	
  short i; 			// i loops through the MWSPT_NSEC number of sections
  int period = 8000 / FREQ;
  float rad = FREQ * 2 * pi;
  int j = 0;

  //  Do the filtering if DIP switch 1 up
  if (get_DIP1() == 1) {
			
	// In the first section, we read in the x-value, apply the first stage gain
	sectionOut = NUM[0][0] * get_sample();

	for (i=1; i<MWSPT_NSEC; i++) { // Loop through all the sections
	
		// Get the new delay_w[0];
		delay_w[i][0] = sectionOut - DEN[i][1]*delay_w[i][1] - DEN[i][2]*delay_w[i][2];

		// Get the output of this section		
		sectionOut = NUM[i][0]*delay_w[i][0] + NUM[i][1]*delay_w[i][1] + NUM[i][2]*delay_w[i][2];
	
		// Delay the w's for the next interrupt
		delay_w[i][2] = delay_w[i][1];
		delay_w[i][1] = delay_w[i][0];

	}

	//Add a tone to sectionOut
	sectionOut = sectionOut + A*sin(rad*j); //Add a sine wave of freq rad to sectionOut
	j = (j + 1) % period; //Increment the sine wave counter

	// Apply the gain, convert to short and send out
	send_output((short)(2 * sectionOut));
	// Gain of 2 chosen heuristically for speech from PC

  } else { // If DIP switch 1 down, == 0, then just pass through signal.

	send_output(get_sample());
  }

 	return;							// return from interrupt
}

void main()
{
	short i,j;

 	for (i=0; i<MWSPT_NSEC; i++)
 		for (j=0; j<3; j++) 
			delay_w[i][j] = 0;      // init intermediate array
			
	init_all();                  	// init all
	while(1);    		  	// infinite loop
}

Dual DIP-switched IIR Filter

Ron April 1, 2011 Coded in C for the TI C67x
//iirFilterSwitch.c  

// Include the filter coefficients and corresponding variables
#include "IIRLPF.h"
#include "IIRHPF.h"

// The intermediate values in the Direct Form II filter
float delay_w1[MWSPT_NSEC1][3];		
float delay_w2[MWSPT_NSEC2][3];
						// delay_w[i][j] <=> w_i(n-j), j=0,1,2
						// i is the section, j the delay
						
float sectionOut;		// yk[n] <=> sectionout

interrupt void isr()	 //Interrupt function
{	
  short i; 			// i loops through the MWSPT_NSEC number of sections

  //  Use the LPF if DIP switch 1 up
  if (get_DIP1() == 1) {
			
	// In the first section, we read in the x-value, apply the first stage gain
	sectionOut = NUM1[0][0] * get_sample();

	for (i=1; i<MWSPT_NSEC1; i++) { // Loop through all the sections
	
		// Get the new delay_w1[0];
		delay_w1[i][0] = sectionOut - DEN1[i][1]*delay_w1[i][1] - DEN1[i][2]*delay_w1[i][2];

		// Get the output of this section		
		sectionOut = NUM1[i][0]*delay_w1[i][0] + NUM1[i][1]*delay_w1[i][1] + NUM1[i][2]*delay_w1[i][2];
	
		// Delay the w's for the next interrupt
		delay_w1[i][2] = delay_w1[i][1];
		delay_w1[i][1] = delay_w1[i][0];

	}

	// Apply the gain, convert to short and send out
	send_output((short)(2 * sectionOut));
	// Gain of 2 chosen heuristically for speech from PC

  } else { // If DIP switch 1 down, == 0, then use HPF

	sectionOut = NUM2[0][0] * get_sample();

	for (i=1; i<MWSPT_NSEC2; i++) { // Loop through all the sections
	
		// Get the new delay_w2[0];
		delay_w2[i][0] = sectionOut - DEN2[i][1]*delay_w2[i][1] - DEN2[i][2]*delay_w2[i][2];

		// Get the output of this section		
		sectionOut = NUM2[i][0]*delay_w2[i][0] + NUM2[i][1]*delay_w2[i][1] + NUM2[i][2]*delay_w2[i][2];
	
		// Delay the w's for the next interrupt
		delay_w2[i][2] = delay_w2[i][1];
		delay_w2[i][1] = delay_w2[i][0];

	}

	// Apply the gain, convert to short and send out
	send_output((short)(2 * sectionOut));
	// Gain of 2 chosen heuristically for speech from PC

  }

 	return;							// return from interrupt
}

void main()
{
	short i,j;

 	for (i=0; i<MWSPT_NSEC1; i++)
 		for (j=0; j<3; j++) 
			delay_w1[i][j] = 0;      // init intermediate array

 	for (i=0; i<MWSPT_NSEC2; i++)
 		for (j=0; j<3; j++) 
			delay_w2[i][j] = 0;      // init intermediate array
			
	init_all();                  	// init all
	while(1);    			// infinite loop
}

Circular Buffer FIR Filter

Ron April 1, 20111 comment Coded in C for the TI C67x
// These coefficients are used to filter
#include "yourfiltercoeffs.h"

// scale factor, S_h = 2^K.
#define K	15

int	filterout = 0; // In fixed-point implementation, the accumulator is 32-bit int
short delay[LENGTH];  // Short is the 16-bit integer variable

interrupt void isr()	 // Interrupt function
{
	short 	i;
	int 	Sx = 1;
	
	//  Do the filtering as per fixfilt if DIP switch 1 up
	if (get_DIP1() == 1) {
		// Direct-Form FIR
		delay[0] = Sx * get_sample();     	// input for filter
		filterout = hMax[0] * delay[0];   	// Set up filter sum
		// Notice, C-arrays go from 0..LENGTH-1
		for (i = LENGTH-1; i > 0; i--){		// Get sum of products
     			filterout += hMax[i] * delay[i];	
		     	delay[i] = delay[i-1];      	// Renew input array
		}			
		filterout = (filterout>>K); //Move into the lower 16 bits, reverses scaling
			//Sign extension carries the upper bit down if negative
	 	send_output(filterout);		// output for filter
	} else { // If DIP switch 1 down, == 0, then just pass through signal.
		send_output(get_sample());
	}

 	return;						// return from interrupt
}

void main()
{
	short i;

 	for (i=0; i<= LENGTH-1; i++) { 
		delay[i] = 0;          	// init filter processing array
    	}
	init_all();            		// init all
	while(1);    		   	// infinite loop
}

Max Amplitude Sine Wave

Ron April 1, 2011 Coded in C for the TI C67x
#define ARRAY_LEN  8
short index = 0;
int sample[ARRAY_LEN] = {0,23170,32767,23170,0,-23170,-32767,-23170};
interrupt void isr()	 
{
  	send_output(sample[index]); 
  	index = (++index) % ARRAY_LEN;
	return;	//interrupt servicing complete
}

FIR 2kHz Bandstop Comb Filter

Ron April 1, 2011 Coded in C for the TI C67x
#define COMB_FILTER_ORDER		3	// Filter Order, i.e., M
#define DELAY_ARRAY_SIZE		9	// Number of (delayed) input samples to store

int   filtersum;			 	// Sum-of-products accumulator for calculating the filter output value
int   delay[DELAY_ARRAY_SIZE];			// Array for storing (delayed) input samples

static int gain1 = 1;				
static int gain2 = 4;				

interrupt void isr()	 			//Interrupt service routine
{							
     short i; 		 			// Loop counter	 
     delay[0] = get_sample() / gain1;		// Read filter input sample from ADC  and scale the sample		
     filtersum = delay[0];			// Initialize the accumulator
     for (i = COMB_FILTER_ORDER; i > 0; i--)
     {
     	filtersum += delay[i];			// Accumulate array elements
	delay[i] = delay[i-1];			// Shift delay elements by one sample
     }
     filtersum *= gain2;			// Scale the sum-of-products
     send_output(filtersum);			// write filter output sample to DAC
     return;					// interrupt servicing complete
}

void main()
{
     short i;	// loop counter
     for (i=0; i< DELAY_ARRAY_SIZE; i++)	// Initialize delay elements with 0
     { 
          delay[i] = 0;
     }
     init_all();                  		// global initialization
     while(1);    		   		// infinite loop
						// do nothing except wait for the interrupt	 
}

Passthrough a2d2a

Ron April 1, 2011 Coded in C for the TI C67x
int sample;            		       
interrupt void isr()	 //interrupt start
{
	sample = get_sample();	//input from A2D
 	send_output(sample);	//output to D2A
 	return;			//interrupt complete
}

Interrupt.c - DSK6713 External Interruptions via GPIO

David Valencia February 2, 20111 comment Coded in C for the TI C67x
/* Interruption.c 
JOSE DAVID VALENCIA PESQUEIRA
UPIITA-IPN 
THIS PROGRAM DETECTS AN EXTERNAL INTERRUPTION ON A GPIO PIN*/ 

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include <time.h> 
#include <csl_gpio.h> 
#include <csl_gpiohal.h> 
#include <csl_irq.h> 

/* GLOBAL VARIABLES
The flags are marked as volatile, to tell the compiler
that these can be modified by an external event, avoiding
to put them on registers and rather putting them on RAM
so they're ready for immediate use
*/

volatile int startflag = 0; 

GPIO_Handle gpio_handle;  /* Handle for the GPIO */ 

// GPIO Registers configuration

GPIO_Config gpio_config = {          
	0x00000000,  
	// gpgc = Interruption passtrhrough mode and direct GPIO control mode
	0x0000FFFF, // gpen = All GPIO 0-15 pins enabled
	0x00000000, // gdir = All GPIO pins as inputs
	0x00000000, // gpval = Stores logical level of pins
	0x00000010, // IRQ enable for pin 4
	0x00000010, // Enable Event for pin 4
	0x00000000  // gppol -- default state  
}; 

irq_ext_enable(){ 
	/* First, globally disable interruptions
	in the CSR (Control Status Register).
	Bit 0 is the GIE (Global Interrupt Enable)*/
	CSR = (CSR)&(0xFFFFFFFE); 
	// Enable NMIE bit in the IER (bit 1)
	IER |= 0x00000002; 
	// Enable INT4 bit in the IER (4th bit)
	IER |= 0x00000010; 
	// Finally, globally enable interruptions in the CSR
	CSR |= 0x00000001; 
} 

main() 
{      
	// To configure the GPIO
	gpio_handle = GPIO_open( GPIO_DEV0, GPIO_OPEN_RESET ); 
	GPIO_config(gpio_handle,&gpio_config); 
	irq_ext_enable(); 
	comm_intr();      //init DSK 
	DSK6713_LED_init(); 
	while(startflag == 0);  
	printf(“La interrupción externa encendió el led 3 del dsk6713\n”); 
	while(1) // Infinite While
}    

interrupt void c_int04()           //ISR 
{ 
	startflag = 1; 
	iter = 0; 
	DSK6713_LED_on(0);   //To turn on the led
}

GPIO.c - Using the GPIO as output

David Valencia January 31, 20114 comments Coded in C for the TI C67x
// GPIO.c	UPIITA-IPN 
// JOSE DAVID VALENCIA PESQUEIRA
//
// THIS PROGRAM ALLOWS THE DSP TO SEND A BIT THROUGH A GPIO PIN (PIN 7 IN THIS EXAMPLE)
// TO TURN ON A LED ON

#include <stdio.h>
#include <stdlib.h>
#include <csl_gpio.h>
#include <csl_gpiohal.h>
#include <csl_irq.h>

// GLOBAL VARIABLES
volatile int flag = 1;
volatile int pulse_flag = 1;

// CODE TO DEFINE GPIO HANDLE
GPIO_Handle gpio_handle; 

// GPIO REGISTER CONFIGURATION
GPIO_Config gpio_config = {         
    0x00000000, // gpgc = Interruption passthrough mode
    0x0000FFFF, // gpen = All GPIO 0-15 pins enabled
    0x0000FFFF, // gdir = All GPIO pins as outputs
    0x00000000, // gpval = Saves the logical states of pins
    0x00000000, // gphm All interrupts disabled for IO pins
    0x00000000, // gplm All interrupts for CPU EDMA disabled
    0x00000000  // gppol -- default state */
};

// Function prototypes
void send_edge();
void delay();

// Function definitions
void delay()
{
	// Delay function
	int count, count2;
	for(count = 0; count < 200; count++)
	{
		for(count2 = 0; count2 < 50000; count2++);
	}
}

void send_edge()
{	
    DSK6713_init();  
    // Open and configure the GPIO
    gpio_handle = GPIO_open( GPIO_DEV0, GPIO_OPEN_RESET );
    GPIO_config(gpio_handle,&gpio_config);
    // Send values through the pins
	
    GPIO_pinWrite(gpio_handle,GPIO_PIN7,0);
    delay();
    GPIO_pinWrite(gpio_handle,GPIO_PIN7,1);
    delay();
    GPIO_pinWrite(gpio_handle,GPIO_PIN7,0);
}

main()
{     
	send_edge(); // Configures the pins and sends a rising edge
	printf(“ ¡Felicidades! ¡Has logrado encender el led! ”);
	
}
// End of program>>

Constant complexity rectangular QAM demodulator

Vashishth November 14, 2010 Coded in C for the TI C67x
% N is the size of the constellation.
% v is the input data.
% For details email me.
void function_qam()
{

co=0;
 m=0;

for(q=0; q<N; q++)

        {

 a=v[m];// a holds the real part
 b=v[m+1];// b holds the imaginary part

 d=a-onern[max];  %onern stores the negative real axis points and onein stores the negative imag axis points. eg for 256 QAM, each will store 0 to -8 as an array.
 l=d;         //l stores the distance
 s=(d-l);
 if(l<max-1) % max is the maximum value that the 1-d coordinate can take.
       {

if(d>0){
 if(s<=.5)
       {
 hi=max-l;
 flagtwo=1;
       }
 else
 {
 hi=max-l-1;
 flagtwo=1;
 }
      }
	  else
	  {
	  hi=max;
	  flagtwo=1;
	  }
        
      }

 if(l==max-1)
 {
 hi=1;
 flagtwo=1;
 }
 if(l==max)//
 {
 flagone=1;
 hi=1;
 } 

 if(l>max)
        {
		if(d<2*max)
		{

 if(s<=.5)
 {
 hi=l-max;
 flagone=1;
 }
 else
 {
 hi=l-max+1;
 flagone=1;
 }
        }
		else
		{
		hi=max;
		flagone=1;
		}
       
          
        }

 de=b-onein[max];
 le=de;         //l stores the distance
 se=(de-le);

 if(le<max-1)
      {

	  if(de>0)
	  {
 if(se<=.5)
 {
 hm=max-le;
 flagfour=1;
 }
 else
 {
 hm=max-le-1;
 flagfour=1;
 }
      
      }
	  else
	  {
	  hm=max;
	  flagfour=1;
      }
      
      
      }

 if(le==max-1)
 {
 hm=1;
 flagfour=1;
 }
 if(le==max)
 {
 flagthree=1;
 hm=1;
 }

 if(le>max)
     {

if(de<2*max){

 if(se<=.5)
 {
 hm=le-max;
 flagthree=1;
 }
 else
 {
 hm=le-max+1;
 flagthree=1;
 }
    }
	else
	{
	hm=max;
	flagthree=1;
	}
    
       
    
    }

 if(flagone==1)
 {
  vv[m]=hi;  // now v[m] holds one x coordinate of the qam mapping and v[m+1] holds the y coordinate
 }
 
 else
 {
 vv[m]= 100-hi;
 }

 
 if(flagthree==1)
  {
 vv[m+1]=hm;  
  }
		else
		{
 vv[m+1]= 100-hm;
        }

flagone=0;
flagtwo=0;
flagthree=0;
flagfour=0;
        r=0;
        
        
          
           output[co]= valuetable[ vv[m] ][ vv[m+1] ] ;
       
       
          
          
                        
      m = m+ 2;
	  
	  co = co+ 1;
                
                
                 }

  for(t=0;t<(N/2);t++)
  {
   p[t] = (output[t]);
 
  }

 

}