1

and am trying to relate fftshift/ifftshift to circular shift.

N = 5 
Y = 0:N-1

X = [0 1 2 3 4] 

When I fftshift(X), I get

[3 4 0 1 2] 

When I ifftshift(X), I get

[2 3 4 0 1] 

How do I relate fftshift/ifftshift to circular shift? Is it simply moving the numbers in X about in different directions?

I need to know this as I'm trying to implement these two functions in terms of circular shift in C++, which is a function I already have done.

Many thanks.

lppier
  • 1,927
  • 3
  • 24
  • 62
  • 2
    Just type `open fftshift` and `open ifftshift` in the command windows. Both are no built-in functions. They are programmed using `circshift`. You can simply adapt to code to you C++ program. – Nemesis Jan 21 '16 at 08:44
  • @Nemesis thanks, I didn't know you could do that. Realised there's only one difference between them - the way it determines where to swap left/right of the matrix. For fftshift is uses ceil(N/2) whereas ifftshift uses floor(N/2). – lppier Jan 21 '16 at 09:09

1 Answers1

0

After looking at the Matlab codes, which doesn't directly use circular shift, but rather Matlab syntax.

Say N = no. of elements

To implement fftshift,

circularShiftRightBy = floor(N/2)

To implement ifftshift,

circularShiftRightBy = ceil(N/2) 

Being N/2, there is only a difference between fftshift and ifftshift if N is odd.

Where circular shift code is:

template<typename ty>
void circshift(ty *out, const ty *in, int xdim, int ydim, int xshift, int yshift)
{
 for (int i =0; i < xdim; i++) {
   int ii = (i + xshift) % xdim;
   if (ii<0) ii = xdim + ii;
   for (int j = 0; j < ydim; j++) {
     int jj = (j + yshift) % ydim;
     if (jj<0) jj = ydim + jj;
     out[ii * ydim + jj] = in[i * ydim + j];
   }
 }
}

(modified from fftshift/ifftshift C/C++ source code to support left (-ve) shifting as well. )

EDIT: I've since found a better way to do this: https://kerpanic.wordpress.com/2016/04/08/more-efficient-ifftshift-fftshift-in-c/

Community
  • 1
  • 1
lppier
  • 1,927
  • 3
  • 24
  • 62