Skip to content

Instantly share code, notes, and snippets.

@robertmaxwilliams
Last active February 22, 2018 04:19
Show Gist options
  • Save robertmaxwilliams/72283376e9d51e9883aa372243612160 to your computer and use it in GitHub Desktop.
Save robertmaxwilliams/72283376e9d51e9883aa372243612160 to your computer and use it in GitHub Desktop.
1d convolution in c for lidar data smoothing
#include <stdio.h>
/* Useful vidoes:
1d kernel for data processing:
https://www.youtube.com/watch?v=D0F-NPerCIE
numberphile on image bluring (same thing in 2d):
https://www.youtube.com/watch?v=C_zFhWdM4ic
excellent explanations in there. It introduced me to the concept.
*/
// 9 wide 1d kernel, no padding so it cuts out early
// shift by 4 to align with original data
int main(){
// gassian kernel from: http://dev.theomader.com/gaussian-kernel-calculator/
float kernel[9] = {0.000229, 0.005977, 0.060598, 0.241732, 0.382928, 0.241732, 0.060598, 0.005977, 0.000229};
// random number from python, irl this would come from lidar
float data[100] = {7.230, 16.98, 17.99, 1.703, 16.44, 4.484, 7.843, 13.44, 7.815, 11.91, 2.050, 6.138, 3.049, 0.167, 1.756, 10.46, 10.02, 10.48, 13.14, 7.329, 14.93, 7.275, 18.61, 13.82, 15.97, 11.43, 10.27, 5.290, 14.13, 2.671, 3.267, 6.149, 14.56, 13.11, 18.14, 16.47, 17.49, 16.20, 7.835, 5.883, 0.967, 0.237, 4.359, 13.15, 15.92, 16.94, 14.30, 17.47, 5.118, 5.142, 19.41, 5.046, 16.78, 3.944, 12.17, 7.983, 15.35, 7.839, 11.65, 12.56, 9.564, 14.30, 4.670, 1.893, 9.304, 0.173, 3.921, 15.63, 6.561, 16.25, 1.634, 4.870, 15.03, 0.269, 11.92, 0.390, 15.57, 2.918, 8.966, 14.04, 11.23, 7.519, 7.943, 6.570, 18.74, 15.54, 1.303, 14.01, 1.797, 1.526, 12.90, 3.051, 8.602, 7.094, 14.39, 14.13, 11.20, 2.637, 2.644, 2.810};
// empty array to store the output
float output[100-9];
// keep track of max and max_index
float max = 0;
int max_index;
printf("\nshow data and output as it is calculated:\n");
//loop over data
for (int i = 0; i < 100-9+1; i++){
// don't trust that c will zero the array for us
output[i] = 0;
//loop over kernel for each datapoint
for (int j = 0; j < 9; j++){
output[i] += kernel[j] * data[i+j];
}
// check if it wins against max, then set max_index
if (output[i] > max){
max = output[i];
max_index = i;
}
// print data then blurred output as it is calculated
printf("%d: %f\t%f\n", i, data[i], output[i]);
}
printf("\nShifted over to show true location:\n");
// iterate over data again and show where value is centered on
for (int i = 0; i < 100; i++){
// print data then blurred output shifted
if (i >= 4 && i < 100-4){
printf("%d: %f\t%f\n", i, data[i], output[i-4]);
} else{
printf("%d: %f\t\n", i, data[i]);
}
}
printf("Max value: %f, location of max value: %d\n", max, max_index+4);
}
@robertmaxwilliams
Copy link
Author

output:

