Converts the brainwave long data

Hi everyone, I get a series of long datatype from my TGAM EEG hardware and I would like to convert it to a small range of data so that I could use these variables to control my visuals. I mapped these long datatype roughly and directly but I found it's not a good way cause the result is incoherent and bouncing. Now I have no idea about it. Is there anyone has and idea about it? Any help will be appreciated.

        float delta_ = map(delta, 0, 3072000, 0, 100);
        float theta_ = map(theta, 0, 1300000, 0, 100);
        float low_alpha_ = map(low_alpha, 0, 600000, 0, 100);
        float high_alpha_ = map(high_alpha, 0, 300000, 0, 100);
        float low_beta_ = map(low_beta, 0, 150000, 0, 100);
        float high_beta_ = map(delta, 0, 3000000, 0, 100);
        float low_gamma_ = map(delta, 0, 3000000, 0, 100);
        float high_gamma_ = map(delta, 0, 3000000, 0, 100);

Answers

  • edited October 2017

    I found it's not a good way cause the result is incoherent and bouncing

    What does that mean?

    What is the data type assigned to delta, theta etc?

  • Hi quark, they are the long datatype.

  • edited October 2017

    Just (cast) it to int datatype: *-:)

    final long delta = 1_450_903;
    final int deltaInt = (int) delta;
    final float delta_ = map(deltaInt, 0, 3072000, 0, 100);
    
    println(delta_); // 47.229916
    exit();
    
  • Rather than casing a long to an int, you might try this

    float delta_ = (float) map((double) delta, 0, 3072000, 0, 100);
    float theta_ = (float) map((double) theta, 0, 1300000, 0, 100);
    float low_alpha_ = (float) map((double) low_alpha, 0, 600000, 0, 100);
    ...
    

    This will cause the range values to be cast to doubles so that map returns a double which is then cast to a float

  • edited October 2017

    Hi GoToLoop, I already have some previous steps like this

    delta = Integer.parseInt(incomingValues[3]);
    theta = Integer.parseInt(incomingValues[4]); 
    ...
    float delta_ = map(delta, 0, 3072000, 0, 100);
    float theta_ = map(theta, 0, 1300000, 0, 100);
    

    So I think I have already converted the long datatype before map maybe... I don't really know what's the function '_' in the numbers 3_072_000, but I just do it but I get a error "unexpected token:float".

    Now the code like this: delta = Integer.parseInt(incomingValues[3]); theta = Integer.parseInt(incomingValues[4]); low_alpha = Integer.parseInt(incomingValues[5]); ... float delta_ = map(delta, 0, 3_072_000, 0, 100); float theta_ = map(theta, 0, 1_300_000, 0, 100); float low_alpha_ = map(low_alpha, 0, 600_000, 0, 100);

  • Hi quark, thanks for the response. I tried but I got the error that "the function 'map()' expects parameters like: 'map(float, float, float, float, float)'". I thinks maybe 'double' is on the right track but not in this way. Do you have some further advice?

  • final int[] incomingValuesInt = int(incomingValues);
    final int delta = incomingValuesInt[3];
    final float delta_ = map(delta, 0, 3072000, 0, 100);
    
  • Answer ✓

    Sorry my mistake. I looked at the source code for PApplet and although there is a version of map using the double data type it is commented out.

    The problem with converting long data values to int data values is that you overflow the capacity of the int data type.

    This code

    long l = 1234567890123456789L;
    int i = (int) l;
    float f = (float) l;
    println("i = " + i);
    println("f = " + f);
    

    produces the output

    i = 2112454933
    f = 1.23456794E18
    

    I suggest that you convert the long directly to float like this

    float delta_ = map((float) delta, 0, 3072000, 0, 100);
    float theta_ = map((float) theta, 0, 1300000, 0, 100);
    float low_alpha_ = map((float) low_alpha, 0, 600000, 0, 100);
    ...
    
  • edited October 2017 Answer ✓

    The problem with converting long data values to int data values is that you overflow the capacity of the int data type.

    That is just an IF. However, the max values 3072000, 1300000 & 600000 are within int's range. L-)

  • Thanks for your response. But I still get the very bouncing and jumping data as before. Is that because my mapping method not that appropriate?

  • But I still get the very bouncing and jumping data as before

    Perhaps the input data is very bouncy and jumping. There is nothing wrong with the mapping provided the input values are inside the ranges you have specified.

    e.g. delta is always in the range 0 to 3072000

  • Hi quark, thanks a lot. I will double check with the range values.

  • I have made a brain visualizer which maps the value dynamically based on the received the max and min value.

    //------------------------------------------Graph Plotter -----------------------------------
    void BrainWave() {
      noFill();
      strokeWeight(2);
      //---------HighA--------------------
      beginShape();
      stroke(#FF0000);
      for (int i=0;i<HighA.size();i++) {
        Points P = (Points) HighA.get(i);
        float t = map(P.y, min_AH, max_AH, 0, height/2);
        vertex(P.x, t);
        P.x-=5/10.0;
      }
      endShape();
      //---------LowA-------------------
      beginShape();
      stroke(#FFAF00);
      for (int i=0;i<LowA.size();i++) {
        Points P = (Points) LowA.get(i);
        float t = map(P.y, min_AL, max_AL, 0, height/2);
        vertex(P.x, t);
        P.x-=5/10.0;
      }
      endShape();
      //---------HighB-------------------
      beginShape();
      stroke(#0081FF);
      for (int i=0;i<HighB.size();i++) {
        Points P = (Points) HighB.get(i);
        float t = map(P.y, min_BH, max_BH, 0, height/2);
        vertex(P.x, t);
        P.x-=5/10.0;
      }
      endShape();
      //---------LowB-------------------
      beginShape();
      stroke(#00B9FF);
      for (int i=0;i<LowB.size();i++) {
        Points P = (Points) LowB.get(i);
        float t = map(P.y, min_BL, max_BL, 0, height/2);
        vertex(P.x, t);
        P.x-=5/10.0;
      }
      endShape();
      //---------MidG--------------------
      beginShape();
      stroke(#14FF00);
      for (int i=0;i<MidG.size();i++) {
        Points P = (Points) MidG.get(i);
        float t = map(P.y, min_GM, max_GM, 0, height/2);
        vertex(P.x, t);
        P.x-=5/10.0;
      }
      endShape();
      //---------LowG-------------------
      beginShape();
      stroke(#F6FF00);
      for (int i=0;i<LowG.size();i++) {
        Points P = (Points) LowG.get(i);
        float t = map(P.y, min_GL, max_GL, 0, height/2);
        vertex(P.x, t);
        P.x-=5/10.0;
      }
      endShape();
      //---------DeltaX-------------------
      beginShape();
      stroke(#333431);
      for (int i=0;i<DeltaX.size();i++) {
        Points P = (Points) DeltaX.get(i);
        float t = map(P.y, min_D, max_D, 0, height/2);
        vertex(P.x, t);
        P.x-=5/10.0;
      }
      endShape();
      //---------ThetaX-------------------
      beginShape();
      stroke(0);
      for (int i=0;i<ThetaX.size();i++) {
        Points P = (Points) ThetaX.get(i);
        float t = map(P.y, min_T, max_T, 0, height/2);
        vertex(P.x, t);
        P.x-=5/10.0;
      }
      endShape();
    }
    
    //----------------------------
    
        import neurosky.*;
        import org.json.*;
        import java.util.*;
        import java.net.*;
        //-------------------------------------------
        ThinkGearSocket neuroSocket;
        //-------------------------------------------
        ArrayList<Points> HighA = new ArrayList();
        ArrayList<Points> LowA = new ArrayList();
        ArrayList<Points> HighB = new ArrayList();
        ArrayList<Points> LowB = new ArrayList();
        ArrayList<Points> MidG = new ArrayList();
        ArrayList<Points> LowG = new ArrayList();
        ArrayList<Points> DeltaX = new ArrayList();
        ArrayList<Points> ThetaX = new ArrayList();
        //--------------------------------------------
        float med, attain;
        int max_AH=0, max_AL=0, max_BH=0, max_BL=0, max_GM=0, max_GL=0, max_D=0, max_T=0; // max value cutoff
        int min_AH=0, min_AL=0, min_BH=0, min_BL=0, min_GM=0, min_GL=0, min_D=0, min_T=0; // min value cutoff
        //--------------------------------------------
        void setup() {
          size(600, 400);
          ThinkGearSocket neuroSocket = new ThinkGearSocket(this);
          try {
            neuroSocket.start();
          } 
          catch (ConnectException e) {
          }
        }
    
        void draw() {
          background(-1);
          BrainWave();
        }
    
    
        class Points {
          float x, y;
          Points(float x, float y) {
            this.x = x;
            this.y = y;
          }
        }
        //-------------------------------------------
        void poorSignalEvent(int sig) {
          println("SignalEvent "+sig);
        }
    
        public void attentionEvent(int attentionLevel) {
          println("Attention Level: " + attentionLevel);
          // attention = attentionLevel;
        }
    
    
        void meditationEvent(int meditationLevel) {
          println("Meditation Level: " + meditationLevel);
          // meditation = meditationLevel;
        }
    
        void blinkEvent(int blinkStrength) {
    
          println("blinkStrength: " + blinkStrength);
        }
    
        public void eegEvent(int delta, int theta, int low_alpha, int high_alpha, int low_beta, int high_beta, int low_gamma, int mid_gamma) {
          //--------------------------------------------------
          if (high_alpha > max_AH) {
            max_AH = high_alpha;
          }
          else if (high_alpha < min_AH) {
            min_AH = high_alpha;
          }
          Points HA = new Points(width, high_alpha);
          HighA.add(HA);
          //--------------------------------------------------
          if (low_alpha > max_AL) {
            max_AL = low_alpha;
          }
          else if (low_alpha < min_AL) {
            min_AL = low_alpha;
          }
          Points LA = new Points(width, low_alpha);
          LowA.add(LA);
          //--------------------------------------------------
          if (high_beta > max_BH) {
            max_BH = high_beta;
          }
          else if (high_beta < min_BH) {
            min_BH = high_beta;
          }
          Points HB = new Points(width, high_beta);
          HighB.add(HB);
          //--------------------------------------------------
          if (low_beta > max_BL) {
            max_BL = low_beta;
          }
          else if (low_beta < min_BL) {
            min_BL = low_beta;
          }
          Points LB = new Points(width, low_beta);
          LowB.add(LB);
          //--------------------------------------------------
          if (mid_gamma > max_GM) {
            max_GM = mid_gamma;
          }
          else if (mid_gamma < min_GM) {
            min_GM = mid_gamma;
          }
          Points MG = new Points(width, mid_gamma);
          MidG.add(MG);
          //--------------------------------------------------
          if (low_gamma > max_GL) {
            max_GL = low_gamma;
          }
          else if (low_gamma < min_GL) {
            min_GL = low_gamma;
          }
          Points LG = new Points(width, low_gamma);
          LowG.add(LG);
          //--------------------------------------------------
          if (delta > max_D) {
            max_D = delta;
          }
          else if (delta < min_D) {
            min_D = delta;
          }
          Points del = new Points(width, delta);
          DeltaX.add(del);
          //--------------------------------------------------
          if (theta > max_T) {
            max_T = theta;
          }
          else if (theta < min_T) {
            min_T = theta;
          }
          Points the = new Points(width, theta);
          ThetaX.add(the);
          //--------------------------------------------------
          println(" MAX of all " + max_AH + " " + max_AL + " " +  max_BH + " " +  max_BL + " " +  max_GM + " " +  max_GL + " " +  max_D + " " +  max_T);
          //--------------------------------------------------
        }
    
        void rawEvent(int[] raw) {
        }
    
        void stop() {
          neuroSocket.stop();
          super.stop();
        }
    
Sign In or Register to comment.