A test for a digital envelope detector using a rectifier and a low-pass filter. The program first creates sample vectors for sinusoidal waves of 2Hz and 20Hz (considering 1kHz sampling frequency). Then, every 10ms it generates a new sample for a modulated wave (multiply both waves), and passes it through a full-wave rectifier and a Chebyshev low-pass filter with cutoff frequency of 5Hz. Then it sends the original wave and the recovered envelope through serial port in CSV format. I used 10ms interval instead of 1ms because otherwise the serial speed would have to be too high.

Dependencies:   mbed

Committer:
quevedo
Date:
Tue Aug 26 17:02:29 2014 +0000
Revision:
0:1ba9469c839f
Original program;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
quevedo 0:1ba9469c839f 1 #include "mbed.h"
quevedo 0:1ba9469c839f 2 #include "math.h"
quevedo 0:1ba9469c839f 3 #define PI 3.1415926536
quevedo 0:1ba9469c839f 4 //#define GAIN 2.661812926e+05 // For Buttherworth
quevedo 0:1ba9469c839f 5 #define GAIN 3.676489487e+05 // For Chebyshev
quevedo 0:1ba9469c839f 6
quevedo 0:1ba9469c839f 7 // Pin and timer setup
quevedo 0:1ba9469c839f 8 DigitalIn b0(PTA4);
quevedo 0:1ba9469c839f 9 Serial pc(USBTX, USBRX);
quevedo 0:1ba9469c839f 10 Ticker samp;
quevedo 0:1ba9469c839f 11
quevedo 0:1ba9469c839f 12 double in, rect, out;
quevedo 0:1ba9469c839f 13 double xv[4], yv[4];
quevedo 0:1ba9469c839f 14 double s2[500], s20[50];
quevedo 0:1ba9469c839f 15 int i, i2, i20;
quevedo 0:1ba9469c839f 16
quevedo 0:1ba9469c839f 17 // ISR for periodical interrupt
quevedo 0:1ba9469c839f 18 void newsample(void) {
quevedo 0:1ba9469c839f 19 in = s2[i2] * s20[i20];
quevedo 0:1ba9469c839f 20 i2++;
quevedo 0:1ba9469c839f 21 if(i2 > 499) {
quevedo 0:1ba9469c839f 22 i2 = 0;
quevedo 0:1ba9469c839f 23 }
quevedo 0:1ba9469c839f 24 i20++;
quevedo 0:1ba9469c839f 25 if(i20 > 49) i20 = 0;
quevedo 0:1ba9469c839f 26 i++;
quevedo 0:1ba9469c839f 27
quevedo 0:1ba9469c839f 28 rect = abs(in); // Full-wave rectification
quevedo 0:1ba9469c839f 29 // The filter is low-pass IIR 3rd-order -0.5dB Chebyshev, cutoff frequency 5Hz for 1kHz samples.
quevedo 0:1ba9469c839f 30 // Design was made through the website: http://www-users.cs.york.ac.uk/~fisher/mkfilter/
quevedo 0:1ba9469c839f 31 xv[0] = xv[1];
quevedo 0:1ba9469c839f 32 xv[1] = xv[2];
quevedo 0:1ba9469c839f 33 xv[2] = xv[3];
quevedo 0:1ba9469c839f 34 xv[3] = rect / GAIN;
quevedo 0:1ba9469c839f 35 yv[0] = yv[1];
quevedo 0:1ba9469c839f 36 yv[1] = yv[2];
quevedo 0:1ba9469c839f 37 yv[2] = yv[3];
quevedo 0:1ba9469c839f 38 //yv[3] = xv[0] + xv[3] + 3 * (xv[1] + xv[2]) + (0.9390989403 * yv[0]) + (-2.8762997235 * yv[1]) + (2.9371707284 * yv[2]); // For Butterworth
quevedo 0:1ba9469c839f 39 yv[3] = xv[0] + xv[3] + 3 * (xv[1] + xv[2]) + (0.9614041736 * yv[0]) + (-2.9213338983 * yv[1]) + (2.9599079648 * yv[2]); // For Chebyshev
quevedo 0:1ba9469c839f 40 out = yv[3];
quevedo 0:1ba9469c839f 41
quevedo 0:1ba9469c839f 42 printf("%f,%f\r\n", in, out);
quevedo 0:1ba9469c839f 43 }
quevedo 0:1ba9469c839f 44
quevedo 0:1ba9469c839f 45 void initfilter(void) {
quevedo 0:1ba9469c839f 46 char k;
quevedo 0:1ba9469c839f 47 for(k = 0; k < 4; k++) {
quevedo 0:1ba9469c839f 48 xv[k] = 0;
quevedo 0:1ba9469c839f 49 yv[k] = 0;
quevedo 0:1ba9469c839f 50 }
quevedo 0:1ba9469c839f 51 }
quevedo 0:1ba9469c839f 52
quevedo 0:1ba9469c839f 53 // Main function
quevedo 0:1ba9469c839f 54 int main() {
quevedo 0:1ba9469c839f 55 initfilter();
quevedo 0:1ba9469c839f 56 pc.baud(57600);
quevedo 0:1ba9469c839f 57
quevedo 0:1ba9469c839f 58 for(i = 0; i < 50; i++) {
quevedo 0:1ba9469c839f 59 s20[i] = sin(i*PI/25);
quevedo 0:1ba9469c839f 60 }
quevedo 0:1ba9469c839f 61 for(i = 0; i < 500; i++) {
quevedo 0:1ba9469c839f 62 s2[i] = sin(i*PI/250);;
quevedo 0:1ba9469c839f 63 }
quevedo 0:1ba9469c839f 64 i2 = 0;
quevedo 0:1ba9469c839f 65 i20 = 0;
quevedo 0:1ba9469c839f 66 i = 0;
quevedo 0:1ba9469c839f 67 while(b0); // waits for PB0 push
quevedo 0:1ba9469c839f 68 samp.attach(&newsample, 0.01); // periodical interrupt every 10ms
quevedo 0:1ba9469c839f 69 while(i < 5000) {
quevedo 0:1ba9469c839f 70 //
quevedo 0:1ba9469c839f 71 }
quevedo 0:1ba9469c839f 72 samp.detach();
quevedo 0:1ba9469c839f 73
quevedo 0:1ba9469c839f 74 while(1) {
quevedo 0:1ba9469c839f 75 }
quevedo 0:1ba9469c839f 76 }