- #1
Ele38
- 23
- 0
Hello to everyone :) Since I did not notice a presentation section I put some info about myself before the question,.
My name is Enrico, I am Italian (form Modena) and I am 18. I am studying Electronics&Telecommunications in High School and I have applied for UCSD/UCLA/Berkeley (I hope they want me ). If you have questions, feel free to ask more about me.
Here the question. I am working on a routine which should perform the DFT of a discrete signal in C. More or less it works, but I noticed that the amplitude of the harmonics is not right. Here a little plot of the spectrum (is this the right word?) af a square wave, whose amplitude is (should be) 255.
[PLAIN]http://img407.imageshack.us/img407/4979/quadramigliore.jpg
Here there is my program:
//---------------------------------------------------------------------------
#include <clx.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define N 2048
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
FILE *fp;
int Input[N];
int OutputR[N/2];
int OutputI[N/2];
int OutputAmp[N/2];
int OutputPha[N/2];
double Hamming[N];
int i,k;
double angolo;
//input square wave
for(i=0;i<N;i++){
angolo = (i*M_PI)/180;
Input = (int)(324*sin(angolo) + 108*sin(3*angolo) + 65*sin(5*angolo) + 47*sin(7*angolo) + 36*sin(9*angolo) + 30*sin(11*angolo) + 25*sin(13*angolo) );
}
fp = fopen("file.txt","w");
for(i=0;i<N;i++){
fprintf(fp,"%d \n",Input);
}
//Hamming Window data
for(i=0;i<N;i++){
Hamming = 0.54-0.56*cos(i*M_PI/512);
Input = (int)((double)Input*Hamming);
}
//DFT
//azzero i vettori
for(i=0;i<N/2;i++){
OutputR = 0;
OutputI = 0;
}
//calcolo i valori con la formula
for(k=0;k<N/2;k++){
for(i=0;i<N;i++){
OutputR[k] = OutputR[k]+Input*cos(2*M_PI*k*i/N);
OutputI[k] = OutputI[k]-Input*sin(2*M_PI*k*i/N);
}
}
//conversione polare->rettangolare
for(i=0;i<N/2;i++){
OutputAmp = (int)sqrt(abs(OutputR^2+OutputI^2));
}
//scrivo su file
fp = fopen("file1.txt","w");
for(i=0;i<N/2;i++){
fprintf(fp,"%d \n",OutputAmp);
}
fclose(fp);
return 0;
}
//---------------------------------------------------------------------------
Does someone have advice?
Thank you.
My name is Enrico, I am Italian (form Modena) and I am 18. I am studying Electronics&Telecommunications in High School and I have applied for UCSD/UCLA/Berkeley (I hope they want me ). If you have questions, feel free to ask more about me.
Here the question. I am working on a routine which should perform the DFT of a discrete signal in C. More or less it works, but I noticed that the amplitude of the harmonics is not right. Here a little plot of the spectrum (is this the right word?) af a square wave, whose amplitude is (should be) 255.
[PLAIN]http://img407.imageshack.us/img407/4979/quadramigliore.jpg
Here there is my program:
//---------------------------------------------------------------------------
#include <clx.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define N 2048
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
FILE *fp;
int Input[N];
int OutputR[N/2];
int OutputI[N/2];
int OutputAmp[N/2];
int OutputPha[N/2];
double Hamming[N];
int i,k;
double angolo;
//input square wave
for(i=0;i<N;i++){
angolo = (i*M_PI)/180;
Input = (int)(324*sin(angolo) + 108*sin(3*angolo) + 65*sin(5*angolo) + 47*sin(7*angolo) + 36*sin(9*angolo) + 30*sin(11*angolo) + 25*sin(13*angolo) );
}
fp = fopen("file.txt","w");
for(i=0;i<N;i++){
fprintf(fp,"%d \n",Input);
}
//Hamming Window data
for(i=0;i<N;i++){
Hamming = 0.54-0.56*cos(i*M_PI/512);
Input = (int)((double)Input*Hamming);
}
//DFT
//azzero i vettori
for(i=0;i<N/2;i++){
OutputR = 0;
OutputI = 0;
}
//calcolo i valori con la formula
for(k=0;k<N/2;k++){
for(i=0;i<N;i++){
OutputR[k] = OutputR[k]+Input*cos(2*M_PI*k*i/N);
OutputI[k] = OutputI[k]-Input*sin(2*M_PI*k*i/N);
}
}
//conversione polare->rettangolare
for(i=0;i<N/2;i++){
OutputAmp = (int)sqrt(abs(OutputR^2+OutputI^2));
}
//scrivo su file
fp = fopen("file1.txt","w");
for(i=0;i<N/2;i++){
fprintf(fp,"%d \n",OutputAmp);
}
fclose(fp);
return 0;
}
//---------------------------------------------------------------------------
Does someone have advice?
Thank you.
Last edited by a moderator: