Tuesday, December 18, 2018

python - Implementing inverse FFT using forward FFT


I am trying to implement an inverse FFT using the forward FFT. For clarity:


Let S[t] be a signal in time, and S[w] the transformed signal. As per this site, it seems one can reverse S[w], use the forward FFT routine, then reverse the resulting signal again and this should give S[t]. I won't go into why should it work but it's all in the link provided.



I attempted to try this method, and it seems to recreate the signal when I use a high sample rate. However, it seems that even if I do not go over the niquist frequency, I encounter some weird effect. Here is my code in numpy:


# Generate a signal of s cosine with 200 [Hz]
f = 200
Fs = 10
t = np.linspace(-10,10,Fs*20)
s = cos(2*np.pi*f*t)

# Take fft
u = np.fft.fft(s)


# Reverse in time
u = u[::-1]

# Transform again
u_t = np.fft.fft(u)

# Reverse and normalize

s_new = np.divide(u_t[::-1],s.shape[-1])


# Finally slice for easier viewing
plt.plot(t[1:100],s_new[1:100])
plt.plot(t[1:100],s[1:100])

This code yields the following graph: enter image description here


I am a little confused, the theory looked sound and I can't think what I did wrong here.


Note: I tried calling np.fft.fft and then np.fft.ifft and the reconstruction goes as planned. Therefore I believe this problem is not due to aliasing.


Edit: I made way by simply taking the complex conjugation instead of reversing (using np.conj() where I reverse). This solves the problem, but I still do not understand why reversing in time does not accomplish a conjugation, so I'd love someone to explain it to me. I leave the modified code in case it helps someone else:


# Generate a signal
f = 200

Fs = 10
t = np.linspace(-10,10,Fs*20)
s = cos(2*np.pi*f*t)

# Take fft
u = np.fft.fft(s)

# Conjugate
u = np.conj(u)


# Transform again
u_t = np.fft.fft(u)

# conjugate and normalize

s_new = np.divide(np.conj(u_t),s.shape[-1])

# Finally slice for easier viewing
plt.plot(t[1:100],s_new[1:100])
plt.plot(t[1:100],s[1:100])



No comments:

Post a Comment

digital communications - Understanding the Matched Filter

I have a question about matched filtering. Does the matched filter maximise the SNR at the moment of decision only? As far as I understand, ...