]> git.sesse.net Git - c64tapwav/blob - decode.c
Add some hysteresis to get rid of the worst noise.
[c64tapwav] / decode.c
1 #include <stdio.h>
2 #include <math.h>
3 #include <unistd.h>
4 #include <assert.h>
5 #include <algorithm>
6
7 #define LANCZOS_RADIUS 30
8 #define LEN 813440
9 #define HYSTERESIS_LIMIT 1000
10
11 double sinc(double x)
12 {
13         if (fabs(x) < 1e-6) {
14                 return 1.0f - fabs(x);
15         } else {
16                 return sin(x) / x;
17         }
18 }
19
20 #if 1
21 double weight(double x)
22 {
23         if (fabs(x) > LANCZOS_RADIUS) {
24                 return 0.0f;
25         }
26         return sinc(M_PI * x) * sinc(M_PI * x / LANCZOS_RADIUS);
27 }
28 #else
29 double weight(double x)
30 {
31         if (fabs(x) > 1.0f) {
32                 return 0.0f;
33         }
34         return 1.0f - fabs(x);
35 }
36 #endif
37
38 double interpolate(const short *in, double i)
39 {
40         int lower = std::max(int(ceil(i - LANCZOS_RADIUS)), 0);
41         int upper = std::min(int(floor(i + LANCZOS_RADIUS)), LEN - 1);
42         double sum = 0.0f;
43
44         for (int x = lower; x <= upper; ++x) {
45                 sum += in[x] * weight(i - x);
46         }
47         return sum;
48 }
49         
50 short in[LEN];
51
52 // between [x,x+1]
53 double find_zerocrossing(int x)
54 {
55         if (in[x] == 0) {
56                 return x;
57         }
58         if (in[x + 1] == 0) {
59                 return x + 1;
60         }
61
62         assert(in[x + 1] > 0);
63         assert(in[x] < 0);
64
65         double lower = x;
66         double upper = x + 1;
67         while (upper - lower > 1e-6) {
68                 double mid = 0.5f * (upper + lower);
69                 if (interpolate(in, mid) > 0) {
70                         upper = mid;
71                 } else {
72                         lower = mid;
73                 }
74         }
75
76         return 0.5f * (upper + lower);
77 }
78
79 int main(int argc, char **argv)
80 {
81         fread(in, LEN*2, 1, stdin);
82
83 #if 0
84         for (int i = 0; i < LEN; ++i) {
85                 in[i] += rand() % 10000;
86         }
87 #endif
88
89 #if 0
90         for (int i = 0; i < LEN; ++i) {
91                 printf("%d\n", in[i]);
92         }
93 #endif
94         int last_bit = -1;
95         double last_upflank = -1;
96         int last_max_level = 0;
97         for (int i = 0; i < LEN; ++i) {
98                 int bit = (in[i] > 0) ? 1 : 0;
99                 if (bit == 1 && last_bit == 0 && last_max_level > HYSTERESIS_LIMIT) {
100                         // up-flank!
101                         double t = find_zerocrossing(i - 1) * (123156.0/44100.0);
102                         if (last_upflank > 0) {
103 //                              fprintf(stderr, "length: %f (0x%x)\n", t - last_upflank, lrintf(t - last_upflank));
104                                 int len = lrintf(t - last_upflank);
105                                 printf("0x%x\n", len);
106                         }
107                         last_upflank = t;
108                         last_max_level = 0;
109                 }
110                 last_max_level = std::max(last_max_level, abs(in[i]));
111                 last_bit = bit;
112         }
113 }