MRFausti -> Faustine in Makefile.in.
[Faustine.git] / interpretor / signal.ml
1 (**
2 Module: Signal
3 Description: signal definition and operations.
4 @author WANG Haisheng
5 Created: 03/06/2013 Modified: 03/06/2013
6 *)
7
8 open Types;;
9 open Basic;;
10 open Value;;
11
12 exception Signal_operation of string;;
13
14 let delay_memory_length = 10000;;
15
16 class signal : int -> (time -> value_type) -> signal_type =
17 fun (freq_init : int) ->
18 fun (func_init : time -> value_type) ->
19 object (self)
20 val mutable signal_func = func_init
21 val mutable memory_length = 0
22 method frequency = freq_init
23 method at = signal_func
24
25 method private check_freq : signal_type list -> int =
26 fun (sl : signal_type list) ->
27 let check : int -> signal_type -> int =
28 fun (f : 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
34
35 method add_memory : int -> unit =
36 fun (length : int) ->
37 assert (length >= 0);
38 if memory_length >= length then ()
39 else
40 let memory = Hashtbl.create length in
41 let func : time -> value =
42 fun (t : time) ->
43 try Hashtbl.find memory t
44 with Not_found ->
45 let result = func_init t in
46 let () = Hashtbl.replace memory t result in
47 let () =
48 if (t - length) >= 0 then
49 Hashtbl.remove memory (t - length)
50 else () in
51 result in
52 memory_length <- length;
53 signal_func <- func
54
55 method private delay_by : int -> time -> value =
56 fun i -> fun t ->
57 if (t - i) >= 0 then
58 self#at (t - i)
59 else if t >= 0 && (t - i) < 0 then
60 (self#at 0)#zero
61 else raise (Signal_operation "Delay time < 0.")
62
63 method private prim1 :
64 (time -> value_type) -> signal_type =
65 fun (func : time -> value_type) ->
66 let freq = self#frequency in
67 new signal freq func
68
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
75 new signal freq func
76
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)
84
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)
93
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 =
99 fun (t : time) ->
100 let i = (s#at t)#to_int in
101 self#delay_by i t in
102 new signal freq func
103
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 1 t in
108 new signal freq func
109
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
117 new signal freq func
118
119 method select2 : signal_type -> signal_type -> signal_type =
120 fun s_first ->
121 fun s_second ->
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
128 new signal freq func
129
130 method select3 :
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
140 new signal freq func
141
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 =
146 fun t ->
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
151
152
153 method vectorize : signal_type -> signal_type =
154 fun s_size ->
155 let size = (s_size#at 0)#to_int in
156 if size <= 0 then
157 raise (Signal_operation "Vectorize: size <= 0.")
158 else
159 let freq = self#frequency / size in
160 let func : time -> value_type =
161 fun t ->
162 let vec = fun i -> (self#at (size * t + i))#get in
163 new value (Vec (new vector size vec)) in
164 new signal freq func
165
166
167 method serialize : signal_type =
168 let size =
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 =
174 fun t ->
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
179 new signal freq func
180
181 method vconcat : signal_type -> signal_type =
182 fun s ->
183 let freq = self#check_freq [s] in
184 let func : time -> value_type =
185 fun t ->
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
191 let vec = fun i ->
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
196 new signal freq func
197
198 method vpick : signal_type -> signal_type =
199 fun s_index ->
200 let freq = self#check_freq [s_index] in
201 let func : time -> value_type =
202 fun t ->
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
207 new signal freq func
208
209 end;;