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

**URL:** https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343
**Category:** Coding Questions
**Created:** [December 5, 2018, 7:24pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343 "2018-12-05T19:24:07Z")
**Posts on this page:** 20
**Page:** 2

<div class="post-metadata">

### Author: ![netphysicist](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@netphysicist](https://discourse.processing.org/u/netphysicist)
#### Post date: [December 10, 2018, 2:27pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/21 "2018-12-10T14:27:00Z")

</div>

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

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [December 10, 2018, 2:30pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/22 "2018-12-10T14:30:36Z")

</div>

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

---

<div class="post-metadata">

### Author: ![netphysicist](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@netphysicist](https://discourse.processing.org/u/netphysicist)
#### Post date: [December 10, 2018, 2:37pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/23 "2018-12-10T14:37:12Z")

</div>

> [@kll](#):
>
> 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); println("findTHRESHOLD\_average\_intens “+average\_intens+” maxIntensity “+maxIntensity+” THRESH: "+THRESH); }

if you replace line 107 with:

> THRESH=((average\_intens + maxIntensity)/2);

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

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [December 10, 2018, 2:40pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/24 "2018-12-10T14:40:09Z")

</div>

NO  
sorry also still 5

---

<div class="post-metadata">

### Author: ![netphysicist](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@netphysicist](https://discourse.processing.org/u/netphysicist)
#### Post date: [December 10, 2018, 3:11pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/25 "2018-12-10T15:11:58Z")

</div>

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

---

<div class="post-metadata">

### Author: ![netphysicist](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@netphysicist](https://discourse.processing.org/u/netphysicist)
#### Post date: [December 10, 2018, 3:14pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/26 "2018-12-10T15:14:19Z")

</div>

Also could you please explain me the line:

> if ( intensity \> maxIntensity ) maxIntensity = intensity;

and how do you find max values???

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [December 10, 2018, 3:22pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/27 "2018-12-10T15:22:50Z")

</div>

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

 ![snap-046](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/575d774b56d57474930390c021e557bf530fcdf3.png)

so you give us wrong data for the CSV

anyhow can you pls. use the CODE

```auto
</>

```

thing for code and your long data lists

---

<div class="post-metadata">

### Author: ![netphysicist](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@netphysicist](https://discourse.processing.org/u/netphysicist)
#### Post date: [December 10, 2018, 3:43pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/28 "2018-12-10T15:43:35Z")

</div>

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

```auto
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???

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [December 10, 2018, 3:51pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/29 "2018-12-10T15:51:47Z")

</div>

well, instead this line  
what is inside your loop over all intensity values  
can use the math max()  
[https://processing.org/reference/max\_.html](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.

---

<div class="post-metadata">

### Author: ![netphysicist](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@netphysicist](https://discourse.processing.org/u/netphysicist)
#### Post date: [December 10, 2018, 4:05pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/30 "2018-12-10T16:05:18Z")

</div>

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???

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [December 10, 2018, 4:08pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/31 "2018-12-10T16:08:16Z")

</div>

> [@netphysicist](#):
>
> Is your way accepted???

use max() if it makes you happy

---

<div class="post-metadata">

### Author: ![netphysicist](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@netphysicist](https://discourse.processing.org/u/netphysicist)
#### Post date: [December 10, 2018, 4:10pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/32 "2018-12-10T16:10:25Z")

</div>

Thank you so much…!!! I am sorry if i upset or tire you 🤔😊😉

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [December 11, 2018, 6:27am UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/33 "2018-12-11T06:27:41Z")

</div>

@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

---

<div class="post-metadata">

### Author: ![netphysicist](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@netphysicist](https://discourse.processing.org/u/netphysicist)
#### Post date: [December 11, 2018, 8:55am UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/35 "2018-12-11T08:55:03Z")

</div>

```auto
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);

}

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [December 11, 2018, 9:31am UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/36 "2018-12-11T09:31:42Z")

</div>

! 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() )

 ![snap-047](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/5b8d9b0ded29f24dbead5b11d0f49faa55f956af.png)

the fixed findTHRESHOLD() i provided already

> [@kfrajer](#):
>
> there could be a better strategies to get those peaks

still your input is welcome!

---

<div class="post-metadata">

### Author: ![netphysicist](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@netphysicist](https://discourse.processing.org/u/netphysicist)
#### Post date: [December 11, 2018, 9:34am UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/37 "2018-12-11T09:34:40Z")

</div>

Yeah this is right, yours:

```auto
//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);
}

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [December 11, 2018, 9:52am UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/38 "2018-12-11T09:52:30Z")

</div>

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

```auto
// 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?

---

<div class="post-metadata">

### Author: ![netphysicist](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@netphysicist](https://discourse.processing.org/u/netphysicist)
#### Post date: [December 11, 2018, 10:51am UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/39 "2018-12-11T10:51:06Z")

</div>

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

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [December 11, 2018, 10:53am UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/40 "2018-12-11T10:53:15Z")

</div>

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

---

<div class="post-metadata">

### Author: ![netphysicist](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@netphysicist](https://discourse.processing.org/u/netphysicist)
#### Post date: [December 11, 2018, 11:50am UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/41 "2018-12-11T11:50:07Z")

</div>

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…

[Previous page](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343.md?page=1)

[Next page](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343.md?page=3)
