comparison Meerwald/gen_corvi_sig.c @ 0:be303a3f5ea8

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

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