# filter realization help

Joined May 12, 2020
6
I need a help with filter realization. I dont understand how this works. This is a multiply and accumulate filter, I dont understand why cop[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;

}
I dont understand why cof[0] constant is multiplied. what is the purpose?

#### MrAl

Joined Jun 17, 2014
7,594
I need a help with filter realization. I dont understand how this works. This is a multiply and accumulate filter, I dont understand why cop[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;

}
cof[] must be the coefficient array you would have for any 'filter' or equation like a,b,c in:
y=a*x^2+b*x+c

What is this filter used for?

Joined May 12, 2020
6
Thank you for the reply. This FIR filter used with decimation. I also dont understand why item /= 4 like this?

#### Papabravo

Joined Feb 24, 2006
13,741
cof[0] is not a constant. cof is declared as a pointer to integers. cof[0] is where the pointer points when it is passed into the function. After it is used, after being cast as a pointer to (long long) it is incremented by the value of item. The way this code is written looks kinda sketchy, but it looks like it is stepping through an array of coefficients defined as integers, but used as long longs in computation.

Joined May 12, 2020
6
Thank you for the reply. This is a decimating filter. Thank you very much about the explanation about cof[0].It is clear for me. I have a another question. This is also part of code and related. I dont understand how this works. I dont understand why mac function called twice.

Code:
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;

}

#### MrAl

Joined Jun 17, 2014
7,594
Thank you for the reply. This is a decimating filter. Thank you very much about the explanation about cof[0].It is clear for me. I have a another question. This is also part of code and related. I dont understand how this works. I dont understand why mac function called twice.

Code:
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;

}
It would help if you could describe what the arrays are holding, such as fcs, item, etc.

Joined May 12, 2020
6
It would help if you could describe what the arrays are holding, such as fcs, item, etc.
This is the struct
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;
fcs array holds the filter coefficents. item array holds the coefficient items to skip. dec array holds decimate rate. decount array holds the decimate rate.

#### MrAl

Joined Jun 17, 2014
7,594
This is f***### wrong code Evertthing is messed up ?
I dont think you should use language like that in this forum it may be against the TOS.

I would be willing to help but i can not yet tell what all the variables are for.
If you could annotate the code a little bit better describing what everything is supposed to do that would help myself an others to understand what the code is supposed to do
Once i get the full info i can probably help with this, the filter code itself is fairly simple.
Also, i guess this is a DFT algorithm (using DIT)?

Joined May 12, 2020
6
I dont think you should use language like that in this forum it may be against the TOS.

I would be willing to help but i can not yet tell what all the variables are for.
If you could annotate the code a little bit better describing what everything is supposed to do that would help myself an others to understand what the code is supposed to do
Once i get the full info i can probably help with this, the filter code itself is fairly simple.
Also, i guess this is a DFT algorithm (using DIT)?
Hi, Please refer to the above my post . I described the variables, what are they for.

Joined May 12, 2020
6
Hi Could you please remove the whole thread. It is useless. Whole thread is wrong. It is confusing. Thank you