Matlab fft function swapping indices

Viewed 105

I've written a simple and very small Matlab code that calculates the Discrete Fourier Transfrom of a given array (or vector).

I worked it out manually and got an answer and my Matlab code also giving the same answer. But fft is giving an answer different from this by swapping indices. Here are the mannual calculations that I've done:

This is the first image

This is the second image:

This is the second image

This is the third image:

This is the third image

From those calculations it is clear that my answer would be {12, -3-3j, -2, -3+3j}

Here is the Matlab code that I've used:

clc;
clear all;
close all;
inp=[1,2,3,4];
j=sqrt(-1);
op=zeros(1,length(inp));
for k=1:length(inp)
    sigma=0;
    for n=1:length(inp)
        sigma=sigma+inp(n)*exp((j*2*pi*(k-1)*(n-1))/length(inp));
    end
    op(k)=sigma;
end
% Checking with fft
fft(inp)

Now I'm getting output as this:

Matlab Output

It is very much unexpected that I'm getting values swapped. It is swapping the indices 2 and 4.

1 Answers
Related