Advertisement
ReubenFrankel

mbed LPC1768 Digital Low-Pass Filter

Oct 27th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.87 KB | None | 0 0
  1. #include "mbed.h"
  2. #include <cstdlib>
  3.  
  4. // preprocessor directives
  5. #define M 19                        // number of samples processed at any given time
  6. #define N_SAMPLES 1000              // number of samples to output to file
  7. #define SAMPLE_RATE 20000           // sample rate of the DAC
  8. #define InOutMode 0                 // operation mode: 0 = DAC (real-time), 1 = file (non-real-time)
  9.  
  10. // object declarations
  11. AnalogIn a_in(p15);                 // analog input pin
  12. AnalogOut a_out(p18);               // analog output pin
  13. DigitalOut d_out(p21);              // digital output pin
  14. LocalFileSystem local("local");     // create local filesystem under "local"
  15. Ticker trigger;
  16.  
  17. // function prototypes
  18. void shiftLocalArrayElements();     // shifts x_local array elements up
  19. void writeToLocalArray();           // writes the analog input pin value to the last element of x_local
  20. double processInputSignal();        // applys filter processing to x_local, defined by the coefficients in b
  21. void filter();                      // a consolidation of the functions used in the filtering process
  22.  
  23. // global variables
  24. double x_local[M] = {0.0f};         // sample buffer array
  25. double b[M] = {0.0f};               // weighting array of low-pass filter coefficients
  26.  
  27. int main()
  28. {
  29.  
  30.     // initialise p21 as low (logic 0)
  31.     d_out = 0;
  32.  
  33.     // low-pass filter coefficients
  34.     b[0] = b[18] = -0.002693840;
  35.     b[1] = b[17] = -0.002519748;
  36.     b[2] = b[16] = 0.005014695;
  37.     b[3] = b[15] = 0.015641050;
  38.     b[4] = b[14] = 0.000000000;
  39.     b[5] = b[13] = -0.046914239;
  40.     b[6] = b[12] = -0.048021820;
  41.     b[7] = b[11] = 0.083481298;
  42.     b[8] = b[10] = 0.294332820;
  43.     b[9] = 0.400000000;
  44.  
  45. // output to file
  46. #if InOutMode
  47.  
  48.     printf("InOutMode 1: file\n");
  49.  
  50.     // create input and output signal files
  51.     FILE *s_in = fopen("/local/signalInput.txt", "w");
  52.     FILE *s_out = fopen("/local/signalOutput.txt", "w");
  53.  
  54.     // error checking
  55.     if ((s_in == NULL) || (s_out == NULL)) {
  56.  
  57.         printf("Program terminated due to file error\n");
  58.         exit(-1);
  59.     }
  60.  
  61.     else {
  62.  
  63.         printf("Signal input and output files ready for write\n");
  64.     }
  65.  
  66.     printf("Writing to files\n");
  67.  
  68.     // write input and output signals to files N_SAMPLES times
  69.     for (int i = 0; i < N_SAMPLES; i++) {
  70.  
  71.         shiftLocalArrayElements();
  72.         writeToLocalArray();
  73.  
  74.         double input_signal = x_local[M - 1];
  75.         double output_signal = processInputSignal();
  76.  
  77.         fprintf(s_in, "%.4d %8.4f\n", i, input_signal);
  78.         fprintf(s_out, "%.4d %8.4f\n", i, output_signal);
  79.     };
  80.  
  81.     printf("Writes complete\n");
  82. // close files
  83.     fclose(s_in);
  84.     fclose(s_out);
  85.     printf("Files closed\n");
  86.  
  87. // output to DAC
  88. #else
  89.  
  90.     printf("InOutMode 0: DAC\n");
  91.  
  92.     // attach filter function to the Ticker object; to be called every sample period
  93.     trigger.attach(&filter, 1 / SAMPLE_RATE);
  94.    
  95.     // loop infinitely
  96.     while(1) {
  97.  
  98.     }
  99.  
  100. #endif
  101.  
  102.     return 0;
  103. }
  104.  
  105. void shiftLocalArrayElements()
  106. {
  107.    
  108.     // shift index value i to i - 1
  109.     for (int i = 1; i < M; i++) {
  110.        
  111.         x_local[i - 1] = x_local[i];
  112.     }
  113. }
  114.  
  115. void writeToLocalArray()
  116. {
  117.    
  118.     // set p21 high (logic 1)
  119.     d_out = 1;
  120.     // write analog input pin value to the last element of x_local
  121.     x_local[M - 1] = a_in.read();
  122.     // set p21 low
  123.     d_out = 0;
  124. }
  125.  
  126. double processInputSignal()
  127. {
  128.  
  129.     double y = 0.0f;
  130.    
  131.     // apply filtering on x_local elements, based on the coefficients stored in b
  132.     for (int i = 0; i < M; i++) {
  133.  
  134.         y += b[i] * x_local[(M - 1) - i];
  135.     }
  136.    
  137.     // return the filtered sample
  138.     return y;
  139. }
  140.  
  141. void filter()
  142. {
  143.  
  144.     shiftLocalArrayElements();
  145.     writeToLocalArray();
  146.     double output_sample = processInputSignal();
  147.     // write the filtered sample to the Analog output pin
  148.     a_out.write(output_sample);
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement