release 0.1.13
[liba.git] / lua / src / hpf.c
blob463f86cccdf2a55afa493092d15d96f6cf8aabde
1 /***
2 High Pass Filter
3 @module liba.hpf
4 */
6 #include "hpf.h"
7 #include "a/hpf.h"
9 /***
10 constructor for High Pass Filter
11 @tparam number fc cut-off frequency unit(hz)
12 @tparam number ts sampling time unit(s)
13 @treturn a.hpf High Pass Filter userdata
14 @function new
16 int liba_hpf_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_hpf *const ctx = lua_newclass(L, a_hpf);
24 lua_registry_get(L, liba_hpf_new);
25 lua_setmetatable(L, -2);
26 a_hpf_init(ctx, A_HPF_GEN(fc, ts));
27 return 1;
29 if (top > 0)
31 a_float alpha = (a_float)luaL_checknumber(L, 1);
32 a_hpf *const ctx = lua_newclass(L, a_hpf);
33 lua_registry_get(L, liba_hpf_new);
34 lua_setmetatable(L, -2);
35 a_hpf_init(ctx, alpha);
36 return 1;
38 return 0;
41 /***
42 generate for High Pass Filter
43 @tparam a.hpf ctx High Pass Filter userdata
44 @tparam number fc cut-off frequency unit(hz)
45 @tparam number ts sampling time unit(s)
46 @treturn a.hpf High Pass Filter userdata
47 @function gen
49 int liba_hpf_gen(lua_State *L)
51 int top = lua_gettop(L);
52 if (top > 2)
54 luaL_checktype(L, 1, LUA_TUSERDATA);
55 a_hpf *const ctx = (a_hpf *)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_HPF_GEN(fc, ts);
59 lua_pushvalue(L, 1);
60 return 1;
62 if (top > 1)
64 luaL_checktype(L, 1, LUA_TUSERDATA);
65 a_hpf *const ctx = (a_hpf *)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 High Pass Filter
75 @tparam a.hpf ctx High Pass Filter userdata
76 @tparam number x input value
77 @treturn number output value
78 @function iter
80 int liba_hpf_iter(lua_State *L)
82 a_hpf *const ctx = (a_hpf *)lua_touserdata(L, 1);
83 if (ctx)
85 a_float x = (a_float)luaL_checknumber(L, 2);
86 lua_pushnumber(L, (lua_Number)a_hpf_iter(ctx, x));
87 return 1;
89 return 0;
92 /***
93 zeroing for High Pass Filter
94 @tparam a.hpf ctx High Pass Filter userdata
95 @treturn a.hpf High Pass Filter userdata
96 @function zero
98 int liba_hpf_zero(lua_State *L)
100 a_hpf *const ctx = (a_hpf *)lua_touserdata(L, 1);
101 if (ctx)
103 a_hpf_zero(ctx);
104 return 1;
106 return 0;
109 static int liba_hpf_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_hpf_get(lua_State *L)
128 a_hpf const *const ctx = (a_hpf 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 0x41FAB016: // input
138 lua_pushnumber(L, (lua_Number)ctx->input);
139 break;
140 case 0xA65758B2: // __index
141 lua_registry_get(L, liba_hpf_new);
142 lua_num_set(L, -1, "alpha", ctx->alpha);
143 lua_num_set(L, -1, "input", ctx->input);
144 lua_num_set(L, -1, "output", ctx->output);
145 break;
146 default:
147 lua_getmetatable(L, 1);
148 lua_replace(L, 1);
149 lua_rawget(L, 1);
151 return 1;
154 static int liba_hpf_(lua_State *L)
156 lua_pushcfunction(L, liba_hpf_new);
157 lua_replace(L, 1);
158 lua_call(L, lua_gettop(L) - 1, 1);
159 return 1;
162 int luaopen_liba_hpf(lua_State *L)
164 static lua_fun const funcs[] = {
165 {"new", liba_hpf_new},
166 {"gen", liba_hpf_gen},
167 {"iter", liba_hpf_iter},
168 {"zero", liba_hpf_zero},
170 lua_createtable(L, 0, A_LEN(funcs));
171 lua_fun_reg(L, -1, funcs, A_LEN(funcs));
172 lua_createtable(L, 0, 1);
173 lua_fun_set(L, -1, "__call", liba_hpf_);
174 lua_setmetatable(L, -2);
176 static lua_fun const metas[] = {
177 {"__newindex", liba_hpf_set},
178 {"__index", liba_hpf_get},
179 {"__call", liba_hpf_iter},
181 lua_createtable(L, 0, A_LEN(metas) + A_LEN(funcs) + 1);
182 lua_fun_reg(L, -1, metas, A_LEN(metas));
183 lua_fun_reg(L, -1, funcs, A_LEN(funcs));
184 lua_str_set(L, -1, "__name", "a.hpf");
185 lua_registry_set(L, liba_hpf_new);
187 return 1;