我试图让在C红宝石独立FFT扩展,基于this recipe传递红宝石数组值成C数组
我已经注意到通过红宝石和C之间的不同值的几种方法。然而,即时通讯相当新的红宝石和C,并不能解决如何将数组从一个VALUE ruby对象复制到C数组。
的编译错误: SimpleFFT.c:47:错误:下标值既不是数组,也不指针
,代码:
#include "ruby.h"
#include "fft.c" // the c file I wish to wrap in ruby
VALUE SimpleFFT = Qnil;
void Init_simplefft();
VALUE method_rfft(VALUE self, VALUE anObject);
void Init_simplefft() {
SimpleFFT = rb_define_module("SimpleFFT");
rb_define_method(SimpleFFT, "rfft", method_rfft, 1);
}
VALUE method_rfft(VALUE self, VALUE inputArr) {
int N = RARRAY_LEN(inputArr); // this works :)
// the FFT function takes an array of real and imaginary paired values
double (*x)[2] = malloc(2 * N * sizeof(double));
// and requires as an argument another such array (empty) for the transformed output
double (*X)[2] = malloc(2 * N * sizeof(double));
for(i=0; i<N; i++) {
x[i][0]=NUM2DBL(inputArr[i]); // ***THIS LINE CAUSES THE ERROR***
x[i][1]=0; // setting all the imaginary values to zero for simplicity
}
fft(N, x, X); // the target function call
// this bit should work in principle, dunno if it's optimal
VALUE outputArr = rb_ary_new();
for(i=0; i<N; i++){
rb_ary_push(outputArr, DBL2NUM(X[i][0]));
}
free(x);
free(X);
return outputArr;
}
感谢提前:)
感谢您的提示:)。我没有清醒地回答自己,哎呀!访问数组条目是否有任何原因可能比弹出数值更好或更差? – Nat 2010-12-24 02:24:08