Use into setup() a float value from a method that finds it

If you run this code, what is your result??
How many peaks are in the end in the console??

pls study above picture, you see the graph and the result.
at 1.23 i see and get result 5 peaks

if you replace line 107 with:

THRESH=((average_intens + maxIntensity)/2);

will your result be 7 peaks at the end???

NO
sorry also still 5

please load the data below:

data

Time,Intensity
10,0.2
20,0.4
30,0.4
40,0.3
50,0.6
60,0.6
70,0.9
80,0.7
90,0.5
100,0.5
110,0.3
120,0.1
130,0.3
140,0.5
150,0.6
160,0.4
170,0.3
180,0.1
190,0.1
200,0.2
210,0.2
220,0.3
230,0.1
240,0.2
250,0.5
260,0.6
270,0.6
280,0.7
290,1.2
300,1.3
310,1.1
320,0.9
330,0.6
340,0.6
350,0.5
360,0.3
370,0.3
380,0.1
390,0.1
400,0.2
410,0.4
420,0.4
430,0.6
440,0.6
450,0.7
460,0.7
470,0.7
480,0.9
490,1
500,0.8
510,0.5
520,0.5
530,0.2
540,0.2
550,0.2
560,0.1
570,0.1
580,0.1
590,0.2
600,0.2
610,0.2
620,0.6
630,0.6
640,0.5
650,0.5
660,0.5
670,0.8
680,0.8
690,0.9
700,0.9
710,0.9
720,0.9
730,1.2
740,1.6
750,1.3
760,1.1
770,0.8
780,0.8
790,0.8
800,0.5
810,0.3
820,0.3
830,0.1
840,0.1
850,0.2
860,0.2
870,0.2
880,0.5
890,0.5
900,0.7
910,0.8
920,0.8
930,0.9
940,0.9
950,0.9
960,1.2
970,1.4
980,1.1
990,1
1000,0.8
1010,0.8
1020,0.7
1030,0.5
1040,0.5
1050,0.2
1060,0.2
1070,0.1
1080,0.1
1090,0.3
1100,0.5
1110,0.6
1120,0.9
1130,1.2
1140,1.8
1150,1.6
1160,1.5
1170,1.2
1180,1.1
1190,0.9
1200,0.9
1210,0.7
1220,0.5
1230,0.3
1240,0.1
1250,0.3
1260,0.5
1270,0.6
1280,0.4
1290,0.3
1300,0.1
1310,0.1
1320,0.2
1330,0.2
1340,0.3
1350,0.1
1360,0.2
1370,0.5
1380,0.6
1390,0.6
1400,0.7
1410,1.2
1420,1.3
1430,1.1
1440,0.9
1450,0.6
1460,0.6
1470,0.4
1480,0.6
1490,0.6
1500,0.5
1510,0.5
1520,0.3
1530,0.2
1540,0.2
1550,0.3
1560,0.5
1570,0.5
1580,0.2
1590,0.2
1600,0.2
1610,0.1
1620,0.1
1630,0.1
1640,0.2
1650,0.2
1660,0.2
1670,0.6
1680,0.6
1690,0.7
1700,1.2
1710,1.3
1720,1.1
1730,0.9
1740,0.6
1750,0.6
1760,0.5
1770,0.3
1780,0.3
1790,0.1
1800,0.1
1810,0.2
1820,0.1
1830,0.1
1840,0.2
1850,0.2
1860,0.2
1870,0.5
1880,0.5
1890,0.7
1900,0.8
1910,0.9
1920,1.2
1930,1.4
1940,1.1
1950,1
1960,0.7
1970,0.7
1980,0.3

Also could you please explain me the line:

if ( intensity > maxIntensity ) maxIntensity = intensity;

and how do you find max values???

you have the code,
i set max to 0
and in loop i remember it if a bigger value comes,
that is a very usual find max way

so you give us wrong data for the CSV

anyhow can you pls. use the CODE

</>

thing for code and your long data lists

I am sorry for the wrong data…
Thank you for your time and for your help…
One more question, there isn’t a function to find max value ?
As long as we declare the line

if ( intensity > maxIntensity ) maxIntensity = intensity;

and the maxIntensity is a float variable, are enough to find the max value of 200 values in a table???

well, instead this line
what is inside your loop over all intensity values
can use the math max()
https://processing.org/reference/max_.html

