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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
// A rust binding for the GSL library by Guillaume Gomez (guillaume1.gomez@gmail.com)
//

use crate::{FilterEnd, FilterScale, Value, VectorF64, VectorI32};
use ffi::FFI;

ffi_wrapper!(
    FilterGaussianWorkspace,
    *mut sys::gsl_filter_gaussian_workspace,
    gsl_filter_gaussian_free
);

impl FilterGaussianWorkspace {
    #[doc(alias = "gsl_filter_gaussian_alloc")]
    pub fn new(K: usize) -> Option<Self> {
        let s = unsafe { sys::gsl_filter_gaussian_alloc(K) };
        if s.is_null() {
            None
        } else {
            Some(Self::wrap(s))
        }
    }

    /// This function applies a Gaussian filter parameterized by `alpha` to the input vector `x`,
    /// storing the output in `y`. The derivative order is specified by `order`, with `0`
    /// corresponding to a Gaussian, `1` corresponding to a first derivative Gaussian, and so on.
    /// The parameter `endtype` specifies how the signal end points are handled. It is allowed for
    /// `x` = `y` for an in-place filter.
    #[doc(alias = "gsl_filter_gaussian")]
    pub fn gaussian(
        &mut self,
        endtype: FilterEnd,
        alpha: f64,
        order: usize,
        x: &VectorF64,
        y: &mut VectorF64,
    ) -> Result<(), Value> {
        let ret = unsafe {
            sys::gsl_filter_gaussian(
                endtype.into(),
                alpha,
                order,
                x.unwrap_shared(),
                y.unwrap_unique(),
                self.unwrap_unique(),
            )
        };
        result_handler!(ret, ())
    }
}

ffi_wrapper!(
    FilterMedianWorkspace,
    *mut sys::gsl_filter_median_workspace,
    gsl_filter_median_free
);

impl FilterMedianWorkspace {
    #[doc(alias = "gsl_filter_median_alloc")]
    pub fn new(K: usize) -> Option<Self> {
        let s = unsafe { sys::gsl_filter_median_alloc(K) };
        if s.is_null() {
            None
        } else {
            Some(Self::wrap(s))
        }
    }

    #[doc(alias = "gsl_filter_median")]
    pub fn median(
        &mut self,
        endtype: FilterEnd,
        x: &VectorF64,
        y: &mut VectorF64,
    ) -> Result<(), Value> {
        let ret = unsafe {
            sys::gsl_filter_median(
                endtype.into(),
                x.unwrap_shared(),
                y.unwrap_unique(),
                self.unwrap_unique(),
            )
        };
        result_handler!(ret, ())
    }
}

ffi_wrapper!(
    FilterRMedianWorkspace,
    *mut sys::gsl_filter_rmedian_workspace,
    gsl_filter_rmedian_free
);

impl FilterRMedianWorkspace {
    #[doc(alias = "gsl_filter_rmedian_alloc")]
    pub fn new(K: usize) -> Option<Self> {
        let s = unsafe { sys::gsl_filter_rmedian_alloc(K) };
        if s.is_null() {
            None
        } else {
            Some(Self::wrap(s))
        }
    }

    #[doc(alias = "gsl_filter_rmedian")]
    pub fn rmedian(
        &mut self,
        endtype: FilterEnd,
        x: &VectorF64,
        y: &mut VectorF64,
    ) -> Result<(), Value> {
        let ret = unsafe {
            sys::gsl_filter_rmedian(
                endtype.into(),
                x.unwrap_shared(),
                y.unwrap_unique(),
                self.unwrap_unique(),
            )
        };
        result_handler!(ret, ())
    }
}

ffi_wrapper!(
    FilterImpulseWorkspace,
    *mut sys::gsl_filter_impulse_workspace,
    gsl_filter_impulse_free
);

impl FilterImpulseWorkspace {
    #[doc(alias = "gsl_filter_impulse_alloc")]
    pub fn new(K: usize) -> Option<Self> {
        let s = unsafe { sys::gsl_filter_impulse_alloc(K) };
        if s.is_null() {
            None
        } else {
            Some(Self::wrap(s))
        }
    }

    /// Returns `noutlier`.
    #[doc(alias = "gsl_filter_impulse")]
    pub fn impulse(
        &mut self,
        endtype: FilterEnd,
        scale_type: FilterScale,
        t: f64,
        x: &VectorF64,
        y: &mut VectorF64,
        xmedian: &mut VectorF64,
        xsigma: &mut VectorF64,
        ioutlier: &mut VectorI32,
    ) -> Result<usize, Value> {
        let mut noutlier = 0;
        let ret = unsafe {
            sys::gsl_filter_impulse(
                endtype.into(),
                scale_type.into(),
                t,
                x.unwrap_shared(),
                y.unwrap_unique(),
                xmedian.unwrap_unique(),
                xsigma.unwrap_unique(),
                &mut noutlier,
                ioutlier.unwrap_unique(),
                self.unwrap_unique(),
            )
        };
        result_handler!(ret, noutlier)
    }
}