-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalyze.c
140 lines (117 loc) · 2.94 KB
/
analyze.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <fcntl.h>
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <stdarg.h>
#include <memory.h>
#include <math.h>
#include "fourier.h"
int rate = 8000;
int bps = 10;
int generate_xpm = 0;
double threshold = 0.25;
FILE* infile = NULL;
FILE* outfile = NULL;
FILE* xpmfile = NULL;
void usage(char* cmd) {
printf("Usage: %s [-r sample rate] [-b baud] [-h] [-x] [ -o file ] [ file ]\n"
"\t -r : Set sample rate (default = 8000)\n"
"\t -o : Write data to file\n"
"\t -x : Generate XPM with frequency spectrum\n"
"\t -b : Set transferred bits per second\n"
"\t -h : Display this message\n\n", cmd);
}
int main(int argc, char* argv[]) {
/* Set default arguments */
infile = stdin;
outfile = stdout;
xpmfile = fopen("out.xpm", "w");
for(;;) switch(getopt(argc, argv, "r:shb:o:x")) {
case 'r':
rate = atoi(optarg);
break;
case 'b': bps = atoi(optarg);
break;
case 'x':
generate_xpm = 1;
break;
case 'o':
outfile = fopen(optarg, "w+");
if(outfile == NULL) {
perror(optarg);
exit(EXIT_FAILURE);
}
break;
case 'h': usage(argv[0]);
return EXIT_SUCCESS;
break;
case -1: goto done;
default: exit(EXIT_FAILURE);
}
done:
if(optind < argc) {
infile = fopen(argv[optind], "r");
if(infile == NULL) {
perror(argv[optind]);
exit(EXIT_FAILURE);
}
}
/* All-purpose index */
int i = 0;
/* Calculate file length */
int file_length = 0;
fseek(infile, 0, SEEK_END);
file_length = ftell(infile);
fseek(infile, 0, SEEK_SET);
/* The buffer length to use */
int length = rate / (4 * bps);
/* Equivalent of a 400 Hz signal */
double frq = 400 * length / rate;
/* Raw data buffer */
char* buffer = malloc(length);
/* Massaged data buffer */
double* dbuffer = malloc(length);
int signal_length = 0;
int bit = 0;
unsigned char bit_data = 0;
if(generate_xpm) fourier_xpm_start(length, file_length / length, xpmfile);
while((fread(buffer, 1, length, infile))) {
double sum = 0;
/* Transform into a double in the range -1 ... 1 */
for (i = 0; i < length; i++) {
dbuffer[i] = buffer[i] / 128.0;
}
if(generate_xpm) fourier_xpm_line(dbuffer, length, xpmfile);
/* See the Shannon-Nyquist sampling theorem for why
* it's only meaningful to iterate over half the frequency
* spectrum */
int harmonic;
for(harmonic = 1; 2*harmonic < length / frq; harmonic++)
sum += fourier1(dbuffer, frq*harmonic, length);
if(sum < threshold) {
if(signal_length) {
if(signal_length > 10) {
/* End of bit has been received
* but 8 full bits have not been
* received. Strong indicator that
* we've gotten into corruption.
*/
if(bit != 0) printf("(?)");
bit = 0;
signal_length = 0;
} else {
bit_data = 2 * bit_data + (signal_length < 6);
if(++bit == 8) {
printf("%c", bit_data);
fflush(NULL);
bit = 0;
}
}
signal_length = 0;
}
} else {
signal_length++;
}
}
return EXIT_SUCCESS;
}