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
16 int liba_lpf_new(lua_State
*L
)
18 int top
= lua_gettop(L
);
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
));
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
);
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
49 int liba_lpf_gen(lua_State
*L
)
51 int top
= lua_gettop(L
);
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
);
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);
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
80 int liba_lpf_iter(lua_State
*L
)
82 a_lpf
*const ctx
= (a_lpf
*)lua_touserdata(L
, 1);
85 a_float x
= (a_float
)luaL_checknumber(L
, 2);
86 lua_pushnumber(L
, (lua_Number
)a_lpf_iter(ctx
, x
));
93 zeroing for Low Pass Filter
94 @tparam a.lpf ctx Low Pass Filter userdata
95 @treturn a.lpf Low Pass Filter userdata
98 int liba_lpf_zero(lua_State
*L
)
100 a_lpf
*const ctx
= (a_lpf
*)lua_touserdata(L
, 1);
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
119 lua_getmetatable(L
, 1);
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
);
134 case 0x23C9C461: // output
135 lua_pushnumber(L
, (lua_Number
)ctx
->output
);
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
);
143 lua_getmetatable(L
, 1);
150 static int liba_lpf_(lua_State
*L
)
152 lua_pushcfunction(L
, liba_lpf_new
);
154 lua_call(L
, lua_gettop(L
) - 1, 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
);