Vous êtes sur la page 1sur 4

#include <18f4550.

h>
#include <math.h>
#FUSES NOWDT
#FUSES WDT128
#FUSES HSPLL

//No Watch Dog Timer


//Watch Dog Timer uses 1:128 Postscale
//High Speed Crystal/Resonator with PLL enabl

ed
#FUSES NOPROTECT
#FUSES NOBROWNOUT
#FUSES PUT
#FUSES NOCPD
#FUSES NOSTVREN
#FUSES NODEBUG
#FUSES NOLVP
18) used for I/O
#FUSES NOWRT
#FUSES NOWRTD
#FUSES NOIESO
d
#FUSES NOFCMEN
#FUSES NOPBADEN
RESET
#FUSES NOWRTC
#FUSES NOWRTB
#FUSES NOEBTR
#FUSES NOEBTRB
#FUSES NOCPB
#FUSES MCLR
#FUSES LPT1OSC
#FUSES NOXINST
ng mode disabled (Legacy mode)
#FUSES PLL5
#FUSES CPUDIV1
#FUSES USBDIV
#FUSES NOVREGEN
#FUSES NOICPRT
#define
#define
#define
#define
#define

//Code not protected from reading


//No brownout reset
//Power Up Timer
//No EE protection
//Stack full/underflow will not cause reset
//No Debug mode for ICD
//No low voltage prgming, B3(PIC16) or B5(PIC
//Program memory not write protected
//Data EEPROM not write protected
//Internal External Switch Over mode enable
//Fail-safe clock monitor enabled
//PORTB pins are configured as digital I/O on
//configuration not registers write protected
//Boot block not write protected
//Memory not protected from table reads
//Boot block not protected from table reads
//No Boot Block code protection
//Master Clear pin enabled
//Timer1 configured for low-power operation
//Extended set extension and Indexed Addressi
//Divide By 5(20MHz oscillator input)
//No System Clock Postscaler
//USB clock source comes from PLL divide by 2
//USB voltage regulator disabled
//ICPRT enabled

N 32
N_2 16
pi2 6.28318530718
J pi2/N_2
J1 pi2/N

