File: mlgsl_fft.c

package info (click to toggle)
ocamlgsl 0.3.5-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,444 kB
  • ctags: 2,901
  • sloc: ml: 7,956; ansic: 6,796; makefile: 303; sh: 87; awk: 13
file content (332 lines) | stat: -rw-r--r-- 9,495 bytes parent folder | download
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* ocamlgsl - OCaml interface to GSL                        */
/* Copyright () 2002 - Olivier Andrieu                     */
/* distributed under the terms of the GPL version 2         */

#include <caml/fail.h>
#include <caml/memory.h>
#include <caml/callback.h>

#include <gsl/gsl_fft.h>
#include <gsl/gsl_fft_complex.h>
#include <gsl/gsl_fft_halfcomplex.h>
#include <gsl/gsl_fft_real.h>

#include "wrappers.h"

enum mlgsl_fft_array_layout {
  LAYOUT_REAL    = 0 ,
  LAYOUT_HC      = 1 ,
  LAYOUT_HC_RAD2 = 2 ,
  LAYOUT_C       = 3 ,
} ;

static void check_layout(value fft_arr, 
				 enum mlgsl_fft_array_layout layout)
{
  static value *layout_exn = NULL;
  if(Int_val(Field(fft_arr, 0)) != layout) { 
    if(!layout_exn) {
      layout_exn = caml_named_value("mlgsl_layout_exn");
      if(!layout_exn) /* Gromeleu */
	invalid_argument("wrong fft_array layout");
    }
    raise_constant(*layout_exn);
  }
}

static inline void update_layout(value fft_arr, 
				 enum mlgsl_fft_array_layout layout)
{
  Store_field(fft_arr, 0, Val_int(layout));
}



/* WORKSPACE AND WAVETABLES */

#define GSL_REAL_WS(v)        ((gsl_fft_real_workspace *)Field((v),0))
#define GSL_COMPLEX_WS(v)     ((gsl_fft_complex_workspace *)Field((v),0))
#define GSL_REAL_WT(v)        ((gsl_fft_real_wavetable *)Field((v),0))
#define GSL_HALFCOMPLEX_WT(v) ((gsl_fft_halfcomplex_wavetable *)Field((v),0))
#define GSL_COMPLEX_WT(v)     ((gsl_fft_complex_wavetable *)Field((v),0))

ML1_alloc(gsl_fft_real_workspace_alloc, Int_val, Abstract_ptr)
ML1_alloc(gsl_fft_complex_workspace_alloc, Int_val, Abstract_ptr)
ML1_alloc(gsl_fft_real_wavetable_alloc, Int_val, Abstract_ptr)
ML1_alloc(gsl_fft_halfcomplex_wavetable_alloc, Int_val, Abstract_ptr)
ML1_alloc(gsl_fft_complex_wavetable_alloc, Int_val, Abstract_ptr)

ML1(gsl_fft_real_workspace_free, GSL_REAL_WS, Unit)
ML1(gsl_fft_complex_workspace_free, GSL_COMPLEX_WS, Unit)
ML1(gsl_fft_real_wavetable_free, GSL_REAL_WT, Unit)
ML1(gsl_fft_halfcomplex_wavetable_free, GSL_HALFCOMPLEX_WT, Unit)
ML1(gsl_fft_complex_wavetable_free, GSL_COMPLEX_WT, Unit)


/* UNPACKING ROUTINES */

value ml_gsl_fft_real_unpack(value stride, value r, value c)
{
  const size_t c_stride = Opt_arg(stride, Int_val, 1);
  const size_t n = Double_array_length(r);
  gsl_fft_real_unpack(Double_array_val(r), Double_array_val(c), c_stride, n) ;
  return Val_unit;
}

value ml_gsl_fft_halfcomplex_unpack(value stride, value hc, value c)
{
  const size_t c_stride = Opt_arg(stride, Int_val, 1);
  const size_t n = Double_array_length(hc);
  gsl_fft_halfcomplex_unpack(Double_array_val(hc), Double_array_val(c),
			     c_stride, n) ;
  return Val_unit;
}

value ml_gsl_fft_halfcomplex_unpack_rad2(value stride, value hc, value c)
{
  const size_t c_stride = Opt_arg(stride, Int_val ,1);
  const size_t n = Double_array_length(hc);
  gsl_fft_halfcomplex_radix2_unpack(Double_array_val(hc), Double_array_val(c),
				    c_stride, n) ;
  return Val_unit;
}



