-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcrackle.c
95 lines (81 loc) · 1.98 KB
/
crackle.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
#include <stdio.h>
#include <stdlib.h>
#include "gwc.h"
#define NMAX 201
double drand48(void)
{
return (double)rand()/(double)RAND_MAX ;
}
int main(int argc, char **argv)
{
double t[NMAX] ;
double y[NMAX] ; /* sinusoidal y value */
double ypr[NMAX] ; /* y + occasional random component */
double y_hat[NMAX] ; /* estimated original y */
double wgt[NMAX] ;
double factor ;
double x ;
int i ;
for(i = 0 ; i < NMAX ; i++) {
t[i] = (double)i/(double)(NMAX-1) ;
y[i] = sin(t[i]*2.0*M_PI) ;
if(drand48() > 0.8)
ypr[i] = y[i] + 0.9*(drand48()-0.5) ;
else
ypr[i] = y[i] ;
}
for(factor = 0.01 ; factor < 0.05 ; factor += 0.01) {
for(i = 0 ; i < NMAX ; i++) {
double dy0 = 0 ;
double dy1 = 0 ;
double dy2dx = 0 ;
if(i < 2) {
y_hat[i] = ypr[i] ;
wgt[i] = 1.0 ;
} else {
dy0 = y_hat[i-1] - y_hat[i-2] ;
dy1 = ypr[i-0] - y_hat[i-1] ;
dy2dx = dy1 - dy0 ;
if(dy2dx > factor) {
wgt[i] = factor/dy2dx ;
dy2dx = factor ;
dy1 = dy2dx + dy0 ;
/* dy1 = (1.0 + factor)*dy0 ; */
y_hat[i] = dy1 + y_hat[i-1] ;
} else if(dy2dx < -factor) {
wgt[i] = -factor/dy2dx ;
dy2dx = -factor ;
dy1 = dy2dx + dy0 ;
/* dy1 = (1.0 - factor)*dy0 ; */
y_hat[i] = dy1 + y_hat[i-1] ;
} else {
y_hat[i] = ypr[i] ;
wgt[i] = 1.0 ;
}
}
}
for(i = 0 ; i < NMAX ; i++) {
int first, last, j, n=0 ;
int w = 4 ;
double sum = 0.0 ;
double sumwgt = 0.0 ;
double dy0 = 0 ;
double dy1 = 0 ;
double dy2dx = 0 ;
if(i >= 2) {
dy0 = y_hat[i-1] - y_hat[i-2] ;
dy1 = y_hat[i-0] - y_hat[i-1] ;
dy2dx = dy1 - dy0 ;
}
first = i-w ;
if(first < 0) first = 0 ;
last = i+w ;
if(last > NMAX-1) last = NMAX-1 ;
for(j = first ; j <= last ; j++) {
sum += y_hat[j]*wgt[j] ;
sumwgt += wgt[j] ;
}
printf("%5.3lf %3d %lg %lg %lg %lg %lg %lg\n", factor, i, y[i], ypr[i], sum/sumwgt, dy0, dy1, dy2dx) ;
}
}
}