#use fast_io(b)
float Twid_factor_R[N];
float Twid_factor_C[N];
const float coseno[N_2][N_2]={ {1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1
.0,1.0,1.0,1.0,1.0},{1.0,0.9238795325112867,0.7071067811865476,0.382683432365089
84,0.0,-0.3826834323650897,-0.7071067811865475,-0.9238795325112867,-1.0,-0.92387
95325112868,-0.7071067811865477,-0.38268343236509034,0.0,0.38268343236509,0.7071
067811865474,0.9238795325112865},
{1.0,0.7071067811865476,0.0,-0.707106781186547
5,-1.0,-0.7071067811865477,0.0,0.7071067811865474,1.0,0.7071067811865477,0.0,-0.
7071067811865467,-1.0,-0.7071067811865471,0.0,0.7071067811865466},{1.0,0.3826834
3236508984,-0.7071067811865475,-0.9238795325112868,0.0,0.9238795325112865,0.7071
067811865477,-0.3826834323650899,-1.0,-0.38268343236509056,0.7071067811865466,0.
9238795325112867,0.0,-0.9238795325112864,-0.7071067811865474,0.38268343236508956
},
{1.0,0.0,-1.0,0.0,1.0,0.0,-1.0,0.0,1.0,0.0,-1.

0,0.0,1.0,0.0,-1.0,0.0},{1.0,-0.3826834323650897,-0.7071067811865477,0.923879532
5112865,0.0,-0.9238795325112867,0.7071067811865466,0.38268343236509067,-1.0,0.38
268343236508956,0.7071067811865475,-0.923879532511287,0.0,0.9238795325112876,-0.
7071067811865461,-0.3826834323650912},
{1.0,-0.7071067811865475,0.0,0.707106781186547
7,-1.0,0.7071067811865466,0.0,-0.7071067811865474,1.0,-0.7071067811865464,0.0,0.
7071067811865476,-1.0,0.707106781186546,0.0,-0.7071067811865479},{1.0,-0.9238795
325112867,0.7071067811865474,-0.3826834323650899,0.0,0.38268343236509067,-0.7071
067811865474,0.9238795325112875,-1.0,0.923879532511287,-0.7071067811865461,0.382
6834323650891,0.0,-0.38268343236509145,0.7071067811865505,-0.9238795325112865},
{1.0,-1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,
1.0,-1.0,1.0,-1.0,1.0,-1.0},{1.0,-0.9238795325112868,0.7071067811865477,-0.38268
343236509056,0.0,0.38268343236508956,-0.7071067811865464,0.923879532511287,-1.0,
0.9238795325112877,-0.7071067811865479,0.3826834323650883,0.0,-0.382683432365088
5,0.7071067811865481,-0.9238795325112851},
{1.0,-0.7071067811865477,0.0,0.707106781186546
6,-1.0,0.7071067811865475,0.0,-0.7071067811865461,1.0,-0.7071067811865479,0.0,0.
7071067811865482,-1.0,0.7071067811865508,0.0,-0.7071067811865454},{1.0,-0.382683
43236509034,-0.7071067811865467,0.9238795325112874,0.0,-0.9238795325112856,0.707
1067811865501,0.3826834323650858,-1.0,0.3826834323650949,0.7071067811865432,-0.9
238795325112893,0.0,0.9238795325112837,-0.7071067811865536,-0.3826834323650813},
{1.0,0.0,-1.0,0.0,1.0,0.0,-1.0,0.0,1.0,0.0,-1.
0,0.0,1.0,0.0,-1.0,0.0},{1.0,0.38268343236509,-0.7071067811865471,-0.92387953251
12871,0.0,0.9238795325112863,0.7071067811865486,-0.3826834323650849,-1.0,-0.3826
834323650885,0.7071067811865458,0.9238795325112892,0.0,-0.923879532511287,-0.707
106781186555,0.38268343236508306},
{1.0,0.7071067811865474,0.0,-0.707106781186547
4,-1.0,-0.7071067811865461,0.0,0.7071067811865505,1.0,0.7071067811865481,0.0,-0.
7071067811865486,-1.0,-0.7071067811865449,0.0,0.7071067811865467},{1.0,0.9238795
325112865,0.7071067811865466,0.3826834323650863,0.0,-0.3826834323650912,-0.70710
67811865529,-0.9238795325112893,-1.0,-0.9238795325112851,-0.7071067811865454,-0.
3826834323650813,0.0,0.3826834323650962,0.7071067811865567,0.9238795325112886}
};
const float seno[N_2][N_2]={ {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
,0.0,0.0,0.0,0.0},{0.0,-0.3826834323650898,-0.7071067811865475,-0.92387953251128
67,-1.0,-0.9238795325112867,-0.7071067811865476,-0.3826834323650899,0.0,0.382683
43236508967,0.7071067811865475,0.9238795325112865,1.0,0.9238795325112866,0.70710
67811865477,0.3826834323650904},
{0.0,-0.7071067811865475,-1.0,-0.707106781186547
6,0.0,0.7071067811865475,1.0,0.7071067811865477,0.0,-0.7071067811865474,-1.0,-0.
7071067811865483,0.0,0.7071067811865479,1.0,0.7071067811865485},{0.0,-0.92387953
25112867,-0.7071067811865476,0.38268343236508967,1.0,0.3826834323650904,-0.70710
67811865474,-0.9238795325112867,0.0,0.9238795325112864,0.7071067811865485,-0.382
6834323650897,-1.0,-0.3826834323650907,0.7071067811865477,0.9238795325112868},
{0.0,-1.0,0.0,1.0,0.0,-1.0,0.0,1.0,0.0,-1.0,0.0,
1.0,0.0,-1.0,0.0,1.0},{0.0,-0.9238795325112867,0.7071067811865475,0.382683432365
0904,-1.0,0.38268343236508984,0.7071067811865485,-0.9238795325112864,0.0,0.92387
95325112868,-0.7071067811865477,-0.38268343236508934,1.0,-0.3826834323650876,-0.
7071067811865489,0.9238795325112862},
{0.0,-0.7071067811865476,1.0,-0.7071067811865474
,0.0,0.7071067811865485,-1.0,0.7071067811865477,0.0,-0.7071067811865488,1.0,-0.7
071067811865475,0.0,0.707106781186549,-1.0,0.7071067811865471},{0.0,-0.382683432
3650899,0.7071067811865477,-0.9238795325112867,1.0,-0.9238795325112864,0.7071067
811865477,-0.38268343236508784,0.0,0.38268343236508945,-0.7071067811865489,0.923
8795325112871,-1.0,0.9238795325112861,-0.7071067811865446,0.38268343236509034},
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0
,0.0,0.0,0.0,0.0},{0.0,0.38268343236508967,-0.7071067811865474,0.923879532511286
4,-1.0,0.9238795325112868,-0.7071067811865488,0.38268343236508945,0.0,-0.3826834
323650874,0.7071067811865471,-0.9238795325112874,1.0,-0.9238795325112873,0.70710

6781186547,-0.3826834323650937},
{0.0,0.7071067811865475,-1.0,0.7071067811865485,
0.0,-0.7071067811865477,1.0,-0.7071067811865489,0.0,0.7071067811865471,-1.0,0.70
71067811865468,0.0,-0.7071067811865442,1.0,-0.7071067811865498},{0.0,0.923879532
5112865,-0.7071067811865483,-0.38268343236508806,1.0,-0.3826834323650926,-0.7071
067811865449,0.9238795325112884,0.0,-0.9238795325112846,0.7071067811865518,0.382
68343236508356,-1.0,0.3826834323650971,0.7071067811865415,-0.9238795325112903},
{0.0,1.0,0.0,-1.0,0.0,1.0,0.0,-1.0,0.0,1.0,0.0,1.0,0.0,1.0,0.0,-1.0},{0.0,0.9238795325112866,0.7071067811865479,-0.382683432365
0891,-1.0,-0.3826834323650909,0.7071067811865465,0.9238795325112887,0.0,-0.92387
95325112873,-0.7071067811865492,0.382683432365084,1.0,0.38268343236508945,-0.707
1067811865401,-0.9238795325112895},
{0.0,0.7071067811865477,1.0,0.7071067811865477,0
.0,-0.7071067811865489,-1.0,-0.7071067811865446,0.0,0.707106781186547,1.0,0.7071
067811865465,0.0,-0.7071067811865501,-1.0,-0.7071067811865483},{0.0,0.3826834323
650904,0.7071067811865485,0.9238795325112882,1.0,0.9238795325112862,0.7071067811
865421,0.3826834323650838,0.0,-0.3826834323650937,-0.7071067811865498,-0.9238795
325112903,-1.0,-0.9238795325112841,-0.7071067811865384,-0.38268343236508534} };

