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
}

vectors_intr.asm - External Interruption Configuration

David Valencia February 2, 2011 Coded in ASM for the TI C67x
*Vectors_intr.asm Vector file for interrupt INT11 
   .global _vectors     ;global symbols 
   .global _c_int00 
   .global _vector1 
   .global _vector2 
   .global _vector3 
   .global _c_int04     ; símbolo para EXT_INT4 
   .global _vector5 
   .global _vector6 
   .global _vector7 
   .global _vector8 
   .global _vector9    
   .global _vector10  
   .global _c_int11        ;for INT11 
   .global _vector12   
   .global _vector13    
   .global _vector14 
   .global _vector15 
 
   .ref _c_int00        ;entry address 
 
VEC_ENTRY .macro addr      ;macro for ISR 
    STW   B0,*--B15 
    MVKL  addr,B0 
    MVKH  addr,B0 
    B     B0 
    LDW   *B15++,B0 
    NOP   2 
    NOP    
    NOP    
   .endm 
 
_vec_nmi: 
  B    NRP 
  NOP  5 
 
_vec_dummy: 
  B    IRP 
  NOP  5 
 
 .sect ".vecs"        ;aligned IST section 
 .align 1024 
_vectors: 
_vector0:   VEC_ENTRY _c_int00     ;RESET 
_vector1:   VEC_ENTRY _vec_nmi   ;NMI 
_vector2:   VEC_ENTRY _vec_dummy    ;RSVD 
_vector3:   VEC_ENTRY _vec_dummy 
_vector4:   VEC_ENTRY _c_int04    ;INT04 Externa 
_vector5:   VEC_ENTRY _vec_dummy 
_vector6:   VEC_ENTRY _vec_dummy 
_vector7:   VEC_ENTRY _vec_dummy 
_vector8:   VEC_ENTRY _vec_dummy 
_vector9:   VEC_ENTRY _vec_dummy 
_vector10:  VEC_ENTRY _vec_dummy 
_vector11:  VEC_ENTRY _c_int11      ;ISR address 
_vector12:  VEC_ENTRY _vec_dummy 
_vector13:  VEC_ENTRY _vec_dummy 
_vector14:  VEC_ENTRY _vec_dummy 
_vector15:  VEC_ENTRY _vec_dummy

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>>