disable warning C5250: _BitScanReverse: intrinsic function not declared
[liba.git] / lua / src / lpf.c
blobfe50a9f5fffccbef80a60038810659a5425999e9
1 /***
2 Low Pass Filter
3 @module liba.lpf
4 */
6 #include "lpf.h"
7 #include "a/lpf.h"
9 /***
10 constructor for Low Pass Filter
11 @tparam number fc cut-off frequency unit(hz)
12 @tparam number ts sampling time unit(s)
13 @treturn a.lpf Low Pass Filter userdata
14 @function new
16 int liba_lpf_new(lua_State *L)
18 int top = lua_gettop(L);
19 if (top > 1)
21 a_float fc = (a_float)luaL_checknumber(L, 1);
22 a_float ts = (a_float)luaL_checknumber(L, 2);
23 a_lpf *const ctx = lua_newclass(L, a_lpf);
24 lua_registry_get(L, liba_lpf_new);
25 lua_setmetatable(L, -2);
26 a_lpf_init(ctx, A_LPF_GEN(fc, ts));
27 return 1;
29 if (top > 0)
31 a_float alpha = (a_float)luaL_checknumber(L, 1);
32 a_lpf *const ctx = lua_newclass(L, a_lpf);
33 lua_registry_get(L, liba_lpf_new);
34 lua_setmetatable(L, -2);
35 a_lpf_init(ctx, alpha);
36 return 1;
38 return 0;
41 /***
42 generate for Low Pass Filter
43 @tparam a.lpf ctx Low Pass Filter userdata
44 @tparam number fc cut-off frequency unit(hz)
45 @tparam number ts sampling time unit(s)
46 @treturn a.lpf Low Pass Filter userdata
47 @function gen
49 int liba_lpf_gen(lua_State *L)
51 int top = lua_gettop(L);
52 if (top > 2)
54 luaL_checktype(L, 1, LUA_TUSERDATA);
55 a_lpf *const ctx = (a_lpf *)lua_touserdata(L, 1);
56 a_float fc = (a_float)luaL_checknumber(L, 2);
57 a_float ts = (a_float)luaL_checknumber(L, 3);
58 ctx->alpha = A_LPF_GEN(fc, ts);
59 lua_pushvalue(L, 1);
60 return 1;
62 if (top > 1)
64 luaL_checktype(L, 1, LUA_TUSERDATA);
65 a_lpf *const ctx = (a_lpf *)lua_touserdata(L, 1);
66 ctx->alpha = (a_float)luaL_checknumber(L, 2);
67 lua_pushvalue(L, 1);
68 return 1;
70 return 0;
73 /***
74 calculate for Low Pass Filter
75 @tparam a.lpf ctx Low Pass Filter userdata
76 @tparam number x input value
77 @treturn number output value
78 @function iter
80 int liba_lpf_iter(lua_State *L)
82 a_lpf *const ctx = (a_lpf *)lua_touserdata(L, 1);
83 if (ctx)
85 a_float x = (a_float)luaL_checknumber(L, 2);
86 lua_pushnumber(L, (lua_Number)a_lpf_iter(ctx, x));
87 return 1;
89 return 0;
92 /***
93 zeroing for Low Pass Filter
94 @tparam a.lpf ctx Low Pass Filter userdata
95 @treturn a.lpf Low Pass Filter userdata
96 @function zero
98 int liba_lpf_zero(lua_State *L)
100 a_lpf *const ctx = (a_lpf *)lua_touserdata(L, 1);
101 if (ctx)
103 a_lpf_zero(ctx);
104 return 1;
106 return 0;
109 static int liba_lpf_set(lua_State *L)
111 switch (a_hash_bkdr(lua_tostring(L, 2), 0))
113 case 0xE8859EEB: // __name
114 case 0xE70C48C6: // __call
115 case 0xA65758B2: // __index
116 case 0xAEB551C6: // __newindex
117 break;
118 default:
119 lua_getmetatable(L, 1);
120 lua_replace(L, 1);
121 lua_rawset(L, 1);
123 return 0;
126 static int liba_lpf_get(lua_State *L)
128 a_lpf const *const ctx = (a_lpf const *)lua_touserdata(L, 1);
129 switch (a_hash_bkdr(lua_tostring(L, 2), 0))
131 case 0xB5485B9E: // alpha
132 lua_pushnumber(L, (lua_Number)ctx->alpha);
133 break;
134 case 0x23C9C461: // output
135 lua_pushnumber(L, (lua_Number)ctx->output);
136 break;
137 case 0xA65758B2: // __index
138 lua_registry_get(L, liba_lpf_new);
139 lua_num_set(L, -1, "alpha", ctx->alpha);
140 lua_num_set(L, -1, "output", ctx->output);
141 break;
142 default:
143 lua_getmetatable(L, 1);
144 lua_replace(L, 1);
145 lua_rawget(L, 1);
147 return 1;
150 static int liba_lpf_(lua_State *L)
152 lua_pushcfunction(L, liba_lpf_new);
153 lua_replace(L, 1);
154 lua_call(L, lua_gettop(L) - 1, 1);
155 return 1;
158 int luaopen_liba_lpf(lua_State *L)
160 static lua_fun const funcs[] = {
161 {"new", liba_lpf_new},
162 {"gen", liba_lpf_gen},
163 {"iter", liba_lpf_iter},
164 {"zero", liba_lpf_zero},
166 lua_createtable(L, 0, A_LEN(funcs));
167 lua_fun_reg(L, -1, funcs, A_LEN(funcs));
168 lua_createtable(L, 0, 1);
169 lua_fun_set(L, -1, "__call", liba_lpf_);
170 lua_setmetatable(L, -2);
172 static lua_fun const metas[] = {
173 {"__newindex", liba_lpf_set},
174 {"__index", liba_lpf_get},
175 {"__call", liba_lpf_iter},
177 lua_createtable(L, 0, A_LEN(metas) + A_LEN(funcs) + 1);
178 lua_fun_reg(L, -1, metas, A_LEN(metas));
179 lua_fun_reg(L, -1, funcs, A_LEN(funcs));
180 lua_str_set(L, -1, "__name", "a.lpf");
181 lua_registry_set(L, liba_lpf_new);
183 return 1;