X-Git-Url: https://scm.cri.mines-paristech.fr/git/Faustine.git/blobdiff_plain/7aa377f6b67020aa1dff235ebb100943375cac94..e8e5ebaac3fd20af4989400e61811dc59ccbc6b8:/interpretor/signal.ml?ds=inline diff --git a/interpretor/signal.ml b/interpretor/signal.ml index ae312b0..66089e9 100644 --- a/interpretor/signal.ml +++ b/interpretor/signal.ml @@ -13,8 +13,45 @@ exception Signal_operation of string;; let delay_memory_length = 10000;; -class signal : int -> (time -> value_type) -> signal_type = - fun (freq_init : int) -> +class rate : int -> int -> rate_type = + fun (num_init : int) -> + fun (denom_init : int) -> + let rec pgcd : int -> int -> int = + fun i1 -> fun i2 -> + let r = i1 mod i2 in + if r = 0 then i2 else pgcd i2 r in + let num_positive = + if num_init >= 0 then num_init + else (-num_init) in + let denom_positive = + if denom_init > 0 then denom_init + else if denom_init < 0 then -denom_init + else raise (Signal_operation "sample rate denominater = 0.") in + let factor = pgcd num_positive denom_positive in + let num_corrected = num_init / factor in + let denom_corrected = denom_init / factor in + object (self) + val _num = num_corrected + val _denom = denom_corrected + method num = _num + method denom = _denom + method to_int = + self#num / self#denom + method to_float = + (float_of_int self#num) /. (float_of_int self#denom) + method to_string = + (string_of_int self#num) ^ "/" ^ (string_of_int self#denom) + method equal : rate_type -> bool = + fun (r : rate_type) -> (self#num = r#num) && (self#denom = r#denom) + method mul : int -> rate_type = + fun (i : int) -> new rate (self#num * i) self#denom + method div : int -> rate_type = + fun (i : int) -> new rate self#num (self#denom * i) + end + + +class signal : rate_type -> (time -> value_type) -> signal_type = + fun (freq_init : rate_type) -> fun (func_init : time -> value_type) -> object (self) val mutable signal_func = func_init @@ -22,13 +59,13 @@ class signal : int -> (time -> value_type) -> signal_type = method frequency = freq_init method at = signal_func - method private check_freq : signal_type list -> int = + method private check_freq : signal_type list -> rate_type = fun (sl : signal_type list) -> - let check : int -> signal_type -> int = - fun (f : int) -> + let check : rate_type -> signal_type -> rate_type = + fun (f : rate_type) -> fun (s : signal_type) -> - if f = s#frequency || s#frequency = 0 then f - else if f = 0 then s#frequency + if f#equal s#frequency || s#frequency#num = 0 then f + else if f#num = 0 then s#frequency else raise (Signal_operation "frequency not matched.") in List.fold_left check self#frequency sl @@ -76,20 +113,44 @@ class signal : int -> (time -> value_type) -> signal_type = method neg = self#prim1 (fun t -> (self#at t)#neg) method floor = self#prim1 (fun t -> (self#at t)#floor) + method ceil = self#prim1 (fun t -> (self#at t)#ceil) + method rint = self#prim1 (fun t -> (self#at t)#rint) method sin = self#prim1 (fun t -> (self#at t)#sin) + method asin = self#prim1 (fun t -> (self#at t)#asin) method cos = self#prim1 (fun t -> (self#at t)#cos) + method acos = self#prim1 (fun t -> (self#at t)#acos) + method tan = self#prim1 (fun t -> (self#at t)#tan) method atan = self#prim1 (fun t -> (self#at t)#atan) + method exp = self#prim1 (fun t -> (self#at t)#exp) method sqrt = self#prim1 (fun t -> (self#at t)#sqrt) + method ln = self#prim1 (fun t -> (self#at t)#ln) + method lg = self#prim1 (fun t -> (self#at t)#lg) method int = self#prim1 (fun t -> (self#at t)#int) + method float = self#prim1 (fun t -> (self#at t)#float) + method abs = self#prim1 (fun t -> (self#at t)#abs) method add = self#prim2 (fun t -> (self#at t)#add) method sub = self#prim2 (fun t -> (self#at t)#sub) method mul = self#prim2 (fun t -> (self#at t)#mul) method div = self#prim2 (fun t -> (self#at t)#div) + method power = self#prim2 (fun t -> (self#at t)#power) + method _and = self#prim2 (fun t -> (self#at t)#_and) + method _or = self#prim2 (fun t -> (self#at t)#_or) + method _xor = self#prim2 (fun t -> (self#at t)#_xor) method atan2 = self#prim2 (fun t -> (self#at t)#atan2) method _mod = self#prim2 (fun t -> (self#at t)#_mod) - method larger = self#prim2 (fun t -> (self#at t)#larger) - method smaller = self#prim2 (fun t -> (self#at t)#smaller) + method fmod = self#prim2 (fun t -> (self#at t)#fmod) + method remainder = self#prim2 (fun t -> (self#at t)#remainder) + method gt = self#prim2 (fun t -> (self#at t)#gt) + method lt = self#prim2 (fun t -> (self#at t)#lt) + method geq = self#prim2 (fun t -> (self#at t)#geq) + method leq = self#prim2 (fun t -> (self#at t)#leq) + method eq = self#prim2 (fun t -> (self#at t)#eq) + method neq = self#prim2 (fun t -> (self#at t)#neq) + method max = self#prim2 (fun t -> (self#at t)#max) + method min = self#prim2 (fun t -> (self#at t)#min) + method shl = self#prim2 (fun t -> (self#at t)#shl) + method shr = self#prim2 (fun t -> (self#at t)#shr) method delay : signal_type -> signal_type = fun (s : signal_type) -> @@ -98,13 +159,13 @@ class signal : int -> (time -> value_type) -> signal_type = let func : time -> value_type = fun (t : time) -> let i = (s#at t)#to_int in - self#delay_by t i in + self#delay_by i t in new signal freq func method mem : signal_type = let freq = self#frequency in let () = self#add_memory 1 in - let func = fun (t : time) -> self#delay_by t 1 in + let func = fun (t : time) -> self#delay_by 1 t in new signal freq func method rdtable : signal_type -> signal_type -> signal_type = @@ -116,6 +177,25 @@ class signal : int -> (time -> value_type) -> signal_type = self#at ((s_index#at t)#to_int) in new signal freq func + method rwtable : signal_type -> signal_type -> + signal_type -> signal_type -> signal_type = + fun init -> fun wstream -> fun windex -> fun rindex -> + let freq = self#check_freq [init; wstream; windex; rindex] in + let () = init#add_memory ((self#at 0)#to_int) in + let () = wstream#add_memory ((self#at 0)#to_int) in + let func : time -> value_type = fun (ti : time) -> + let rec table : time -> index -> value_type = + fun t -> fun i -> + if t > 0 then + (if i = (windex#at t)#to_int then (wstream#at t) + else table (t - 1) i) + else if t = 0 then + (if i = (windex#at 0)#to_int then (wstream#at 0) + else init#at i) + else raise (Signal_operation "signal time should be > 0") in + table ti ((rindex#at ti)#to_int) in + new signal freq func + method select2 : signal_type -> signal_type -> signal_type = fun s_first -> fun s_second -> @@ -156,7 +236,7 @@ class signal : int -> (time -> value_type) -> signal_type = if size <= 0 then raise (Signal_operation "Vectorize: size <= 0.") else - let freq = self#frequency / size in + let freq = self#frequency#div size in let func : time -> value_type = fun t -> let vec = fun i -> (self#at (size * t + i))#get in @@ -169,7 +249,7 @@ class signal : int -> (time -> value_type) -> signal_type = match (self#at 0)#get with | Vec vec -> vec#size | _ -> raise (Signal_operation "Serialize: scalar input.") in - let freq = self#frequency * size in + let freq = self#frequency#mul size in let func : time -> value_type = fun t -> match (self#at (t/size))#get with