summaryrefslogtreecommitdiffstats
path: root/src/filter.h
blob: 2e2839cd745c59b516f8abd38c715f8cc69fe10d (plain) (blame)
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
/*
 * filter.h
 *
 * Copyright (c) 2018 Disi A
 * 
 * Author: Disi A
 * Email: adis@live.cn
 *  https://www.mathworks.com/matlabcentral/profile/authors/3734620-disi-a
 */
#ifndef filter_h
#define filter_h

#if __cplusplus
extern "C"{
#endif

#define DOUBLE_PRECISION 0

#if DOUBLE_PRECISION
#define FTR_PRECISION double
#if defined(_WIN32) || defined(__ZEPHYR__)
#define M_PI 3.141592653589793238462643383279502884197163993751
#endif
#else
#define FTR_PRECISION float
#if defined(_WIN32) || defined(__ZEPHYR__)
#define M_PI 3.1415927f
#endif
#endif

  typedef struct {
    int n;
    FTR_PRECISION *A;
    FTR_PRECISION *d1;
    FTR_PRECISION *d2;
    FTR_PRECISION *w0;
    FTR_PRECISION *w1;
    FTR_PRECISION *w2;
  } BWLowPass;
  // BWHighPass uses exactly the same struct
  typedef BWLowPass BWHighPass;

  typedef struct {
    int m;
    FTR_PRECISION ep;
    FTR_PRECISION *A;
    FTR_PRECISION *d1;
    FTR_PRECISION *d2;
    FTR_PRECISION *w0;
    FTR_PRECISION *w1;
    FTR_PRECISION *w2;
  } CHELowPass;
  typedef CHELowPass CHEHighPass;

  typedef struct {
    int n;
    FTR_PRECISION *A;
    FTR_PRECISION *d1;
    FTR_PRECISION *d2;
    FTR_PRECISION *d3;
    FTR_PRECISION *d4;
    FTR_PRECISION *w0;
    FTR_PRECISION *w1;
    FTR_PRECISION *w2;
    FTR_PRECISION *w3;
    FTR_PRECISION *w4;
  } BWBandPass;

  typedef struct {
    int m;
    FTR_PRECISION ep;
    FTR_PRECISION *A;
    FTR_PRECISION *d1;
    FTR_PRECISION *d2;
    FTR_PRECISION *d3;
    FTR_PRECISION *d4;
    FTR_PRECISION *w0;
    FTR_PRECISION *w1;
    FTR_PRECISION *w2;
    FTR_PRECISION *w3;
    FTR_PRECISION *w4;
  } CHEBandPass;

  typedef struct {
    int n;
    FTR_PRECISION r;
    FTR_PRECISION s;
    FTR_PRECISION *A;
    FTR_PRECISION *d1;
    FTR_PRECISION *d2;
    FTR_PRECISION *d3;
    FTR_PRECISION *d4;
    FTR_PRECISION *w0;
    FTR_PRECISION *w1;
    FTR_PRECISION *w2;
    FTR_PRECISION *w3;
    FTR_PRECISION *w4;
  } BWBandStop;

  typedef struct {
    int m;
    FTR_PRECISION ep;
    FTR_PRECISION r;
    FTR_PRECISION s;
    FTR_PRECISION *A;
    FTR_PRECISION *d1;
    FTR_PRECISION *d2;
    FTR_PRECISION *d3;
    FTR_PRECISION *d4;
    FTR_PRECISION *w0;
    FTR_PRECISION *w1;
    FTR_PRECISION *w2;
    FTR_PRECISION *w3;
    FTR_PRECISION *w4;
  } CHEBandStop;

  void update_bw_low_pass_filter(BWLowPass* filter, FTR_PRECISION s, FTR_PRECISION f, FTR_PRECISION q);
  
  BWLowPass* create_bw_low_pass_filter(int order, FTR_PRECISION sampling_frequency, FTR_PRECISION half_power_frequency);
  BWHighPass* create_bw_high_pass_filter(int order, FTR_PRECISION sampling_frequency, FTR_PRECISION half_power_frequency);
  BWBandPass* create_bw_band_pass_filter(int order, FTR_PRECISION sampling_frequency, FTR_PRECISION lower_half_power_frequency, FTR_PRECISION upper_half_power_frequency);
  BWBandStop* create_bw_band_stop_filter(int order, FTR_PRECISION sampling_frequency, FTR_PRECISION lower_half_power_frequency, FTR_PRECISION upper_half_power_frequency);

  CHELowPass* create_che_low_pass_filter(int order, FTR_PRECISION epsilon, FTR_PRECISION sampling_frequency, FTR_PRECISION half_power_frequency);
  CHEHighPass* create_che_high_pass_filter(int order, FTR_PRECISION epsilon, FTR_PRECISION sampling_frequency, FTR_PRECISION half_power_frequency);
  CHEBandPass* create_che_band_pass_filter(int order, FTR_PRECISION epsilon, FTR_PRECISION sampling_frequency, FTR_PRECISION lower_half_power_frequency, FTR_PRECISION upper_half_power_frequency);
  CHEBandStop* create_che_band_stop_filter(int order, FTR_PRECISION epsilon, FTR_PRECISION sampling_frequency, FTR_PRECISION lower_half_power_frequency, FTR_PRECISION upper_half_power_frequency);


  void free_bw_low_pass(BWLowPass* filter);
  void free_bw_high_pass(BWHighPass* filter);
  void free_bw_band_pass(BWBandPass* filter);
  void free_bw_band_stop(BWBandStop* filter);

  void free_che_low_pass(CHELowPass* filter);
  void free_che_high_pass(CHEHighPass* filter);
  void free_che_band_pass(CHEBandPass* filter);
  void free_che_band_stop(CHEBandStop* filter);


  FTR_PRECISION bw_low_pass(BWLowPass* filter, FTR_PRECISION input);
  FTR_PRECISION bw_high_pass(BWHighPass* filter, FTR_PRECISION input);
  FTR_PRECISION bw_band_pass(BWBandPass* filter, FTR_PRECISION input);
  FTR_PRECISION bw_band_stop(BWBandStop* filter, FTR_PRECISION input);

  FTR_PRECISION che_low_pass(CHELowPass* filter, FTR_PRECISION input);
  FTR_PRECISION che_high_pass(CHEHighPass* filter, FTR_PRECISION input);
  FTR_PRECISION che_band_pass(CHEBandPass* filter, FTR_PRECISION input);
  FTR_PRECISION che_band_stop(CHEBandStop* filter, FTR_PRECISION input);

  FTR_PRECISION softmax(FTR_PRECISION* data, int size, int target_ind);

  // Output should be pre-allocated which has the same(or larger) size as the input.
  void spike_filter_upward(FTR_PRECISION * input, int size, FTR_PRECISION * output, FTR_PRECISION strength);

#if __cplusplus
}
#endif

#endif /* filter_h */