Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 00003 00004 DigitalOut myled(LED1); 00005 DigitalOut led(LED2); 00006 DigitalOut led2(LED3); 00007 DigitalOut led3(LED4); 00008 const int N=1024; // Sample number 00009 struct TWIDDLE { // Structure factor for calculate fft 00010 00011 float cosval [N/2]; 00012 float sinval [N/2]; 00013 00014 }; 00015 void FFT(float datos[N], int n, TWIDDLE &w); //Declaration of functions 00016 void enviodatos(float datos[N], int n); 00017 Serial pc(USBTX, USBRX); //Declaration Serial port 00018 00019 int main() { //Main program 00020 myled.write(0); 00021 led.write(0); 00022 led2.write(0); 00023 led3.write(0); 00024 int i; //Parameters to calculate the values of a sine function. DATOS[N]=Amp*sin(w0*t) 00025 float arg; 00026 arg=2*3.1416/N; //argument of the structure TWIDDLE 00027 float tm,t,w0,T, Amp; 00028 tm=0.1; // increments of time (0,1seg) 00029 t=0; //initial time 00030 T=2*3.1416; //Period of signal 00031 w0=(2*3.1416/T); // angular frequency 00032 Amp=3; //Amplitude 00033 struct TWIDDLE W; //Declarate of a type structure 00034 00035 for (i=0; i<N/2; i++) { //Calculate all the TWIDDLE factors 00036 W.cosval[i]= cos(i*arg); 00037 W.sinval[i]= -sin(i*arg); 00038 } 00039 00040 float DATOS[N]; //array to save the sinus values 00041 for (i=0; i<N; i++) { //calculate the sinus values 00042 DATOS[i]=Amp*sin(w0*t); 00043 t+=tm; 00044 } 00045 led.write(1); //Execution of the program to here OK 00046 FFT(DATOS,N,W); //Function to calculate FFT 00047 enviodatos(DATOS,N); //Function to send values to serial port 00048 led3.write(1); //Execution of the program to here OK 00049 while (1); // 00050 00051 } 00052 00053 void FFT(float datos[N], int n,TWIDDLE &w) { 00054 float temp1, temp2, temp3,temp4; //variables needed to calculate FFT 00055 int i,j,k,p; 00056 int upper_leg, lower_leg; 00057 int leg_diff; 00058 int num_stages=0; 00059 int index, step; 00060 float datos_real[n], datos_imag[n]; //real and imaginary values 00061 for (p=0; p<n; p++) { 00062 datos_real[p]=datos[p]; 00063 datos_imag[p]=0; 00064 } 00065 led2.write(1); // Execution of the program to here break it if N=>2048, else OK. 00066 i=1; 00067 do { 00068 num_stages+=1; 00069 i=i*2; 00070 } while (i!=n); 00071 00072 leg_diff=n/2; 00073 step=1; 00074 00075 for (i=0; i<num_stages; i++) { 00076 index=0; 00077 for (j=0; j<leg_diff; j++) { 00078 for (upper_leg=j; upper_leg<n; upper_leg+=(2*leg_diff)) { 00079 lower_leg=upper_leg+leg_diff; 00080 00081 temp1= (datos_real[upper_leg]+ datos_real[lower_leg]); 00082 temp2= (datos_real[upper_leg]- datos_real[lower_leg]); 00083 temp3= (datos_imag[upper_leg]+ datos_imag[lower_leg]); 00084 temp4= (datos_imag[upper_leg]- datos_imag[lower_leg]); 00085 datos_real[lower_leg]= (temp2*w.cosval[index])-(temp4*w.sinval[index]); 00086 datos_imag[lower_leg]= (temp2*w.sinval[index]+temp4*w.cosval[index]); 00087 datos_real[upper_leg]= temp1; 00088 datos_imag[upper_leg]= temp3; 00089 } 00090 index+=step; 00091 } 00092 leg_diff=leg_diff/2; 00093 step*=2; 00094 } 00095 //bit reversal 00096 00097 j=0; 00098 for (i=1; i<(N-2); i++) { 00099 k=n/2; 00100 while (k<=j) { 00101 j=j-k; 00102 k=k/2; 00103 } 00104 j=j+k; 00105 00106 if (i<j) { 00107 temp1= datos_real[j]; 00108 temp2= datos_imag[j]; 00109 datos_real[j]=datos_real[i]; 00110 datos_imag[j]=datos_imag[i]; 00111 datos_real[i]=temp1; 00112 datos_imag[i]=temp2; 00113 } 00114 } 00115 00116 for (i=0; i<n; i++) { 00117 00118 datos[i]= pow(sqrt(pow(datos_real[i],2)+pow(datos_imag[i],2)),2); //Calculate the power fft 00119 }; 00120 00121 } 00122 00123 void enviodatos(float datos[N],int n) { 00124 int i=0,j=0,k=0,aux=0; //send to serial port the values correctly. 00125 unsigned char bytes[4]; 00126 float dato; 00127 00128 for (i=0; i<n; i++) { 00129 dato=datos[i]; 00130 aux=dato; 00131 for (j=0; j<4; j++) { 00132 switch (j) { 00133 case 0: 00134 bytes[j]=aux; 00135 aux=aux>>8; 00136 break; 00137 case 1: 00138 bytes[j]=aux; 00139 aux=aux>>8; 00140 break; 00141 case 2: 00142 bytes[j]=aux; 00143 aux=aux>>8; 00144 break; 00145 case 3: 00146 bytes[j]=aux; 00147 aux=aux>>8; 00148 break; 00149 } 00150 } 00151 for (k=3; k>=0; k--) { 00152 00153 while (pc.writeable()==0); 00154 pc.putc(bytes[k]); 00155 00156 } 00157 } 00158 }
Generated on Fri Jul 15 2022 16:56:00 by
1.7.2