annotate Meerwald/gen_corvi_sig.c @ 5:8191b855cd5e

Added tag v0.5 for changeset 9cf6ec970448
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Tue, 14 Aug 2007 21:13:29 +0200
parents be303a3f5ea8
children f83ef905a63d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
1 #include "wm.h"
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
2
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
3 char *progname;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
4
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
5 void usage(void) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
6 fprintf(stderr, "usage: %s [-a n] [-d n] [-e n] [-f n] [-F file] [-m n] [-n n] [-o file] [-s n]\n\n", progname);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
7 fprintf(stderr, "\t-a n\t\talpha factor (default 0.1)\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
8 fprintf(stderr, "\t-d n\t\tdeviation (default 1.0)\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
9 fprintf(stderr, "\t-e n\t\twavelet filtering method (default 2)\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
10 fprintf(stderr, "\t-f n\t\tfilter number (default 1)\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
11 fprintf(stderr, "\t-F file\t\tfilter definition file (default 'filter.dat')\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
12 fprintf(stderr, "\t-h\t\tprint usage\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
13 fprintf(stderr, "\t-m n \t\tmean value (default 0.0)\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
14 fprintf(stderr, "\t-n n\t\twatermark length (default 1000)\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
15 fprintf(stderr, "\t-o file\t\toutput file\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
16 fprintf(stderr, "\t-s n\t\tseed\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
17 exit(0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
18 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
19
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
20 int main(int argc, char *argv[]) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
21 FILE *out = stdout;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
22
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
23 char output_name[MAXPATHLEN] = "(stdout)";
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
24
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
25 int c;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
26 int i;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
27 int n = 1000;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
28 int s = 0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
29 int e = 2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
30 int f = 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
31 char F[MAXPATHLEN] = "filter.dat";
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
32 double a = 0.1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
33 double m = 0.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
34 double d = 1.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
35
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
36 progname = argv[0];
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
37
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
38 while ((c = getopt(argc, argv, "a:d:e:f:F:h?m:n:o:s:")) != EOF) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
39 switch (c) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
40 case 'a':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
41 a = atof(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
42 if (a <= 0.0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
43 fprintf(stderr, "%s: alpha factor %f out of range\n", progname, a);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
44 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
45 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
46 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
47 case 'd':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
48 d = atof(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
49 if (d <= 0.0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
50 fprintf(stderr, "%s: deviation %f out of range\n", progname, d);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
51 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
52 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
53 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
54 case 'e':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
55 e = atoi(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
56 if (e < 0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
57 fprintf(stderr, "%s: wavelet filtering method %d out of range\n", progname, e);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
58 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
59 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
60 case 'f':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
61 f = atoi(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
62 if (f <= 0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
63 fprintf(stderr, "%s: filter number %d out of range\n", progname, f);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
64 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
65 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
66 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
67 case 'F':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
68 strcpy(F, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
69 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
70 case 'h':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
71 case '?':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
72 usage();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
73 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
74 case 'm':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
75 m = atof(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
76 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
77 case 'n':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
78 n = atoi(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
79 if (n < 1 || n > 32000) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
80 fprintf(stderr, "%s: watermark length %d out of range\n", progname, n);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
81 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
82 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
83 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
84 case 'o':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
85 if ((out = fopen(optarg, "w")) == NULL) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
86 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
87 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
88 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
89 strcpy(output_name, optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
90 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
91 case 's':
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
92 s = atoi(optarg);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
93 break;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
94 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
95 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
96
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
97 argc -= optind;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
98 argv += optind;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
99
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
100 if (argc > 0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
101 usage();
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
102 exit(1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
103 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
104
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
105 if (s)
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
106 srandom(s);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
107 else
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
108 srandom(time(NULL) * getpid());
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
109
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
110 fprintf(out, "CVSG\n");
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
111 fprintf(out, "%d\n", n);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
112 fprintf(out, "%f\n", a);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
113 fprintf(out, "%d\n", e);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
114 fprintf(out, "%d\n", f);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
115 fprintf(out, "%s\n", F);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
116
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
117 n >>= 1;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
118 while (n > 0) {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
119 double x;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
120 double x1, x2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
121
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
122 /*
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
123 * Algorithm P (Polar method for normal deviates),
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
124 * Knuth, D., "The Art of Computer Programming", Vol. 2, 3rd Edition, p. 122
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
125 */
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
126 do {
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
127 x1 = 2.0 * ((random() & RAND_MAX) / ((double) RAND_MAX + 1.0)) - 1.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
128 x2 = 2.0 * ((random() & RAND_MAX) / ((double) RAND_MAX + 1.0)) - 1.0;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
129 x = x1 * x1 + x2 * x2;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
130 } while (x >= 1.0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
131 x1 *= sqrt((-2.0) * log(x) / x);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
132 x2 *= sqrt((-2.0) * log(x) / x);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
133
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
134 fprintf(out, "%f\n", m + d * x1);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
135 fprintf(out, "%f\n", m + d * x2);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
136
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
137 n--;
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
138 }
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
139
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
140 fclose(out);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
141
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
142 exit(0);
Peter Meerwald <pmeerw@cosy.sbg.ac.at>
parents:
diff changeset
143 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.