how to use fft in c++

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

how to use fft in c++

Progressive
Hi,

I have a c++ file with octave embedded in it. Calculating via Matrices and so on works fine but I don't get it how do I use functions, e.q. the fft in c++ / upon variables from/in c++ ?

The documentation https://www.gnu.org/software/octave/doc/v4.0.1/Signal-Processing.html just tells fft(x).

Just briefly: I have an array in c++ and I want to apply fft on it. How do I do that?

Thank you very much!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to use fft in c++

Carlo de Falco-2

> On 7 Jun 2017, at 21:27, Progressive <[hidden email]> wrote:
>
> Hi,
>
> I have a c++ file with octave embedded in it. Calculating via Matrices and
> so on works fine but I don't get it how do I use functions, e.q. the fft in
> c++ / upon variables from/in c++ ?
>
> The documentation
> https://www.gnu.org/software/octave/doc/v4.0.1/Signal-Processing.html just
> tells fft(x).
>
> Just briefly: I have an array in c++ and I want to apply fft on it. How do I
> do that?
>
> Thank you very much!
>

you could invoke the interpreter doing something like this (untested):

 ColumnVector x, y;
 octave_value_list in, out;

 in(0) = x;
 out = feval ("fft", in, 1);
 y = out(0).column_vector_value ();

but if you are looking for performance you are probably better off using fftw's API directly.

c.




_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to use fft in c++

Pantxo
Carlo de Falco-2 wrote
> On 7 Jun 2017, at 21:27, Progressive <[hidden email]> wrote:
>
> Hi,
>
> I have a c++ file with octave embedded in it. Calculating via Matrices and
> so on works fine but I don't get it how do I use functions, e.q. the fft in
> c++ / upon variables from/in c++ ?
>
> The documentation
> https://www.gnu.org/software/octave/doc/v4.0.1/Signal-Processing.html just
> tells fft(x).
>
> Just briefly: I have an array in c++ and I want to apply fft on it. How do I
> do that?
>
> Thank you very much!
>

you could invoke the interpreter doing something like this (untested):

 ColumnVector x, y;
 octave_value_list in, out;

 in(0) = x;
 out = feval ("fft", in, 1);
 y = out(0).column_vector_value ();

but if you are looking for performance you are probably better off using fftw's API directly.

c.




_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Since fft and related function are builtin you probably can include <octave/builtin-defun-decls.h> 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))
 
Pantxo
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to use fft in c++

Andreas Weber-6
In reply to this post by Progressive
Am 07.06.2017 um 21:27 schrieb Progressive:
> I have a c++ file with octave embedded in it. Calculating via Matrices and
> so on works fine but I don't get it how do I use functions, e.q. the fft in
> c++ / upon variables from/in c++ ?

Crossposting on stackoverflow
https://stackoverflow.com/questions/44419812/how-to-use-octave-for-data-from-c-in-c

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to use fft in c++

Progressive
This post was updated on .
In reply to this post by Pantxo
Well, at first thank you very much!

I'm trying to implement to that and since it doesn't work I stick to a basic try:

                DEFUN_DLD (helloworld, args, nargout,"Hello World Help String")
                {

                }

from https://www.gnu.org/software/octave/doc/v4.0.1/Getting-Started-with-Oct_002dFiles.html already doesn't work. I receive the following:

user@User-PC:~/analysis/analyse_tree/Filter$ make clean && make
rm -f filter *.o
g++ -c -I/usr/include/octave-4.0.2/octave -g -Wall `root-config --cflags --libs` -fPIC -O3 -pthread -std=c++11 -Wno-deprecated-declarations -m64 -I/home/user/root/include Filter.cpp -o Filter.o -I/home/user/analysis/analyse_tree/Filter
In file included from /usr/include/octave-4.0.2/octave/defun-dld.h:30:0,
                 from /usr/include/octave-4.0.2/octave/oct.h:36,
                 from Filter.cpp:5:
Filter.cpp: In function ‘int main()’:
/usr/include/octave-4.0.2/octave/defun-int.h:101:10: error: expected unqualified-id before string constant
   extern "C" \
          ^
