Real Imag: Complex Complex
Real Imag: Complex Complex
h>
#define PTS 64 //# of points for FFT
#define PI 3.14159265358979
typedef struct {float real,imag;} COMPLEX;
void FFT(COMPLEX *Y, int n); //FFT prototype
float iobuffer[PTS]; //as input and output buffer
float x1[PTS]; //intermediate buffer
short i; //general purpose index variable
short buffercount = 0; //number of new samples in iobuffer
short flag = 0; //set to 1 by ISR when iobuffer full
COMPLEX w[PTS]; //twiddle constants stored in w
COMPLEX samples[PTS]; //primary working buffer
main()
{
for (i = 0 ; i<PTS ; i++) // set up twiddle constants in w
{
w[i].real = cos(2*PI*i/(PTS*2.0)); //Re component of twiddle constants
w[i].imag =-sin(2*PI*i/(PTS*2.0)); //Im component of twiddle constants
}
for (i = 0 ; i < PTS ; i++) //swap buffers
{
iobuffer[i] = sin(2*PI*5*i/64.0);/*10- > freq, 64 -> sampling freq*/
samples[i].real=0.0;
samples[i].imag=0.0;
}
for (i = 0 ; i < PTS ; i++) //swap buffers
{
samples[i].real=iobuffer[i]; //buffer with new data
}
for (i = 0 ; i < PTS ; i++)
samples[i].imag = 0.0; //imag components = 0
FFT(samples,PTS); //call function FFT.c
for (i = 0 ; i < PTS ; i++) //compute magnitude
{
x1[i] = sqrt(samples[i].real*samples[i].real+ samples[i].imag*samples[i].imag);
}
} //end of main
#include<stdio.h>
#include<math.h>
#define pi 3.14
typedef struct {
float real;
float img;
}com;
void main()
{
com x[20],y[20],temp[20];
int pofd,i,k,n;
float WN,c,s,WK;
for(i=0;i<=20;i++) /*make output array value to 0.0*/
{
y[i].real=0.0;
y[i].img=0.0;
}
printf("\nEnter the no of points of idft \t:");
scanf("%d",&pofd);
printf("\nEnter the function variables of f(n) to calculate IDFT in the
complex form (e.g. 3+j4 enter as a 3 4)\n");
for(i=0;i<pofd;i++)
{
scanf("%f\t%f",&x[i].real,&x[i].img);
}
WN=(2*pi)/pofd;
for(k=0;k<pofd;k++)
{
temp[k].real=0.0;
temp[k].img=0.0;
WK=k*WN;
for(n=0;n<pofd;n++) /*DFT calculation*/
{
c=cos(WK*n);
s=sin(WK*n);
temp[k].real=temp[k].real+(x[n].real*c)-
(x[n].img*s);
temp[k].img=temp[k].img+(x[n].img*c)+
(x[n].real*s);
}
y[k].real=1/sqrt(pofd)*temp[k].real;
y[k].img=1/sqrt(pofd)*temp[k].img;
}
/*Display values of F[k]*/
printf("\nIDFT OF GIVEN SIGNAL:-\n"); for(i=0;i<pofd;i++)
{
printf("\nf(%d)=(%0.1f)+j(%0.1f)\n",i,y[i].real,y[i].img);
}
}
Dft
#include<stdio.h>
#include<math.h>
int N,k,n,i;
float pi=3.1416,sumre=0, sumim=0,out_real[8]={0.0}, out_imag[8]={0.0};
int x[32];
void main(void)
{
printf(" enter the length of the sequence\n");
scanf("%d",&N);
printf(" enter the sequence\n");
for(i=0;i<N;i++)
scanf("%d",&x[i]);
for(k=0;k<N;k++)
{
sumre=0;
sumim=0;
for(n=0;n<N;n++)
{
sumre=sumre+x[n]* cos(2*pi*k*n/N);
sumim=sumim-x[n]* sin(2*pi*k*n/N);
}
out_real[k]=sumre;
out_imag[k]=sumim;
printf("X([%d])=\t%f\t+\t%fi\n",k,out_real[k],out_imag[k]);
}
}