source: buchla-68k/orig/BUCHLA/HTGEN.C@ 5f1d169

Last change on this file since 5f1d169 was 3ae31e9, checked in by Thomas Lopatic <thomas@…>, 7 years ago

Imported original source code.

  • Property mode set to 100755
File size: 6.3 KB
RevLine 
[3ae31e9]1/*
2 =============================================================================
3 htgen.c -- Harmonic table file generator for MIDAS-VII
4 Written by: D.N. Lynx Crowe
5
6 See VERSION, below, for version number and date.
7
8 This program creates a .h file to be included as harmonic
9 coefficient tables for MIDAS-VII.
10
11 WIDTH is the number of points in the table
12 CENTER is the center point in the table
13 N is the number of points scanned on either side of CENTER
14 K and L are the lower and upper limits of the scan, respectively
15 PER is the number of values generated per line
16
17 HARM is the number of harmonics generated
18 i is the harmonic number (1 is the fundamental)
19 j is the point number, K LE j LE L
20 =============================================================================
21*/
22
23#include "stdio.h"
24#include "math.h"
25#include "stddefs.h"
26
27#define PGMNAME "htgen"
28#define VERSION "Version 2 -- 1988-09-20"
29
30#define FILENAME "knmtab.h" /* output file name */
31#define TABNAME "knmtab" /* table name */
32
33#define WIDTH 256 /* table width */
34#define RANGE 1023 /* maximum value */
35#define HARM 32 /* number of harmonics */
36
37#define N 115 /* number of points scanned */
38#define PER 16 /* values per line */
39
40#define CENTER ((WIDTH/2)-1) /* center of the table */
41#define K (CENTER-N) /* first point scanned */
42#define L (CENTER+N) /* last point scanned */
43
44#define M_PI 3.14159265358979323846
45#define M_HALFPI (M_PI / 2.0)
46
47#define ACOSFN(x) atan(sqrt(1.0 - (x * x)) / x)
48
49extern char *now(); /* get current date-time group */
50extern char *strcat(); /* append a string onto another */
51extern char *strcpy(); /* copy a string into another */
52
53/*
54
55*/
56
57/* variables */
58
59double a; /* unscaled harmonic coefficient */
60double c; /* scaled harmonic coefficient */
61double y; /* intermediate coefficient value */
62
63double ltend; /* left endpoint value */
64double rtend; /* right endpoint value */
65
66double hartab[WIDTH]; /* the current harmonic table */
67
68int nol; /* number of values on the line so far */
69
70FILE *fp; /* output file pointer */
71
72char bar[81]; /* bar of equal signs */
73char dtg[32]; /* date-time group */
74char intc[32]; /* value output work area */
75char result[128]; /* line output work area */
76
77/*
78
79*/
80
81/*
82 =============================================================================
83 CalcArc() -- Calculate the scaled Arc Cosine of 'arg'
84 =============================================================================
85*/
86
87double
88CalcArc(arg)
89double arg;
90{
91 double y;
92
93 if (arg EQ 0.0) /* Return PI/2 if arg = 0.0 */
94 return((double)M_HALFPI);
95
96 y = ACOSFN(arg);
97
98 if (arg < 0.0) /* Adjust by PI if arg was negative */
99 y = y + (double)M_PI;
100
101 return(y);
102}
103
104/*
105 =============================================================================
106 DoOut() -- Output result string
107 =============================================================================
108*/
109
110DoOut(j, k)
111int j, k;
112{
113 if (j EQ (WIDTH - 1)) { /* end of line ? */
114
115 if (k EQ HARM) /* last table ? */
116 fprintf(fp, "\t%s }\t/* %3d */\n", result, j);
117 else
118 fprintf(fp, "\t%s },\t/* %3d */\n", result, j);
119
120 } else {
121
122 fprintf(fp, "\t%s,\t/* %3d */\n", result, j);
123 }
124
125 result[0] = '\0';
126 nol = 0;
127}
128
129/*
130
131*/
132
133main()
134{
135 register int c, i, j;
136
137 printf("\n%s -- %s -- run on ",
138 PGMNAME, VERSION);
139
140 now(dtg);
141
142 printf("%10.10s at %s\n\n",
143 dtg, &dtg[12]);
144
145 for (c = 0; c < 3; c++)
146 bar[c] = ' ';
147
148 for (; c < 80; c++)
149 bar[c] = '=';
150
151 bar[80] = '\0';
152
153 printf("Generating coefficient tables for %d harmonics\n\n", HARM);
154
155 printf("Full scale range = %d\n", RANGE);
156 printf("Width of table = %d\n", WIDTH);
157 printf("Center of table = %d\n", CENTER);
158 printf("Scan width (N) = %d\n", N);
159 printf("Points scanned = %d thru %d\n\n", K, L);
160
161 printf("Creating file \"%s\"\n", FILENAME);
162
163 if ((FILE *)NULL EQ (fp = fopen(FILENAME, "w"))) {
164
165 printf("ERROR: unable to open \"%s\" for writing\n", FILENAME);
166 exit(1);
167 }
168
169 printf("\n");
170
171 fprintf(fp, "/*\n%s\n\t %s -- MIDAS-VII Harmonic coefficient tables\n",
172 bar, FILENAME);
173
174 fprintf(fp, "\tCalculated %10.10s -- %s\n%s\n*/\n\n",
175 dtg, &dtg[12], bar);
176
177 fprintf(fp, "/*\n%s\n", bar);
178 fprintf(fp, "\tCalculated by htgen.c -- %s\n", VERSION);
179 fprintf(fp, "\tfor MIDAS-VII using:\n");
180 fprintf(fp, "\n");
181 fprintf(fp, "\t\tFull scale range = %d\n", RANGE);
182 fprintf(fp, "\n");
183 fprintf(fp, "\t\tWidth of table = %d\n", WIDTH);
184 fprintf(fp, "\t\tCenter of table = %d\n", CENTER);
185 fprintf(fp, "\t\tScan width (N) = %d\n", N);
186 fprintf(fp, "\t\tPoints scanned = %d thru %d\n", K, L);
187 fprintf(fp, "%s\n*/\n\n", bar);
188
189 fprintf(fp, "short\t%s[%d][%d] = {\n", TABNAME, HARM, WIDTH);
190 fprintf(fp, "\n");
191
192/*
193
194*/
195 for (i = 1; i < (HARM + 1); i++) {
196
197 printf("Calculating harmonic %d ...\r", i);
198
199 for (j = K; j < (L + 1); j++) {
200
201 y = CalcArc((double)(j - CENTER) / (double)N);
202 a = cos((double)i * y);
203 hartab[j] = ((double)RANGE * a) + 0.5;
204 }
205
206 ltend = hartab[K];
207 rtend = hartab[L];
208
209 printf("Outputting harmonic %d ...\r", i);
210
211 if (i & 1) {
212
213 fprintf(fp, "/* \f\n");
214 fprintf(fp, "*/\n");
215 }
216
217 fprintf(fp, "\n");
218 fprintf(fp, "/* Harmonic table # %d */\n", i);
219 fprintf(fp, "\n");
220
221 nol = 1;
222 result[0] = '\0';
223 sprintf(intc, "%5d", (int)ltend);
224
225 for (j = 0; j < K; j++) {
226
227 if (nol EQ 1) {
228
229 if (j EQ 0)
230 strcat(strcat(result, "{"), intc);
231 else
232 strcat(strcat(result, " "), intc);
233
234 } else {
235
236 strcat(strcat(result, ","), intc);
237 }
238
239 if (nol EQ PER)
240 DoOut(j, i);
241
242 ++nol;
243 }
244/*
245
246*/
247 for (; j < (L + 1); j++) {
248
249 sprintf(intc, "%5d", (int)hartab[j]);
250
251 if (nol EQ 1)
252 strcat(strcat(result, " "), intc);
253 else
254 strcat(strcat(result, ","), intc);
255
256 if (nol EQ PER)
257 DoOut(j, i);
258
259 ++nol;
260 }
261
262 sprintf(intc, "%5d", (int)rtend);
263
264 for (; j < WIDTH; j++) {
265
266 if (nol EQ 1)
267 strcat(strcat(result, " "), intc);
268 else
269 strcat(strcat(result, ","), intc);
270
271 if (nol EQ PER)
272 DoOut(j, i);
273
274 ++nol;
275 }
276
277 if (nol NE 1)
278 DoOut(j - 1, i);
279 }
280
281 fprintf(fp, "};\n");
282 fclose(fp);
283
284 printf("Output complete. End of program.\n");
285 exit(0);
286}
Note: See TracBrowser for help on using the repository browser.