极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 8206|回复: 0

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

[复制链接]
发表于 2013-9-26 11:05:34 | 显示全部楼层 |阅读模式
大家好!我现在在做一个传感器,然后我在processing上面实时监测我的传感器信号。我现在想在我的传感器的每个轴的信号上的极大值处画个圆以做个标记,并且标记后,我的圆还得一直跟着我的极大值往左移!我现在有一个判断极大值并且画圆的代码。
但是结果是并不能在所有的极大值处画圆!
   for(count=1;count<RawX.length-2;count++)
{
    if((ScaledX[RawX.length-2-count]-ScaledX[RawX.length-3-count])>0)
    if((ScaledX[RawX.length-2-count]-ScaledX[RawX.length-1-count])>0)
    ellipse(RawX.length-2-count,ScaledX[RawX.length-2-count]+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[490];

int currentTime,w=0,lastTime=0;
float [] RwEst = new float[3];
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[PulseWindowWidth];
  RawY = new float[PulseWindowWidth];          // initialize raw pulse waveform array
  RawZ = new float[PulseWindowWidth];
  ScaledX = new int[PulseWindowWidth];
  ScaledY = new int[PulseWindowWidth];       // initialize scaled pulse waveform array
  ScaledZ = new int[PulseWindowWidth];
  
  rate = new int [BPMWindowWidth];           // 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[i] = 555;      // Place BPM graph line at bottom of BPM Window
   }
for (int i=0; i<RawY.length; i++){
    RawY[i] = 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()[0], 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[0]*vector[0] + vector[1]*vector[1] + vector[2]*vector[2]);
  vector[0] /= R;
  vector[1] /= R;  
  vector[2] /= 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[RawX.length-1] = Sensor_X;
  RawY[RawY.length-1] = Sensor_Y;   // place the new raw datapoint at the end of the array
  RawZ[RawZ.length-1] = 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[i] = RawX[i+1];                         // shifting all raw datapoints one pixel left
    float dummy = RawX[i] * zoom + offset;       // adjust the raw data to the selected scale
    ScaledX[i] = 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[RawX.length-2-count]-ScaledX[RawX.length-3-count])>0)
    if((ScaledX[RawX.length-2-count]-ScaledX[RawX.length-1-count])>0)
    //if((ScaledX[count+1]-ScaledX[count+2])>0)
    //if((ScaledX[count+1]-ScaledX[count])>0)
    //if(A==1)
//    {
    //ellipse(count,ScaledX[count]+100,8,8);
    ellipse(RawX.length-2-count,ScaledX[RawX.length-2-count]+100,8,8);
    //ellipse(RawX.length-2,ScaledX[RawX.length-2]+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[i] = RawY[i+1];                         // shifting all raw datapoints one pixel left
    float dummy = RawY[i] * zoom + offset;       // adjust the raw data to the selected scale
    ScaledY[i] = 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[i] = RawZ[i+1];                         // shifting all raw datapoints one pixel left
    float dummy = RawZ[i] * zoom + offset;       // adjust the raw data to the selected scale
    ScaledZ[i] = 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[RawY.length-2-count]-ScaledY[RawY.length-3-count])>0)
//    if((ScaledY[RawY.length-2-count]-ScaledY[RawY.length-1-count])>0)
//    {
//     
//    ellipse(RawY.length-2-count,ScaledY[RawY.length-2-count]+200,8,8);
//   
//   
//    //ellipse(RawX.length-2,ScaledX[RawX.length-2]+100,8,8);
//    }}
//    fill(0,0,255);
//    //if(C==1)  {
//    for(count=0;count<RawZ.length-2;count++)
//    if((ScaledZ[RawZ.length-2-count]-ScaledZ[RawZ.length-3-count])>0)
//    if((ScaledZ[RawZ.length-2-count]-ScaledZ[RawZ.length-1-count])>0)
//    {
////    countx++;
//     
//    ellipse(RawZ.length-2-count,ScaledZ[RawZ.length-2-count],8,8);
//   
//   
//    //ellipse(RawX.length-2,ScaledX[RawX.length-2]+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[x]+100);
  }
endShape();

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

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




THX

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-24 17:25 , Processed in 0.041380 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表