but outside? you think of a table.max() ?
i don’t know, its possible,
your trick reverse sort get(0) worked also
but fired back as long you did it on the original table.

I was just thinking about a table.max() but it’s ok if your way is right…
I am a new one in processing and i try to understand…
Is your way accepted???

use max() if it makes you happy

1 Like

Thank you so much…!!! I am sorry if i upset or tire you :thinking::blush::wink:

@netphysicist I see you are implementing a peak finding algorithm. Can you elaborate how your picking the peaks in your data? Are you using THRESH=((average_intens + maxIntensity)/2); to select the peaks of interest?
If you are going to apply this algorithm to other data sets, there could be a better strategies to get those peaks. However, if this does the job, then you can disregard this message.

Kf

import java.util.Map;
 
final int ROWZERO=0;
final int COL_TIME=0;
final int COL_INTENSITY=1;
float THRESH;
 
Table table;
ArrayList<PVector> timePeakPair;
 
void setup() {
 
  table=loadTable("data2.csv", "header");
  timePeakPair=new ArrayList<PVector>();
 
  table.setColumnType("Time", Table.INT);
  table.setColumnType("Intensity", Table.FLOAT);
 
 
  boolean aboveThresh=false;
  int ctr=0;
  float sum_xfx=0;
  float sum_x=0;
  int rowBeginSection=-1;
 
 findTHRESHOLD();
  for (int i=0; i<table.getRowCount(); i++) {
 
    int time = table.getInt(i, COL_TIME);
    float intensity = table.getFloat(i, COL_INTENSITY);
 
    if (intensity>=THRESH) {  //Above or equal
 
      aboveThresh=true;
 
      sum_xfx+=time*intensity;
      sum_x+=time;  //Store the first time entry
      ctr+=1;
      if (rowBeginSection<0) rowBeginSection=i;
 
      //println(ctr,sum_x,sum_xfx);
    } else {
 
      //If finish a section above threshold, calculate peak follow by reseting fields
      if (aboveThresh==true) {
 
        //Calculating...        
        float intPeak=sum_xfx/sum_x;  
        float timePeak=getTimeEntry(rowBeginSection, intPeak, ctr); //sum_x/ctr;
        PVector pt = new PVector(timePeak, intPeak);
        timePeakPair.add(pt);        
        //println("CALC: ", ctr, timePeak, intPeak);
 
        //Reseting...
        sum_xfx=0;
        sum_x=0;
        ctr=0;
        rowBeginSection=-1;
        aboveThresh=false;
      }
    }
  }
 
  //REPORT:
 
  println("Number of peaks = "+timePeakPair.size());
 
}
 float getTimeEntry(int rowStart, float peakValue, int ctr) {
 
  //Trivial case
  if (ctr==1)
    return table.getInt(rowStart, COL_TIME);
 
  float ctime=-1;  
  for (int i=rowStart; i<rowStart+ctr+1; i++) { 
    int time = table.getInt(i, COL_TIME);
    float intensity = table.getFloat(i, COL_INTENSITY);
 
 
    if (intensity>peakValue) {      
      int idx=i-1;
      ctime=table.getInt(idx, COL_TIME);    
      break;
    }
  }
 
  return ctime;
}

void findTHRESHOLD(){
float sum_Intensity=0;
float Num = 0;
   for (int i=0; i<table.getRowCount(); i++) {
    float intensity = table.getFloat(i, COL_INTENSITY);
    sum_Intensity+=intensity;
    Num ++;
  }
    table.sortReverse("Intensity");
  float maxIntensity= table.getFloat(0, "Intensity");
  float average_intens= (sum_Intensity/Num);
  THRESH=((average_intens + maxIntensity)/2);
  println(THRESH);

}

! warning
this is again the same code from the beginning what never worked

with the temp fix from @jb4x
it runs,
with the drawing utility i provided later possibly you see clearer what
data OP try to analyze ( after a bad trick in findTHRESHOLD() )

the fixed findTHRESHOLD() i provided already

still your input is welcome!

Yeah this is right, yours:

//import java.util.Map;

//final int ROWZERO=0;
//final int COL_TIME=0;
//final int COL_INTENSITY=1;
float THRESH;

Table table;
ArrayList<PVector> timePeakPair;
boolean dprint = true;

