Hamid Reza AZ - 3 years ago 252

C++ Question

what is the exact equivalent of this MATLAB line code in C++ and using FFTW?

`fftshift(fft(x,4096)));`

note: X is an array of 4096 double data.

now I use these lines of code in c++ and FFTW to compute fft

`int n = 4096`

fftw_complex *x;

fftw_complex *y;

x = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * n);

y = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * n);

for (int i=0; i<n; i++)

{

x[i][REAL] = MyDoubleData[i];

x[i][IMAG] = 0;

}

fftw_plan plan = fftw_plan_dft_1d(n, x, y, FFTW_FORWARD, FFTW_ESTIMATE);

fftw_execute(plan);

fftw_destroy_plan(plan);

fftw_cleanup();

It is just equivalent of FFT function in MATLAB.

Is there any equivalent function for FftShift in FFTW library?

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

The FFTW function calls you've provided would be the equivalent of `fft(x,4096)`

. ~~If x is real, matlab knows to give you the conjugate symmetric FFT (I think). If you want to do this with FFTW, you need to use the ~~`r2c`

and `c2r`

functions (real-to-complex/complex-to-real).

You have to do the shift yourself. You can do direct substitution (poor performance, but should be intuitive)

```
for (int i=0; i<n; i++)
{
fftw_complex tmp;
int src = i;
int dst = (i + n/2 - 1) % n;
tmp=y[src];
y[src]=x[dst];
y[dst]=tmp;
}
```

Alternatively use a couple memcpy's (and/or memmove's) or modify your input data

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**