Sysnthesizin a sine wave at an exact frequency since the reconstruction frequency is fixed.

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
tgartlan
Date:
Tue Mar 07 12:20:07 2017 +0000
Commit message:
Sinewave reconstructed from memory with a precise frequency

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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Mar 07 12:20:07 2017 +0000
@@ -0,0 +1,44 @@
+#include "mbed.h"
+
+DigitalOut myled(LED1);
+AnalogOut Aout(p18);
+Ticker tick1;
+//Note in this case could reconstruct at known frequency using timer(ticker)
+//125 us ticker gives us 8KHz reconstruction
+//8 samples/period means frequency should be 1KHz
+//works perfectly
+
+
+
+
+unsigned short n=0;
+#define pi 3.1415192
+#define N 8                 //Numebr of samples in one period 
+#define w_hat 2*pi/N          //Normalised radian frequency               
+float sine1 [N];              //contains sine wave sample values
+
+void output_sample(void);
+void init_sine_array(void);
+
+int main() {
+    init_sine_array();
+    tick1.attach_us(&output_sample,3);    //125 us is 8Khz    //limit is 3us which means ~333KHz max fs
+    while(1) {     
+    }
+}
+
+void output_sample(void)
+{
+    Aout = sine1[n];
+    n=(n+1)%N;
+}
+
+void init_sine_array(void)
+{
+    unsigned short n = 0;
+    for (n=0;n<N;n++)
+    {
+        sine1[n] = 0.5 + 0.5*sin(n*w_hat);   //note the 0.5 V of offset since DAC outputs voltage between 0 and 3.3V
+    }
+    
+ }   
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Mar 07 12:20:07 2017 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/8a40adfe8776
\ No newline at end of file