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

No comments:

Post a Comment