1

So I have a simulation to get Fourier Transform of an given array.

This is my Python code to get Fourier Transform

import numpy as np
import matplotlib.pyplot as plt
import scipy.signal as signal

w1=np.arange(-2*np.pi,2*np.pi,0.01)

x = np.array([1,2,3,4,3,2,1])
#fourier transform
s=0

for n in range(len(x)-1):
    s=s+(x[n+1]*np.exp(-1j*w1*n))

n=np.arange(len(x))
plt.figure(1)
plt.stem(n,x)
plt.title("Signal")

plt.figure(2)
plt.subplot(1,2,1)
plt.plot(w1,np.abs(s))
plt.title("Phase response")
plt.subplot(1,2,2)
plt.plot(w1,np.angle(s))
plt.title("Frequency Response")

plt.figure(3)
w,h=signal.freqz(x)

plt.subplot(2,1,1)
plt.plot(w, 20 * np.log10(abs(h)), 'b')

plt.subplot(2,1,2)
plt.plot(w, np.angle(h), 'g')
plt.suptitle("Frequency response")

I think I have got my fourier transform, but when I thy to do inverse Fourier Transform

#inverse fourier transform

s1=0
n1=np.arange(0,len(x))
for n in np.arange(0,len(s)-1):
    s1=s1+(s[n]*(np.exp(1j*w1[n]*n1)))

s1=s1/(2*np.pi*200)

plt.figure(4)
n=range(len(s1))
plt.stem(n,s1)

I can't get the 1st element backenter image description here

Anyway to get 1st element back.

(I know there is an command to get IFT but I have to do it without the command)

  • What have you tried? Where are you stuck? Does (Inverse transform)[https://en.wikipedia.org/wiki/Discrete_Fourier_transform#Inverse_transform] help? – GrapefruitIsAwesome Feb 13 '22 at 11:22

1 Answers1

1

The FFT is an algorithm to calculate the Discrete Fourier Transform which is defined as

$$X[k] = \sum_{n=0}^{N-1}x[n] \cdot e^{-j2\pi\frac{kn}{N}} $$

The DFT of a 7 sample sequence is also 7 samples long. Whatever your code is doing, it's NOT a DFT.

I think I have got my fourier transform,

How do you know? What unit testing did you do and what test vectors have you checked against a known good reference? Don't go to step B before you have fully validated step A.

for n in range(len(x)-1): s=s+(x[n+1]*np.exp(-1j*w1*n))

What's up with len(x)-1 and x[n+1]? You seem to be excluding the first sample from your sum, so it's not part of your result. That also means you can't get it back since it didn't make it into your result in the first place.

Hilmar
  • 29,048
  • 1
  • 21
  • 45