-3

I'm trying to apply FFT (this rosettacode.org C++ implementation of FFT : void fft(CArray &x) { ... }, or should I use the C implementation ?) to an array given by this data :

float *x
VstInt32 sampleFrames    // basically the length of the array

When I do:

fft(x);

I get :

error C2664: 'void fft(CArray &)' : cannot convert argument 1 from 'float *' to 'CArray &'

How to solve this kind of error?


Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
Basj
  • 41,386
  • 99
  • 383
  • 673
  • Where's the rest of the code? You've shown the error but not the code that caused the error – EdChum Jul 05 '16 at 10:01
  • Sorry @EdChum, you're right. I added : `void fft(CArray &x) { ... }` and I call it with `fft(x);` – Basj Jul 05 '16 at 10:03
  • why would you expect this to work? In the link it's `typedef`ed as `typedef std::valarray CArray;` which clearly is not a `float*` – EdChum Jul 05 '16 at 10:05
  • So sorry @EdChum, I'm trying to recode my DSP algo (which works great in Python) in C++, but my C++ memories are a bit far... Should I turn the valarray into a `float*` ? – Basj Jul 05 '16 at 10:08
  • 1
    I think you need to take a step back and try to get the sample code, either of them, to compile first and then adapt it to your needs currently you seem to be very far away from your solution as you're experiencing syntax errors so you should start with something that works and then modify it – EdChum Jul 05 '16 at 10:14

1 Answers1

1

You will have to convert array to CArray type alias:

http://coliru.stacked-crooked.com/a/20adde65619732f8

typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;

void fft(CArray& x)
{   
}

int main()
{
    float sx[] = {1,2,3,4};

    float *x = sx;
    int sampleFrames = sizeof(sx)/sizeof(sx[0]);

    // Convert array of floats to CArray
    CArray ca;
    ca.resize(sampleFrames);
    for (size_t i = 0; i < sampleFrames; ++i)
      ca[i] = x[i];

    // Make call
    fft(ca);
}
marcinj
  • 48,511
  • 9
  • 79
  • 100
  • Thanks! This does a copy of the array into a new `CArray`. Isn't there a way to make a CArray from a `float *` in place, without copying ? – Basj Jul 05 '16 at 10:21
  • I dont think so, the problem is that your source data is of type float and you need to convert it to complex, even C - version would require copying. You can find all valarray constructors here : http://en.cppreference.com/w/cpp/numeric/valarray/valarray – marcinj Jul 05 '16 at 10:32
  • True, thanks @MarcinJędrzejewski. This makes me think I need rfft anyway (real FFT like here : http://docs.scipy.org/doc/numpy/reference/generated/numpy.fft.rfft.html) – Basj Jul 05 '16 at 10:39