int data[N];
float complex_factor,W2,temp,temp2,complex_factor2;
int k=0,nt=0,contador=0,magnitud[N_2],magnitud1[N_2];
float X1_R[N_2];
float X1_C[N_2];
float X2_R[N_2];
float X2_C[N_2];

void Init_Twid_factors(void) {
float TwidComplex_R, TwidComplex_I;
int i;
for (i=0;i<N_2;i++) {
W2=J1*i;
TwidComplex_R=cos(W2);
Twid_factor_R[i]=TwidComplex_R;
TwidComplex_I=-sin(W2);
Twid_factor_C[i]=TwidComplex_I;
}
}
void FFT(){
for(k=0;k<N_2;k++){
X1_R[k]=0;
X2_R[k]=0;
X1_C[k]=0;
X2_C[k]=0;
for(nt=0;nt<N_2;nt++){

//k controla posicion de la transfomada

//nt controla posicion de cada coheficien

te
complex_factor=coseno[k][nt];
complex_factor2=seno[k][nt];
/////////////////TRANSFORMADA MUESTRAS PARES E IMPARES //////////
//////////
temp=complex_factor*data[2*nt];
temp2=complex_factor*data[2*nt+1];

X1_R[k]=X1_R[k]+temp;//+tempo_R;
X2_R[k]=X2_R[k]+temp2;//+tempo_R;
temp=complex_factor2*data[2*nt];
temp2=complex_factor2*data[2*nt+1];
X1_C[k]=X1_C[k]+temp;//+tempo_R;
X2_C[k]=X2_C[k]+temp2;//+tempo_R;
/////////////////////////////////////////////////////////////////
//////////
}
}
for(k=0;k<N_2;k++){
/////////////////////MULTIPLICO MUESTRAS IMPARES POR TWD //////////
//////////////
temp=X2_R[K];
temp2=X2_C[k];
X2_R[k]=(temp*Twid_factor_R[k])-(temp2*Twid_factor_C[k]);
X2_C[k]=(temp*Twid_factor_C[k])+(temp2*Twid_factor_R[k]);
///////////////////////////////////////////////////////////////////
///////////////
/////////////////////TRANSFORMADA FINAL SUMA DE N/2 ///////////////
/////////
temp=X1_R[k];
temp2=X1_C[k];
X1_R[k]=temp+X2_R[k];
X1_C[k]=temp2+X2_C[k];
///////////////////////////////////////////////////////////////////
////////
}
/////////////////////MAGNITUD DE LA TRANSFORMADA//////////////////////////
/
for(k=0;k<N_2;k++){
magnitud[k]=sqrt((X1_R[k]*X1_R[k])+(X1_C[k]*X1_C[k]));
}
//////////////////////////////////////////////////////////////////////////
/
}
Void main (){
/////////////////////////CALCULO DE TWIDLE FACTOR/////////////////////////
/
Init_Twid_factors();
//////////////////////////////////////////////////////////////////////////
/
while(1){
/////////////////////////CALCULO DE LA TRANSFORMADA///////////////////////
/
FFT();
//FFT DE DATA
//////////////////////////////////////////////////////////////////////////
/
}
}