如何在 C++ 中对来自 C++ 的数据使用 Octave

How to use octave for data from c++ in c++

我有一个 c++ 文件,它以 Float_t-arrays 的形式读入一些数据。我想在这些上应用一些八度函数 (e.q.fft)。我怎么做?我是否必须首先将 Float_t 转换为八度变量? 谢谢!

我有:

int main()
{
Float values[10];

//do magic with octave, e.q. fft

// store the data back into values or keep them (?)
}

我知道我可以做到以下几点:

int main()
{
Float values[10]={0,1,2,3,4,5,6,7,8,9};

Matrix a_matrix = Matrix (2,2);
a_matrix(0,0) = values[0];

cout << "Matrix: " << a_matrix << endl;
}

如何在值数组上使用 fft 执行此操作?

Pantxo 给了你正确的 answer on the help mailinglist。为了完整起见,我在这里添加它:

Since fft and related function are builtin you probably can include and call Ffft directly (without the need for feval) in your code. As an example the following code can be compiled with mkocfile and works for me:

//////////////////////////testfft.cc//////////////////
#include <octave/oct.h>
#include <octave/builtin-defun-decls.h>

DEFUN_DLD(testfft, args, nargout, "\
testfft\n\
")
{
  octave_value_list retval;
  int nargin = args.length ();

  retval = Ffft (args);
  return retval;
}
/////////////////////////////////////////////////////

Compile and test in Octave:

mkoctfile testfft.cc
x = 1:10;
all (testfft (x) == fft (x))

编辑

由于您在将其作为独立版本进行调整时遇到问题,我将创建另一个示例:

// file main.cc
// compile and link with mkoctfile --link-stand-alone main.cc -o bentest
#include <iostream>
#include <octave/oct.h>
#include <octave/builtin-defun-decls.h>

int main ()
{
  Matrix a = Matrix (1,4);
  for (int k = 0; k < a.columns (); ++k)
    a(0, k) = k % 2;

  std::cout << "in:" << a << std::endl;

  octave_value_list in;
  in(0) = a;

  octave_value_list out = Ffft (in, 1);
  ComplexMatrix o = out(0).complex_matrix_value ();
  std::cout << "out:" << o << std::endl;

  return 0;
}

输出。

in: 0 1 0 1

out: (2,0) (0,0) (-2,0) (0,0)