BillyJean
BillyJean

Reputation: 1577

compute inverse fft manually

I am trying to compute the inverse FFT of a fft-output manually. I am using the following script, which first uses fft to compute the FFT of a data set. I then try to find the inverse FFT manually, but it doesn't resemble the result I get from ifft.

Can you spot my error? I am merely using the standard inverse formula of the FFT presented here, https://en.wikipedia.org/wiki/Fast_Fourier_transform#Definition_and_speed

data = [
   -0.0005
   -0.0004
   -0.0003
   -0.0002
   -0.0001
   -0.0000
    0.0001
    0.0001
    0.0001
    0.0002
    0.0002
    0.0002
    0.0002
    0.0002
    0.0002
    0.0002
    0.0002
    0.0002
    0.0003
    0.0004
    0.0005
    0.0006
    0.0007
    0.0009
    0.0010
    0.0011
    0.0011
    0.0012
    0.0011
    0.0011
    0.0011
    0.0010
    0.0011];


delta = 0.0125;
fs    = 1/delta;
x     = (0:1:length(data)-1)/fs;

X=fft(data);

%find fft
N=length(data);
ws = 2*pi/N;
wnorm = -pi:ws:pi;
wnorm = wnorm(1:length(x));
w = wnorm*fs;
figure(2)
plot(w/(2*pi),abs(fftshift(X)))


%find inverse fft manually
for m=1:length(X)
    for k=1:length(data)
        X_real(m) = X(k)*exp(i*k*ws*(m-1));
    end
end

figure(3)
plot(1:length(data), abs(X_real), 1:length(data), ifft(X))

Upvotes: 1

Views: 1930

Answers (1)

KKS
KKS

Reputation: 1389

Please, change your for loop like below.

for m=1:length(X)
    for k=1:length(data)
        temp(k) = X(k)*exp(i*(m-1)*ws*(k-1));
    end
    X_real(m)=(1/N)*sum(temp);
end

figure(3)
plot(1:length(data), real(X_real))

You can find the equation of ifft in matlab, here.

You missed two things.

One thing is normalization, another is summing.

Upvotes: 2

Related Questions