/usr/include/octave-4.0.2/octave/defun-int.h:98:3: note: in expansion of macro ‘DEFINE_FUNX_INSTALLER_FUN’
   DEFINE_FUNX_INSTALLER_FUN(#name, F ## name, G ## name, doc)
   ^
/usr/include/octave-4.0.2/octave/defun-dld.h:61:3: note: in expansion of macro ‘DEFINE_FUN_INSTALLER_FUN’
   DEFINE_FUN_INSTALLER_FUN (name, doc) \
   ^
Filter.cpp:303:3: note: in expansion of macro ‘DEFUN_DLD’
   DEFUN_DLD (helloworld, FADC2_ev18_crystal_12, nargout,"Hello World Help String")
   ^
Filter.cpp:444:1: error: expected ‘}’ at end of input
 }
 ^
Filter.cpp:257:9: warning: unused variable ‘Outputfile_filtered’ [-Wunused-variable]
  TFile *Outputfile_filtered=new TFile(outputfile_filter, "RECREATE");          // Outputfile
         ^
Filter.cpp:444:1: error: expected ‘}’ at end of input
 }

Without DEFUN_DLD it works. The description says that the first argument of DEFUN_DLD has to be name of the function within octave. But as it is an example it should work though (despite it does not work, too, by changin helloworld to fft).

What am I doing wrongly?
Thank you in advance!

PS: The full code is available on https://github.com/DeLaRiva/Code/upload/master
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to use fft in c++

Pantxo


Le 09/06/2017 à 17:28, Progressive [via Octave] a écrit :


Well, at first thank you very much!


I'm trying to implement to that and since it doesn't work I stick to a basic try:


                DEFUN_DLD (helloworld, args, nargout,"Hello World Help String")

                {


                }


from https://www.gnu.org/software/octave/doc/v4.0.1/Getting-Started-with-Oct_002dFiles.html already doesn't work. I receive the following:


user@User-PC:~/analysis/analyse_tree/Filter$ make clean && make

rm -f filter *.o

g++ -c -I/usr/include/octave-4.0.2/octave -g -Wall `root-config --cflags --libs` -fPIC -O3 -pthread -std=c++11 -Wno-deprecated-declarations -m64 -I/home/user/root/include Filter.cpp -o Filter.o -I/home/user/analysis/analyse_tree/Filter

In file included from /usr/include/octave-4.0.2/octave/defun-dld.h:30:0,

                 from /usr/include/octave-4.0.2/octave/oct.h:36,

                 from Filter.cpp:5:

Filter.cpp: In function ‘int main()’:

/usr/include/octave-4.0.2/octave/defun-int.h:101:10: error: expected unqualified-id before string constant

   extern "C" \

          ^

/usr/include/octave-4.0.2/octave/defun-int.h:98:3: note: in expansion of macro ‘DEFINE_FUNX_INSTALLER_FUN’

   DEFINE_FUNX_INSTALLER_FUN(#name, F ## name, G ## name, doc)

   ^

/usr/include/octave-4.0.2/octave/defun-dld.h:61:3: note: in expansion of macro ‘DEFINE_FUN_INSTALLER_FUN’

   DEFINE_FUN_INSTALLER_FUN (name, doc) \

   ^

Filter.cpp:303:3: note: in expansion of macro ‘DEFUN_DLD’

   DEFUN_DLD (helloworld, FADC2_ev18_crystal_12, nargout,"Hello World Help String")

   ^

Filter.cpp:444:1: error: expected ‘}’ at end of input

 }

 ^

Filter.cpp:257:9: warning: unused variable ‘Outputfile_filtered’ [-Wunused-variable]

  TFile *Outputfile_filtered=new TFile(outputfile_filter, "RECREATE");          // Outputfile

         ^

Filter.cpp:444:1: error: expected ‘}’ at end of input

 }


Without DEFUN_DLD it works. The description says that the first argument of DEFUN_DLD has to be name of the function within octave. But as it is an example it should work though (despite it does not work, too, by changin helloworld to fft).


What am I doing wrongly?

Thank you in advance!











If you reply to this email, your message will be added to the discussion below:

http://octave.1599824.n4.nabble.com/how-to-use-fft-in-c-tp4683590p4683621.html



To unsubscribe from how to use fft in c++, click here.

NAML



First the DEFUN_DLD is a macro that is useful only if you want the Octave interpreter to be able to find and use your function. In your case this doesn't look useful: Octave functions from a C++ program.