void setup() {
  size(400, 500);
  table=loadTable("data2.csv", "header");
  timePeakPair=new ArrayList<PVector>();

  table.setColumnType("Time", Table.INT);
  table.setColumnType("Intensity", Table.FLOAT);


  boolean aboveThresh=false;
  int ctr=0;
  float sum_xfx=0;
  float time, intensity, sum_x=0;
  int rowBeginSection=-1;

  findTHRESHOLD();

  for (int i=0; i<table.getRowCount(); i++) {
    time = table.getInt(i, "Time");  
    intensity = table.getFloat(i, "Intensity");
//    if (dprint) println("time: "+time+" intensity: "+intensity);
    if (intensity>=THRESH) {  //Above or equal

      aboveThresh=true;

      sum_xfx+=time*intensity;
      sum_x+=time;  //Store the first time entry
      ctr+=1;
      if (rowBeginSection<0) rowBeginSection=i;

      //println(ctr,sum_x,sum_xfx);
    } else {

      //If finish a section above threshold, calculate peak follow by reseting fields
      if (aboveThresh==true) {

        //Calculating...        
        float intPeak=sum_xfx/sum_x;  
        float timePeak=getTimeEntry(rowBeginSection, intPeak, ctr); //sum_x/ctr;
        PVector pt = new PVector(timePeak, intPeak);
        timePeakPair.add(pt);        
        //println("CALC: ", ctr, timePeak, intPeak);

        //Reseting...
        sum_xfx=0;
        sum_x=0;
        ctr=0;
        rowBeginSection=-1;
        aboveThresh=false;
      }
    }
  }

  //REPORT:

  println("Number of peaks = "+timePeakPair.size());
}



float getTimeEntry(int rowStart, float peakValue, int ctr) {

  //Trivial case
  if (ctr==1)
    return table.getInt(rowStart, "Time");

  float ctime=-1;  
  for (int i=rowStart; i<rowStart+ctr+1; i++) { 
    int time = table.getInt(i, "Time");
    float intensity = table.getFloat(i, "Intensity");


    if (intensity>peakValue) {      
      int idx=i-1;
      ctime=table.getInt(idx, "Time");    
      break;
    }
  }

  return ctime;
}

void findTHRESHOLD() {
  float sum_Intensity=0;
  float intensity, maxIntensity=0, average_intens;
  int trow = table.getRowCount();
  for (int i=0; i<trow; i++) {
    intensity = table.getFloat(i, "Intensity");
    if ( intensity > maxIntensity ) maxIntensity = intensity;
    sum_Intensity += intensity;
  }
  average_intens= (sum_Intensity/(trow-1));
//  THRESH=(average_intens + maxIntensity/2);
  THRESH=((average_intens + maxIntensity)/1.9);
  println("findTHRESHOLD_average_intens "+average_intens+" maxIntensity "+maxIntensity+" THRESH: "+THRESH);
}


void draw() {
  background(200,200,0);
  draw_graph();
}

void draw_graph() {
  float intensity, x0= 20, y0 = 450, w= 2, zoom = 200;
  for (int i=0; i<table.getRowCount(); i++) {
    intensity = table.getFloat(i, "Intensity");
    fill(0,200,0); noStroke();
    rect(x0+i*w,y0,w,-zoom*intensity);
  }
  stroke(200,0,0);
  line(x0,y0-zoom*THRESH,x0+w*table.getRowCount(),y0-zoom*THRESH);
}

more or less, that was the version using the too short dataset
and a intermediate test of your change:

//  THRESH=(average_intens + maxIntensity/2);
  THRESH=((average_intens + maxIntensity)/1.9);

@netphysicist
now a look at your SORTED data from CSV file
( the picture above )
not only might help to understand the sort problem i try to tell you about

it is also very interesting about a other point:
your measuring signal ( called Intensity ) and unit [mV]
shows these steps
( you know from your CSV already )
you have data from 0 … 1.8 come with one decimal only
that is a resolution of 18 steps
if the problem is not in

  • the initial signal amplifying
  • the cheapest AD converter ever
  • a wrong calculation from AD value to float

it must be in the data transport ( sending one decimal only )
so i just recommend sending raw data
like from arduino INT 0 … 1023
or do a ranging to 0 … 100.0x %
what again to be LOSSLESS would have be to send with 2 decimals

does this all tell you anything?

No, i did not understand anything…I download my data from Internet so there aren’t converted or filtered or anything else…

pls. LINK,
where you got the data ?csv?
and where come the code from?

I don’t remember from where i have downloaded the data maybe in physionet but the code i tried to develop it on my own with your and forum’s help…