fir filter. c-implementation (fir filter) #include #include #include "coeff_ccs_16int.h"...

21
FIR Filter FIR Filter

Upload: anne-matthey

Post on 14-Dec-2015

232 views

Category:

Documents


3 download

TRANSCRIPT

FIR FilterFIR Filter

C-Implementation (FIR filter)C-Implementation (FIR filter)#include <stdio.h>#include <stdio.h>

#include <stdlib.h>#include <stdlib.h>

#include <math.h>#include <math.h>

#include "coeff_ccs_16int.h"#include "coeff_ccs_16int.h"

int in_buffer[300];int in_buffer[300];

int out_buffer[300];int out_buffer[300];

#define TRUE 1#define TRUE 1

/*Function *//*Function */

static int firfilter(int *input, int *output);static int firfilter(int *input, int *output);

static void dataIO(void);static void dataIO(void);

void main()void main(){{/* Declare file pointers *//* Declare file pointers */int *input = &inp_buffer[0];int *input = &inp_buffer[0];int *output= &out_buffer[0];int *output= &out_buffer[0];

/* Start of main program *//* Start of main program */ while(TRUE)while(TRUE) { { dataIO(); / dataIO(); / * insert probe point &attach .dat file* insert probe point &attach .dat file

firfilter(input, output); firfilter(input, output); }}} }

static int firfilter(int *input, int *output)static int firfilter(int *input, int *output){{/* Define variable arrays, define and clear variables *//* Define variable arrays, define and clear variables */

long yn = 0; long yn = 0; /* y(n), output from FIR filter *//* y(n), output from FIR filter */

int xnbuf[96];int xnbuf[96]; int i,j,k;int i,j,k; int size = 300;int size = 300; for(k=0;k<NL;k++)for(k=0;k<NL;k++) {{ xnbuf[k]=0;xnbuf[k]=0; }}

while (size--)while (size--)

{ {

for (i=NL-1;i>0;i--)for (i=NL-1;i>0;i--)

{{

xnbuf[i]=xnbuf[i-1];xnbuf[i]=xnbuf[i-1];

}}

xnbuf[0]=*input++;xnbuf[0]=*input++;

/* /* FIR filtering :y(n) = Sum[x(n-i)*coeff(i)] for i = 0to FIR filtering :y(n) = Sum[x(n-i)*coeff(i)] for i = 0to NL-1 */yn = 0; */NL-1 */yn = 0; */

for (j=0; j< NL; j++)for (j=0; j< NL; j++) {{ yn += (((long)(int)xnbuf[j](long)(int16_T)NUM[j])>>15);yn += (((long)(int)xnbuf[j](long)(int16_T)NUM[j])>>15); } } *output++ = (int)(yn);*output++ = (int)(yn); }} return(TRUE);return(TRUE);}}

static void dataIO()static void dataIO()

{{

return;return;

}}

FIR using DSKFIR using DSK#include "bs2700.cof" //coefficient file #include "bs2700.cof" //coefficient file #include "dsk6713_aic23.h" #include "dsk6713_aic23.h" //codec-dsk support file //codec-dsk support file

Uint32 fs=DSK6713_AIC23_FREQ_8KHZ; Uint32 fs=DSK6713_AIC23_FREQ_8KHZ; //set sampling rate //set sampling rate

int yn = 0; //initialize filter's output int yn = 0; //initialize filter's output short dly[N]; //delay samples short dly[N]; //delay samples interrupt void c_int11() //ISR interrupt void c_int11() //ISR {{ short i; short i; dly[0]=input_sample(); //input newest sample dly[0]=input_sample(); //input newest sample yn = 0; //initialize filter's output yn = 0; //initialize filter's output

for (i = 0; i< N; i++) for (i = 0; i< N; i++) yn += (h[i] * dly[i]); //y(n) += h(i)* x(n-i) yn += (h[i] * dly[i]); //y(n) += h(i)* x(n-i) for (i = N-1; i > 0; i--) //starting @ end of buffer for (i = N-1; i > 0; i--) //starting @ end of buffer dly[i] = dly[i-1]; //update delays with datamove dly[i] = dly[i-1]; //update delays with datamove output_sample(yn >> 15); //scale output filter sample output_sample(yn >> 15); //scale output filter sample return; return; }}