/* REAL AND HALFCOMPLEX MIXED-RADIX FFT */

value ml_gsl_fft_real_transform(value stride, value fft_arr, value wt, value ws)
{
  value data = Field(fft_arr, 1);
  const size_t c_stride = Opt_arg(stride, Int_val, 1);
  const size_t n = Double_array_length(data);

  check_layout(fft_arr, LAYOUT_REAL);
  gsl_fft_real_transform(Double_array_val(data), c_stride, n,
			 GSL_REAL_WT(wt), GSL_REAL_WS(ws)) ;
  update_layout(fft_arr, LAYOUT_HC);

  return Val_unit;
}

value ml_gsl_fft_halfcomplex_transform(value stride, value fft_arr, 
				       value wt, value ws)
{
  value data = Field(fft_arr, 1);
  const size_t c_stride = Opt_arg(stride, Int_val, 1);
  const size_t n = Double_array_length(data);

  check_layout(fft_arr, LAYOUT_HC);
  gsl_fft_halfcomplex_transform(Double_array_val(data), c_stride, n,
				GSL_HALFCOMPLEX_WT(wt), 
				GSL_REAL_WS(ws)) ;

  return Val_unit;
}

value ml_gsl_fft_halfcomplex_backward(value stride, value fft_arr,
				      value wt, value ws)
{
  value data = Field(fft_arr, 1);
  const size_t c_stride = Opt_arg(stride, Int_val, 1);
  const size_t n = Double_array_length(data);

  check_layout(fft_arr, LAYOUT_HC);
  gsl_fft_halfcomplex_backward(Double_array_val(data), c_stride, n,
			       GSL_HALFCOMPLEX_WT(wt), 
			       GSL_REAL_WS(ws)) ;
  update_layout(fft_arr, LAYOUT_REAL);

  return Val_unit;
}

value ml_gsl_fft_halfcomplex_inverse(value stride, value fft_arr,
				     value wt, value ws)
{
  value data = Field(fft_arr, 1);
  const size_t c_stride = Opt_arg(stride, Int_val, 1);
  const size_t n = Double_array_length(data);

  check_layout(fft_arr, LAYOUT_HC);
  gsl_fft_halfcomplex_inverse(Double_array_val(data), c_stride, n,
			      GSL_HALFCOMPLEX_WT(wt), 
			      GSL_REAL_WS(ws)) ;
  update_layout(fft_arr, LAYOUT_REAL);

  return Val_unit;
}



/* REAL AND HALFCOMPLEX RADIX2 FFT */

value ml_gsl_fft_real_radix2_transform(value stride, value fft_arr)
{
  value data = Field(fft_arr, 1);
  size_t N = Double_array_length(data);
  size_t c_stride = Opt_arg(stride, Int_val, 1);

  check_layout(fft_arr, LAYOUT_REAL);
  gsl_fft_real_radix2_transform(Double_array_val(data), c_stride, N);
  update_layout(fft_arr, LAYOUT_HC_RAD2);

  return Val_unit;
}

value ml_gsl_fft_halfcomplex_radix2_transform(value stride, value fft_arr)
{
  value data = Field(fft_arr, 1);
  size_t N = Double_array_length(data);
  size_t c_stride = Opt_arg(stride, Int_val, 1);

  check_layout(fft_arr, LAYOUT_HC_RAD2);
  gsl_fft_halfcomplex_radix2_transform(Double_array_val(data), c_stride, N);

  return Val_unit;
}

value ml_gsl_fft_halfcomplex_radix2_backward(value stride, value fft_arr)
{
  value data = Field(fft_arr, 1);
  size_t N = Double_array_length(data);
  size_t c_stride = Opt_arg(stride, Int_val, 1);

  check_layout(fft_arr, LAYOUT_HC_RAD2);
  gsl_fft_halfcomplex_radix2_backward(Double_array_val(data), 
				      c_stride, N);
  update_layout(fft_arr, LAYOUT_REAL);

  return Val_unit;
}

value ml_gsl_fft_halfcomplex_radix2_inverse(value stride, value fft_arr)
{
  value data = Field(fft_arr, 1);
  size_t N = Double_array_length(data);
  size_t c_stride = Opt_arg(stride, Int_val, 1);

  check_layout(fft_arr, LAYOUT_HC_RAD2);
  gsl_fft_halfcomplex_radix2_inverse(Double_array_val(data), c_stride, N);
  update_layout(fft_arr, LAYOUT_REAL);

  return Val_unit;
}



