home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Enigma Amiga Life 109
/
EnigmaAmiga109CD.iso
/
software
/
musica
/
resample
/
src
/
rtest.c
< prev
Wrap
C/C++ Source or Header
|
1999-10-31
|
3KB
|
98 lines
/*************************************************************************\
"rtest.c": test program for the Remez Exchange algorithm ("remez.c"),
creates a low pass FIR filter
With option '-p', a gnuplot file of the frequency response is generated.
\*************************************************************************/
#include <math.h>
#include <stdio.h>
#include "remez.h"
float fpass, fstop; /* describe a low pass filter */
float stopwt; /* extra weight for the stop band */
extern double grid[];
extern int iext[]; /* extremal frquencies */
int main( int argc, char* argv[] )
{
int n, j, plot_it;
float fr, pi, sum, err;
char filename[] = "filter.dat";
FILE *outfile;
plot_it = (argc>1 && *argv[1] == '-' && *(argv[1]+1) == 'p');
/* input and setup section */
printf("filter length (max. %d): ",NFMAX); scanf("%d", &n);
printf("pass and stop frequency (0 < fp < fs < 0.5): ");
scanf("%f %f", &fpass, &fstop);
printf("extra weight for errors in the stop band (>= 1): ");
scanf("%f", &stopwt);
remez_talk = 1;
if (makefilter(n) > REMEZ_UNSURE)
return 10;
/* output section */
printf("FIR filter designed by remez exchange algorithm.\n");
printf("filter length = %d\n", n);
for (j = 0; j<n; j++)
printf("%11.8f # h(%2d)\n", h[j], j+1);
if (plot_it)
outfile = fopen(filename, "w");
else
outfile = NULL;
if (outfile != NULL)
{
printf("writing frequency response to file '%s'\n", filename);
pi = 4 * atan(1);
for (fr = 0; fr < 0.5; fr += 0.002)
{
sum = 0;
for (j = 0; j<n; j++)
sum += h[j] * cos(pi*fr * (n-1-2*j));
fprintf(outfile, "%f %e \n", fr, fabs(sum));
}
fclose(outfile);
}
printf("extremal frequencies:\n");
for (j = 0; 2*j < n; j++)
{
fr = grid[iext[j]];
sum = extreme(fr);
err = sum - desire(fr);
printf("%.3f: %8.5f, error: %8.5f (%.0f dB)\n",
fr, sum, err, 20*log10(fabs(err)));
}
}
float desire(float freq)
/* Calculates the desired magnitude response as a function of frequency,
* in this implementation that of a low pass.
* Note that the parameter is the normalized frequency, where a value of
* 1.0 denotes the sampling rate (i. e. only values 0..0.5 will occur).
*/
{
if (freq > fstop)
return 0;
else
return 1;
}
float weight(float freq)
/* Calculates the weight function, i. e. how strict are we to follow the
* magnitude response specified by eff()? It is a good idea to put more
* weight on stop bands than on pass bands, and there had really better
* be transition bands (with a weight of zero) between them.
*/
{
if (freq > fstop)
return stopwt;
else if (freq > fpass)
return 0;
else
return 1;
}