This repository has been archived by the owner on Nov 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter.h
90 lines (79 loc) · 1.91 KB
/
filter.h
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
// Copyright (c) 2016 Kai Luo. All rights reserved.
#ifndef ZLI_FILTER_H_
#define ZLI_FILTER_H_
#include <cmath>
#include "core_math.h"
namespace zLi {
namespace filter {
class Tent1D {
public:
Tent1D() : r_(1) {}
explicit Tent1D(Float r) : r_(r) {}
Float f(Float x) { return tent(x / r_) / r_; }
template <typename Func>
Float Convolve(Func F, Float x) {
auto conv = [&](Float t) { return f(t) * F(x - t); };
return EstimateIntegration1D(conv, -r_, r_);
}
private:
static Float tent(Float x) {
x = std::abs(x);
return x >= 1 ? 0 : 1 - x;
}
Float r_;
};
class Box1D {
public:
Box1D() : r_(1) {}
explicit Box1D(Float r) : r_(r) {}
Float f(Float x) {
x = std::abs(x);
if (x >= r_) {
return 0;
}
return 1 / (2 * r_);
}
template <typename Func>
Float Convolve(Func F, Float x) {
auto conv = [&](Float t) { return f(t) * F(x - t); };
return EstimateIntegration1D(conv, -r_, r_);
}
private:
Float r_;
};
class B_Spline1D {
public:
static Float f(Float x) {
x = std::abs(x);
if (x <= 1) {
auto y = 1 - x;
return 1 / 6 * (-3 * std::pow(y, 3) + 3 * std::pow(y, 2) + 3 * y + 1);
} else if (x <= 2) {
auto y = 2 - x;
return 1 / 6 * std::pow(y, 3);
}
return 0;
}
template <typename Func>
static Float Convolve(Func F, Float x) {
auto conv = [&](Float t) { return f(t) * F(x - t); };
return EstimateIntegration1D(conv, -2, 2);
}
};
class Gauss1D {
public:
Gauss1D() : A_(1), fx_(1) {}
Gauss1D(Float A, Float fx) : A_(A), fx_(fx) {}
Float f(Float x) { return A_ * std::exp(-(x * x) / (2 * fx_ * fx_)); }
template <typename Func>
Float Convolve(Func F, Float x) {
static const Float r = std::sqrt(fx_ * fx_); // FIXME
auto conv = [&](Float t) { return f(t) * F(x - t); };
return EstimateIntegration1D(conv, -r, r);
}
private:
Float A_, fx_;
};
} // namespace filter
} // namespace zLi
#endif