Fourier series square wave

Dependencies:   mbed

Committer:
oguro
Date:
Thu Mar 09 02:29:54 2017 +0000
Revision:
0:3052351a8da9
Square wave Fourier series;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
oguro 0:3052351a8da9 1 #include "mbed.h"
oguro 0:3052351a8da9 2 #include <math.h>
oguro 0:3052351a8da9 3 int i=1,N=50,q=0;
oguro 0:3052351a8da9 4
oguro 0:3052351a8da9 5 float wt=0;
oguro 0:3052351a8da9 6
oguro 0:3052351a8da9 7
oguro 0:3052351a8da9 8 float aout1=0,aout2=0;
oguro 0:3052351a8da9 9 AnalogOut aout(PA_4);
oguro 0:3052351a8da9 10 AnalogIn V_adc(PB_1);
oguro 0:3052351a8da9 11 float Vr_adc;
oguro 0:3052351a8da9 12 Serial pc(USBTX,USBRX);
oguro 0:3052351a8da9 13
oguro 0:3052351a8da9 14 DigitalOut myled(LED1);
oguro 0:3052351a8da9 15
oguro 0:3052351a8da9 16 Timer timer1;
oguro 0:3052351a8da9 17
oguro 0:3052351a8da9 18
oguro 0:3052351a8da9 19 int main(){
oguro 0:3052351a8da9 20
oguro 0:3052351a8da9 21 pc.baud(128000);
oguro 0:3052351a8da9 22 const float freq = 10;//50
oguro 0:3052351a8da9 23
oguro 0:3052351a8da9 24 timer1.start();
oguro 0:3052351a8da9 25
oguro 0:3052351a8da9 26 while(1) {
oguro 0:3052351a8da9 27 Vr_adc=V_adc.read();
oguro 0:3052351a8da9 28 if(Vr_adc<0.005f){
oguro 0:3052351a8da9 29 N=1;
oguro 0:3052351a8da9 30 }
oguro 0:3052351a8da9 31 if((Vr_adc>0.005f)&&(Vr_adc<=0.1f)){
oguro 0:3052351a8da9 32 N=2;
oguro 0:3052351a8da9 33 }
oguro 0:3052351a8da9 34 if((Vr_adc>0.1f)&&(Vr_adc<=0.2f)){
oguro 0:3052351a8da9 35 N=5;
oguro 0:3052351a8da9 36 }
oguro 0:3052351a8da9 37 if((Vr_adc>0.2f)&&(Vr_adc<=0.4f)){
oguro 0:3052351a8da9 38 N=10;
oguro 0:3052351a8da9 39 }
oguro 0:3052351a8da9 40 if((Vr_adc>0.4f)&&(Vr_adc<=0.6f)){
oguro 0:3052351a8da9 41 N=20;
oguro 0:3052351a8da9 42 }
oguro 0:3052351a8da9 43 if((Vr_adc>0.6f)&&(Vr_adc<=0.8f)){
oguro 0:3052351a8da9 44 N=30;
oguro 0:3052351a8da9 45 }
oguro 0:3052351a8da9 46 if(Vr_adc>0.8f){
oguro 0:3052351a8da9 47 N=60;
oguro 0:3052351a8da9 48 }
oguro 0:3052351a8da9 49 float s = timer1.read();
oguro 0:3052351a8da9 50 myled = ((int)s) % 2 == 0; /* LED output(0.5Hz) */
oguro 0:3052351a8da9 51 wt=fmodf(s * freq, 1) * 3.14159265359 * 2;
oguro 0:3052351a8da9 52 for(i=1;i<=2*N-1;i=i+2){
oguro 0:3052351a8da9 53 //aout2 = VDD/2 +2/VDD* (sin(wt)+sin(3*wt)/3+sin(5*wt)/5+.......)
oguro 0:3052351a8da9 54 if(i==0){
oguro 0:3052351a8da9 55 aout1=sin((i-1)*wt)/(i-1); //if i==N i even
oguro 0:3052351a8da9 56 }
oguro 0:3052351a8da9 57 else{
oguro 0:3052351a8da9 58 aout1=sin(i*wt)/i;
oguro 0:3052351a8da9 59 }
oguro 0:3052351a8da9 60 aout2=aout2+aout1;
oguro 0:3052351a8da9 61
oguro 0:3052351a8da9 62 }
oguro 0:3052351a8da9 63
oguro 0:3052351a8da9 64 aout=0.1+(3.3/2+(2*3.3/3.14159265359)*aout2)/5; //VDD=3.3
oguro 0:3052351a8da9 65 aout2=0;
oguro 0:3052351a8da9 66 myled = !myled;
oguro 0:3052351a8da9 67 if(q>100000){
oguro 0:3052351a8da9 68 timer1.reset();
oguro 0:3052351a8da9 69 q=0;
oguro 0:3052351a8da9 70 }
oguro 0:3052351a8da9 71 q++;
oguro 0:3052351a8da9 72 // if((q%100)==0){
oguro 0:3052351a8da9 73 pc.printf("%.3f \r" ,aout*10);
oguro 0:3052351a8da9 74 // }
oguro 0:3052351a8da9 75
oguro 0:3052351a8da9 76 }
oguro 0:3052351a8da9 77
oguro 0:3052351a8da9 78
oguro 0:3052351a8da9 79
oguro 0:3052351a8da9 80
oguro 0:3052351a8da9 81 }