Thomas Gartlan
/
DSP1_Sine_wave_2
Sysnthesizin a sine wave at an exact frequency since the reconstruction frequency is fixed.
main.cpp@0:508f6e503222, 2017-03-07 (annotated)
- Committer:
- tgartlan
- Date:
- Tue Mar 07 12:20:07 2017 +0000
- Revision:
- 0:508f6e503222
Sinewave reconstructed from memory with a precise frequency
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tgartlan | 0:508f6e503222 | 1 | #include "mbed.h" |
tgartlan | 0:508f6e503222 | 2 | |
tgartlan | 0:508f6e503222 | 3 | DigitalOut myled(LED1); |
tgartlan | 0:508f6e503222 | 4 | AnalogOut Aout(p18); |
tgartlan | 0:508f6e503222 | 5 | Ticker tick1; |
tgartlan | 0:508f6e503222 | 6 | //Note in this case could reconstruct at known frequency using timer(ticker) |
tgartlan | 0:508f6e503222 | 7 | //125 us ticker gives us 8KHz reconstruction |
tgartlan | 0:508f6e503222 | 8 | //8 samples/period means frequency should be 1KHz |
tgartlan | 0:508f6e503222 | 9 | //works perfectly |
tgartlan | 0:508f6e503222 | 10 | |
tgartlan | 0:508f6e503222 | 11 | |
tgartlan | 0:508f6e503222 | 12 | |
tgartlan | 0:508f6e503222 | 13 | |
tgartlan | 0:508f6e503222 | 14 | unsigned short n=0; |
tgartlan | 0:508f6e503222 | 15 | #define pi 3.1415192 |
tgartlan | 0:508f6e503222 | 16 | #define N 8 //Numebr of samples in one period |
tgartlan | 0:508f6e503222 | 17 | #define w_hat 2*pi/N //Normalised radian frequency |
tgartlan | 0:508f6e503222 | 18 | float sine1 [N]; //contains sine wave sample values |
tgartlan | 0:508f6e503222 | 19 | |
tgartlan | 0:508f6e503222 | 20 | void output_sample(void); |
tgartlan | 0:508f6e503222 | 21 | void init_sine_array(void); |
tgartlan | 0:508f6e503222 | 22 | |
tgartlan | 0:508f6e503222 | 23 | int main() { |
tgartlan | 0:508f6e503222 | 24 | init_sine_array(); |
tgartlan | 0:508f6e503222 | 25 | tick1.attach_us(&output_sample,3); //125 us is 8Khz //limit is 3us which means ~333KHz max fs |
tgartlan | 0:508f6e503222 | 26 | while(1) { |
tgartlan | 0:508f6e503222 | 27 | } |
tgartlan | 0:508f6e503222 | 28 | } |
tgartlan | 0:508f6e503222 | 29 | |
tgartlan | 0:508f6e503222 | 30 | void output_sample(void) |
tgartlan | 0:508f6e503222 | 31 | { |
tgartlan | 0:508f6e503222 | 32 | Aout = sine1[n]; |
tgartlan | 0:508f6e503222 | 33 | n=(n+1)%N; |
tgartlan | 0:508f6e503222 | 34 | } |
tgartlan | 0:508f6e503222 | 35 | |
tgartlan | 0:508f6e503222 | 36 | void init_sine_array(void) |
tgartlan | 0:508f6e503222 | 37 | { |
tgartlan | 0:508f6e503222 | 38 | unsigned short n = 0; |
tgartlan | 0:508f6e503222 | 39 | for (n=0;n<N;n++) |
tgartlan | 0:508f6e503222 | 40 | { |
tgartlan | 0:508f6e503222 | 41 | 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 |
tgartlan | 0:508f6e503222 | 42 | } |
tgartlan | 0:508f6e503222 | 43 | |
tgartlan | 0:508f6e503222 | 44 | } |