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 signal : int -> (time -> value_type) -> signal_type =
17 fun (freq_init : int) ->
18 fun (func_init : time -> value_type) ->
20 val mutable signal_func = func_init
21 val mutable memory_length = 0
22 method frequency = freq_init
23 method at = signal_func
25 method private check_freq : signal_type list -> int =
26 fun (sl : signal_type list) ->
27 let check : int -> signal_type -> int =
29 fun (s : signal_type) ->
30 if f = s#frequency || s#frequency = 0 then f
31 else if f = 0 then s#frequency
32 else raise (Signal_operation "frequency not matched.") in
33 List.fold_left check self#frequency sl
35 method add_memory : int -> unit =
38 if memory_length >= length then ()
40 let memory = Hashtbl.create length in
41 let func : time -> value =
43 try Hashtbl.find memory t
45 let result = func_init t in
46 let () = Hashtbl.replace memory t result in
48 if (t - length) >= 0 then
49 Hashtbl.remove memory (t - length)
52 memory_length <- length;
55 method private delay_by : int -> time -> value =
59 else if t >= 0 && (t - i) < 0 then
61 else raise (Signal_operation "Delay time < 0.")
63 method private prim1 :
64 (time -> value_type) -> signal_type =
65 fun (func : time -> value_type) ->
66 let freq = self#frequency in
69 method private prim2 :
70 (time -> value_type -> value_type) -> signal_type -> signal_type =
71 fun (func_binary : time -> value_type -> value_type) ->
72 fun (s : signal_type) ->
73 let freq = self#check_freq [s] in
74 let func = fun t -> (func_binary t) (s#at t) in
77 method neg = self#prim1 (fun t -> (self#at t)#neg)
78 method floor = self#prim1 (fun t -> (self#at t)#floor)
79 method sin = self#prim1 (fun t -> (self#at t)#sin)
80 method cos = self#prim1 (fun t -> (self#at t)#cos)
81 method atan = self#prim1 (fun t -> (self#at t)#atan)
82 method sqrt = self#prim1 (fun t -> (self#at t)#sqrt)
83 method int = self#prim1 (fun t -> (self#at t)#int)
85 method add = self#prim2 (fun t -> (self#at t)#add)
86 method sub = self#prim2 (fun t -> (self#at t)#sub)
87 method mul = self#prim2 (fun t -> (self#at t)#mul)
88 method div = self#prim2 (fun t -> (self#at t)#div)
89 method atan2 = self#prim2 (fun t -> (self#at t)#atan2)
90 method _mod = self#prim2 (fun t -> (self#at t)#_mod)
91 method larger = self#prim2 (fun t -> (self#at t)#larger)
92 method smaller = self#prim2 (fun t -> (self#at t)#smaller)
94 method delay : signal_type -> signal_type =
95 fun (s : signal_type) ->
96 let freq = self#check_freq [s] in
97 let () = self#add_memory delay_memory_length in
98 let func : time -> value_type =
100 let i = (s#at t)#to_int in
104 method mem : signal_type =
105 let freq = self#frequency in
106 let () = self#add_memory 1 in
107 let func = fun (t : time) -> self#delay_by t 1 in
110 method rdtable : signal_type -> signal_type -> signal_type =
111 fun (s_size : signal_type) ->
112 fun (s_index : signal_type) ->
113 let freq = self#check_freq [s_index] in
114 let () = self#add_memory ((s_size#at 0)#to_int) in
115 let func : time -> value_type = fun t ->
116 self#at ((s_index#at t)#to_int) in
119 method select2 : signal_type -> signal_type -> signal_type =
122 let freq = self#check_freq [s_first; s_second] in
123 let func : time -> value_type =
124 fun t -> let i = (self#at t)#to_int in
125 if i = 0 then s_first#at t
126 else if i = 1 then s_second#at t
127 else raise (Signal_operation "select2 index 0|1.") in
131 signal_type -> signal_type -> signal_type -> signal_type =
132 fun s_first -> fun s_second -> fun s_third ->
133 let freq = self#check_freq [s_first; s_second; s_third] in
134 let func : time -> value_type =
135 fun t -> let i = (self#at t)#to_int in
136 if i = 0 then s_first#at t
137 else if i = 1 then s_second#at t
138 else if i = 2 then s_third#at t
139 else raise (Signal_operation "select2 index 0|1.") in
142 method prefix : signal_type -> signal_type =
143 fun (s_init : signal_type) ->
144 let () = self#add_memory 1 in
145 let func : time -> value_type =
147 if t = 0 then s_init#at 0
148 else if t > 0 then self#at (t - 1)
149 else raise (Signal_operation "prefix time < 0.") in
150 new signal self#frequency func
153 method vectorize : signal_type -> signal_type =
155 let size = (s_size#at 0)#to_int in
157 raise (Signal_operation "Vectorize: size <= 0.")
159 let freq = self#frequency / size in
160 let func : time -> value_type =
162 let vec = fun i -> (self#at (size * t + i))#get in
163 new value (Vec (new vector size vec)) in
167 method serialize : signal_type =
169 match (self#at 0)#get with
170 | Vec vec -> vec#size
171 | _ -> raise (Signal_operation "Serialize: scalar input.") in
172 let freq = self#frequency * size in
173 let func : time -> value_type =
175 match (self#at (t/size))#get with
176 | Vec vec -> new value (vec#nth (t mod size))
177 | _ -> raise (Signal_operation
178 "Serialize: signal type not consistent.") in
181 method vconcat : signal_type -> signal_type =
183 let freq = self#check_freq [s] in
184 let func : time -> value_type =
186 match ((self#at t)#get, (s#at t)#get) with
187 | (Vec vec1, Vec vec2) ->
188 let size1 = vec1#size in
189 let size2 = vec2#size in
190 let size = size1 + size2 in
192 if i < size1 then vec1#nth i
193 else vec2#nth (i - size1) in
194 new value (Vec (new vector size vec))
195 | _ -> raise (Signal_operation "Vconcat: scalar.") in
198 method vpick : signal_type -> signal_type =
200 let freq = self#check_freq [s_index] in
201 let func : time -> value_type =
203 let i = (s_index#at t)#to_int in
204 match (self#at t)#get with
205 | Vec vec -> new value (vec#nth i)
206 | _ -> raise (Signal_operation "Vpick: scalar.") in