Mercurial > hg > wm
comparison Meerwald/cmp_kund3_sig.c @ 3:acb6967ee76d
update to 0.5
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Tue, 14 Aug 2007 21:11:21 +0200 |
parents | |
children | f83ef905a63d |
comparison
equal
deleted
inserted
replaced
2:b92f06d9a967 | 3:acb6967ee76d |
---|---|
1 #include "wm.h" | |
2 #include "signature.h" | |
3 | |
4 char *progname; | |
5 | |
6 void usage(void) { | |
7 fprintf(stderr, "usage: %s [-h] [-C] [-o file] [-v n] -s file file\n\n", progname); | |
8 fprintf(stderr, "\t-C\t\toutput correlation only\n"); | |
9 fprintf(stderr, "\t-h\t\tprint usage\n"); | |
10 fprintf(stderr, "\t-o file\t\toutput file\n"); | |
11 fprintf(stderr, "\t-s file\t\toriginal signature file\n"); | |
12 fprintf(stderr, "\t-v n\t\tverbosity level\n"); | |
13 exit(0); | |
14 } | |
15 | |
16 int main(int argc, char *argv[]) { | |
17 | |
18 FILE *in = stdin; | |
19 FILE *out = stdout; | |
20 FILE *sig = NULL; | |
21 | |
22 char signature_name[MAXPATHLEN]; | |
23 char output_name[MAXPATHLEN] = "(stdout)"; | |
24 char input_name[MAXPATHLEN] = "(stdin)"; | |
25 | |
26 char *binstr1; | |
27 char *binstr2; | |
28 | |
29 int correlation_only = 0; | |
30 | |
31 int c, i; | |
32 int quality = 0; | |
33 int corr = 0, match = 0; | |
34 int verbose = 0; | |
35 int filter = 0; | |
36 int method = 0; | |
37 int level = 0; | |
38 char filter_name[MAXPATHLEN] = ""; | |
39 | |
40 int seed; | |
41 char line[32]; | |
42 | |
43 progname = argv[0]; | |
44 | |
45 while ((c = getopt(argc, argv, "h?Co:s:v:")) != EOF) { | |
46 switch (c) { | |
47 case 'h': | |
48 case '?': | |
49 usage(); | |
50 break; | |
51 case 'C': | |
52 correlation_only = 1; | |
53 break; | |
54 case 'o': | |
55 if ((out = fopen(optarg, "w")) == NULL) { | |
56 fprintf(stderr, "%s: unable to open output file %s\n", progname, optarg); | |
57 exit(1); | |
58 } | |
59 strcpy(output_name, optarg); | |
60 break; | |
61 case 's': | |
62 if ((sig = fopen(optarg, "r")) == NULL) { | |
63 fprintf(stderr, "%s: unable to open signature file %s\n", progname, optarg); | |
64 exit(1); | |
65 } | |
66 strcpy(signature_name, optarg); | |
67 break; | |
68 case 'v': | |
69 verbose = atoi(optarg); | |
70 if (verbose < 0) { | |
71 fprintf(stderr, "%s: verbosity level %d out of range\n", progname, verbose); | |
72 exit(1); | |
73 } | |
74 break; | |
75 } | |
76 } | |
77 | |
78 argc -= optind; | |
79 argv += optind; | |
80 | |
81 if (argc > 1) { | |
82 usage(); | |
83 exit(1); | |
84 } | |
85 | |
86 if (argc == 1 && *argv[0] != '-') | |
87 if ((in = fopen(argv[0], "r")) == NULL) { | |
88 fprintf(stderr, "%s: unable to open input file %s\n", progname, argv[0]); | |
89 exit(1); | |
90 } | |
91 else | |
92 strcpy(input_name, argv[0]); | |
93 | |
94 if (sig) { | |
95 fgets(line, sizeof(line), sig); | |
96 if (strspn(line, "KD3SG") >= 5) { | |
97 fscanf(sig, "%d\n", &nbit_signature1); | |
98 fscanf(sig, "%d\n", &quality); | |
99 fscanf(sig, "%d\n", &method); | |
100 fscanf(sig, "%d\n", &filter); | |
101 fscanf(sig, "%[^\n\r]\n", &filter_name); | |
102 fscanf(sig, "%d\n", &level); | |
103 fscanf(sig, "%d\n", &seed); | |
104 srandom(seed); | |
105 n_signature1 = NBITSTOBYTES(nbit_signature1); | |
106 | |
107 binstr1 = malloc((nbit_signature1 + 1) * sizeof(char)); | |
108 fscanf(sig, "%[01]\n", binstr1); | |
109 binstr_to_sig1(binstr1); | |
110 free(binstr1); | |
111 } | |
112 else { | |
113 fprintf(stderr, "%s: invalid signature file %s\n", progname, signature_name); | |
114 exit(1); | |
115 } | |
116 close(sig); | |
117 } | |
118 else { | |
119 fprintf(stderr, "%s: original signature file not specified, use -s file option\n", progname); | |
120 exit(1); | |
121 } | |
122 | |
123 fgets(line, sizeof(line), in); | |
124 if (strspn(line, "KD3WM") >= 5) { | |
125 fscanf(in, "%d\n", &nbit_signature2); | |
126 n_signature2 = NBITSTOBYTES(nbit_signature2); | |
127 binstr2 = malloc((nbit_signature2 + 1) * sizeof(char)); | |
128 fscanf(in, "%[01]\n", binstr2); | |
129 binstr_to_sig2(binstr2); | |
130 free(binstr2); | |
131 } | |
132 else { | |
133 fprintf(stderr, "%s: invalid watermark file %s\n", progname, input_name); | |
134 exit(1); | |
135 } | |
136 | |
137 if (verbose > 0) { | |
138 fprintf(stderr, "signature length: %d\n", nbit_signature1); | |
139 fprintf(stderr, "watermark length: %d\n", nbit_signature2); | |
140 } | |
141 | |
142 for (i = 0; i < nbit_signature2; i++) | |
143 if (get_signature1_bit(i % nbit_signature1) == get_signature2_bit(i)) | |
144 corr++, match++; | |
145 else | |
146 corr--; | |
147 | |
148 if (correlation_only) | |
149 fprintf(out, "%lf\n", (double) corr / nbit_signature2); | |
150 else { | |
151 fprintf(out, "bit matches: %d/%d\n", match, nbit_signature2); | |
152 fprintf(out, "correlation: %lf\n", (double) corr / nbit_signature2); | |
153 } | |
154 | |
155 exit(0); | |
156 } |