Source file owl_fft_generic.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# 1 "src/owl/fftpack/owl_fft_generic.ml"
(*
 * OWL - OCaml Scientific Computing
 * Copyright (c) 2016-2022 Liang Wang <liang@ocaml.xyz>
 *)

open Owl_dense_ndarray_generic

type tnorm =
  | Backward
  | Forward
  | Ortho

let tnorm_to_int = function
  | Backward -> 0
  | Forward -> 2
  | Ortho -> 1

let fft ?axis ?(norm : tnorm = Backward) ?(nthreads : int = 1) x =
  let axis =
    match axis with
    | Some a -> a
    | None -> num_dims x - 1
  in
  let axis = if axis < 0 then num_dims x + axis else axis in
  assert (axis < num_dims x);
  let y = empty (kind x) (shape x) in
  Owl_fftpack._owl_cfftf (kind x) x y axis (tnorm_to_int norm) nthreads;
  y


let ifft ?axis ?(norm : tnorm = Forward) ?(nthreads : int = 1) x =
  let axis =
    match axis with
    | Some a -> a
    | None -> num_dims x - 1
  in
  let axis = if axis < 0 then num_dims x + axis else axis in
  assert (axis < num_dims x);
  let y = empty (kind x) (shape x) in
  Owl_fftpack._owl_cfftb (kind x) x y axis (tnorm_to_int norm) nthreads;
  y


let rfft ?axis ?(norm : tnorm = Backward) ?(nthreads : int = 1) ~(otyp : ('a, 'b) kind) x =
  let axis =
    match axis with
    | Some a -> a
    | None -> num_dims x - 1
  in
  let axis = if axis < 0 then num_dims x + axis else axis in
  assert (axis < num_dims x);
  let s = shape x in
  s.(axis) <- (s.(axis) / 2) + 1;
  let y = empty otyp s in
  let ityp = kind x in
  Owl_fftpack._owl_rfftf ityp otyp x y axis (tnorm_to_int norm) nthreads;
  y


let irfft ?axis ?n ?(norm : tnorm = Forward) ?(nthreads : int = 1) ~(otyp : ('a, 'b) kind) x =
  let axis =
    match axis with
    | Some a -> a
    | None -> num_dims x - 1
  in
  let axis = if axis < 0 then num_dims x + axis else axis in
  assert (axis < num_dims x);
  let s = shape x in
  let _ =
    match n with
    | Some n -> s.(axis) <- n
    | None -> s.(axis) <- (s.(axis) - 1) * 2
  in
  let y = empty otyp s in
  let ityp = kind x in
  Owl_fftpack._owl_rfftb ityp otyp x y axis (tnorm_to_int norm) nthreads;
  y


let fft2 ?(norm : tnorm = Backward) ?(nthreads : int = 1) x =
  (fft ~axis:0 ~norm ~nthreads x) |> (fft ~axis:1 ~norm ~nthreads)

let ifft2 ?(norm : tnorm = Forward) ?(nthreads : int = 1) x =
  (ifft ~axis:0 ~norm ~nthreads x) |> (ifft ~axis:1 ~norm ~nthreads)

type ttrig_transform  =
  | I
  | II
  | III
  | IV

let ttrig_transform_to_int = function
  | I -> 1
  | II -> 2
  | III -> 3
  | IV -> 4

let dct ?axis ?(ttype: ttrig_transform = II) ?(norm : tnorm = Backward) ?(ortho : bool option) ?(nthreads = 1) x =
  let axis =
    match axis with
    | Some a -> a
    | None -> num_dims x - 1
  in
  let axis = if axis < 0 then num_dims x + axis else axis in
  assert (axis < num_dims x);
  let ortho =
    match ortho with
    | Some o -> o
    | None -> if norm = Ortho then true else false
  in
  let y = empty (kind x) (shape x) in
  Owl_fftpack._owl_dctf (kind x) x y axis (ttrig_transform_to_int ttype) (tnorm_to_int norm) ortho nthreads;
  y


let idct ?axis ?(ttype: ttrig_transform = II) ?(norm : tnorm = Forward) ?(ortho : bool option) ?(nthreads = 1) x =
  let axis =
    match axis with
    | Some a -> a
    | None -> num_dims x - 1
  in
  let axis = if axis < 0 then num_dims x + axis else axis in
  assert (axis < num_dims x);
  let ortho =
    match ortho with
    | Some o -> o
    | None -> if norm = Ortho then true else false
  in
  let y = empty (kind x) (shape x) in
  Owl_fftpack._owl_dctb (kind x) x y axis (ttrig_transform_to_int ttype) (tnorm_to_int norm) ortho nthreads;
  y


let dst ?axis ?(ttype: ttrig_transform = II) ?(norm : tnorm = Backward) ?(ortho : bool option) ?(nthreads = 1) x =
  let axis =
    match axis with
    | Some a -> a
    | None -> num_dims x - 1
  in
  let axis = if axis < 0 then num_dims x + axis else axis in
  assert (axis < num_dims x);
  let ortho =
    match ortho with
    | Some o -> o
    | None -> if norm = Ortho then true else false
  in
  let y = empty (kind x) (shape x) in
  Owl_fftpack._owl_dstf (kind x) x y axis (ttrig_transform_to_int ttype) (tnorm_to_int norm) ortho nthreads;
  y


let idst ?axis ?(ttype = III) ?(norm : tnorm = Forward) ?(ortho : bool option) ?(nthreads = 1) x =
  let axis =
    match axis with
    | Some a -> a
    | None -> num_dims x - 1
  in
  let axis = if axis < 0 then num_dims x + axis else axis in
  assert (axis < num_dims x);
  let ortho =
    match ortho with
    | Some o -> o
    | None -> if norm = Ortho then true else false
  in
  let y = empty (kind x) (shape x) in
  Owl_fftpack._owl_dstb (kind x) x y axis (ttrig_transform_to_int ttype) (tnorm_to_int norm) ortho nthreads;
  y