From 14f4f64258a4603e3aa2349a67d6a64839c23eb3 Mon Sep 17 00:00:00 2001 From: Kumar Appaiah Date: Fri, 9 Apr 2021 15:48:13 +0530 Subject: [PATCH] Add MLI file, alter tests --- src/owl/signal/owl_signal.ml | 13 ++----------- src/owl/signal/owl_signal.mli | 36 +++++++++++++++++++++++++++++++++++ test/unit_signal.ml | 8 ++++++-- 3 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 src/owl/signal/owl_signal.mli diff --git a/src/owl/signal/owl_signal.ml b/src/owl/signal/owl_signal.ml index 7ba906562..bd1b173e8 100644 --- a/src/owl/signal/owl_signal.ml +++ b/src/owl/signal/owl_signal.ml @@ -53,21 +53,12 @@ let dtftw r x = (*dtft for full circle (i.e whole is true)*) let a = resize r x in Owl_fft.D.fft a -let freq n = (*n is the number of frequencies where freqz is to be calculated*) - let w = Ndarray.D.linspace 0. Owl_const.pi (n+1) in - Ndarray.D.get_slice [[0;n-1]] w - -let freqf n = (*n is the number of frequencies where freqz is to be calculated (if whole is true)*) - let w = Ndarray.D.linspace 0. (2. *. Owl_const.pi) (2*n+1) in - Ndarray.D.get_slice [[0;(2*n-1)]] w - - let freqz ?(n=512) ?(whole=false) b a = (*b represents numerator array while a represent denominator array*) if whole then let x = dtftw a n in let y = dtftw b n in - Ndarray.Z.div y x + (Ndarray.D.linspace (-1.0 *. Owl_const.pi) Owl_const.pi (n+1), Ndarray.Z.div y x) else let x = dtft a n in let y = dtft b n in - Ndarray.Z.div y x + (Ndarray.D.linspace 0. Owl_const.pi (n+1), Ndarray.Z.div y x) diff --git a/src/owl/signal/owl_signal.mli b/src/owl/signal/owl_signal.mli new file mode 100644 index 000000000..fba315b78 --- /dev/null +++ b/src/owl/signal/owl_signal.mli @@ -0,0 +1,36 @@ +(* + * OWL - OCaml Scientific and Engineering Computing + * Copyright (c) 2021 Chandra Shekhar , Kumar Appaiah + *) + +(** Signal: Fundamental Signal Processing functions. *) + +open Owl_dense + +(** {Basic window functions} *) + +val blackman : int -> Ndarray.D.arr +(** Blackman window is a taper formed by using the first three terms of a summation of cosines. It was designed to have close to the minimal leakage possible. +``blackman m`` returns a blackman window. +*) + +val hamming : int -> Ndarray.D.arr +(** Hamming window is a taper formed by using a raised cosine with non-zero endpoints, optimized to minimize the nearest side lobe. ``hamming m`` +returns a hamming window. +*) + + +val hann : int -> Ndarray.D.arr +(** Hann window is a taper formed by using a raised cosine or sine-squared with ends that touch zero. ``hann m`` +returns a hann window. +*) + +(** {Filter response function} *) + +val freqz : ?n:int -> ?whole:bool -> float array -> float array -> Ndarray.D.arr * Ndarray.Z.arr +(** freqz computes the frequency response of a digital filter. + +``freqz b a`` computes the frequency response of digital filter with numerator filter coeffecient given by ``b`` (float array) while the denominator filter coeffecient given by ``a`` (float array), and returns the frequencies and the frequency response respectively in real and complex ndarrays. Two optional parameters may be specified: ``n`` is an integer that determines the number of frequencies where the frequency response is to be evaluated, and ``whole`` is a boolean that decides whether the frequency response is two-sided or one-sided. Default values of ``n`` and ``whole`` are 512 and false. +*) + +(*ends here*) diff --git a/test/unit_signal.ml b/test/unit_signal.ml index 2e094c15d..85a0a5f80 100644 --- a/test/unit_signal.ml +++ b/test/unit_signal.ml @@ -59,7 +59,8 @@ module To_test = struct M.set freqz_ref [|1|] {Complex.re = 0.6536; im = -0.7536}; M.set freqz_ref [|2|] {Complex.re = -0.5; im = -0.5}; M.set freqz_ref [|3|] {Complex.re = -0.0536; im = 0.0464}; - let f = freqz ~n:4 ~whole:false (freqz_num |> M.to_array) (freqz_den |> M.to_array) in + let f = (freqz ~n:4 ~whole:false (freqz_num |> M.to_array) (freqz_den |> M.to_array)) + |> (fun (a, b) -> b) in let max_err = (Owl_dense_ndarray.Z.map2 (fun x a-> Complex.sub x a) f freqz_ref |> Owl_dense_ndarray.Z.abs |> Owl_dense_ndarray.Z.max |> Owl_dense_ndarray.Z.re |> Arr.get) [|0|] in max_err < 1e-3 @@ -74,4 +75,7 @@ let hann () = Alcotest.(check bool) "hann" true (To_test.hann ()) let freqz () = Alcotest.(check bool) "freqz" true (To_test.freqz ()) let test_set = - [ "blackman", `Slow, blackman; "hamming", `Slow, hamming; "hann", `Slow, hann ] + [ "blackman", `Slow, blackman; + "hamming", `Slow, hamming; + "hann", `Slow, hann; + "freqz", `Slow, freqz; ]