Skip to content

Commit

Permalink
Add MLI file, alter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kumanna authored and ryanrhymes committed Apr 10, 2021
1 parent a735e6f commit 14f4f64
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
13 changes: 2 additions & 11 deletions src/owl/signal/owl_signal.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
36 changes: 36 additions & 0 deletions src/owl/signal/owl_signal.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
(*
* OWL - OCaml Scientific and Engineering Computing
* Copyright (c) 2021 Chandra Shekhar <[email protected]>, Kumar Appaiah <[email protected]>
*)

(** 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*)
8 changes: 6 additions & 2 deletions test/unit_signal.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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; ]

0 comments on commit 14f4f64

Please sign in to comment.