Class: FFTS::Plan

Inherits:
Object
  • Object
show all
Defined in:
ext/ffts/ffts.c

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rb_frames, rb_sign) ⇒ Object



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
# File 'ext/ffts/ffts.c', line 13

static VALUE
ffts_plan_initialize(VALUE self, VALUE rb_frames, VALUE rb_sign) {
  ffts_plan_t *plan;

  size_t n = RARRAY_LEN(rb_frames);
  if (!is_power_of_two(n)) {
    rb_raise(rb_eRuntimeError, "Frames must be a power of 2.");
    return Qnil;
  }

  int sign_v = NUM2INT(rb_sign);
  if (sign_v != -1 && sign_v != 1) {
    rb_raise(rb_eRuntimeError, "Sign must be 1 or -1.");
    return Qnil;
  }

  plan = ffts_init_1d(n, sign_v);

  VALUE rb_plan = Data_Wrap_Struct(cCPlan, NULL, ffts_free, plan);

  rb_funcall(self, rb_intern("n="), 1, INT2NUM(n));
  rb_funcall(self, rb_intern("frames="), 1, rb_frames);
  rb_funcall(self, rb_intern("sign="), 1, rb_sign);
  rb_funcall(self, rb_intern("plan="), 1, rb_plan);

  return self;
}

Instance Attribute Details

#framesObject

#nObject

#planObject

#signObject

Instance Method Details

#executeObject



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
# File 'ext/ffts/ffts.c', line 41

static VALUE
ffts_plan_execute(VALUE self) {


  VALUE rb_plan = rb_funcall(self, rb_intern("plan"), 0);
  VALUE rb_frames = rb_funcall(self, rb_intern("frames"), 0);
  VALUE rb_n = rb_funcall(self, rb_intern("n"), 0);

  ffts_plan_t *plan;
  Data_Get_Struct(rb_plan, ffts_plan_t, plan);

  size_t n = NUM2INT(rb_n);
  if (!is_power_of_two(n)) {
    return Qnil;
  }

  size_t alloc_length = 2 * n * sizeof(float);
  float __attribute__ ((aligned(32))) *input = valloc(alloc_length);
  float __attribute__ ((aligned(32))) *output = valloc(alloc_length);

  for (int i = 0; i < n; ++i) {
    input[i] = (float)NUM2DBL(RARRAY_AREF(rb_frames, i));
  }

  ffts_execute(plan, input, output);

  VALUE rb_output = rb_ary_new();
  for (int i = 0; i < n; ++i) {
    rb_ary_push(rb_output, DBL2NUM(output[i]));
  }

  free(input);
  free(output);

  return rb_output;
}