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


#1

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


#2

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.


#3

Bellow is my code that works perfect:

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



#4

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…

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

}
 

#5

Can anyone help me please???


#6

@netphysicist well i’m not sure:

  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?
http://processing.github.io/processing-javadocs/core/processing/data/Table.html#setColumnType-int-int-

the same thing happens in


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.


#7

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 :thinking: :disappointed_relieved::disappointed_relieved:

@jb4x any idea???


#8

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


#9

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

#10

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


#11

I just changed this:

if (intensity>peakValue) {

by this:

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

and I got those results in the console:

1.4505949
Number of peaks = 1

Is it what it is supposed to do?


#12

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


#13

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…:disappointed_relieved::disappointed_relieved::disappointed_relieved::disappointed_relieved::disappointed_relieved::disappointed_relieved:

Any idea @kll @jeremydouglass @kfrajer ???


#14

-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

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


#15

My findTHRESHOLD is this:

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.


#16

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

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

Number of peaks = 2

pls show us your graph


#17

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

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

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


#18

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


#19

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.


#20

you not see that i did that??

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

findTHRESHOLD() in the setup

i run now:

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