annotate fir/freqresp.c @ 376:9b3e5be96bab

fir2freq: a tool for analyzing captured FIR coefficient sets
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 02 Aug 2021 04:59:46 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
376
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This program computes the frequency response of a Calypso DSP FIR filter
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * whose coefficients have been captured somewhere out in the wild.
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * The math has been taken from section 2.2.2 of this article:
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 *
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 * https://dspguru.com/dsp/faqs/fir/properties/
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 */
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <math.h>
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdio.h>
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdlib.h>
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #define NCOEFF 31
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 int coeff_int[NCOEFF];
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 float coeff_float[NCOEFF];
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 unsigned nsteps;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 float freq_step, omega_step;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 float freq, omega;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 static void
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 coeff_to_float()
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 {
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 unsigned n;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 for (n = 0; n < NCOEFF; n++)
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 coeff_float[n] = coeff_int[n] / 16384.0f;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 }
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 static void
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 do_one_freq()
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 {
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 unsigned n;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 float angle_arg, rsum, isum;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 float gain, db;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 angle_arg = 0;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 rsum = 0;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 isum = 0;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 for (n = 0; n < NCOEFF; n++) {
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 rsum += coeff_float[n] * cosf(angle_arg);
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 isum -= coeff_float[n] * sinf(angle_arg);
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 angle_arg += omega;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 }
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 gain = hypotf(rsum, isum);
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 db = log10f(gain) * 20.0f;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 printf("%.2f\t%f\t%.2f\n", freq, gain, db);
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 }
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 main(argc, argv)
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 char **argv;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 {
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 unsigned n;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 if (argc != 3) {
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 fprintf(stderr, "usage: %s coeff-file nsteps\n", argv[0]);
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 exit(1);
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 }
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 if (read_fir_coeff_table_int(argv[1], coeff_int) < 0)
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 exit(1); /* error msg already printed */
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 coeff_to_float();
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 nsteps = strtoul(argv[2], 0, 0);
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 if (nsteps < 1) {
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 fprintf(stderr, "error: nsteps argument must be >= 1\n");
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 exit(1);
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 }
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 freq_step = 4000.0f / nsteps;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 omega_step = M_PI / nsteps;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 n = 0;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 freq = 0;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 omega = 0;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 for (;;) {
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 do_one_freq();
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 if (n >= nsteps)
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 break;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 n++;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 freq += freq_step;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 omega += omega_step;
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 }
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 exit(0);
9b3e5be96bab fir2freq: a tool for analyzing captured FIR coefficient sets
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 }