Joel Murphy / Mbed 2 deprecated ADS_StreamRawData

Dependencies:   mbed

Committer:
biomurph
Date:
Mon Mar 23 19:22:04 2015 +0000
Revision:
0:675506e540be
Publishing this old ADS1299 code for the first time!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
biomurph 0:675506e540be 1
biomurph 0:675506e540be 2 #include "mbed.h"
biomurph 0:675506e540be 3 #include "ADS1299.h"
biomurph 0:675506e540be 4
biomurph 0:675506e540be 5 Serial pc(USBTX, USBRX); // set up the COM
biomurph 0:675506e540be 6 SPI spi (PTD2,PTD3,PTD1); // mosi, miso, sck
biomurph 0:675506e540be 7
biomurph 0:675506e540be 8
biomurph 0:675506e540be 9 //define how I'd like my channels setup
biomurph 0:675506e540be 10 #define MAX_N_CHANNELS (8) //must be less than or equal to length of channelData in ADS1299 object!!
biomurph 0:675506e540be 11 int nActiveChannels = 8; //how many active channels would I like?
biomurph 0:675506e540be 12 byte gainCode = ADS_GAIN24; //how much gain do I want
biomurph 0:675506e540be 13 byte inputType = ADSINPUT_NORMAL; //here's the normal way to setup the channels
biomurph 0:675506e540be 14 //byte inputType = ADSINPUT_SHORTED; //here's another way to setup the channels
biomurph 0:675506e540be 15 //byte inputType = ADSINPUT_TESTSIG; //here's a third way to setup the channels
biomurph 0:675506e540be 16
biomurph 0:675506e540be 17 //other variables
biomurph 0:675506e540be 18 long sampleCounter = 0; // used to time the tesing loop
biomurph 0:675506e540be 19 boolean is_running = false; // this flag is set in serialEvent on reciept of prompt
biomurph 0:675506e540be 20 #define PIN_STARTBINARY (7) //pull this pin to ground to start binary transfer
biomurph 0:675506e540be 21 //define PIN_STARTBINARY_OPENEEG (6)
biomurph 0:675506e540be 22 boolean startBecauseOfPin = false;
biomurph 0:675506e540be 23 boolean startBecauseOfSerial = false;
biomurph 0:675506e540be 24
biomurph 0:675506e540be 25 #define OUTPUT_NOTHING (0)
biomurph 0:675506e540be 26 #define OUTPUT_TEXT (1)
biomurph 0:675506e540be 27 #define OUTPUT_BINARY (2)
biomurph 0:675506e540be 28 #define OUTPUT_BINARY_4CHAN (4)
biomurph 0:675506e540be 29 #define OUTPUT_BINARY_OPENEEG (6)
biomurph 0:675506e540be 30 #define OUTPUT_BINARY_OPENEEG_SYNTHETIC (7)
biomurph 0:675506e540be 31 int outputType;
biomurph 0:675506e540be 32
biomurph 0:675506e540be 33 //Design filters (This BIQUAD class requires ~6K of program space! Ouch.)
biomurph 0:675506e540be 34 //For frequency response of these filters: http://www.earlevel.com/main/2010/12/20/biquad-calculator/
biomurph 0:675506e540be 35 #include <Biquad_multiChan.h> //modified from this source code: http://www.earlevel.com/main/2012/11/26/biquad-c-source-code/
biomurph 0:675506e540be 36 #define SAMPLE_RATE_HZ (250.0) //default setting for OpenBCI
biomurph 0:675506e540be 37 #define FILTER_Q (0.5) //critically damped is 0.707 (Butterworth)
biomurph 0:675506e540be 38 #define FILTER_PEAK_GAIN_DB (0.0) //we don't want any gain in the passband
biomurph 0:675506e540be 39 #define HP_CUTOFF_HZ (0.5) //set the desired cutoff for the highpass filter
biomurph 0:675506e540be 40 Biquad_multiChan stopDC_filter(MAX_N_CHANNELS,bq_type_highpass,HP_CUTOFF_HZ / SAMPLE_RATE_HZ, FILTER_Q, FILTER_PEAK_GAIN_DB); //one for each channel because the object maintains the filter states
biomurph 0:675506e540be 41 //Biquad_multiChan stopDC_filter(MAX_N_CHANNELS,bq_type_bandpass,10.0 / SAMPLE_RATE_HZ, 6.0, FILTER_PEAK_GAIN_DB); //one for each channel because the object maintains the filter states
biomurph 0:675506e540be 42 #define NOTCH_FREQ_HZ (60.0)
biomurph 0:675506e540be 43 #define NOTCH_Q (4.0) //pretty shap notch
biomurph 0:675506e540be 44 #define NOTCH_PEAK_GAIN_DB (0.0) //doesn't matter for this filter type
biomurph 0:675506e540be 45 Biquad_multiChan notch_filter1(MAX_N_CHANNELS,bq_type_notch,NOTCH_FREQ_HZ / SAMPLE_RATE_HZ, NOTCH_Q, NOTCH_PEAK_GAIN_DB); //one for each channel because the object maintains the filter states
biomurph 0:675506e540be 46 Biquad_multiChan notch_filter2(MAX_N_CHANNELS,bq_type_notch,NOTCH_FREQ_HZ / SAMPLE_RATE_HZ, NOTCH_Q, NOTCH_PEAK_GAIN_DB); //one for each channel because the object maintains the filter states
biomurph 0:675506e540be 47 boolean useFilters = false;
biomurph 0:675506e540be 48
biomurph 0:675506e540be 49
biomurph 0:675506e540be 50
biomurph 0:675506e540be 51 DigitalOut CS (PTD0); // arduino pin 10 - chip select
biomurph 0:675506e540be 52 DigitalOut RST(PTD5); // arduino pin 9 - reset pin
biomurph 0:675506e540be 53 DigitalIn DRDY(PTA13); // arduino pin 8 - data ready pin
biomurph 0:675506e540be 54 DigitalOut myled(LED3); // debugging LED CONFLICT WITH SPI & LED1
biomurph 0:675506e540be 55
biomurph 0:675506e540be 56
biomurph 0:675506e540be 57
biomurph 0:675506e540be 58 int main() {
biomurph 0:675506e540be 59
biomurph 0:675506e540be 60 ADS1299Manager ADSManager;
biomurph 0:675506e540be 61 int OpenBCI_version = OPENBCI_V2; //assume V2
biomurph 0:675506e540be 62 ADSManager.initialize(OpenBCI_version); //must do this VERY early in the setup...preferably first
biomurph 0:675506e540be 63
biomurph 0:675506e540be 64 spi.format(8,1); // spi: 8 bit, mode 1
biomurph 0:675506e540be 65 spi.frequency(1000000); // spi sck frequency
biomurph 0:675506e540be 66
biomurph 0:675506e540be 67 pc.baud(115200);
biomurph 0:675506e540be 68 pc.printf("\nADS1299 Stream Raw Data\n");
biomurph 0:675506e540be 69 wait(2.0);
biomurph 0:675506e540be 70
biomurph 0:675506e540be 71 pc.printf("ADS1299-Arduino UNO - Stream Raw Data"); //read the string from Flash to save RAM
biomurph 0:675506e540be 72 pc.printf("Configured as OpenBCI_Version code = ");pc.printf(OpenBCI_version);
biomurph 0:675506e540be 73 Serial.flush();
biomurph 0:675506e540be 74
biomurph 0:675506e540be 75 // setup the channels as desired on the ADS1299..set gain, input type, referece (SRB1), and patient bias signal
biomurph 0:675506e540be 76 for (int chan=1; chan <= nActiveChannels; chan++) {
biomurph 0:675506e540be 77 ADSManager.activateChannel(chan, gainCode, inputType);
biomurph 0:675506e540be 78 }
biomurph 0:675506e540be 79
biomurph 0:675506e540be 80 //print state of all registers
biomurph 0:675506e540be 81 ADSManager.printAllRegisters();Serial.flush();
biomurph 0:675506e540be 82
biomurph 0:675506e540be 83 // setup hardware to allow a jumper or button to start the digitaltransfer
biomurph 0:675506e540be 84 pinMode(PIN_STARTBINARY,INPUT); digitalWrite(PIN_STARTBINARY,HIGH); //activate pullup
biomurph 0:675506e540be 85 //pinMode(PIN_STARTBINARY_OPENEEG,INPUT); digitalWrite(PIN_STARTBINARY_OPENEEG,HIGH); //activate pullup
biomurph 0:675506e540be 86
biomurph 0:675506e540be 87 // tell the controlling program that we're ready to start!
biomurph 0:675506e540be 88 pc.printf("Press '?' to query and print ADS1299 register settings again"); //read it straight from flash
biomurph 0:675506e540be 89 pc.printf("Press 1-8 to disable EEG Channels, q-i to enable (all enabled by default)");
biomurph 0:675506e540be 90 pc.printf("Press 'F' to enable filters. 'f' to disable filters (disabled by default)");
biomurph 0:675506e540be 91 pc.printf("Press 'x' (text) or 'b' (binary) to begin streaming data...");
biomurph 0:675506e540be 92
biomurph 0:675506e540be 93 boolean firstReport = true;
biomurph 0:675506e540be 94 unsigned long totalMicrosBusy = 0; //use this to count time
biomurph 0:675506e540be 95
biomurph 0:675506e540be 96 while(1) {
biomurph 0:675506e540be 97
biomurph 0:675506e540be 98 if (is_running) {
biomurph 0:675506e540be 99
biomurph 0:675506e540be 100 //is data ready?
biomurph 0:675506e540be 101 while(!(ADSManager.isDataAvailable())){ // watch the DRDY pin
biomurph 0:675506e540be 102 delayMicroseconds(100);
biomurph 0:675506e540be 103 }
biomurph 0:675506e540be 104 unsigned long start_micros = micros();
biomurph 0:675506e540be 105
biomurph 0:675506e540be 106 //get the data
biomurph 0:675506e540be 107 ADSManager.updateChannelData(); // update the channelData array
biomurph 0:675506e540be 108 sampleCounter++; // increment my sample counter
biomurph 0:675506e540be 109
biomurph 0:675506e540be 110 //Apply filers to the data
biomurph 0:675506e540be 111 if (useFilters) applyFilters();
biomurph 0:675506e540be 112
biomurph 0:675506e540be 113 //print the data
biomurph 0:675506e540be 114 //if ((sampleCounter % 1) == 0) {
biomurph 0:675506e540be 115 switch (outputType) {
biomurph 0:675506e540be 116 case OUTPUT_NOTHING:
biomurph 0:675506e540be 117 //don't output anything...the Arduino is still collecting data from the OpenBCI board...just nothing is being done with it
biomurph 0:675506e540be 118 break;
biomurph 0:675506e540be 119 case OUTPUT_BINARY:
biomurph 0:675506e540be 120 ADSManager.writeChannelDataAsBinary(8,sampleCounter); //print all channels, whether active or not
biomurph 0:675506e540be 121 break;
biomurph 0:675506e540be 122 case OUTPUT_BINARY_4CHAN:
biomurph 0:675506e540be 123 ADSManager.writeChannelDataAsBinary(4,sampleCounter); //print all channels, whether active or not
biomurph 0:675506e540be 124 break;
biomurph 0:675506e540be 125 case OUTPUT_BINARY_OPENEEG:
biomurph 0:675506e540be 126 ADSManager.writeChannelDataAsOpenEEG_P2(sampleCounter); //print all channels, whether active or not
biomurph 0:675506e540be 127 break;
biomurph 0:675506e540be 128 case OUTPUT_BINARY_OPENEEG_SYNTHETIC:
biomurph 0:675506e540be 129 ADSManager.writeChannelDataAsOpenEEG_P2(sampleCounter,true); //print all channels, whether active or not
biomurph 0:675506e540be 130 break;
biomurph 0:675506e540be 131 default:
biomurph 0:675506e540be 132 ADSManager.printChannelDataAsText(8,sampleCounter); //print all channels, whether active or not
biomurph 0:675506e540be 133 }// end of if(is_running)
biomurph 0:675506e540be 134 }// end of while(1)
biomurph 0:675506e540be 135
biomurph 0:675506e540be 136 if(pc.readable()) {
biomurph 0:675506e540be 137 serialEvent();
biomurph 0:675506e540be 138 }
biomurph 0:675506e540be 139
biomurph 0:675506e540be 140 }// end of main()
biomurph 0:675506e540be 141
biomurph 0:675506e540be 142
biomurph 0:675506e540be 143 #define ACTIVATE_SHORTED (2)
biomurph 0:675506e540be 144 #define ACTIVATE (1)
biomurph 0:675506e540be 145 #define DEACTIVATE (0)
biomurph 0:675506e540be 146 void serialEvent(){ //
biomurph 0:675506e540be 147 char inChar = pc.getc();
biomurph 0:675506e540be 148 switch (inChar)
biomurph 0:675506e540be 149 {
biomurph 0:675506e540be 150 case '1':
biomurph 0:675506e540be 151 changeChannelState_maintainRunningState(1,DEACTIVATE); break;
biomurph 0:675506e540be 152 case '2':
biomurph 0:675506e540be 153 changeChannelState_maintainRunningState(2,DEACTIVATE); break;
biomurph 0:675506e540be 154 case '3':
biomurph 0:675506e540be 155 changeChannelState_maintainRunningState(3,DEACTIVATE); break;
biomurph 0:675506e540be 156 case '4':
biomurph 0:675506e540be 157 changeChannelState_maintainRunningState(4,DEACTIVATE); break;
biomurph 0:675506e540be 158 case '5':
biomurph 0:675506e540be 159 changeChannelState_maintainRunningState(5,DEACTIVATE); break;
biomurph 0:675506e540be 160 case '6':
biomurph 0:675506e540be 161 changeChannelState_maintainRunningState(6,DEACTIVATE); break;
biomurph 0:675506e540be 162 case '7':
biomurph 0:675506e540be 163 changeChannelState_maintainRunningState(7,DEACTIVATE); break;
biomurph 0:675506e540be 164 case '8':
biomurph 0:675506e540be 165 changeChannelState_maintainRunningState(8,DEACTIVATE); break;
biomurph 0:675506e540be 166 case 'q':
biomurph 0:675506e540be 167 changeChannelState_maintainRunningState(1,ACTIVATE); break;
biomurph 0:675506e540be 168 case 'w':
biomurph 0:675506e540be 169 changeChannelState_maintainRunningState(2,ACTIVATE); break;
biomurph 0:675506e540be 170 case 'e':
biomurph 0:675506e540be 171 changeChannelState_maintainRunningState(3,ACTIVATE); break;
biomurph 0:675506e540be 172 case 'r':
biomurph 0:675506e540be 173 changeChannelState_maintainRunningState(4,ACTIVATE); break;
biomurph 0:675506e540be 174 case 't':
biomurph 0:675506e540be 175 changeChannelState_maintainRunningState(5,ACTIVATE); break;
biomurph 0:675506e540be 176 case 'y':
biomurph 0:675506e540be 177 changeChannelState_maintainRunningState(6,ACTIVATE); break;
biomurph 0:675506e540be 178 case 'u':
biomurph 0:675506e540be 179 changeChannelState_maintainRunningState(7,ACTIVATE); break;
biomurph 0:675506e540be 180 case 'i':
biomurph 0:675506e540be 181 changeChannelState_maintainRunningState(8,ACTIVATE); break;
biomurph 0:675506e540be 182 case '0':
biomurph 0:675506e540be 183 activateAllChannelsToTestCondition(ADSINPUT_SHORTED,ADSTESTSIG_NOCHANGE,ADSTESTSIG_NOCHANGE); break;
biomurph 0:675506e540be 184 case '-':
biomurph 0:675506e540be 185 activateAllChannelsToTestCondition(ADSINPUT_TESTSIG,ADSTESTSIG_AMP_1X,ADSTESTSIG_PULSE_SLOW); break;
biomurph 0:675506e540be 186 case '+':
biomurph 0:675506e540be 187 activateAllChannelsToTestCondition(ADSINPUT_TESTSIG,ADSTESTSIG_AMP_1X,ADSTESTSIG_PULSE_FAST); break;
biomurph 0:675506e540be 188 case '=':
biomurph 0:675506e540be 189 //repeat the line above...just for human convenience
biomurph 0:675506e540be 190 activateAllChannelsToTestCondition(ADSINPUT_TESTSIG,ADSTESTSIG_AMP_1X,ADSTESTSIG_PULSE_FAST); break;
biomurph 0:675506e540be 191 case 'p':
biomurph 0:675506e540be 192 activateAllChannelsToTestCondition(ADSINPUT_TESTSIG,ADSTESTSIG_AMP_2X,ADSTESTSIG_DCSIG); break;
biomurph 0:675506e540be 193 case '[':
biomurph 0:675506e540be 194 activateAllChannelsToTestCondition(ADSINPUT_TESTSIG,ADSTESTSIG_AMP_2X,ADSTESTSIG_PULSE_SLOW); break;
biomurph 0:675506e540be 195 case ']':
biomurph 0:675506e540be 196 activateAllChannelsToTestCondition(ADSINPUT_TESTSIG,ADSTESTSIG_AMP_2X,ADSTESTSIG_PULSE_FAST); break;
biomurph 0:675506e540be 197 case 'n':
biomurph 0:675506e540be 198 toggleRunState(OUTPUT_NOTHING);
biomurph 0:675506e540be 199 startBecauseOfSerial = is_running;
biomurph 0:675506e540be 200 if (is_running) Serial.println(F("FRDM: Starting, but not outputing to PC..."));
biomurph 0:675506e540be 201 break;
biomurph 0:675506e540be 202 case 'b':
biomurph 0:675506e540be 203 toggleRunState(OUTPUT_BINARY);
biomurph 0:675506e540be 204 startBecauseOfSerial = is_running;
biomurph 0:675506e540be 205 if (is_running) Serial.println(F("FRDM: Starting binary..."));
biomurph 0:675506e540be 206 break;
biomurph 0:675506e540be 207 case 'v':
biomurph 0:675506e540be 208 toggleRunState(OUTPUT_BINARY_4CHAN);
biomurph 0:675506e540be 209 startBecauseOfSerial = is_running;
biomurph 0:675506e540be 210 if (is_running) Serial.println(F("FRDM: Starting binary 4-chan..."));
biomurph 0:675506e540be 211 break;
biomurph 0:675506e540be 212 case 's':
biomurph 0:675506e540be 213 stopRunning();
biomurph 0:675506e540be 214 startBecauseOfSerial = is_running;
biomurph 0:675506e540be 215 break;
biomurph 0:675506e540be 216 case 'x':
biomurph 0:675506e540be 217 toggleRunState(OUTPUT_TEXT);
biomurph 0:675506e540be 218 startBecauseOfSerial = is_running;
biomurph 0:675506e540be 219 if (is_running) Serial.println(F("FRDM: Starting text..."));
biomurph 0:675506e540be 220 break;
biomurph 0:675506e540be 221 case 'f':
biomurph 0:675506e540be 222 useFilters = false;
biomurph 0:675506e540be 223 Serial.println(F("FRDM: disabling filters"));
biomurph 0:675506e540be 224 break;
biomurph 0:675506e540be 225 case 'F':
biomurph 0:675506e540be 226 useFilters = true;
biomurph 0:675506e540be 227 Serial.println(F("FRDM: enabaling filters"));
biomurph 0:675506e540be 228 break;
biomurph 0:675506e540be 229 case '?':
biomurph 0:675506e540be 230 //print state of all registers
biomurph 0:675506e540be 231 ADSManager.printAllRegisters();
biomurph 0:675506e540be 232 break;
biomurph 0:675506e540be 233 default:
biomurph 0:675506e540be 234 break;
biomurph 0:675506e540be 235 }
biomurph 0:675506e540be 236 }
biomurph 0:675506e540be 237 }
biomurph 0:675506e540be 238
biomurph 0:675506e540be 239 boolean toggleRunState(int OUT_TYPE)
biomurph 0:675506e540be 240 {
biomurph 0:675506e540be 241 if (is_running) {
biomurph 0:675506e540be 242 return stopRunning();
biomurph 0:675506e540be 243 } else {
biomurph 0:675506e540be 244 return startRunning(OUT_TYPE);
biomurph 0:675506e540be 245 }
biomurph 0:675506e540be 246 }
biomurph 0:675506e540be 247
biomurph 0:675506e540be 248 boolean stopRunning(void) {
biomurph 0:675506e540be 249 ADSManager.stop(); // stop the data acquisition
biomurph 0:675506e540be 250 is_running = false;
biomurph 0:675506e540be 251 return is_running;
biomurph 0:675506e540be 252 }
biomurph 0:675506e540be 253
biomurph 0:675506e540be 254 boolean startRunning(int OUT_TYPE) {
biomurph 0:675506e540be 255 outputType = OUT_TYPE;
biomurph 0:675506e540be 256 ADSManager.start(); //start the data acquisition
biomurph 0:675506e540be 257 is_running = true;
biomurph 0:675506e540be 258 return is_running;
biomurph 0:675506e540be 259 }
biomurph 0:675506e540be 260
biomurph 0:675506e540be 261 int changeChannelState_maintainRunningState(int chan, int start)
biomurph 0:675506e540be 262 {
biomurph 0:675506e540be 263 boolean is_running_when_called = is_running;
biomurph 0:675506e540be 264 int cur_outputType = outputType;
biomurph 0:675506e540be 265
biomurph 0:675506e540be 266 //must stop running to change channel settings
biomurph 0:675506e540be 267 stopRunning();
biomurph 0:675506e540be 268 if (start == true) {
biomurph 0:675506e540be 269 Serial.print("Activating channel ");
biomurph 0:675506e540be 270 Serial.println(chan);
biomurph 0:675506e540be 271 ADSManager.activateChannel(chan,gainCode,inputType);
biomurph 0:675506e540be 272 } else {
biomurph 0:675506e540be 273 Serial.print("Deactivating channel ");
biomurph 0:675506e540be 274 Serial.println(chan);
biomurph 0:675506e540be 275 ADSManager.deactivateChannel(chan);
biomurph 0:675506e540be 276 }
biomurph 0:675506e540be 277
biomurph 0:675506e540be 278 //restart, if it was running before
biomurph 0:675506e540be 279 if (is_running_when_called == true) {
biomurph 0:675506e540be 280 startRunning(cur_outputType);
biomurph 0:675506e540be 281 }
biomurph 0:675506e540be 282 }
biomurph 0:675506e540be 283
biomurph 0:675506e540be 284 int activateAllChannelsToTestCondition(int testInputCode, byte amplitudeCode, byte freqCode)
biomurph 0:675506e540be 285 {
biomurph 0:675506e540be 286 boolean is_running_when_called = is_running;
biomurph 0:675506e540be 287 int cur_outputType = outputType;
biomurph 0:675506e540be 288
biomurph 0:675506e540be 289 //set the test signal to the desired state
biomurph 0:675506e540be 290 ADSManager.configureInternalTestSignal(amplitudeCode,freqCode);
biomurph 0:675506e540be 291
biomurph 0:675506e540be 292 //must stop running to change channel settings
biomurph 0:675506e540be 293 stopRunning();
biomurph 0:675506e540be 294
biomurph 0:675506e540be 295 //loop over all channels to change their state
biomurph 0:675506e540be 296 for (int Ichan=1; Ichan <= 8; Ichan++) {
biomurph 0:675506e540be 297 ADSManager.activateChannel(Ichan,gainCode,testInputCode); //Ichan must be [1 8]...it does not start counting from zero
biomurph 0:675506e540be 298 }
biomurph 0:675506e540be 299
biomurph 0:675506e540be 300 //restart, if it was running before
biomurph 0:675506e540be 301 if (is_running_when_called == true) {
biomurph 0:675506e540be 302 startRunning(cur_outputType);
biomurph 0:675506e540be 303 }
biomurph 0:675506e540be 304 }
biomurph 0:675506e540be 305
biomurph 0:675506e540be 306 long int runningAve[MAX_N_CHANNELS];
biomurph 0:675506e540be 307 int applyFilters(void) {
biomurph 0:675506e540be 308 //scale factor for these coefficients was 32768 = 2^15
biomurph 0:675506e540be 309 const static long int a0 = 32360L; //16 bit shift?
biomurph 0:675506e540be 310 const static long int a1 = -2L*a0;
biomurph 0:675506e540be 311 const static long int a2 = a0;
biomurph 0:675506e540be 312 const static long int b1 = -64718L; //this is a shift of 17 bits!
biomurph 0:675506e540be 313 const static long int b2 = 31955L;
biomurph 0:675506e540be 314 static long int z1[MAX_N_CHANNELS], z2[MAX_N_CHANNELS];
biomurph 0:675506e540be 315 long int val_int, val_in_down9, val_out, val_out_down9;
biomurph 0:675506e540be 316 float val;
biomurph 0:675506e540be 317 for (int Ichan=0; Ichan < MAX_N_CHANNELS; Ichan++) {
biomurph 0:675506e540be 318 switch (1) {
biomurph 0:675506e540be 319 case 1:
biomurph 0:675506e540be 320 //use BiQuad
biomurph 0:675506e540be 321 val = (float) ADSManager.channelData[Ichan]; //get the stored value for this sample
biomurph 0:675506e540be 322 val = stopDC_filter.process(val,Ichan); //apply DC-blocking filter
biomurph 0:675506e540be 323 break;
biomurph 0:675506e540be 324 case 2:
biomurph 0:675506e540be 325 //do fixed point, 1st order running ave
biomurph 0:675506e540be 326 val_int = ADSManager.channelData[Ichan]; //get the stored value for this sample
biomurph 0:675506e540be 327 //runningAve[Ichan]=( ((512-1)*(runningAve[Ichan]>>2)) + (val_int>>2) )>>7; // fs/0.5Hz = ~512 points..9 bits
biomurph 0:675506e540be 328 //runningAve[Ichan]=( ((256-1)*(runningAve[Ichan]>>2)) + (val_int>>2) )>>6; // fs/1.0Hz = ~256 points...8 bits
biomurph 0:675506e540be 329 runningAve[Ichan]=( ((128-1)*(runningAve[Ichan]>>1)) + (val_int>>1) )>>6; // fs/2.0Hz = ~128 points...7 bits
biomurph 0:675506e540be 330 val = (float)(val_int - runningAve[Ichan]); //remove the DC
biomurph 0:675506e540be 331 break;
biomurph 0:675506e540be 332 // case 3:
biomurph 0:675506e540be 333 // val_in_down9 = ADSManager.channelData[Ichan] >> 9; //get the stored value for this sample...bring 24-bit value down to 16-bit
biomurph 0:675506e540be 334 // val_out = (val_in_down9 * a0 + (z1[Ichan]>>9)) >> (16-9); //8bits were already removed...results in 24-bit value
biomurph 0:675506e540be 335 // val_out_down9 = val_out >> 9; //remove eight bits to go from 24-bit down to 16 bit
biomurph 0:675506e540be 336 // z1[Ichan] = (val_in_down9 * a1 + (z2[Ichan] >> 9) - b1 * val_out_down9 ) >> (16-9); //8-bits were pre-removed..end in 24 bit number
biomurph 0:675506e540be 337 // z2[Ichan] = (val_in_down9 * a2 - b2 * val_out_down9) >> (16-9); //8-bits were pre-removed...end in 24-bit number
biomurph 0:675506e540be 338 // val = (float)val_out;
biomurph 0:675506e540be 339 // break;
biomurph 0:675506e540be 340 }
biomurph 0:675506e540be 341 val = notch_filter1.process(val,Ichan); //apply 60Hz notch filter
biomurph 0:675506e540be 342 val = notch_filter2.process(val,Ichan); //apply it again
biomurph 0:675506e540be 343 ADSManager.channelData[Ichan] = (long) val; //save the value back into the main data-holding object
biomurph 0:675506e540be 344 }
biomurph 0:675506e540be 345 return 0;
biomurph 0:675506e540be 346 }
biomurph 0:675506e540be 347
biomurph 0:675506e540be 348
biomurph 0:675506e540be 349
biomurph 0:675506e540be 350
biomurph 0:675506e540be 351