3 Description: signal definition and operations.
5 Created: 03/06/2013 Modified: 03/06/2013
12 exception Signal_operation of string;;
14 let delay_memory_length = 10000;;
16 class rate : int -> int -> rate_type =
17 fun (num_init : int) ->
18 fun (denom_init : int) ->
19 let rec pgcd : int -> int -> int =
22 if r = 0 then i2 else pgcd i2 r in
24 if denom_init = 0 then
25 raise (Signal_operation "sample rate denominater = 0.")
27 pgcd (abs num_init) (abs denom_init) in
29 val _num = num_init / factor
30 val _denom = denom_init / factor
36 (float_of_int self#num) /. (float_of_int self#denom)
38 (string_of_int self#num) ^ "/" ^ (string_of_int self#denom)
39 method equal : rate_type -> bool =
40 fun (r : rate_type) -> (self#num = r#num) && (self#denom = r#denom)
41 method mul : int -> rate_type =
42 fun (i : int) -> new rate (self#num * i) self#denom
43 method div : int -> rate_type =
44 fun (i : int) -> new rate self#num (self#denom * i)
48 class signal : rate_type -> (time -> value_type) -> signal_type =
49 fun (freq_init : rate_type) ->
50 fun (func_init : time -> value_type) ->
52 val mutable signal_func = func_init
53 val mutable memory_length = 0
54 method frequency = freq_init
55 method at = signal_func
57 method private check_freq : signal_type list -> rate_type =
58 fun (sl : signal_type list) ->
59 let check : rate_type -> signal_type -> rate_type =
60 fun (f : rate_type) ->
61 fun (s : signal_type) ->
62 if f#equal s#frequency || s#frequency#num = 0 then f
63 else if f#num = 0 then s#frequency
64 else raise (Signal_operation "frequency not matched.") in
65 List.fold_left check self#frequency sl
67 method add_memory : int -> unit =
70 if memory_length >= length then ()
72 let memory = Hashtbl.create length in
73 let func : time -> value =
75 try Hashtbl.find memory t
77 let result = func_init t in
78 let () = Hashtbl.replace memory t result in
80 if (t - length) >= 0 then
81 Hashtbl.remove memory (t - length)
84 memory_length <- length;
87 method private delay_by : int -> time -> value =
91 else if t >= 0 && (t - i) < 0 then
93 else raise (Signal_operation "Delay time < 0.")
95 method private prim1 :
96 (time -> value_type) -> signal_type =
97 fun (func : time -> value_type) ->
98 let freq = self#frequency in
101 method private prim2 :
102 (time -> value_type -> value_type) -> signal_type -> signal_type =
103 fun (func_binary : time -> value_type -> value_type) ->
104 fun (s : signal_type) ->
105 let freq = self#check_freq [s] in
106 let func = fun t -> (func_binary t) (s#at t) in
109 method neg = self#prim1 (fun t -> (self#at t)#neg)
110 method floor = self#prim1 (fun t -> (self#at t)#floor)
111 method ceil = self#prim1 (fun t -> (self#at t)#ceil)
112 method rint = self#prim1 (fun t -> (self#at t)#rint)
113 method sin = self#prim1 (fun t -> (self#at t)#sin)
114 method asin = self#prim1 (fun t -> (self#at t)#asin)
115 method cos = self#prim1 (fun t -> (self#at t)#cos)
116 method acos = self#prim1 (fun t -> (self#at t)#acos)
117 method tan = self#prim1 (fun t -> (self#at t)#tan)
118 method atan = self#prim1 (fun t -> (self#at t)#atan)
119 method exp = self#prim1 (fun t -> (self#at t)#exp)
120 method sqrt = self#prim1 (fun t -> (self#at t)#sqrt)
121 method ln = self#prim1 (fun t -> (self#at t)#ln)
122 method lg = self#prim1 (fun t -> (self#at t)#lg)
123 method int = self#prim1 (fun t -> (self#at t)#int)
124 method float = self#prim1 (fun t -> (self#at t)#float)
125 method abs = self#prim1 (fun t -> (self#at t)#abs)
127 method add = self#prim2 (fun t -> (self#at t)#add)
128 method sub = self#prim2 (fun t -> (self#at t)#sub)
129 method mul = self#prim2 (fun t -> (self#at t)#mul)
130 method div = self#prim2 (fun t -> (self#at t)#div)
131 method power = self#prim2 (fun t -> (self#at t)#power)
132 method _and = self#prim2 (fun t -> (self#at t)#_and)
133 method _or = self#prim2 (fun t -> (self#at t)#_or)
134 method _xor = self#prim2 (fun t -> (self#at t)#_xor)
135 method atan2 = self#prim2 (fun t -> (self#at t)#atan2)
136 method _mod = self#prim2 (fun t -> (self#at t)#_mod)
137 method fmod = self#prim2 (fun t -> (self#at t)#fmod)
138 method remainder = self#prim2 (fun t -> (self#at t)#remainder)
139 method gt = self#prim2 (fun t -> (self#at t)#gt)
140 method lt = self#prim2 (fun t -> (self#at t)#lt)
141 method geq = self#prim2 (fun t -> (self#at t)#geq)
142 method leq = self#prim2 (fun t -> (self#at t)#leq)
143 method eq = self#prim2 (fun t -> (self#at t)#eq)
144 method neq = self#prim2 (fun t -> (self#at t)#neq)
145 method max = self#prim2 (fun t -> (self#at t)#max)
146 method min = self#prim2 (fun t -> (self#at t)#min)
147 method shl = self#prim2 (fun t -> (self#at t)#shl)
148 method shr = self#prim2 (fun t -> (self#at t)#shr)
150 method delay : signal_type -> signal_type =
151 fun (s : signal_type) ->
152 let freq = self#check_freq [s] in
153 let () = self#add_memory delay_memory_length in
154 let func : time -> value_type =
156 let i = (s#at t)#to_int in
160 method mem : signal_type =
161 let freq = self#frequency in
162 let () = self#add_memory 1 in
163 let func = fun (t : time) -> self#delay_by 1 t in
166 method rdtable : signal_type -> signal_type -> signal_type =
167 fun (s_size : signal_type) ->
168 fun (s_index : signal_type) ->
169 let freq = self#check_freq [s_index] in
170 let () = self#add_memory ((s_size#at 0)#to_int) in
171 let func : time -> value_type = fun t ->
172 self#at ((s_index#at t)#to_int) in
175 method rwtable : signal_type -> signal_type ->
176 signal_type -> signal_type -> signal_type =
177 fun init -> fun wstream -> fun windex -> fun rindex ->
178 let freq = self#check_freq [init; wstream; windex; rindex] in
179 let () = init#add_memory ((self#at 0)#to_int) in
180 let () = wstream#add_memory ((self#at 0)#to_int) in
181 let func : time -> value_type = fun (ti : time) ->
182 let rec table : time -> index -> value_type =
185 (if i = (windex#at t)#to_int then (wstream#at t)
186 else table (t - 1) i)
188 (if i = (windex#at 0)#to_int then (wstream#at 0)
190 else raise (Signal_operation "signal time should be > 0") in
191 table ti ((rindex#at ti)#to_int) in
194 method select2 : signal_type -> signal_type -> signal_type =
197 let freq = self#check_freq [s_first; s_second] in
198 let func : time -> value_type =
199 fun t -> let i = (self#at t)#to_int in
200 if i = 0 then s_first#at t
201 else if i = 1 then s_second#at t
202 else raise (Signal_operation "select2 index 0|1.") in
206 signal_type -> signal_type -> signal_type -> signal_type =
207 fun s_first -> fun s_second -> fun s_third ->
208 let freq = self#check_freq [s_first; s_second; s_third] in
209 let func : time -> value_type =
210 fun t -> let i = (self#at t)#to_int in
211 if i = 0 then s_first#at t
212 else if i = 1 then s_second#at t
213 else if i = 2 then s_third#at t
214 else raise (Signal_operation "select2 index 0|1.") in
217 method prefix : signal_type -> signal_type =
218 fun (s_init : signal_type) ->
219 let () = self#add_memory 1 in
220 let func : time -> value_type =
222 if t = 0 then s_init#at 0
223 else if t > 0 then self#at (t - 1)
224 else raise (Signal_operation "prefix time < 0.") in
225 new signal self#frequency func
228 method vectorize : signal_type -> signal_type =
230 let size = (s_size#at 0)#to_int in
232 raise (Signal_operation "Vectorize: size <= 0.")
234 let freq = self#frequency#div size in
235 let func : time -> value_type =
237 let vec = fun i -> (self#at (size * t + i))#get in
238 new value (Vec (new vector size vec)) in
241 method serialize : signal_type =
243 match (self#at 0)#get with
244 | Vec vec -> vec#size
245 | _ -> raise (Signal_operation "Serialize: scalar input.") in
246 let freq = self#frequency#mul size in
247 let func : time -> value_type =
249 match (self#at (t/size))#get with
250 | Vec vec -> new value (vec#nth (t mod size))
251 | _ -> raise (Signal_operation
252 "Serialize: signal type not consistent.") in
255 method vconcat : signal_type -> signal_type =
257 let freq = self#check_freq [s] in
258 let func : time -> value_type =
260 match ((self#at t)#get, (s#at t)#get) with
261 | (Vec vec1, Vec vec2) ->
262 let size1 = vec1#size in
263 let size2 = vec2#size in
264 let size = size1 + size2 in
266 if i < size1 then vec1#nth i
267 else vec2#nth (i - size1) in
268 new value (Vec (new vector size vec))
269 | _ -> raise (Signal_operation "Vconcat: scalar.") in
272 method vpick : signal_type -> signal_type =
274 let freq = self#check_freq [s_index] in
275 let func : time -> value_type =
277 let i = (s_index#at t)#to_int in
278 match (self#at t)#get with
279 | Vec vec -> new value (vec#nth i)
280 | _ -> raise (Signal_operation "Vpick: scalar.") in