Mercurial > ~darius > hgwebdir.cgi > modulator
comparison test.c @ 4:b6416c4aadc8
Test implementation
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Tue, 04 Feb 2025 14:20:07 +1030 |
parents | |
children | 2db42eaba3c8 |
comparison
equal
deleted
inserted
replaced
3:b10097c3383d | 4:b6416c4aadc8 |
---|---|
1 #include <sys/errno.h> | |
2 #include <stdio.h> | |
3 #include <stdint.h> | |
4 #include <stdlib.h> | |
5 #include <string.h> | |
6 | |
7 #include <q.h> | |
8 | |
9 #include "shaped-trap.h" | |
10 | |
11 int | |
12 main(int argc, char **argv) { | |
13 uint8_t data[50000], shapelen, codegap, slew1, slew2, dcofs; | |
14 uint16_t plen, ncode; | |
15 uint32_t shapesamples, nsamples, idx; | |
16 q_t dcscale, stepsize; | |
17 char *code, tmps[20]; | |
18 | |
19 if (argc != 7) { | |
20 printf("Bad usage\n"); | |
21 exit(1); | |
22 } | |
23 plen = atoi(argv[1]); | |
24 code = argv[2]; | |
25 ncode = strlen(code); | |
26 shapelen = sizeof(shaped_trap); | |
27 codegap = atoi(argv[3]); | |
28 slew1 = atoi(argv[4]); | |
29 slew2 = atoi(argv[5]); | |
30 dcofs = atoi(argv[6]); | |
31 | |
32 dcscale = qdiv(qsub(qint(255), qint(dcofs)), qint(255)); | |
33 qsprint(dcscale, tmps, sizeof(tmps)); | |
34 printf("dcscale = %s\n", tmps); | |
35 | |
36 if (ncode == 1) { | |
37 // Number of samples for half of the pulse | |
38 // Can't use q library here because it is Q16.16 so not large enough | |
39 shapesamples = (plen * shapelen) / 100; | |
40 // Number of samples for everything | |
41 nsamples = shapesamples * 2 + slew1 + slew2; | |
42 } else { | |
43 shapesamples = plen; | |
44 nsamples = shapesamples * 2 * ncode + codegap * (ncode - 1) + slew1 + slew2; | |
45 } | |
46 | |
47 if (nsamples > sizeof(data)) { | |
48 printf("Pulse too long (%d > %lu)\n", nsamples, sizeof(data)); | |
49 exit(1); | |
50 } | |
51 | |
52 // Number of samples per step in the pulse shape | |
53 stepsize = qdiv(qint(shapesamples), qint(shapelen)); | |
54 qsprint(stepsize, tmps, sizeof(tmps)); | |
55 printf("shapelen = %d shapesamples = %u nsamples = %u stepsize = %s\n", shapelen, shapesamples, nsamples, tmps); | |
56 | |
57 if (shapesamples < 2) { | |
58 printf("Pulse too short (%u < %d)\n", shapesamples, 2); | |
59 exit(1); | |
60 } | |
61 | |
62 idx = 0; | |
63 | |
64 // Up slew | |
65 for (uint16_t i = 0; i < slew1; i++) | |
66 data[idx++] = qtoi(qdiv(qmul(qint(dcofs), qint(i)), qint(slew1))); | |
67 | |
68 for (uint16_t c = 0; c < ncode; c++) { | |
69 // Pulse up | |
70 for (uint16_t i = 0; i < shapesamples; i++) | |
71 data[idx++] = dcofs + qtoi(qmul(qint(shaped_trap[qtoi(qdiv(qint(i), stepsize))]), dcscale)); | |
72 | |
73 // Pulse down | |
74 for (uint16_t i = 0; i < shapesamples; i++) | |
75 data[idx++] = dcofs + qtoi(qmul(qint(shaped_trap[qtoi(qdiv(qint(shapesamples - i - 1), stepsize))]), dcscale)); | |
76 | |
77 // Code gap | |
78 if (c < ncode - 1) | |
79 for (uint16_t i = 0; i < codegap; i++) | |
80 data[idx++] = dcofs; | |
81 } | |
82 | |
83 // Down slew | |
84 for (uint16_t i = 0; i < slew2; i++) | |
85 data[idx++] = qtoi(qdiv(qmul(qint(dcofs), qint(slew2 - i)), qint(slew2))); | |
86 | |
87 FILE *fh; | |
88 | |
89 if ((fh = fopen("/tmp/out.bin", "w")) == NULL) { | |
90 printf("Unable to open file: %s\n", strerror(errno)); | |
91 exit(1); | |
92 } | |
93 fwrite(data, idx, 1, fh); | |
94 fclose(fh); | |
95 | |
96 exit(0); | |
97 } |