Thursday 17 October 2013

MATLAB Programme 02: Implement DFT and IDFT in MATLAB using floating point Technique



% MATLAB Programme for implemention of DFT and IDFT using floating point technique

% in this programme input signal is sinusoidal signal whose 
amplitude is 0.9.
% this programme generate another coefficient file input.h which is input signal coefficient
% in matlab if we declare function definition then we give definition of function in another file and name of this file is  same as function calling name. in this programme we use two function "dft" and " idft " . whose definition give in another two file dft.m and idft.m

%****************************************************************
%file name :    spectrum.m 

clc; % for clearing window
close all; % for closing previous graphical window
clear all; % for clearing previously values


fs = 8000;
f0 = 1000;
cycles = 4;
N = fix(cycles*fs/f0);
n = 0:1:N-1;
amp = 0.9;
ts = 1/fs;
x = amp*sin(2*pi*f0*n*ts);

X = dft(x,N);

fid = fopen('input.h','w');
fprintf (fid,'#ifndef\t_INPUT_H_\n');
fprintf (fid,'#define\t_INPUT_H_\n\n');
fprintf (fid,'#define\tSIZE\t%d\n\n',length(x));
fprintf (fid,'float\tx[SIZE]\t=\t{');
fprintf (fid,'%g,%g,%g,%g,%g,%g,%g,%g,%g,%g,\n',x);
fprintf (fid,'};\n\n');
fprintf (fid,'#endif\n');
fclose(fid);

xc = idft(X,N);


stem(real(xc));




%*********************************************************

%function defination
%file name :   dft.m

function X= dft(x,N)

for k=0:1:N-1
    X(k+1) = 0;
    for n=0:1:N-1
        X(k+1) = X(k+1) + (x(n+1)*exp(-j*2*pi*n*k/N));
    end

end



%********************************************************

%function defination
%file name :   idft.m


function x= idft(X,N)

for n=0:1:N-1
    x(n+1) = 0;
    for k=0:1:N-1
        x(n+1) = x(n+1) + (X(k+1)*exp(j*2*pi*n*k/N));
    end
end


x=x/N;

%*********************************************************


for implementing DFT and IDFT in Code Composer Studio V4 visit to another post CCS_Programme 02: Implement DFT and IDFT usign Floating Point Technique in Code Composer Studio....

No comments:

Post a Comment