melexia
Member level 2
Hi, I am writing a C code for understanding of discrete Fourier transform. I am taking one sine function as input signal e.g. x[n]=sin (2*3.14*50*n) where 50 is my frequency F. I am calculating it using dft formula and plotting real[w] vs. w and imag[w] vs. w, but not getting proper output. Please help.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
void main()
{
float x[800],re[800],im[800],intre,intim;
int f,w,k,N,i;
float n;
int gdriver = DETECT, gmode;
initgraph(&gdriver,&gmode,"");
printf("maxx=%d,\tmaxy = %d",getmaxx(),getmaxy());
for(n=0,i=0;n < 0.02,i<800;n=n+0.000025,i++)
{
x = sin(2*3.14*50*n);//*0.5*sin(6.28*75*n);
// putpixel(10000*n,(100*x+100),2);
}
intre = 0;
intim = 0;
k= 0;
N=801;
do
{
for(i=0;i<800;i=i+1)
{
re[k] =x*cos(((6.28*k)/N)*i) + intre;
intre = re[k];
im[k] =x*sin(((6.28*k)/N)*i)+intim;
intim = im[k];
}
printf("\n%f\t%d",re[k],k);
printf("\t%f\t%d",im[k],k);
k = k+1;
}while(k<(N-1));
getch();
closegraph();
}
thanks
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
void main()
{
float x[800],re[800],im[800],intre,intim;
int f,w,k,N,i;
float n;
int gdriver = DETECT, gmode;
initgraph(&gdriver,&gmode,"");
printf("maxx=%d,\tmaxy = %d",getmaxx(),getmaxy());
for(n=0,i=0;n < 0.02,i<800;n=n+0.000025,i++)
{
x = sin(2*3.14*50*n);//*0.5*sin(6.28*75*n);
// putpixel(10000*n,(100*x+100),2);
}
intre = 0;
intim = 0;
k= 0;
N=801;
do
{
for(i=0;i<800;i=i+1)
{
re[k] =x*cos(((6.28*k)/N)*i) + intre;
intre = re[k];
im[k] =x*sin(((6.28*k)/N)*i)+intim;
intim = im[k];
}
printf("\n%f\t%d",re[k],k);
printf("\t%f\t%d",im[k],k);
k = k+1;
}while(k<(N-1));
getch();
closegraph();
}
thanks