# filter realization help

##### Banned
I need a help with filter realization. I dont understand how this works. This is a multiply and accumulate filter, I dont understand why cof[0] constant is multiplied, here is the code cof is the filter coefficients and sample is the samples. Any help with this very appreciated. Thank you

Code:
long long mac(int len, int *sample, int *cof, int item)

{

int i, io;

static long long y;

io = 0;

y = 0;

item /= 4;

for ( i = 0; i < len; i++ )

{

y += (long long)sample[io] * (long long)cof[0];

cof +=item;

io++;

if ( io >= len )

io = 0;

}

return y;

}

#### KlausST

##### Super Moderator
Staff member
Hi,

it seems to be a standard FIR filter function of varaible length.

Some questions arise:
* Do you know how a FIR filter works?
* Do you know how to find the filter coefficients?
* Where is the code from?
* Isn´t there a documentation for the code?

I dont understand why cof[0] constant is multiplied
Because this is how a FIR filter works.

But the index should move as well as the index for the sample.

Klaus

Now I realize it is some kind of special FIR filter. It seems to work interleaved somehow. So the above 4 questions are still valid.
..
A again I reviewed the code. I don´t think it is correct. "i" and "io" have the same value, thus "io" is redundant. Without further informations I think sample[] and cof[] should have the same index.

points: 2

##### Banned
Hi ,

Thank you for the reply. Very much appreciated.

Some questions arise:
* Do you know how a FIR filter works?
* Do you know how to find the filter coefficients?
* Where is the code from?
* Isn´t there a documentation for the code?
I understand the FIR filter works. Also I understand how to find the filter coefficients.I am working on a university project so I found this code through friends. There is no documentation for the code. Yes thats right index should also move for the coefficients but I dont understand why cof[0] just multiplied. Is it not going to be just a constant multiplication everytime it go through the loop.

#### KlausST

##### Super Moderator
Staff member
Hi,

code without documentation, maybe somewhere found in the internet....

My recommendation: throw it away .. and write the couple lines of code on your own -- since you already know how it should work.

Klaus

#### _Eduardo_

##### Full Member level 5
... but I dont understand why cof[0] just multiplied. Is it not going to be just a constant multiplication everytime it go through the loop.
'cof[0]' is not a constant, 'cof' is incremented by 'item' each iteration.

(long)cof[0] equiv *(long*)cof

### FvM

points: 2

points: 2

points: 2

##### Banned
Digital Filter help need to understand

I need a help to understand this filter. I understand how the FIR filter works. But I dont understand why mac function called twice. Any help with this very appreciated. Thank you

Code:
struct __attribute__ ((aligned (32)))
{ int *samplebuf[chan];
int *fcs[chan];                           // Filter Coefficient Start
int  len[chan];
int  offset[chan];
int  dec[chan];
int  decount[chan];
int  item[chan];                             // Skip coefficient Items
}f;

int get_sa(int chan)
{
static long long y;
int  l1;
int *sample;
int *cof;
int  len;
int  item,item4;
int *samplebuf;

samplebuf = f.samplebuf[chan];
if ( f.dec[chan] == 0 )
return samplebuf[0];
len = f.len[chan];
item = f.item[chan];
item4 = item << 2;
cof = f.fcs[chan];
sample = samplebuf + f.Offset[chan];
l1 = len - f.Offset[chan];
y = mac(l1,sample,cof,item);   //multiply and accumulate filter
cof += (l1 * item);
sample = samplebuf;
l1 = len - l1;
y += mac(l1,sample,cof,item4);
y *= item;
y >>= 32;
return (int)y;

}
mac function shown below,

long long mac(int len, int *sample, int *cof, int item)
{
int i, io;

static long long y;

io = 0;

y = 0;

item /= 4;

for ( i = 0; i < len; i++ )

{

y += (long long)sample[io] * (long long)cof[0];

cof +=item;

io++;

if ( io >= len )

io = 0;

}

return y;

}

#### KlausST

##### Super Moderator
Staff member
Hi,

you cross post the same question on several other forums.
All ask for more information: where is it from, where is the documentation, what you want to do with it, what are the input data....

But almost no information from your side.

How can we help?

Klaus