The only thing you need in your program is to include <octave/builtin-defun-decls.h>, then any builtin function such as "fft" should be accessible by appending a capital "F". In fact there is a perfect example: see examples/code/standalonebuitlin.cc and compile it using the mkoctfile utility

mkoctfile --link-stand-alone -o tst ./standalonebuiltin.cc

Pantxo



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to use fft in c++

Progressive
This post was updated on .
Thanks, I will try this out directly.

But before that another question: I need to include CERN ROOT and I guess it is not possible via mkoctfile?

PS: Where is examples/code/standalonebuitlin.cc ?

PSS: I tried it as follows and receive the following:

user@User-PC:~/analysis/analyse_tree/Filter$ mkoctfile --link-stand-alone $(root-config --cflags --libs) -o tst ./Filter.cc
In file included from /usr/include/octave-4.0.2/octave/defun-dld.h:30:0,
                 from /usr/include/octave-4.0.2/octave/oct.h:36,
                 from ./Filter.cc:6:
./Filter.cc: In function ‘int main()’:
/usr/include/octave-4.0.2/octave/defun-int.h:101:10: error: expected unqualified-id before string constant
   extern "C" \
          ^
/usr/include/octave-4.0.2/octave/defun-int.h:98:3: note: in expansion of macro ‘DEFINE_FUNX_INSTALLER_FUN’
   DEFINE_FUNX_INSTALLER_FUN(#name, F ## name, G ## name, doc)
   ^
/usr/include/octave-4.0.2/octave/defun-dld.h:61:3: note: in expansion of macro ‘DEFINE_FUN_INSTALLER_FUN’
   DEFINE_FUN_INSTALLER_FUN (name, doc) \
   ^
./Filter.cc:293:3: note: in expansion of macro ‘DEFUN_DLD’
   DEFUN_DLD(fftF,args,nargout,"dld (array) \nreturn  the  elements  of the  array  in  reverse  order\n")
   ^
./Filter.cc:446:1: error: expected ‘}’ at end of input
 }
 ^
user@User-PC:~/analysis/analyse_tree/Filter$

PSSS: I just noticed that I (obviously) mkoctfile --link-stand-alone already included in my makefile. So that's why it generates the same output..

PSSSS: Another try results in:

user@User-PC:~/analysis/analyse_tree/Filter/Octave$ mkoctfile $(root-config --cflags --libs) Filter.cc
Filter.cc:292:35: warning: missing terminating " character [enabled by default]
 DEFUN_DLD(testfft, args, nargout, "\testfft\n\")
                                   ^
Filter.cc:451:0: error: unterminated argument list invoking macro "DEFUN_DLD"
 
 ^
Filter.cc: In function ‘int main()’:
Filter.cc:292:1: error: ‘DEFUN_DLD’ was not declared in this scope
 DEFUN_DLD(testfft, args, nargout, "\testfft\n\")
 ^
Filter.cc:292:1: error: expected ‘;’ at end of input
Filter.cc:292:1: error: expected ‘}’ at end of input
user@User-PC:~/analysis/analyse_tree/Filter/Octave$
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to use fft in c++

Pantxo


2017-06-09 18:19 GMT+02:00 Progressive [via Octave] <[hidden email]>:
Thanks, I will try this out directly.

But before that another question: I need to include CERN ROOT and I guess it is not possible via mkoctfile?


If you reply to this email, your message will be added to the discussion below:
http://octave.1599824.n4.nabble.com/how-to-use-fft-in-c-tp4683590p4683623.html
To unsubscribe from how to use fft in c++, click here.
NAML

Yes you can. The most basic usage of mkoctfile is to make it a replacement for gcc/g++. In fact since it is a wrapper around gcc/g++ most of the flags you would pass to gcc can be passed to mkoctfile. Just forget about Octave specific flags/options since they will be included by default.

Pantxo

PS: you can also use your stadard g++ command and  generate Octave specific flags with e.g. `mkoctfile -p ALL_CFLAGS` (see mkoctfile --help)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to use fft in c++

Progressive
This post was updated on .
Ok, thanks again! That's nice!

But I still don't get it..   I reduced the code to figure out the problem:


#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <math.h> 

#include <oct.h>
#include <builtin-defun-decls.h>
// #include <toplev.h>

using namespace std;

int main()
{

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

                retval = Ffft (args);
                return retval;
        }

        return 1;
}