show data and output as it is calculated:
0: 7.230000	9.541627
1: 16.980000	8.665515
2: 17.990000	8.891867
3: 1.703000	10.037347
4: 16.440001	9.787935
5: 4.484000	8.197913
6: 7.843000	5.889645
7: 13.440000	4.377524
8: 7.815000	3.060093
9: 11.910000	2.308547
10: 2.050000	4.136067
11: 6.138000	7.597095
12: 3.049000	9.850395
13: 0.167000	10.791012
14: 1.756000	10.959266
15: 10.460000	10.844497
16: 10.020000	11.322549
17: 10.480000	12.354089
18: 13.140000	14.215590
19: 7.329000	14.938155
20: 14.930000	14.050919
21: 7.275000	12.075908
22: 18.610001	9.902005
23: 13.820000	8.897995
24: 15.970000	8.267577
25: 11.430000	6.075421
26: 10.270000	5.238142
27: 5.290000	7.818139
28: 14.130000	11.649817
29: 2.671000	14.423875
30: 3.267000	16.175034
31: 6.149000	16.832472
32: 14.560000	16.285904
33: 13.110000	13.797104
34: 18.139999	9.561876
35: 16.469999	5.513914
36: 17.490000	2.771699
37: 16.200001	2.681153
38: 7.835000	6.070050
39: 5.883000	11.075078
40: 0.967000	14.607856
41: 0.237000	15.705329
42: 4.359000	15.183504
43: 13.150000	12.937164
44: 15.920000	9.607527
45: 16.940001	9.453191
46: 14.300000	11.356426
47: 17.469999	11.340289
48: 5.118000	10.595523
49: 5.142000	9.508675
50: 19.410000	9.574512
51: 5.046000	10.597363
52: 16.780001	11.250751
53: 3.944000	10.907484
54: 12.170000	11.038982
55: 7.983000	11.401179
56: 15.350000	11.208018
57: 7.839000	9.919583
58: 11.650000	6.925673
59: 12.560000	5.066936
60: 9.564000	4.765362
61: 14.300000	4.399135
62: 4.670000	6.392824
63: 1.893000	9.581125
64: 9.304000	10.591143
65: 0.173000	9.559205
66: 3.921000	7.138112
67: 15.630000	7.008229
68: 6.561000	7.923539
69: 16.250000	7.043648
70: 1.634000	6.627078
71: 4.870000	7.135435
72: 15.030000	8.119027
73: 0.269000	8.063100
74: 11.920000	9.208470
75: 0.390000	11.032921
76: 15.570000	10.601000
77: 2.918000	8.932714
78: 8.966000	8.442632
79: 14.040000	10.444634
80: 11.230000	13.212717
81: 7.519000	12.103129
82: 7.943000	8.939800
83: 6.570000	7.339671
84: 18.740000	5.421723
85: 15.540000	5.235331
86: 1.303000	6.806070
87: 14.010000	6.991583
88: 1.797000	7.496590
89: 1.526000	9.460526
90: 12.900000	11.878372
91: 3.051000	12.254933

Shifted over to show true location:
0: 7.230000	
1: 16.980000	
2: 17.990000	
3: 1.703000	
4: 16.440001	9.541627
5: 4.484000	8.665515
6: 7.843000	8.891867
7: 13.440000	10.037347
8: 7.815000	9.787935
9: 11.910000	8.197913
10: 2.050000	5.889645
11: 6.138000	4.377524
12: 3.049000	3.060093
13: 0.167000	2.308547
14: 1.756000	4.136067
15: 10.460000	7.597095
16: 10.020000	9.850395
17: 10.480000	10.791012
18: 13.140000	10.959266
19: 7.329000	10.844497
20: 14.930000	11.322549
21: 7.275000	12.354089
22: 18.610001	14.215590
23: 13.820000	14.938155
24: 15.970000	14.050919
25: 11.430000	12.075908
26: 10.270000	9.902005
27: 5.290000	8.897995
28: 14.130000	8.267577
29: 2.671000	6.075421
30: 3.267000	5.238142
31: 6.149000	7.818139
32: 14.560000	11.649817
33: 13.110000	14.423875
34: 18.139999	16.175034
35: 16.469999	16.832472
36: 17.490000	16.285904
37: 16.200001	13.797104
38: 7.835000	9.561876
39: 5.883000	5.513914
40: 0.967000	2.771699
41: 0.237000	2.681153
42: 4.359000	6.070050
43: 13.150000	11.075078
44: 15.920000	14.607856
45: 16.940001	15.705329
46: 14.300000	15.183504
47: 17.469999	12.937164
48: 5.118000	9.607527
49: 5.142000	9.453191
50: 19.410000	11.356426
51: 5.046000	11.340289
52: 16.780001	10.595523
53: 3.944000	9.508675
54: 12.170000	9.574512
55: 7.983000	10.597363
56: 15.350000	11.250751
57: 7.839000	10.907484
58: 11.650000	11.038982
59: 12.560000	11.401179
60: 9.564000	11.208018
61: 14.300000	9.919583
62: 4.670000	6.925673
63: 1.893000	5.066936
64: 9.304000	4.765362
65: 0.173000	4.399135
66: 3.921000	6.392824
67: 15.630000	9.581125
68: 6.561000	10.591143
69: 16.250000	9.559205
70: 1.634000	7.138112
71: 4.870000	7.008229
72: 15.030000	7.923539
73: 0.269000	7.043648
74: 11.920000	6.627078
75: 0.390000	7.135435
76: 15.570000	8.119027
77: 2.918000	8.063100
78: 8.966000	9.208470
79: 14.040000	11.032921
80: 11.230000	10.601000
81: 7.519000	8.932714
82: 7.943000	8.442632
83: 6.570000	10.444634
84: 18.740000	13.212717
85: 15.540000	12.103129
86: 1.303000	8.939800
87: 14.010000	7.339671
88: 1.797000	5.421723
89: 1.526000	5.235331
90: 12.900000	6.806070
91: 3.051000	6.991583
92: 8.602000	7.496590
93: 7.094000	9.460526
94: 14.390000	11.878372
95: 14.130000	12.254933
96: 11.200000	
97: 2.637000	
98: 2.644000	
99: 2.810000	
Max value: 16.832472, location of max value: 35

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment