0

i am trying to process a signal from bidmc(ppg) database using arduino uno. i am taking the data as an array and only consider 250 of the samples (for testing the code). then i preprocess the signal by mean subtraction, normalization of the signal. then i pass it to a two pole digital resonator and filter it. then using the dft function i try to convert it to frequency spectrum and find the maximum peak of that spectrum and multiply it with 60 to get the respiratory rate.

the code is not giving any output if i print it [Serial.print(resp_rate)]. what mistake did i do in this code?

this is my code. i have given comments in the areas of output i got.

#include <SPI.h>
#include<SD.h>
#define PI 3.1415926535897932384626433832795
/*https://archive.physionet.org/cgi-bin/atm/ATM */

void dft(const double inrealn[],double outreal[],double outimg[],size_t n);

double inreal[251]={-9577,
-9385,
-9257,
-9065,
-8936,
-8808,
-8744,
-8616,
-8616,
-8616,
-8616,
-8680,
-8744,
-8808,
-9065,
-9257,
-9641,
-10025,
-10410,
-10858,
-11307,
-11691,
-12332,
-12908,
-13613,
-14318,
-15150,
-15983,
-16880,
-17777,
-18481,
-19250,
-19891,
-20467,
-21108,
-21685,
-22197,
-22709,
-23158,
-23542,
-23799,
-24055,
-24247,
-24503,
-24631,
-24823,
-24952,
-25144,
-25272,
-25400,
-25528,
-25656,
-25784,
-25848,
-26105,
-26297,
-26553,
-26873,
-27130,
-27386,
-27642,
-27834,
-28155,
-28475,
-28795,
-29180,
-29564,
-29884,
-30269,
-30589,
-30909,
-31165,
-31294,
-31486,
-31486,
-31550,
-31486,
-31358,
-31230,
-31037,
-30781,
-30461,
-30012,
-29500,
-28795,
-28155,
-27386,
-26553,
-25720,
-24823,
-23991,
-23158,
-22325,
-21556,
-20660,
-19827,
-18930,
-17969,
-17200,
-16367,
-15599,
-14830,
-14125,
-13421,
-12716,
-12011,
-11243,
-10474,
-9705,
-8936,
-8232,
-7527,
-6951,
-6310,
-5605,
-4901,
-4068,
-3299,
-2530,
-1762,
-1057,
-288,
545,
1441,
2338,
3299,
4388,
5413,
6502,
7655,
8680,
9641,
10602,
11499,
12460,
13357,
14318,
15278,
16175,
17072,
17905,
18738,
19571,
20467,
21364,
22261,
23222,
24247,
25208,
26233,
27194,
28091,
28923,
29820,
30653,
31550,
32190,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32767,
32062,
31358,
30076,
28795,
27450,
26105,
24823,
23542,
22325,
21172,
19891,
18674,
17328,
16047,
14830,
13613,
12524,
11371,
10282,
9129,
8104,
7015,
6054,
5093,
4260,
3427,
2787,
2146,
1634,
1121,
673,
288,
-32,
-288,
-545,
-801,
-993,
-1185,
-1377,
-1505,
-1634,
-1762,
-1890,
-1954,
-2082

};

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // preprocessing 
  //mean 
  int n=250;
  
  float sum1=0;
  for(int j=0;j<n;j++){ 
    sum1=sum1+inreal[j];
    
  }
  //Serial.println(sum1);
  
  //find mean
  float mean=sum1/n;
  //Serial.println(mean);
  
  for(int i=0;i<n;i++)
  {
    inreal[i]=inreal[i]-mean;
  }
  //for(int i=0;i<n;i++)
  //{
    // Serial.println( inreal[i]);
  //}
  
  //find max value
  int maxVal = inreal[0];
  
  for (int i = 0; i < n; i++) {
      if (inreal[i] > maxVal) {
         maxVal = inreal[i];
      }
      else{
        
      }
   }
   //Serial.println(maxVal);  //there is some garbage value coming as answer here so note
   
   //normalise the signal
   for(int j=0;j<n;j++){
     inreal[j]=inreal[j]/maxVal;
     //Serial.println(inreal[j]);  //it stops with just few statements 
   }

   //this is for coefficient calculation for resonator
   float a1=-2*0.997*cos(0.3*2*PI);
   float a2=0.997*0.997;
   float b=(1-0.997)*sqrt(1+(0.997*0.997)-2*0.997*cos(2*0.3));
   
   //Serial.print("a1: ");
   //Serial.println(a1); ///this part over here does not work answer is just 0
   
   //Serial.print("a2: ");
   //Serial.println(a2);
   
   //Serial.print("b: ");
   //Serial.println(b);

  //resonator
  double inrealn[250]={};
  
  for(int j=2;j<n;j++){
    inrealn[j]=-a1*inrealn[j-1]-a2*inrealn[j-2]+b*inreal[j];
  }
  
  //dft
  //double inmg[250]={};
  double outreal[250]={};
  double outimg[250]={};
  
  dft(inrealn,outreal,outimg,n);

  //find maximum peak in spectrum
  float fmaxi = outreal[0];
  
  for (int i = 0; i < n; i++) {
    fmaxi = max(outreal[i],fmaxi);
    //Serial.print(outimg[i]);
    //Serial.print(",");
    //Serial.print(outreal[i]);
    //Serial.println();
    //Serial.println(fmaxi); //there is no output from here onwards
    
    delay(10);
  }
  
  //find breathing rate
  float resp_rate=fmaxi*60; //output
  //Serial.println(resp_rate); //there is no output in serial monitor
}

//for dft
void dft(const double inrealn[],double outreal[],double outimg[],size_t n){//double inmg is removed ass it is asumed to be 0
  
    for (int k=0;k<n;k++){
      double sumreal=0;
      double sumimg=0;
      for (int t=0;t<n;t++){
          double angles=2*PI*t*k/n;
          sumreal+=inrealn[t]*cos(angles)+0*sin(angles);
          sumimg+=-inrealn[t]*sin(angles)+0*cos(angles);
      }
      
      outreal[k]=sumreal;
      outimg[k]=sumimg;
    }
}
rks
  • 1
  • 2

0 Answers0