program TDS_CALC
%%#include <math.h>

int i;
float t1;
float t2;

int EVENT;
assign EVENT to "FBM:TDS820:EVENT";
monitor EVENT;
int EVENT_OLD;

int VLEAD1;
int VLEAD2;
int VLEAD3;
int VLEAD4;

assign VLEAD1 to "FBM:TDS820:VLEAD1";
assign VLEAD2 to "FBM:TDS820:VLEAD2";
assign VLEAD3 to "FBM:TDS820:VLEAD3";
assign VLEAD4 to "FBM:TDS820:VLEAD4";

double TIME;
assign TIME to "FB_TB4:COL:TIME";

short CH1_WF[500];
assign CH1_WF to "FBM:TDS820:CH1:WF";

float CH1_CURVE[500];
assign CH1_CURVE to "FBM:TDS820:CH1:CURVE";
float XAXIS[500];
assign XAXIS to "FBM:TDS820:XAXIS";

float XINC;
assign XINC to "FBM:TDS820:CH1:XINC:GET";
float YMULT;
assign YMULT to "FBM:TDS820:CH1:YMULT:GET";

%{
float min_search()
{
	int i,j;
	float min;
	int i1,i2;
	float temp,t2;

	if (VLEAD1 > VLEAD2) {
	 i1 = VLEAD2;
	 i2 = VLEAD1;
	}
	else {
	 i1 = VLEAD1;
	 i2 = VLEAD2;
	};
	
	if (i1<0) {
	 i1 = 0;
	};
	if (i2>=500) {
	 i2 = 499;
	};
	
	j= i1;
	min = 1.0*CH1_WF[i1];

	for (i=i1;i<=i2;i++){
	  if (min > CH1_WF[i])	{
	    min = CH1_WF[i];
	    j = i;
	  };
	}
	if (j==0){
	 return(0.0);
	}
	else if (j==499) {
	 return(499.0);
	}
	else { 	
 	 temp = (j-1)*1.0*CH1_WF[j-1]+j*1.0*CH1_WF[j]+(j+1)*1.0*CH1_WF[j+1];
	 t2 = (CH1_WF[j-1]+CH1_WF[j]+CH1_WF[j+1]);
	 if (fabs(t2) <1e-9) {
	 t2 =1e-9;
	 };
	 temp = temp /t2;
	 return(temp);
	}
} 
float max_search()
{
	int i,j;
	float max;
	int i1,i2;
	float temp,t2;

	if (VLEAD3 > VLEAD4) {
	 i1 = VLEAD4;
	 i2 = VLEAD3;
	}
	else {
	 i1 = VLEAD3;
	 i2 = VLEAD4;
	};
	if (i1<0) {
	 i1 = 0;
	};
	if (i2>=500) {
	 i2 = 499;
	};
	
	j= i1;
	max = 1.0*(CH1_WF[i1]);
	for (i=i1;i<=i2;i++){
	  if (max < CH1_WF[i])	{
	    max = CH1_WF[i];
	    j = i;
	  };
	};
	if (j==0){
	 return(0.0);
	}
	else if (j==499) {
	 return(499.0);
	}
	else { 	
 	 temp = (j-1)*1.0*CH1_WF[j-1]+j*1.0*CH1_WF[j]+(j+1)*1.0*CH1_WF[j+1];
	 t2 = (CH1_WF[j-1]+CH1_WF[j]+CH1_WF[j+1]);
	 if (fabs(t2) < 1e-9){ 
	  t2 =1e-9;
	 };
	 temp = temp /t2;
	 return(temp);
	}
} 
}%

ss state_wavecalc
{
	state init
	{
		when()
		{
		pvGet(VLEAD1);
		pvGet(VLEAD2);
		pvGet(VLEAD3);
		pvGet(VLEAD4);
		
		pvGet(XINC);
		pvGet(YMULT);
		
		pvGet(EVENT);
		EVENT_OLD = EVENT;
		}state wavcalc
	}
	state wavcalc
	{
		when(EVENT != EVENT_OLD)
		{
		EVENT_OLD = EVENT;
		pvGet(CH1_WF);
		pvGet(VLEAD1);
		pvGet(VLEAD2);
		pvGet(VLEAD3);
		pvGet(VLEAD4);
		pvGet(XINC);
		pvGet(YMULT);
		
		%{
		t1 = min_search();
		t2 = max_search();
		}%
		TIME = (t2-t1)*XINC*1.0e9;
		pvPut(TIME);
		for (i=0;i<500;i++){
		 XAXIS[i] = i*XINC*1e9;
		 CH1_CURVE[i]=CH1_WF[i]*YMULT;
		}
		pvPut(XAXIS);
		pvPut(CH1_CURVE);
		} state wavcalc
	}
}


mailto: makoto.tobiyama@kek.jp
Last update: 12/Jul/2010