Digital Signal Processing Reference
In-Depth Information
Equation (7.45) can also be expressed as a sum of the real and imaginary
portions of the exponential as in (7.46). The Calc_DigFIR_Resp function
implements (7.46) directly, as shown in Listing 7.1.
N
1
N
1
j
H
(
e
)
=
h
(
k
)
cos(
k
)
+
j
h
(
k
)
sin(
k
)
k
=
0
k
=
0
(7.46)
/*====================================================
Calc_DigFIR_Resp() - calcs response for FIR filters
Prototype: int Calc_DigFIR_Resp(Filt_Params *FP,
Resp_Params *RP);
Return: error value
Arguments: FP - ptr to struct holding filter params
RP - ptr to struct holding respon params
====================================================*/
int Calc_DigFIR_Resp(Filt_Params *FP,Resp_Params *RP)
{ int f,i; /* loop counters */
double rad2deg, /* rad to deg conversion */
omega,i_omega,/* radian freq and incrmnt */
mag, /* magnitude of freq resp */
rea,img; /* real and imag part */
rad2deg = 180.0 / PI; /* set rad2deg */
/* Loop through each of the frequencies */
for(f = 0 ;f < RP->tot_pts; f++)
{ /* Initialize magna and angle */
RP->magna[f] = FP->gain;
RP->angle[f] = 0.0;
/* Pre calc adjusted omega, rea and img */
omega = PI2 * RP->freq[f] / FP->fsamp;
rea = 0.0; img = 0.0;
/* Loop through all the coefs */
for(i = 0 ;i < FP->order; i++)
{ i_omega = i * omega;
rea += FP->acoefs[i] * cos(i_omega);
img += FP->acoefs[i] * sin(i_omega);
}
/* Calc final result and conv to degrees */
mag = sqrt(rea*rea + img*img);
RP->magna[f] *= mag;
/* Guard against atan(0,0) */
if(mag > 0)
{ RP->angle[f] += atan2(img,rea);}
RP->angle[f] *= rad2deg;
}
/* Convert magnitude response to dB if indicated */
if(RP->mag_axis == LOG)
{ for(f = 0 ;f < RP->tot_pts; f++)
{ /* Handle very small numbers */
if(RP->magna[f] < ZERO)
{ RP->magna[f] = ZERO;}
RP->magna[f] = 20 * log10(RP->magna[f]);
}
}
return ERR_NONE;
}
Listing 7.1 Calc_DigFIR_Resp function.
Search WWH ::




Custom Search