Thursday, March 3, 2011

at 12:09 AM Posted by ELECTRIC MACHINES 0 comments

%

% DFT in a direct implementation

%

% Enter Data in y

y=[7.6 7.4 8.2 9.2 10.2 11.5 12.4 13.4 13.7 11.8 10.1 ...

9.0 8.9 9.5 10.6 11.4 12.9 12.7 13.9 14.2 13.5 11.4 10.9 8.1];



% Get length of data vector or number of samples

N=length(y);



% Compute Fourier Coefficients

for p=1:N/2+1

A(p)=0;

B(p)=0;

for n=1:N

A(p)=A(p)+2/N*y(n)*cos(2*pi*(p-1)*n/N)';

B(p)=B(p)+2/N*y(n)*sin(2*pi*(p-1)*n/N)';

end

end

A(N/2+1)=A(N/2+1)/2;



% Reconstruct Signal - pmax is number of frequencies used in increasing order

pmax=13;

for n=1:N

ynew(n)=A(1)/2;

for p=2:pmax

ynew(n)=ynew(n)+A(p)*cos(2*pi*(p-1)*n/N)+B(p)*sin(2*pi*(p-1)*n/N);

end

end



% Plot Data

plot(y,'o')



% Plot reconstruction over data

hold on

plot(ynew,'r')

hold off