/* COMPLEX RADIX-2 FFT */

value ml_gsl_fft_complex_rad2_forward(value dif, value stride, value data)
{
  size_t N = Double_array_length(data);
  size_t c_stride = Opt_arg(stride, Int_val, 1);
  int c_dif = Opt_arg(dif, Bool_val, 0);

  if(c_dif)
    gsl_fft_complex_radix2_dif_forward(Double_array_val(data), c_stride, N);
  else
    gsl_fft_complex_radix2_forward(Double_array_val(data), c_stride, N);
  
  return Val_unit;
}

value ml_gsl_fft_complex_rad2_transform(value dif, value stride, 
					value data, value sign)
{
  size_t N = Double_array_length(data);
  size_t c_stride = Opt_arg(stride, Int_val, 1);
  int c_dif = Opt_arg(dif, Bool_val, 0);
  gsl_fft_direction c_sign = (Int_val(sign)==0) ? forward : backward;

  if(c_dif)
    gsl_fft_complex_radix2_dif_transform(Double_array_val(data), c_stride, 
					 N, c_sign);
  else
    gsl_fft_complex_radix2_transform(Double_array_val(data), c_stride, 
				     N, c_sign);
  
  return Val_unit;
}

value ml_gsl_fft_complex_rad2_backward(value dif, value stride, value data)
{
  size_t N = Double_array_length(data);
  size_t c_stride = Opt_arg(stride, Int_val, 1);
  int c_dif = Opt_arg(dif, Bool_val, 0);

  if(c_dif)
    gsl_fft_complex_radix2_dif_backward(Double_array_val(data), c_stride, N);
  else
    gsl_fft_complex_radix2_backward(Double_array_val(data), c_stride, N);
  
  return Val_unit;
}

value ml_gsl_fft_complex_rad2_inverse(value dif, value stride, value data)
{
  size_t N = Double_array_length(data);
  size_t c_stride = Opt_arg(stride, Int_val, 1);
  int c_dif = Opt_arg(dif, Bool_val, 0);

  if(c_dif)
    gsl_fft_complex_radix2_dif_inverse(Double_array_val(data), c_stride, N);
  else
    gsl_fft_complex_radix2_inverse(Double_array_val(data), c_stride, N);

  return Val_unit;
}




/* COMPLEX MIXED RADIX FFT */

value ml_gsl_fft_complex_forward(value stride, value data, value wt, value ws)
{
  const size_t c_stride = Opt_arg(stride, Int_val, 1);
  const size_t n = Double_array_length(data) / 2;

  gsl_fft_complex_forward(Double_array_val(data), c_stride, n,
			  GSL_COMPLEX_WT(wt), GSL_COMPLEX_WS(ws)) ;
  return Val_unit;
}

value ml_gsl_fft_complex_transform(value stride, value data, 
				   value wt, value ws, value sign)
{
  const size_t c_stride = Opt_arg(stride, Int_val, 1);
  const size_t n = Double_array_length(data) / 2;
  gsl_fft_direction c_sign = (Int_val(sign)==0) ? forward : backward;

  gsl_fft_complex_transform(Double_array_val(data), c_stride, n,
			    GSL_COMPLEX_WT(wt), 
			    GSL_COMPLEX_WS(ws), c_sign) ;

  return Val_unit;
}

value ml_gsl_fft_complex_backward(value stride, value data, 
				  value wt, value ws)
{
  const size_t c_stride = Opt_arg(stride, Int_val, 1);
  const size_t n = Double_array_length(data) / 2;

  gsl_fft_complex_backward(Double_array_val(data), c_stride, n,
			   GSL_COMPLEX_WT(wt), 
			   GSL_COMPLEX_WS(ws)) ;

  return Val_unit;
}

value ml_gsl_fft_complex_inverse(value stride, value data,
				 value wt, value ws)
{
  const size_t c_stride = Opt_arg(stride, Int_val ,1);
  const size_t n = Double_array_length(data) / 2;

  gsl_fft_complex_inverse(Double_array_val(data), c_stride, n,
			  GSL_COMPLEX_WT(wt), 
			  GSL_COMPLEX_WS(ws)) ;

  return Val_unit;
}