and get:

user@User-PC:~/analysis/analyse_tree/Filter/Octave$ mkoctfile try.cc
tIn file included from /usr/include/octave-4.0.2/octave/defun-dld.h:30:0,
                 from /usr/include/octave-4.0.2/octave/oct.h:36,
                 from try.cc:6:
try.cc: In function ‘int main()’:
/usr/include/octave-4.0.2/octave/defun-int.h:101:10: error: expected unqualified-id before string constant
   extern "C" \
          ^
/usr/include/octave-4.0.2/octave/defun-int.h:98:3: note: in expansion of macro ‘DEFINE_FUNX_INSTALLER_FUN’
   DEFINE_FUNX_INSTALLER_FUN(#name, F ## name, G ## name, doc)
   ^
/usr/include/octave-4.0.2/octave/defun-dld.h:61:3: note: in expansion of macro ‘DEFINE_FUN_INSTALLER_FUN’
   DEFINE_FUN_INSTALLER_FUN (name, doc) \
   ^
try.cc:17:2: note: in expansion of macro ‘DEFUN_DLD’
  DEFUN_DLD(testfft, args, nargout, "\testfft\n ")
  ^
try.cc:27:1: error: expected ‘}’ at end of input
 }
 ^
user@User-PC:~/analysis/analyse_tree/Filter/Octave$


I have no idea any more. The only difference I notice is, that I include #include <oct.h> instead of #include <octave/oct.> but the latter does not work. And with the former I can at least do calculations and so on - but not calling functions.
I would highly appreciate any help!


Pantxo wrote
2017-06-09 18:19 GMT+02:00 Progressive [via Octave] <
[hidden email]>:

> Thanks, I will try this out directly.
>
> But before that another question: I need to include CERN ROOT and I guess
> it is not possible via mkoctfile?
>
> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
> http://octave.1599824.n4.nabble.com/how-to-use-fft-in-c-
> tp4683590p4683623.html
> To unsubscribe from how to use fft in c++, click here
> <http://octave.1599824.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4683590&code=cGFudHhvLmRpcmliYXJuZUBnbWFpbC5jb218NDY4MzU5MHwtNTYyMDk1Nzc5>
> .
> NAML
> <http://octave.1599824.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>

Yes you can. The most basic usage of mkoctfile is to make it a replacement
for gcc/g++. In fact since it is a wrapper around gcc/g++ most of the flags
you would pass to gcc can be passed to mkoctfile. Just forget about Octave
specific flags/options since they will be included by default.

Pantxo

PS: you can also use your stadard g++ command and  generate Octave specific
flags with e.g. `mkoctfile -p ALL_CFLAGS` (see mkoctfile --help)
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to use fft in c++

Kire Pudsje


On Fri, Jun 9, 2017 at 7:54 PM, Progressive <[hidden email]> wrote:
Ok, thanks again! That's nice!

But I still don't get it..   I reduced the code to figure out the problem:


#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <math.h>

#include <oct.h>
#include <builtin-defun-decls.h>
// #include <toplev.h>

using namespace std;

int main()
{

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

                retval = Ffft (args);
                return retval;
        }

        return 1;
}
 
Get rid of the main. DEFUN is defining a function.
BTW, I might be missing something, but I now have the feeling you are going the opposite way. Oct files are intended to be for calling a cpp file from octave. I thought you were trying the opposite?
https://www.gnu.org/software/octave/doc/v4.2.1/Standalone-Programs.html#Standalone-Programs


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to use fft in c++

Progressive
At first thank you!

What do you mean by "DEFUN is defining a function"?

Do I have to replace   DEFUN_DLD(testfft, args, nargout, "\testfft\n ")   to something like   fft(testfft, args, nargout, "\testfft\n ")    ?

I want to call octave from a cpp-file. Matrices and so on work fine but calling funtctions does not. Now I'm confused anyway..  DEFUN is not a function I can call?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to use fft in c++

Przemek Klosowski-7
On 06/09/2017 02:19 PM, Progressive wrote:
At first thank you!

