PDA

View Full Version : Re: fftshift equivilent in C


Graham
08-24-2003, 01:09 AM
On 23 Aug 2003, Andrew Nesterov wrote:

> int n; // FFT output vector length
> int n2;
> int i;
> complex x[n]; // you might typedef your own complex type
> complex tmp;
>
> n2 = n / 2; // half of vector length
>
> for (i = 0; i < n2; i++)
> {
> tmp = x[i];
> x[i] = x[i+n2];
> x[i+n2] = tmp;
> }

This is close to what I came up with but it only works for even n.

Matlab gives:

>> fftshift([1,2,3,4,5,6,7,8])
ans = 5 6 7 8 1 2 3 4

>> fftshift([1,2,3,4,5,6,7,8,9])
ans = 6 7 8 9 1 2 3 4 5

Above gives:

n = 8
n2 = 4
in = 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00
out = 5.00 6.00 7.00 8.00 1.00 2.00 3.00 4.00

n = 9
n2 = 4
in = 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00
out = 5.00 6.00 7.00 8.00 1.00 2.00 3.00 4.00 9.00

Any ideas on how to get the same answer as matlab for both even and odd n?

Thanks,
Graham