Search code examples
rtimesignal-processingfftphase

apply fourier shift theorem to complex signal


Im trying to apply the fourier phase shift theorem to a complex signal in R. However, only the magnitude of my signal shifts as I expect it. I think it should be possible to apply this theorem to complex signals, so probably I make an error somewhere. My guess is that there is an error in the frequency axis I calculate.

How do I correctly apply the fourier shift theorem to a complex signal (using R)?

i = complex(0,0,1)
t.in = (1+i)*matrix(c(1,0,0,0,0,0,0,0,0,0))
n.shift = 5

#the output of fft() has the mean / 0 frequency at the first element
#it then increases to the highest frequency, flips to negative frequencies
#and then increases again to the negative frequency closest to 0
N = length(t.in)
if (N%%2){#odd
  kmin = -(N-1)/2
  kmax = (N-1)/2
} else {#even
  kmin = -N/2
  kmax = N/2-1
  #center frequency negative, is that correct?
}

#create frequency axis for fft() output, no sampling frequency or sample duration needed
k = (kmin:kmax)
kflip = floor(N/2)
k = k[c((kflip+1):N,1:kflip)]
f = 2*pi*k/N

shiftterm = exp( -i*n.shift*f )

T.in = fft(t.in)
T.out = T.in*shiftterm
t.out = fft(T.out, inverse=T)/N

par(mfrow=c(2,2))
plot(Mod(t.in),col="green"); 
plot(Mod(t.out), col="red");
plot(Arg(t.in),col="green");
plot(Arg(t.out),col="red");

As you can see the magnitude of the signal is nicely shifted, but the phase is scrambled. I think the negative frequencies are where my error is, but I cant see it.

enter image description here

What am I doing wrong?


The questions about fourier phase shift theorem I could find:

real 2d signal in python

real 2d signal in matlab

real 1d signal in python

math question about what fourier shift does

But these were not about complex signals.


Solution

  • Answer

    As Steve suggested in the comments, I checked the phase on the 6th element.

    > Arg(t.out)[6]
    [1] 0.7853982
    > Arg(t.in)[1]
    [1] 0.7853982
    

    So the only element that has a magnitude (at least one order of magnitude higher than the EPS) does have the phase that I expected.

    TL;DR The result from the original approach in the question was already correct, we see the Gibbs Phenomenon sliding by.


    Just discard low magnitude elements?

    If ever the phase of elements that should be zero will be a problem I can run t.out[Mod(t.out)<epsfactor*.Machine$double.eps] = 0 where in this case epsfactor has to be 10 to get rid of the '0' magnitude elements.

    Adding that line before plotting gives the following result, which is what I expected to get beforehand. However, the 'scrambled' phase might actually be accurate in most cases as I'll explain below.

    enter image description here

    The original result really was correct

    Just setting low magnitude elements to 0 does not make the phase of the shifted signal more intuitive however. This is a plot where I apply a 4.5 sample shift, the phase is still 'scrambled'.

    enter image description here

    Applying fourier shift equivalent to downsmapling shifted fourier interpolation

    It occurred to me that applying a non-integer number of elements phase shift is equivalent to fourier interpolating the signal and then downsample the interpolated signal at points between the original elements. Since the vector I used as input is an impulse function, the fourier interpolated signal is just not well behaved. Then the signal after applying the fourier phase shift theorem can be expected to have exactly the phase that the fourier interpolated signal has, as seen below.

    enter image description here

    Gibbs Ringing

    Its just at the discontinuities where phase is not well behaved and where small rounding errors might cause large errors in the reconstructed phase. So not really related to low magnitude but to not well defined fourier transform of the input vector. This is called Gibbs Ringing, I could use low-pass filtering with a gaussian filter to decrease it.

    enter image description here

    Questions related to fourier interpolation and phase shift

    symbolic approach in R to estimate fourier transform error

    non integer signal shift by use of linear interpolation

    downsampling complex signal

    fourier interpolation application

    estimating sub-sample shift between two signals using fourier transforms

    estimating sub-sample shift between two signals without interpolation