# 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:** 1

<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 5, 2018, 7:24pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/1 "2018-12-05T19:24:07Z")

</div>

Hello,  
i have made a function `void findvalue()` which calculates with maths a float value named intensity and then i call this function into the void setup…My problem is that when i use the value intensity inside the void setup() it returns me zero…Any idea what am i doing wrong??

thanks

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [December 5, 2018, 7:30pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/2 "2018-12-05T19:30:10Z")

</div>

Hi,

You need to post your code so we can help you.

Just looking at your function though, something is off. You declare a return type of void when you actually want to return a float.

Or if you want to not return a float, you need to declare your variable in the global scope outside of all functions.

---

<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 5, 2018, 9:32pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/3 "2018-12-05T21:32:58Z")

</div>

Bellow is my code that works perfect:

```auto
import java.util.Map;
 
final int ROWZERO=0;
final int COL_TIME=0;
final int COL_INTENSITY=1;
final float THRESH=1.2;
 
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;
 
 
  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 ecg = table.getFloat(i, COL_INTENSITY);
 
 
    if (ecg>peakValue) {      
      int idx=i-1;
      ctime=table.getInt(idx, COL_TIME);    
      break;
    }
  }
 
  return ctime;
}
 

```

---

<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 5, 2018, 9:42pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/4 "2018-12-05T21:42:26Z")

</div>

If i add a function named findTHRESHOLD() and call it into setup my result (which is

> "Number of peaks = "+timePeakPair.size())

is zero and i dont know why…

```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:** ![netphysicist](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@netphysicist](https://discourse.processing.org/u/netphysicist)\
**Post date:** [December 6, 2018, 11:30pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/5 "2018-12-06T23:30:52Z")

</div>

Can anyone help me please???

---

<div class="post-metadata">

**Author:** ![Whahahahaha](https://avatars.discourse-cdn.com/v4/letter/w/3be4f8/32.png) [@Whahahahaha](https://discourse.processing.org/u/Whahahahaha)\
**Post date:** [December 7, 2018, 8:56am UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/6 "2018-12-07T08:56:20Z")

</div>

@netphysicist well i’m not sure:

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

```

works as setColumnType() needs an integer and the type,

> setColumnType  
> public void setColumnType(int column,  
> int newType)  
> Sets the column type. If data already exists, then it’ll be converted to the new type.  
> Parameters:  
> column - the column whose type should be changed  
> newType - something fresh, maybe try an int or a float for size?  
> [Table](http://processing.github.io/processing-javadocs/core/processing/data/Table.html#setColumnType-int-int-)

the same thing happens in

```auto

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

```

where in float getTimeEntry() you use table.getFloat(i, COL\_TIME) and table.getFloat(i, COL\_INTENSITY);  
if you changed the String “Intensity” in the findTHRESHOLD() function to COL\_INTENSITY it works.

---

<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 8, 2018, 2:26pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/7 "2018-12-08T14:26:12Z")

</div>

I think i did not understood what i have to change @Whahahahaha …Also the intensity value must be float not integer.  
After a lot of changes and tests that i made i realized that the problem is when i call the function

> findTHRESHOLD();

into the setup. Something is doing wrong in the for loop after findTHRESHOLD() function… I don’t know and i can’t find what is wrong and what i have to do…Maybe something is going wrong with the for loop because inside the findTHRESHOLD() function there is a for loop also…I don’t know…Please help me 🤔 😥😥

@jb4x any idea???

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [December 9, 2018, 7:56pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/8 "2018-12-09T19:56:35Z")

</div>

Well, having your “data2.csv” file would also help a lot in finding what’s going on.

---

<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 9, 2018, 8:02pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/9 "2018-12-09T20:02:46Z")

</div>

Of course

> **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 |
> | 1990 | 0.3 |
> | 2000 | 0.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 9, 2018, 8:04pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/10 "2018-12-09T20:04:55Z")

</div>

Or as the csv file

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

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [December 9, 2018, 8:10pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/11 "2018-12-09T20:10:38Z")

