11721206 发表于 2013-9-26 11:05:34

在曲线上找到极大值并且画圆!

大家好!我现在在做一个传感器,然后我在processing上面实时监测我的传感器信号。我现在想在我的传感器的每个轴的信号上的极大值处画个圆以做个标记,并且标记后,我的圆还得一直跟着我的极大值往左移!我现在有一个判断极大值并且画圆的代码。
但是结果是并不能在所有的极大值处画圆!
   for(count=1;count<RawX.length-2;count++)
{
    if((ScaledX-ScaledX)>0)
    if((ScaledX-ScaledX)>0)
    ellipse(RawX.length-2-count,ScaledX+100,8,8);
}
画的图是











下面是我的整个代码。有兴趣的同学可以看一下!
/*
THIS PROGRAM WORKS WITH PulseSensorAmped_Arduino-xx ARDUINO CODE
THE PULSE DATA WINDOW IS SCALEABLE WITH SCROLLBAR AT BOTTOM OF SCREEN
PRESS 'S' OR 's' KEY TO SAVE A PICTURE OF THE SCREEN IN SKETCH FOLDER (.jpg)
MADE BY JOEL MURPHY AUGUST, 2012
*/


import processing.serial.*;
PFont font;
Scrollbar scaleBar;
ArrayList<Particle> particles;

Serial port;   

float Sensor;      // HOLDS PULSE SENSOR DATA FROM ARDUINO
float Sensor_X;
float Sensor_Y;
float Sensor_Z;

float Sensor_X_angle;
float Sensor_Y_angle;
float Sensor_Z_angle;


int IBI;         // HOLDS TIME BETWEN HEARTBEATS FROM ARDUINO
int BPM;         // HOLDS HEART RATE VALUE FROM ARDUINO
float[] RawX;
float[] RawY;      // HOLDS HEARTBEAT WAVEFORM DATA BEFORE SCALING
float[] RawZ;
int[] ScaledX;
int[] ScaledY;   // USED TO POSITION SCALED HEARTBEAT WAVEFORM
int[] ScaledZ;
int[] rate;      // USED TO POSITION BPM DATA WAVEFORM
float zoom;      // USED WHEN SCALING PULSE WAVEFORM TO PULSE WINDOW
float offset;    // USED WHEN SCALING PULSE WAVEFORM TO PULSE WINDOW
color eggshell = color(255, 253, 248);
int heart = 0;   // This variable times the heart image 'pulse' on screen
//THESE VARIABLES DETERMINE THE SIZE OF THE DATA WINDOWS
int PulseWindowWidth = 490;
int PulseWindowHeight = 512;
int BPMWindowWidth = 180;
int BPMWindowHeight = 340;
boolean beat = false;    // set when a heart beat is detected, then cleared when the BPM graph is advanced
String S;
int A,B,C,D;
boolean A1,B1,C1;
int x;
int S1,SS,SSS;
boolean pedometer=false;
int xx,yy,zz;
int pointx,countx,count;
int countxx=0;
int temp;

Particle[] part = new Particle;

int currentTime,w=0,lastTime=0;
float [] RwEst = new float;
int interval = 0;
float R;
int x1,x2,x3;

void setup() {
size(700, 600,P3D);// Stage size
frameRate(100);
font = loadFont("Arial-BoldMT-24.vlw");
textFont(font);
textAlign(CENTER);
rectMode(CENTER);
ellipseMode(CENTER);
// Scrollbar constructor inputs: x,y,width,height,minVal,maxVal
scaleBar = new Scrollbar (400, 575, 180, 12, 0.5, 1.0);// set parameters for the scale bar
RawX = new float;
RawY = new float;          // initialize raw pulse waveform array
RawZ = new float;
ScaledX = new int;
ScaledY = new int;       // initialize scaled pulse waveform array
ScaledZ = new int;

rate = new int ;         // initialize BPM waveform array

zoom = 0.75;                               // initialize scale of heartbeat window
   
// set the visualizer lines to 0
for (int i=0; i<rate.length; i++){
    rate = 555;      // Place BPM graph line at bottom of BPM Window
   }
for (int i=0; i<RawY.length; i++){
    RawY = height/2; // initialize the pulse window data line to V/2
}
   
// GO FIND THE ARDUINO
println(Serial.list());    // print a list of available serial ports
// choose the number between the [] that is connected to the Arduino
port = new Serial(this, Serial.list(), 38400);// make sure Arduino is talking serial at this baud rate
port.clear();            // flush buffer
port.bufferUntil('\n');// set buffer full flag on receipt of carriage return
}