void main() void main() { { comm_intr(); //init DSK, codec, McBSP comm_intr(); //init DSK, codec, McBSP while(1); //infinite loop while(1); //infinite loop }}

Coefficients Time n Time n+1 Time n+2h(0) x(n) x(n+1) x(n+2)

h(1) x(n-1) x(n) x(n+1)

h(2) x(n-2) x(n-1) x(n)

.. .. x(n-2) x(n-1)

.. .. .. x(n-2)

h(n-3) x(n-(N-3)) .. ..

h(n-2) x(n-(N-2)) x(n-(N-3)) ..

h(n-1) x(n-(N-1)) x(n-(N-2)) x(n-(N-3))

FIR using C-callable asm programFIR using C-callable asm program#include “filter_coeff_bandpass.h" // contain h coeff #include “filter_coeff_bandpass.h" // contain h coeff int yn = 0;int yn = 0; interrupt void c_int11() interrupt void c_int11() {{dly[N-1] = input_sample(); dly[N-1] = input_sample(); yn = fircasmfunc(dly,h,N); yn = fircasmfunc(dly,h,N); output_sample(yn >> 15); output_sample(yn >> 15); return;return; }}void main()void main(){ { short i;short i; for (i = 0; i<N; i++) for (i = 0; i<N; i++) dly[i] = 0; dly[i] = 0; comm_intr(); comm_intr(); while(1); while(1); } }

;FIRCASMfunc.asm ASM function called ;FIRCASMfunc.asm ASM function called from C to implement FIR ;from C to implement FIR ;

A4 = Samples address, B4 = coeff address, A4 = Samples address, B4 = coeff address, A6 = filter order ;Delays organized as:A6 = filter order ;Delays organized as:

x(n-(N-1))...x(n);coeff as h[0]...h[N-1] x(n-(N-1))...x(n);coeff as h[0]...h[N-1] .def _fircasmfunc .def _fircasmfunc

_fircasmfunc:_fircasmfunc: ;ASM function called from C ;ASM function called from C

MV A6,A1 ;setup loop count MV A6,A1 ;setup loop count MPY A6,2,A6 ;since dly buffer data as byte MPY A6,2,A6 ;since dly buffer data as byte

ZERO A8 ;init A8 for ZERO A8 ;init A8 for accumulation accumulation

ADD A6,B4,B4 ;since coeff buffer data as ADD A6,B4,B4 ;since coeff buffer data as

byte byte

SUB B4,1,B4 ;B4=bottom coeff array h[N-1] SUB B4,1,B4 ;B4=bottom coeff array h[N-1]

loop: loop: ;start of FIR loop ;start of FIR loop

LDH *A4++,A2 ;LDH *A4++,A2 ;A2=x[n-(N-1)+i] i=0,1,...,N-1 A2=x[n-(N-1)+i] i=0,1,...,N-1

LDH *B4- -,B2 ;B2=h[N-1-i] i=0,1,...,N-1LDH *B4- -,B2 ;B2=h[N-1-i] i=0,1,...,N-1

NOP 4 NOP 4

MPY A2,B2,A6 ;A6=x[n-(N-1)+i]*h[N-1-i] MPY A2,B2,A6 ;A6=x[n-(N-1)+i]*h[N-1-i]

NOP NOP

ADD A6,A8,A8 ;accumlate in A8 ADD A6,A8,A8 ;accumlate in A8

LDH *A4,A7 ;LDH *A4,A7 ;A7=x[(n-(N-1)+i+1]update delaysA7=x[(n-(N-1)+i+1]update delays NOP 4 ;using data move "up" NOP 4 ;using data move "up"

STH A7,*-A4[1] STH A7,*-A4[1] ;->x[(n-(N-1)+i] update sample;->x[(n-(N-1)+i] update sample

SUB A1,1,A1 ;decrement loop countSUB A1,1,A1 ;decrement loop count

[A1] [A1] B loop ;branch to loop if count # 0 B loop ;branch to loop if count # 0

NOP 5 NOP 5

MV A8,A4 ;result returned in A4 MV A8,A4 ;result returned in A4

B B3 B B3 ;return addr to calling routine ;return addr to calling routine

NOP 4 NOP 4

If we take four coeffs h[0] to h[4] then above If we take four coeffs h[0] to h[4] then above equation becomes equation becomes

Y[3]=h[0]x[3] +h[1]x[2]+h[2]x[1]+h[3]x[0]Y[3]=h[0]x[3] +h[1]x[2]+h[2]x[1]+h[3]x[0]

Y[4]=h[0]x[4] +h[1]x[3]+h[2]x[2]+h[3]x[1]Y[4]=h[0]x[4] +h[1]x[3]+h[2]x[2]+h[3]x[1]

Y[5]=h[0]x[5] +h[1]x[4]+h[2]x[3]+h[3]x[2]Y[5]=h[0]x[5] +h[1]x[4]+h[2]x[3]+h[3]x[2]

1

0

][N

k

knxkhny

Two buffers are created dly for data Two buffers are created dly for data samples and h for coefficients.samples and h for coefficients.

On each interrupt, a new data sample is On each interrupt, a new data sample is acquired and stored at the end (higher-acquired and stored at the end (higher-memory address) of the buffer dly.memory address) of the buffer dly.

The delay samples are stored in memory The delay samples are stored in memory starting with the oldest samplestarting with the oldest sample

The newest sample is at the end of the The newest sample is at the end of the bufferbuffer

The coefficients are arranged in memory with The coefficients are arranged in memory with h(0) at the beginning of the coefficient buffer and h(0) at the beginning of the coefficient buffer and h(N - 1) at the end.h(N - 1) at the end.

The addresses of the delay sample buffer, the The addresses of the delay sample buffer, the filter coefficient buffer, and the sizeof each buffer filter coefficient buffer, and the sizeof each buffer are passed to the ASM function through are passed to the ASM function through registers A4, B4, and A6, respectively.registers A4, B4, and A6, respectively.

The size of each buffer through register A6 is The size of each buffer through register A6 is doubled since data in each memory location are doubled since data in each memory location are stored as bytes.stored as bytes.

the pointers A4 and B4 are incremented or the pointers A4 and B4 are incremented or decremented every two bytes (two decremented every two bytes (two memory locations)memory locations)

Y(n)=h(N-1)x(n-(N-1))+h(N-2)x(n-(N-2)) + Y(n)=h(N-1)x(n-(N-1))+h(N-2)x(n-(N-2)) + ………..+h(1)x(n-1)+h(0)x(n).………..+h(1)x(n-1)+h(0)x(n).

Thank youThank you