• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

gabyfle / SoundML / 40

26 Jul 2025 12:07PM UTC coverage: 68.286% (+14.9%) from 53.422%
40

push

github

gabyfle
Migrating IO to RavenML

25 of 27 new or added lines in 3 files covered. (92.59%)

32 existing lines in 3 files now uncovered.

239 of 350 relevant lines covered (68.29%)

5.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

50.0
/src/window.ml
1
(*****************************************************************************)
2
(*                                                                           *)
3
(*                                                                           *)
4
(*  Copyright (C) 2025                                                       *)
5
(*    Gabriel Santamaria                                                     *)
6
(*                                                                           *)
7
(*                                                                           *)
8
(*  Licensed under the Apache License, Version 2.0 (the "License");          *)
9
(*  you may not use this file except in compliance with the License.         *)
10
(*  You may obtain a copy of the License at                                  *)
11
(*                                                                           *)
12
(*    http://www.apache.org/licenses/LICENSE-2.0                             *)
13
(*                                                                           *)
14
(*  Unless required by applicable law or agreed to in writing, software      *)
15
(*  distributed under the License is distributed on an "AS IS" BASIS,        *)
16
(*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *)
17
(*  See the License for the specific language governing permissions and      *)
18
(*  limitations under the License.                                           *)
19
(*                                                                           *)
20
(*****************************************************************************)
21

22
type window = [`Hanning | `Hamming | `Blackman | `Boxcar]
23

UNCOV
24
let cosine_sum ?(fftbins = false) (dtype : ('a, 'b) Nx.dtype) (a : float array)
×
25
    m =
26
  if m < 0 then invalid_arg "Window length M must be a non-negative integer"
1✔
27
  else if m = 0 then Nx.empty dtype [|0|]
2✔
28
  else if m = 1 then Nx.ones dtype [|1|]
2✔
29
  else
30
    let sym = not fftbins in
13✔
31
    let fac =
32
      if sym then Nx.linspace dtype (-.Float.pi) Float.pi m
7✔
33
      else
34
        let full_range = Nx.linspace dtype (-.Float.pi) Float.pi (m + 1) in
6✔
35
        Nx.slice [R [0; m]] full_range
6✔
36
    in
37
    let w =
38
      if Array.length a > 0 then Nx.full dtype [|m|] a.(0)
11✔
39
      else Nx.zeros dtype [|m|]
2✔
40
    in
41
    let w =
42
      Array.fold_left
43
        (fun acc (k, coeff) ->
44
          if k = 0 || coeff = 0.0 then acc
1✔
45
          else
46
            let cos_args = Nx.mul_s fac (float_of_int k) in
11✔
47
            let cos_terms = Nx.cos cos_args in
11✔
48
            let term = Nx.mul_s cos_terms coeff in
11✔
49
            Nx.add acc term )
11✔
50
        w
51
        (Array.mapi (fun i x -> (i, x)) a)
13✔
52
    in
53
    w
13✔
54

UNCOV
55
let hanning ?(fftbins = false) (dtype : ('a, 'b) Nx.dtype) m =
×
56
  cosine_sum ~fftbins dtype [|0.5; 1. -. 0.5|] m
×
57

UNCOV
58
let hamming ?(fftbins = false) (dtype : ('a, 'b) Nx.dtype) m =
×
59
  cosine_sum ~fftbins dtype [|0.54; 1. -. 0.54|] m
×
60

UNCOV
61
let blackman ?(fftbins = false) (dtype : ('a, 'b) Nx.dtype) m =
×
62
  cosine_sum ~fftbins dtype [|0.42; 0.5; 0.08|] m
×
63

64
let boxcar (dtype : ('a, 'b) Nx.dtype) (size : int) : ('a, 'b) Nx.t =
65
  if size < 0 then failwith "Window length M must be non-negative"
×
UNCOV
66
  else if size = 0 then Nx.empty dtype [|0|]
×
67
  else Nx.ones dtype [|size|]
×
68

69
let get (typ : window) (dtype : ('a, 'b) Nx.dtype) :
70
    ?fftbins:bool -> int -> ('a, 'b) Nx.t =
UNCOV
71
 fun ?fftbins size ->
×
UNCOV
72
  match typ with
×
UNCOV
73
  | `Hanning ->
×
74
      hanning ?fftbins dtype size
UNCOV
75
  | `Hamming ->
×
76
      hamming ?fftbins dtype size
UNCOV
77
  | `Blackman ->
×
78
      blackman ?fftbins dtype size
79
  | `Boxcar ->
×
80
      boxcar dtype size
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc