Monday 28 October 2013

CCS_Programme 03: Implement DFT and IDFT usign Fixed Point Technique in Code Composer Studio

// fixed point implemention of dft and IDFT




#include <stdio.h>

#include <math.h>

#include "input32_DFT_IDFT_float.h"

#include "input32_DFT_IDFT_fixed.h"




#define N 32

#define PI (float)3.1416

#define Q15 (unsigned int)32768

#define Q14 (unsigned int)(Q15>>1)

#define Q8 (unsigned int)256




typedef struct {

float real[N];

float imag[N];


}complex;

complex xn,Xk,xc;



//Declare Structure for storing fixed point numbers

typedef struct {

int real[N];

int imag[N];


}complex_fix;

complex_fix xnn,Xkk,xcc;



float chk_real_fix_dft[N];

float chk_imag_fix_dft[N];

float chk_real_fix_idft[N];

float chk_imag_fix_idft[N];




//declare function prototype for DFT and IDFT Calculation
void dft_fix(complex_fix *g,complex_fix *G,int len);

void idft_fix(complex_fix *g,complex_fix *G,int len);




void main()
{
int n;

 //input values for DFT Calculation

for(n=0;n<N;n++)


{

xn.real[n] = x[n];

xn.imag[n] = 0;

}


//input values for DFT Calculation using Fixed point Technique

for(n=0;n<N;n++)


{

xnn.real[n] = x_fix[n];

xnn.imag[n] = 0;

}

 dft_fix(&xnn,&Xkk,N);

printf ("DFT Fixed Point done\n");


idft_fix(&xcc,&Xkk,N);

printf ("IDFT Fixed Point done\n");


}



void dft_fix(complex_fix *g,complex_fix *G,int len)


{

int n,k;

float alp,alpha,angle =0;

int cf,sf;

long prod1,prod2,sum1 = 0,sum2 = 0;



alp = (2*PI/N);


for (k=0;k<len;k++)


{

G->real[k] = 0;

G->imag[k] = 0;

alpha = alp*k;

angle = 0;

sum1 = 0;

sum2 = 0;

for(n=0;n<len;n++)


{

cf = cos(angle)*Q14; //SI1F14

sf = sin(angle)*Q14; //SI1F14



prod1 = ((long)cf*(long)g->real[n]); //SSI1F29

prod1 += ((long)sf*(long)g->imag[n]); //SSI1F29

prod1 = prod1<<1; //SI1F30



prod2 = ((long)cf*(long)g->imag[n]); //SSI1F29

prod2 -= ((long)sf*(long)g->real[n]); //SSI1F29

prod2 = prod2<<1; //SI1F30



sum1 = sum1+(prod1>>5); //SI6F25

sum2 = sum2+(prod2>>5); //SI6F25



angle = angle+alpha;

}


G->real[k] = (int)(sum1>>16); //SI6F9

chk_real_fix_dft[k] = (float)G->real[k]/(Q15>>6);



G->imag[k] = (int)(sum2>>16); //SI6F9

chk_imag_fix_dft[k] = (float)G->imag[k]/(Q15>>6);


}

}



void idft_fix(complex_fix *g,complex_fix *G,int len)


{

int n,k;

float alp,angle=0,alpha;

int cf,sf;

long prod1,prod2,sum1=0,sum2=0;



alp = (2*PI/N);


for(n=0;n<len;n++)


{

g->real[n] = 0;

g->imag[n] = 0;

alpha = alp*n;

angle = 0;

sum1 =0;

sum2 = 0;


for(k=0;k<len;k++)


{

cf = cos(angle)*Q14; //SI1F14

sf = sin(angle)*Q14; //SI1F14



prod1 = ((long)cf*(long)G->real[k]); //SSI7F23

prod1 -= ((long)sf*(long)G->imag[k]);

prod1 = prod1<<1; //SI7F24



prod2 = ((long)cf*(long)G->imag[k]);

prod2 += ((long)sf*(long)G->real[k]); //SSI7F23

prod2 = prod2<<1; //SI7F24



sum1 = sum1 + (prod1>>5); //SI12F19

sum2 = sum2 + (prod2>>5); //SI12F19


angle = angle +alpha;

}


g->real[n] = (int)(sum1>>16); //SI7F8

chk_real_fix_idft[n] = (float)g->real[n]/(Q15>>7); //for testing CCS calculating with MATLAB Calculation



g->imag[n] = (int)(sum2>>16);

chk_imag_fix_idft[n] = (float)g->imag[n]/(Q15>>7); //for testing CCS calculation with MATLAB Calculation


}

}

MATLAB Programme 03: Implement DFT and IDFT in MATLAB using Fixed point Technique

% Algorithm for Calculating DFT and IDFT using Fixed and Floating Point Technique
clcclear all
close all

N = 32;
M = 16;
x = 0.4 * [ones(1,M),zeros(1,(N-M))];


Q15 = 2^15;
x_fix = fix(Q15 *x); 

X = dft(x,N);
xc = idft(X,N);
stem(real(xc));


%Generate input values for fixed point

fid = fopen('input32_DFT_IDFT_fixed.h','w');
fprintf (fid,'#ifndef\t_INPUT_FIXED_H\n');
fprintf (fid,'#define\t_INPUT_FIXED_H\n');
fprintf (fid,'#define\tSIZE\t%d\n',length(x_fix));
fprintf (fid,'int\tx_fix[SIZE]\t=\t{');
fprintf (fid,'%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,\n',x_fix);
fseek (fid,-1,0);fprintf (fid,'};\n');
fprintf (fid,'#endif\n');
fclose (fid);

%it will generate input file with coefficent of input signal..
 

Thursday 24 October 2013

CCS_Programme 02: Implement DFT and IDFT usign Floating Point Technique in Code Composer Studio

// DFT and IDFT implementation in CCS V4 using floating point technique
//file name: dft_idft.c
// in this programme input signal is sinusoidal whose coefficent are generated using MATLAB (see previous //post)

#include <stdio.h>
#include <math.h>
#include "input.h"

#define N 32                      //for 32 point DFT and IDFT
#define PI (float)3.1416

typedef struct{
float real[N];
float imag[N];
}complex;

complex xn,Xk,xc;

void dft(complex *g,complex *G,int len);
void idft(complex *g,complex *G,int len);

void main()
{
int n;
for(n=0;n<N;n++)
{
xn.real[n] = x[n];
xn.imag[n] = 0;
}
dft(&xn,&Xk,N);
        printf("DFT done\n");
idft(&xc,&Xk,N);
printf ("IDFT done\n");
}

void dft(complex *g,complex *G,int len)
{
int n,k;
float c,s,alpha,alp,angle=0;

alp = (2*PI/N);
for (k=0;k<len;k++)
{
G->real[k] = 0;
G->imag[k] = 0;
alpha = alp*k;
angle = 0;
for (n=0;n<len;n++)
{
c = cos((angle));
s = sin((angle));
G->real[k] += ((c*g->real[n])+(s*g->imag[n]));
G->imag[k] += ((c*g->imag[n])-(s*g->real[n]));
angle = angle+alpha;
}
}
}

void idft(complex *g,complex *G,int len)
{
int n,k;
float c,s,alpha,alp,angle=0;

alp = (2*PI/N);
for (n=0;n<len;n++)
{
g->real[n] = 0;
g->imag[n] = 0;
alpha = alp*n;
angle = 0;
for (k=0;k<len;k++)
{
c = cos((angle));
s = sin((angle));
g->real[n] += ((c*G->real[k])-(s*G->imag[k]));
g->imag[n] += ((c*G->imag[k])+(s*G->real[k]));
angle = angle+alpha;
}
g->real[n]=g->real[n]/N;
g->imag[n]=g->imag[n]/N;
}
}


// check result of CCS (from watch window) with MATLAB computation result ... you will find that both result are same...

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....

MATLAB Programme 01: Implement DFT Without using inbuilt function

DFT Implementation


DFT is used to convert a signal sample array x(n) of size 1 x N to signal spectral component array X(k) of size 1 x N.
 
 
 
%programme
 
clc;close all;

clear all;

xn = input('Enter the sequence');
 
 
ln = length(xn);
xk = zeros(1,ln);
ixk = zeros(1,ln);
 
%Calculating DFT
for k=0:ln-1
   for n=0:ln-1
xk(k+1) = xk(k+1)+(xn(n+1)*exp((-i)*2*pi*k*n/ln));
 
   end
end
%hear xk(k+1); because of in matlab array index start form 1

 
t = 0:ln-1;
subplot(3,2,1);
stem(t,xn);
title ('Input Sequence x(n)');
xlabel('Time Index');
ylabel('Amplitude');
 
t = 0:ln-1;
subplot(3,2,2);
stem(t,xk);
ylabel ('Amplitude');
title ('X(K)');
xlabel ('K');

 

 
t = 0:ln-1;
subplot(3,2,3);
stem(t,abs(xk));
 
 
ylabel ('Amplitude');
xlabel ('K');
title ('Magnitude Response');
 
 
t = 0:ln-1;
subplot(3,2,4);
stem(t,angle(xk));
 
ylabel('Phase');
xlabel('K');
title ('Phase Response');


%-------------------------------------------------------------------%

%Output
%Enter the sequence[ 1 2 3 3 2 1]