Fourier series square wave

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
oguro
Date:
Thu Mar 09 02:29:54 2017 +0000
Commit message:
Square wave Fourier series;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 3052351a8da9 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Mar 09 02:29:54 2017 +0000
@@ -0,0 +1,81 @@
+#include "mbed.h"
+#include <math.h>
+int i=1,N=50,q=0;
+
+float wt=0;
+
+
+float aout1=0,aout2=0;
+AnalogOut aout(PA_4);
+AnalogIn V_adc(PB_1);
+float Vr_adc;
+Serial pc(USBTX,USBRX);
+
+DigitalOut myled(LED1);
+   
+Timer timer1; 
+
+     
+int main(){  
+  
+     pc.baud(128000);
+     const float freq = 10;//50
+     
+    timer1.start();
+
+    while(1) {
+        Vr_adc=V_adc.read();
+        if(Vr_adc<0.005f){
+              N=1;
+              }
+        if((Vr_adc>0.005f)&&(Vr_adc<=0.1f)){
+              N=2;
+              }
+       if((Vr_adc>0.1f)&&(Vr_adc<=0.2f)){
+              N=5;
+              }
+        if((Vr_adc>0.2f)&&(Vr_adc<=0.4f)){
+              N=10;
+              }
+       if((Vr_adc>0.4f)&&(Vr_adc<=0.6f)){
+              N=20;
+              }   
+       if((Vr_adc>0.6f)&&(Vr_adc<=0.8f)){
+              N=30;
+              }
+      if(Vr_adc>0.8f){
+              N=60;
+              }      
+        float s = timer1.read();
+        myled = ((int)s) % 2 == 0; /* LED output(0.5Hz) */
+        wt=fmodf(s * freq, 1) * 3.14159265359 * 2;
+        for(i=1;i<=2*N-1;i=i+2){
+        //aout2 = VDD/2 +2/VDD* (sin(wt)+sin(3*wt)/3+sin(5*wt)/5+.......)
+        if(i==0){
+          aout1=sin((i-1)*wt)/(i-1);  //if i==N i even
+          }
+          else{
+          aout1=sin(i*wt)/i;
+          }
+          aout2=aout2+aout1;
+       
+           }
+         
+          aout=0.1+(3.3/2+(2*3.3/3.14159265359)*aout2)/5; //VDD=3.3
+          aout2=0;
+          myled = !myled;
+          if(q>100000){
+           timer1.reset();
+           q=0;
+           }
+           q++;
+         //  if((q%100)==0){
+            pc.printf("%.3f \r" ,aout*10);
+         //  }
+
+    }
+  
+       
+       
+       
+}
diff -r 000000000000 -r 3052351a8da9 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Mar 09 02:29:54 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/abea610beb85
\ No newline at end of file