2d stereo oscilloscope

edited February 2017 in Library Questions

Hi there, I created this simple oscilloscope with point, and i try without result to use line to merge points, i understand i have to store the x,y points of the two stereo channels into an array but i don't understand how, could some one help me about?

Thanks a lot! Gianmaria

Answers

  • Answer ✓

    This is an example that demonstrates how to plot data points. the concept is that you store the data in a container and then you traverse the container from the first element to the before the last one (Not the last one). The reason is that you connect a point from the [current position] to the [current pos +1]. If you include the last position, then the algorithm would try to access the [last position+1] which generates an out of bounds exception. The code was a modification from a previous post: https://forum.processing.org/two/discussion/comment/88886#Comment_88886

    I generate artificial data which then I process(extract) and plot. Notice I am not plotting x vs. y but I am plotting both x and y against (relative) time.

    Kf

    final int kMAX_DATA_VAL=1024; 
    final int kVERTICAL_MARGIN=int(kMAX_DATA_VAL*0.1);
    
    int bSize;  //buffer size
    int index =-1;
    
    String globalBuffer;
    int[] valuesx;   //Data been displayed on screen
    int[] valuesy;
    
    
    void settings() {
      size(512, 512);
    }
    
    void setup()
    {
      bSize=width;
      valuesx = new int[bSize];
      valuesy = new int[bSize];
    
      noSmooth();
    
      background(87, 36, 124);
      strokeWeight(5);
    }
    
    
    void draw() {
    
      ////ENABLE NEXT LINE WHEN NO SERIAL EVENTS AVAILABLE
      loadArtificialData();
    
      surface.setTitle("Index="+index+" time="+millis()/1000+" sec");
      boolean drawNow=processInputData(globalBuffer);
    
      if (drawNow==true) {
        //Reset background for new data wave
        if ((index)<=1) {
          background(87, 36, 124);
          return;
        }
    
        drawLine(index, valuesx, -kMAX_DATA_VAL/2, kMAX_DATA_VAL/2, color(255, 25, 25));   
        drawLine(index, valuesy, -kMAX_DATA_VAL/2, kMAX_DATA_VAL/2, color(250, 205, 25));
      }
    }
    
    
    //Returns true when valid data is available
    boolean processInputData(String buff) {
    
      if (buff==null)
        return false;
    
      float[] mbuff=float(split(buff, ","));
    
      //Only process data with two tokens, discard anything else
      if (mbuff.length!=2)
        return false;
    
      index++;
      if (index==bSize)
        index=0;
    
      valuesx[index] = int(mbuff[0]);
      valuesy[index] = int(mbuff[1]);
    
      buff=null;  //Reset buffer for next data stream (IMPORTANT!!!)
      return true;
    }
    
    //Draws a line from previous index to current index
    void drawLine(int idx, int[] data, float LRange, float HRange, color cc) {
      float y1=map(data[idx-1], LRange, HRange, height-kVERTICAL_MARGIN, kVERTICAL_MARGIN);
      float y2=map(data[idx], LRange, HRange, height-kVERTICAL_MARGIN, kVERTICAL_MARGIN);
      stroke(cc);
      line(idx-1, y1, idx, y2);
    }
    
    //This function is for testing/debugging purposes
    //Loads data generated from a sine wave
    //FORMAT: "time,xval,yval"
    void loadArtificialData() {
      globalBuffer=str(index)+",";
      globalBuffer+=str(kMAX_DATA_VAL/2*sin(radians(index%width)*2*3.1415/0.8));
    
      //println(globalBuffer);
    }
    
Sign In or Register to comment.