</div>

I just changed this:

```auto
if (intensity>peakValue) {

```

by this:

```auto
if (intensity>peakValue && i > 0) {

```

and I got those results in the console:

```auto
1.4505949
Number of peaks = 1

```

Is it what it is supposed to do?

---

<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 9, 2018, 8:22pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/12 "2018-12-09T20:22:12Z")

</div>

Nooo, the number of peaks must be 7…  
Look the graph bellow is from this data and as you can see we have 7 peaks above THRESH=1.2

 ![3](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/3637206f54e33655fd2ab3fc2ddf73f9bff26ad9.png)

---

<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, 8:49am UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/13 "2018-12-10T08:49:40Z")

</div>

Its a so simple issue and no one can help me…I made everything right… I can’t understand what stuck into the for loop…😥😥😥😥😥😥

Any idea @kll @jeremydouglass @kfrajer ???

---

<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, 12:59pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/14 "2018-12-10T12:59:02Z")

</div>

-a-  
the reverse sort to find a max value DOES a sort on your RAW date  
i think that is bad idea  
-b-  
a diagnostic print can help like

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

```

shows

> findTHRESHOLD\_average\_intens 0.553892 maxIntensity 1.8 THRESH: 1.453892

you sure about that calculation?

> average\_intens + maxIntensity/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, 1:14pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/15 "2018-12-10T13:14:08Z")

</div>

My findTHRESHOLD is this:

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

```

it calculates right all these values(maxIntensity,average\_intens, THRESH) inside and it gives a right THRESH…The problem is when i call findTHRESHOLD() in the setup before for loop.

---

<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, 1:19pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/16 "2018-12-10T13:19:17Z")

</div>

so you confirm the 1.45 as ok.  
but  
there is a reason why i killed your sortReverse for maxIntensity  
your later analysis ( the time over TRESH and under )  
you want that to be done on the raw data or on the for intensity SORTED data???  
think about it again

```auto
void setup() {
  size(400, 500);
//...
}

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);
}

```

 ![snap-044](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b0e148682d55a495682ee8ae3287a676e8f892a2.png)

> Number of peaks = 2

pls show us your graph

---

<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:11pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/17 "2018-12-10T14:11:27Z")

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

I am sorry @kll replace please the line THRESH=(average\_intens + maxIntensity/2); with

```auto
THRESH=((average_intens + maxIntensity)/1.9);

```

My graph is the same as yours and i have uploaded a picture of my graph above…

---

<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:16pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/18 "2018-12-10T14:16:45Z")

</div>

> [@netphysicist](#):
>
> My graph is the same as yours and i have uploaded a picture of my graph above…

?where? i want see your TRESH line?  
did you include my graph code into your?

 ![snap-045](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/1ecfecafdea7524f04d1325563f6cabf287e0387.png)

---

<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:19pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/19 "2018-12-10T14:19:54Z")

</div>

Replace line 107 with this:

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

Yes, i included your graph code into mines and it is correct…

The problem is when i call the when i call findTHRESHOLD() in the setup before for loop.

The number of peaks above THRESH=1.23 must be 7 as you see in the picture bellow.

 ![3](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/3637206f54e33655fd2ab3fc2ddf73f9bff26ad9.png)

---

<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:24pm UTC](https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/20 "2018-12-10T14:24:58Z")

</div>

> [@netphysicist](#):
>
> Replace line 107 with this:
> 
> > THRESH=((average\_intens + maxIntensity)/1.9);
> 
> Yes, i included your graph code into mines and it is correct…

you not see that i did that??

> [@](#):
>
> The problem is when i call the when i call findTHRESHOLD() in the setup before for loop.  
> The number of peaks must be 7

this two remarks i can not understand???  
what is the problem when you use ?your?

> findTHRESHOLD() in the setup

i run now:

```auto
// https://discourse.processing.org/t/use-into-setup-a-float-value-from-a-method-that-finds-it/6343/4?u=kll

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

```

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