What do you mean by "DEFUN is defining a function"?
Sorry for being pedantic but I think we're going in circles, so let's rewind to the beginning. Octave code can be used in one of two ways: either by letting Octave have control, or controlling Octave code from your own code.

The former way is more common: Octave provides the main() function that sets things up and goes into a REPL---Read Evaluate Print Loop. You can add new functionality to Octave by either writing code in its interpreted language (.m files) or in C/C++; the latter requires you to use Octave ABI conventions that are easier to satisfy if you use the DEFUN_DLD macro---its function is to declare an Octave function to the compiler, which then adds it to Octave binary so that it can be called just like any other Octave builtin function.

You can also treat Octave as a library, providing your own main() and control loop. You don't need DEFUN_DLD then, because you will be calling your own code, not Octave (you'll still need to follow Octave's conventions to call Octave's functions, of course, and you may still need DEFUN_DLD if you design your program to call main()->YourCode()->OctaveCode()->yourCode() )

People who are new to interpreted languages tend to instinctively gravitate towards the second model, to retain control of the main loop, but I believe that in most cases writing Octave extensions is simpler and more productive. I think the main  case for the first model is for existing programs that already have their own control loop that is too hard to get rid of.

If you do insist, take look at /usr/share/doc/octave/examples/code/standalonebuiltin.cc


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to use fft in c++

Kire Pudsje
In reply to this post by Progressive


On Fri, Jun 9, 2017 at 8:19 PM, Progressive <[hidden email]> wrote:
At first thank you!

What do you mean by "DEFUN is defining a function"?

Do I have to replace   DEFUN_DLD(testfft, args, nargout, "\testfft\n ")   to
something like   fft(testfft, args, nargout, "\testfft\n ")    ?

I want to call octave from a cpp-file. Matrices and so on work fine but
calling funtctions does not. Now I'm confused anyway..  DEFUN is not a
function I can call?


DEFUN_DLD is a macro that takes care of all the stuff to be able to call a function from octave. You can just use it to define a function and it will do all the necessary stuff without needing to think about it.
However as you just need to call it from C++ and not from octave, you do not need all the extras.
The example that has been given before, that I think is confusing you (sorry I am too lazy to look up the reference to the mail), was to show that when fft is called from octave and Ffft is called from C, they both give the same results. To check this (and this is the part that confuses you, I think), he needed to call this C function from octave, to get the results and compare them.


FYI from the manual:
Included by oct.h is a definition for the macro DEFUN_DLD which creates a dynamically loaded function. This macro takes four arguments:
  1. The function name as it will be seen in Octave,
  2. The list of arguments to the function of type octave_value_list,
  3. The number of output arguments, which can and often is omitted if not used, and
  4. The string to use for the help text of the function.
The return type of functions defined with DEFUN_DLD is always octave_value_list


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to use fft in c++

Pantxo
Kire Pudsje wrote
On Fri, Jun 9, 2017 at 8:19 PM, Progressive <[hidden email]> wrote:

> At first thank you!
>
> What do you mean by "DEFUN is defining a function"?
>
> Do I have to replace   DEFUN_DLD(testfft, args, nargout, "\testfft\n ")
>  to
> something like   fft(testfft, args, nargout, "\testfft\n ")    ?
>
> I want to call octave from a cpp-file. Matrices and so on work fine but
> calling funtctions does not. Now I'm confused anyway..  DEFUN is not a
> function I can call?
>
>
DEFUN_DLD is a macro that takes care of all the stuff to be able to call a
function from octave. You can just use it to define a function and it will
do all the necessary stuff without needing to think about it.
However as you just need to call it from C++ and not from octave, you do
not need all the extras.
The example that has been given before, that I think is confusing you
(sorry I am too lazy to look up the reference to the mail), was to show
that when fft is called from octave and Ffft is called from C, they both
give the same results. To check this (and this is the part that confuses
you, I think), he needed to call this C function from octave, to get the
results and compare them.

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Agreed, sorry about this confusing example.

Pantxo
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: how to use fft in c++

Progressive
In reply to this post by Przemek Klosowski-7
Well, thank you a lot! Andy gave me an example on https://stackoverflow.com/questions/44419812/how-to-use-octave-for-data-from-c-in-c (for those with the same issue).

Thanks for the background and so on! Will think about it now so I surely do understand it.

Kind regards
Loading...