Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "mbed.h"
- #include <cstdlib>
- // preprocessor directives
- #define M 19 // number of samples processed at any given time
- #define N_SAMPLES 1000 // number of samples to output to file
- #define SAMPLE_RATE 20000 // sample rate of the DAC
- #define InOutMode 0 // operation mode: 0 = DAC (real-time), 1 = file (non-real-time)
- // object declarations
- AnalogIn a_in(p15); // analog input pin
- AnalogOut a_out(p18); // analog output pin
- DigitalOut d_out(p21); // digital output pin
- LocalFileSystem local("local"); // create local filesystem under "local"
- Ticker trigger;
- // function prototypes
- void shiftLocalArrayElements(); // shifts x_local array elements up
- void writeToLocalArray(); // writes the analog input pin value to the last element of x_local
- double processInputSignal(); // applys filter processing to x_local, defined by the coefficients in b
- void filter(); // a consolidation of the functions used in the filtering process
- // global variables
- double x_local[M] = {0.0f}; // sample buffer array
- double b[M] = {0.0f}; // weighting array of low-pass filter coefficients
- int main()
- {
- // initialise p21 as low (logic 0)
- d_out = 0;
- // low-pass filter coefficients
- b[0] = b[18] = -0.002693840;
- b[1] = b[17] = -0.002519748;
- b[2] = b[16] = 0.005014695;
- b[3] = b[15] = 0.015641050;
- b[4] = b[14] = 0.000000000;
- b[5] = b[13] = -0.046914239;
- b[6] = b[12] = -0.048021820;
- b[7] = b[11] = 0.083481298;
- b[8] = b[10] = 0.294332820;
- b[9] = 0.400000000;
- // output to file
- #if InOutMode
- printf("InOutMode 1: file\n");
- // create input and output signal files
- FILE *s_in = fopen("/local/signalInput.txt", "w");
- FILE *s_out = fopen("/local/signalOutput.txt", "w");
- // error checking
- if ((s_in == NULL) || (s_out == NULL)) {
- printf("Program terminated due to file error\n");
- exit(-1);
- }
- else {
- printf("Signal input and output files ready for write\n");
- }
- printf("Writing to files\n");
- // write input and output signals to files N_SAMPLES times
- for (int i = 0; i < N_SAMPLES; i++) {
- shiftLocalArrayElements();
- writeToLocalArray();
- double input_signal = x_local[M - 1];
- double output_signal = processInputSignal();
- fprintf(s_in, "%.4d %8.4f\n", i, input_signal);
- fprintf(s_out, "%.4d %8.4f\n", i, output_signal);
- };
- printf("Writes complete\n");
- // close files
- fclose(s_in);
- fclose(s_out);
- printf("Files closed\n");
- // output to DAC
- #else
- printf("InOutMode 0: DAC\n");
- // attach filter function to the Ticker object; to be called every sample period
- trigger.attach(&filter, 1 / SAMPLE_RATE);
- // loop infinitely
- while(1) {
- }
- #endif
- return 0;
- }
- void shiftLocalArrayElements()
- {
- // shift index value i to i - 1
- for (int i = 1; i < M; i++) {
- x_local[i - 1] = x_local[i];
- }
- }
- void writeToLocalArray()
- {
- // set p21 high (logic 1)
- d_out = 1;
- // write analog input pin value to the last element of x_local
- x_local[M - 1] = a_in.read();
- // set p21 low
- d_out = 0;
- }
- double processInputSignal()
- {
- double y = 0.0f;
- // apply filtering on x_local elements, based on the coefficients stored in b
- for (int i = 0; i < M; i++) {
- y += b[i] * x_local[(M - 1) - i];
- }
- // return the filtered sample
- return y;
- }
- void filter()
- {
- shiftLocalArrayElements();
- writeToLocalArray();
- double output_sample = processInputSignal();
- // write the filtered sample to the Analog output pin
- a_out.write(output_sample);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement