Sign in to follow this  

Help solving compatibility with FFTW functions

This topic is 2433 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to store fftw_complex in a vector, but I soon came to realize that fftw_complex is of type double, which is unfortunate, since they are not assignable. So you see, I cannot store then in a vector. So I figure, I would
create a wrapper as follows :
[source]

class ComplexWrapper
{
private:
fftw_complex data;
public:
ComplexWrapper(): data() {}
double& operator[](int i){ return data[i]; }
//make is copyable
ComplexWrapper(const ComplexWrapper& w){
data[0] = w.data[0]; data[1] = w.data[1];
}
//make it assignable
ComplexWrapper& operator = (const ComplexWrapper& w){
data[0] = w.data[0]; data[1] = w.data[1];
return *this;
}
operator double* (){ return &data; }
};
//...
std::vector<ComplexWrapper> _frequencyDomainData;
[/source]

and the call :
[code]
_fftwPlan = fftw_plan_dft_r2c_1d(_currentSample.size(), &_currentSample[0], &_frequencyDomainData[0],FFTW_MEASURE);
[/code]

and I receive a conversion error:
[code]
error: cannot convert 'SDL_OpenGL::MusicAnalyzer::ComplexWrapper*' to 'double (*)[2]' for argument '3' to 'fftw_plan_s* fftw_plan_dft_r2c_1d(int, double*, double (*)[2], unsigned int)'


[/code]

I know its expecting an array-of-double[2], but I guess thats not what I provided. Any help on solving this conversion?

Share this post


Link to post
Share on other sites
the docs state that fftw_complex is of type double[2] not double.

Additionaly they say that there is native support for compley numbers on many compilers through the headers <complex.h> (c) or <complex> (c++)
complex<T> is a templated complex number and said to be binary compatible to the fftw_complex typedef. So you can just pass a pointer to complex<double>
to the fftw functions.

Alternatively I think it would be much simpler to just do:
[source]
struct Complex {
double r;
double i;
};
[/source]

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1305982250' post='4813837']
What is the wrapper buying you? Why don't you just use the FFTW types?
[/quote]

We'll the size might change so I needed to use a vector, but double[2] is not assignable thus can't be in vector.

[quote]
[color=#1C2837][size=2]the docs state that fftw_complex is of type double[2] not double.

Additionaly they say that there is native support for compley numbers on many compilers through the headers <complex.h> © or <complex> (c++)
complex<T> is a templated complex number and said to be binary compatible to the fftw_complex typedef. So you can just pass a pointer to complex<double>
to the fftw functions.

Alternatively I think it would be much simpler to just do:
[size=2]?[/size]1234struct Complex { double r; double i;};[/quote][size=2]I just feel dirty doing that. Exchanging ComplexWrapper for std::complex<double>, thus create a std::vector< std::complex<double> > does not help, in that I get the same conversion error except from 'std::complex<double>*' to double(*)[2]. [/size][size=2]And btw, the reason why I had ComplexWrapper wrapping fftw_complex instead of what you recommended is to try to be cache friendly, since fftw_complex is an array[2]. Not too much help, but was just a thought.[/size][size=2]Any other ideas?[/size][/size][/color]

Share this post


Link to post
Share on other sites
[quote name='D.Chhetri' timestamp='1305992250' post='4813872']
[color="#1C2837"][size="2"][size="2"]And btw, the reason why I had ComplexWrapper wrapping fftw_complex instead of what you recommended is to try to be cache friendly, since fftw_complex is an array[2].[/size][/size][/color]
[/quote]

What's more cache friendly about that?

Share this post


Link to post
Share on other sites
[quote name='Madhed' timestamp='1305997857' post='4813897']
[quote name='D.Chhetri' timestamp='1305992250' post='4813872']
[color="#1C2837"][size="2"][size="2"]And btw, the reason why I had ComplexWrapper wrapping fftw_complex instead of what you recommended is to try to be cache friendly, since fftw_complex is an array[2].[/size][/size][/color]
[/quote]

What's more cache friendly about that?
[/quote]

99.9% it is places in the same cache line, so when you acces one, the other is more likely to be stored in cache. I'm not so sure about structure, although POD is probably the same way.

Share this post


Link to post
Share on other sites
[quote name='alvaro' timestamp='1305995272' post='4813884']
You can probably just reinterpret_cast &frequencyDomainData[0] to a double(*)[2].
[/quote]

I sure hope thats stable.

Share this post


Link to post
Share on other sites
Sign in to follow this