Negsam
Negsam

Reputation: 1

Derivate in Fourier space with C++

I have a sine wave which I would like to transform into Fourier space. Then take derivative in Fourier space. Then transform my data back into real space. The final array should give me a cosine wave. I have used FFT library from FFTW.org.

I'm getting 3 errors on line 79 which are:

E0137 expression must be a modifiable lvalue ConsoleApplication2 E2138 expression must have arithmetic or unscoped enum type C Error C2296 '*': illegal, left operand has type 'fftw_complex'

Line 79 is:

result2[l] = result1[l] * kappa[l] * sqrt(-1);

Here is my entire code:

    #include <fftw3.h>

    #define N 64

    #define L 30

   int i;

   int j;

   int k;

    enter code here

   int l;

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

   #define REAL 0
   #define IMAG 1


   void acquire_from_somewhere(fftw_complex* signal) {  
       for (i = 0; i < N; ++i) {
           double x = (double)L / (double)N * (double) i - (double)L / 2;

           signal[i][REAL] = sin(x);

       }
   }

   void do_something_with(fftw_complex* result2) {
    
    for (j = 0; j < N; ++j) {
        double df = result2[j][REAL];

        printf("%g\n", df);
    }
  }

   int main() { 

    double kappa[N];

    fftw_complex fhat[N];

    fftw_complex dfhat[N];

    fftw_complex signal[N];

    fftw_complex result1[N];

    fftw_complex result2[N];

    fftw_plan plan1 = fftw_plan_dft_1d(N,
        signal,
        result1,
        FFTW_FORWARD,
        FFTW_ESTIMATE);
    fftw_plan plan2 = fftw_plan_dft_1d(N,
        dfhat,
        result2,
        FFTW_BACKWARD,
        FFTW_ESTIMATE);
    acquire_from_somewhere(signal);

    fftw_execute(plan1);

    for (k = -N; -N / 2 < k && N / 2 > k; ++k) {

        kappa[k] = 2 * 3.14159265358979323846 * k / L;
        
    }
    
    for (l = 0; l < N; ++l) {
      result2[l] = result1[l] * kappa[l] * sqrt(-1);
    }

    fftw_execute(plan2);
    do_something_with(result2);

    fftw_destroy_plan(plan1);
    fftw_destroy_plan(plan2);

    return 0;
   }

Upvotes: 0

Views: 199

Answers (1)

Klompengard
Klompengard

Reputation: 304

The answer you're looking for is in your error message. You're trying to multiply an element of type fftw_complex with type double.

According to FFTW's website, fftw_complex is defined like so: typedef double fftw_complex[2];

This means fftw_complex is an array of 2 elements of type double.

If you're just trying to multiply this complex number with a scalar, I'd treat result1[i] as a 2D vector, and multiply the x / y components by kappa[i] (result1[i][0] * kappa[i] for x component).

As an aside, don't use lowercase L as a variable. That's a quick road to future headaches.

Upvotes: 3

Related Questions