void normalize3DVec(float [] vector) {

R = sqrt(vector*vector + vector*vector + vector*vector);
vector /= R;
vector /= R;
vector /= R;
}




void draw() {

background(0);
noStroke();
// DRAW OUT THE PULSE WINDOW AND BPM WINDOW RECTANGLES
fill(eggshell);// color for the window background
rect(255,height/2,PulseWindowWidth,PulseWindowHeight);
rect(600,385,BPMWindowWidth,BPMWindowHeight);

// DRAW THE PULSE WAVEFORM
// prepare pulse data points   
RawX = Sensor_X;
RawY = Sensor_Y;   // place the new raw datapoint at the end of the array
RawZ = Sensor_Z;
zoom = scaleBar.getPos();                      // get current waveform scale value
offset = map(zoom,0.5,1,300,0);                // calculate the offset needed at this scale
for (int i = 0; i < RawX.length-1; i++) {      // move the pulse waveform by
    RawX = RawX;                         // shifting all raw datapoints one pixel left
    float dummy = RawX * zoom + offset;       // adjust the raw data to the selected scale
    ScaledX = constrain(int(dummy),44,556);   // transfer the raw data array to the scaled array
}



    fill(250,0,0);
   // if(A==1){
   // for(countx=0;countx<RawX.length-1;countx++)
    // {
   for(count=1;count<RawX.length-2;count++)
   
{
   
    if((ScaledX-ScaledX)>0)
    if((ScaledX-ScaledX)>0)
    //if((ScaledX-ScaledX)>0)
    //if((ScaledX-ScaledX)>0)
    //if(A==1)
//    {
    //ellipse(count,ScaledX+100,8,8);
    ellipse(RawX.length-2-count,ScaledX+100,8,8);
    //ellipse(RawX.length-2,ScaledX+100,8,8);
   }


   
//}
//    pointx++;
//   
//    if(pointx>RawX.length-2)
//    pointx=0;
//    }

       for (int i = 0; i < RawY.length-1; i++) {      // move the pulse waveform by
    RawY = RawY;                         // shifting all raw datapoints one pixel left
    float dummy = RawY * zoom + offset;       // adjust the raw data to the selected scale
    ScaledY = constrain(int(dummy),44,556);   // transfer the raw data array to the scaled array
}



    for (int i = 0; i < RawZ.length-1; i++) {      // move the pulse waveform by
    RawZ = RawZ;                         // shifting all raw datapoints one pixel left
    float dummy = RawZ * zoom + offset;       // adjust the raw data to the selected scale
    ScaledZ = constrain(int(dummy),44,556);   // transfer the raw data array to the scaled array
   
}
//fill(0,255,0);
//    if(B==1){
//    for(count=0;count<RawY.length-2;count++)
//    if((ScaledY-ScaledY)>0)
//    if((ScaledY-ScaledY)>0)
//    {
//   
//    ellipse(RawY.length-2-count,ScaledY+200,8,8);
//   
//   
//    //ellipse(RawX.length-2,ScaledX+100,8,8);
//    }}
//    fill(0,0,255);
//    //if(C==1){
//    for(count=0;count<RawZ.length-2;count++)
//    if((ScaledZ-ScaledZ)>0)
//    if((ScaledZ-ScaledZ)>0)
//    {
////    countx++;
//   
//    ellipse(RawZ.length-2-count,ScaledZ,8,8);
//   
//   
//    //ellipse(RawX.length-2,ScaledX+100,8,8);
//    }



//stroke(250,0,0);                               // red is a good color for the pulse waveform
noFill();
beginShape();                                  // using beginShape() renders fast
for ( x = 1; x < RawX.length-1;x++) {
    stroke(255,0,0);   
    vertex(x, ScaledX+100);
}
endShape();

    beginShape();                                  // using beginShape() renders fast
for ( x = 1; x < RawY.length-1;x++) {
   stroke(0,255,0);   
    vertex(x, ScaledY+200);
}
endShape();

    beginShape();                                  // using beginShape() renders fast
for ( x = 1; x < RawZ.length-1;x++) {   
    stroke(0,0,255);   
    vertex(x, ScaledZ);
}

endShape();
}




THX
页: [1]
查看完整版本: 在曲线上找到极大值并且画圆!