www.pudn.com > hezha.rar > Unit1.cpp, change:2006-06-28,size:19378b


//--------------------------------------------------------------------------- 
#include<math.h> 
#include<stdio.h> 
#include<malloc.h> 
#include <vcl.h> 
#pragma hdrstop 
#define DATALEN 512 
//#define STAGE 8 
#define app_level 6 
#define det_level 3 
#define stair 2//与断路器的类型有关 
 
#include "Unit1.h" 
//--------------------------------------------------------------------------- 
#pragma package(smart_init) 
#pragma resource "*.dfm" 
TForm1 *Form1; 
float far data[8][512];//数据数组    注意此处有改动 
float far dataex[8][512]; 
float h1[30];//滤波器系数 
float far hhh[512]; //存放H滤波器数值的指针   改为数组 
float far ggg[512]; // 存放G滤波器数值的指针  改为数组 
int filterlen;//滤波器长度 
//--------------------------------------------------------------------------- 
__fastcall TForm1::TForm1(TComponent* Owner) 
        : TForm(Owner) 
{ 
} 
//--------------------------------------------------------------------------- 
 
void __fastcall TForm1::FormClick(TObject *Sender) 
{ 
  void gethdata() ; 
  void getdata1(int DATA); 
  void getdata2(int DATA); 
  float hh(int i,int m) ; 
  float gg(int i,int m); 
  void hhh1(int level); 
  void ggg1(int level); 
  void codeh(int level,int flen,int start,int len); 
  void codeg(int level,int flen,int start,int len); 
  void stagek2(int level,int flen ) ; 
  void de2composek(int STAGE);  //已删除原程序中的量化部分 
 //声明所有正变换函数 
 void chuli(float); 
 void codehrec(int level,int flen,int len); 
 void codegrec(int level,int flen,int len); 
 void rec(int STAGE); 
 int tipan(int ,int ,int ); 
 int Maxzhi(int ,int,int ); 
   
//-------------对行程信号进行小波滤波并输出滤波后的信号------------------------- 
 
  int i ,STAGE1=3; 
  int j,k,LX,level,KL; 
  gethdata(); 
  getdata2(DATALEN); 
  de2composek(STAGE1);//小波变换部分 
  for(level=STAGE1-1;level>0;level--) 
  { 
    LX=DATALEN/pow(2,level); 
     for(i=0;i<pow(2,9-level);i++) 
       data[level][LX+i]=0; 
  } 
  rec(STAGE1); 
  Canvas->MoveTo(20,0); 
  Canvas->LineTo(20,160); 
  Canvas->LineTo(532,160);//画出坐标轴; 
  for(i=0;i<=10;i++) 
  Canvas->TextOut(20+50*i,162,50*i); 
  for(i=1;i<=8;i++) 
  Canvas->TextOut(0,160-20*i,20*i); //标出横纵刻度; 
  float x1[512],p_x1,p_y1; 
  for(i=0;i<512;i++) 
    x1[i]=i; 
    Canvas->MoveTo(20,160); 
    for(i=0;i<512;i++) 
    { 
      p_x1=x1[i]+20; 
      p_y1=-data[0][i]+160; //坐标转换; 
      Canvas->LineTo(p_x1,p_y1); 
    }   //画出滤波后行程信号波形 
    float y[512];//将行程信号存在y数组中 
    for(i=0;i<512;i++) 
    y[i]=data[0][i]; 
 
//------------对电流信号进行小波滤波并输出滤波后的信号-------------------------- 
getdata1(DATALEN); 
de2composek(STAGE1);//小波变换部分 
for(level=STAGE1-1;level>0;level--) 
  { 
    LX=DATALEN/pow(2,level); 
     for(i=0;i<pow(2,9-level);i++) 
       data[level][LX+i]=0; 
  } 
  rec(STAGE1); 
 Canvas->MoveTo(20,180); 
  Canvas->LineTo(20,340); 
  Canvas->LineTo(532,340);//画出坐标轴; 
  for(i=0;i<=10;i++) 
  Canvas->TextOut(20+50*i,342,50*i); 
  for(i=1;i<=8;i++) 
  Canvas->TextOut(0,340-20*i,20*i); //标出横纵刻度; 
  float x2[512],p_x2,p_y2; 
  for(i=0;i<512;i++) 
    x2[i]=i; 
    Canvas->MoveTo(20,340); 
    for(i=0;i<512;i++) 
    { 
      p_x2=x2[i]+20; 
      p_y2=-data[0][i]+340; //坐标转换; 
      Canvas->LineTo(p_x2,p_y2); 
    }   //画出滤波后电流信号波形 
//------------------------------------------------------------------------------ 
 Canvas->MoveTo(20,360); 
  Canvas->LineTo(20,400); 
  Canvas->LineTo(532,400); 
  for(i=0;i<=5;i++) 
  Canvas->TextOut(20+100*i,402,100*i); 
  Canvas->TextOut(0,360,40); //标出横纵刻度; 
  float xa[512],p_xa; 
  float ya[512],p_ya; 
  FILE *fpa; 
  char *file_namea="e:/测试信号/A相.txt"; 
  fpa=fopen(file_namea,"r"); 
  for(i=0;i<512;i++) 
  fscanf(fpa,"%f",&ya[i]); 
  fclose(fpa); //从文件中读出数据,将其赋给数组ya[]; 
  for(i=0;i<512;i++) 
    xa[i]=i; 
    Canvas->MoveTo(20,400); 
    for(i=0;i<512;i++) 
    { 
      p_xa=xa[i]+20; 
      p_ya=-ya[i]+400; //坐标转换; 
      Canvas->LineTo(p_xa,p_ya); 
    } 
 
  Canvas->MoveTo(20,410); 
  Canvas->LineTo(20,450); 
  Canvas->LineTo(532,450); // 画出B相端口信号 
  for(i=0;i<=5;i++) 
  Canvas->TextOut(20+100*i,452,100*i); 
  Canvas->TextOut(0,410,40); //标出横纵刻度; 
  float xb[512],p_xb; 
  float yb[512],p_yb; 
  FILE *fpb; 
  char *file_nameb="e:/测试信号/B相.txt"; 
  fpb=fopen(file_nameb,"r"); 
  for(i=0;i<512;i++) 
  fscanf(fpb,"%f",&yb[i]); 
  fclose(fpb); //从文件中读出数据,将其赋给数组yb[]; 
  for(i=0;i<512;i++) 
    xb[i]=i; 
    Canvas->MoveTo(20,450); 
    for(i=0;i<512;i++) 
    { 
      p_xb=xb[i]+20; 
      p_yb=-yb[i]+450; //坐标转换; 
      Canvas->LineTo(p_xb,p_yb); 
    } 
  Canvas->MoveTo(20,460); 
  Canvas->LineTo(20,500); 
  Canvas->LineTo(532,500); // 画出C相端口信号 
  for(i=0;i<=5;i++) 
  Canvas->TextOut(20+100*i,502,100*i); 
  Canvas->TextOut(0,460,40); //标出横纵刻度; 
  float xc[512],p_xc; 
  float yc[512],p_yc; 
  FILE *fpc; 
  char *file_namec="e:/测试信号/C相.txt"; 
  fpc=fopen(file_namec,"r"); 
  for(i=0;i<512;i++) 
  fscanf(fpc,"%f",&yc[i]); 
  fclose(fpc); //从文件中读出数据,将其赋给数组yc[]; 
  for(i=0;i<512;i++) 
    xc[i]=i; 
    Canvas->MoveTo(20,500); 
    for(i=0;i<512;i++) 
    { 
      p_xc=xc[i]+20; 
      p_yc=-yc[i]+500; //坐标转换; 
      Canvas->LineTo(p_xc,p_yc); 
    }    //画出A、B、C三相断口信号 
//------------------------------------------------------------------------------ 
//机械特性参数的提取 
//  int j,k,LX,level,KL; 
  int STAGE2=8; 
  getdata1(DATALEN); 
  de2composek(STAGE2);//小波变换部分 
   // 提取TIM3时间点(动触头开始运动的时刻) 
   int  PX1=DATALEN/pow(2,app_level); 
   int  PX2=DATALEN/pow(2,det_level); 
   int index1,INDEX,Y1,Y2,I2; 
   float tem1,tem2; 
   int TIM3; 
   tem1=data[app_level][0]; 
   for (i=1;i<PX1;i++) 
    { 
      if(data[app_level][i]>tem1) 
       tem1=data[app_level][i]; 
    } 
    for (i=0;i<PX1;i++) 
     { 
       if(data[app_level][i]==tem1) 
       break; 
      } 
   INDEX=i+2-stair; 
   Y1=(INDEX-1)*pow(2,(app_level-det_level)); 
   Y2=INDEX*pow(2,(app_level-det_level)); 
    tem2=fabs(dataex[det_level][Y1+PX2]); 
   for(i=Y1+PX2+1;i<=Y2-2+PX2;i++) 
   { 
     if(fabs(dataex[det_level][i])>tem2) 
     tem2=fabs(dataex[det_level][i]); 
     } 
     for(i=Y1+PX2;i<=Y2-2+PX2;i++) 
     { 
       if(fabs(dataex[det_level][i])==tem2) 
         break; 
       } 
     I2=i-PX2;   //注意:次数对应Matlab中的I2(end)+Y1-1; 
    TIM3=I2*pow(2,det_level); 
    // 找出电流启动点 
   int Initiate=4,Timex;//假设启动电流为4mA 
   Timex=tipan(0,TIM3,Initiate); 
   int Ta,Tb,Tc,max,min; //找出C相(例)速度参数 
   //找出前/后最大值,并计算出最低电压,假设稳态电压U=1.0V 
   int qianmax,houmax; 
   float Umin,U=1.0; 
   qianmax=Maxzhi(0,0,TIM3); 
   houmax=Maxzhi(0,TIM3,512); 
   Umin=data[0][qianmax]*U/data[0][houmax]; 
   float suduc=0.0,sudute=0.0,sudupj=0.0; 
    for(i=0;i<512;) 
    { 
       if(ya[i]!=0) 
        break; 
       else 
         i++; 
       } 
       Ta=i; 
    for(i=0;i<512;) 
    { 
       if(yb[i]!=0) 
        break; 
       else 
         i++; 
       } 
       Tb=i; 
      for(i=0;i<512;) 
    { 
       if(yc[i]!=0) 
        break; 
       else 
         i++; 
       } 
       Tc=i; 
       max=Ta; 
      if(Tb>Ta) 
           max=Tb; 
      if(Tc>max) 
            max=Tc; 
        min=Ta; 
      if(Tb<Ta) 
           min=Tb; 
      if(Tc<min) 
            min=Tc; 
     for(i=0;i<11;i++) //计算刚合速度 
      suduc+=y[Tc-5+i]; 
      suduc=suduc/11; 
    int TStart=120,TEnd=135; //计算指定时间段平均速度 
    for(i=TStart;i<=TEnd;i++) 
    sudute+=y[i]; 
    sudute=sudute/(TEnd-TStart); 
    for(i=TIM3;i<=Tc;i++) //计算合闸平均速度 
    sudupj+=y[i]; 
    sudupj=sudupj/(Tc-TIM3+1); 
 
   Canvas->MoveTo(Timex+20,340);//标出Timex线 
   Canvas->LineTo(Timex+20,10); 
   Canvas->MoveTo(qianmax+20,340);//标出qianmax线 
   Canvas->LineTo(qianmax+20,10); 
   Canvas->MoveTo(houmax+20,340);//标出houmax线 
   Canvas->LineTo(houmax+20,10); 
   Canvas->Pen->Color=clRed;  //在图中标出TIM3线 
   Canvas->MoveTo(TIM3+20,340); 
   Canvas->LineTo(TIM3+20,10); 
   Canvas->Pen->Color=clBlue;  //在图中标出Tc线 
   Canvas->MoveTo(Tc+20,500); 
   Canvas->LineTo(Tc+20,10); 
    Canvas->Pen->Color=clPurple; 
   Canvas->MoveTo(127,340); 
   Canvas->LineTo(127,10); 
 
 
    Edit1->Text=IntToStr(max-Timex)+"mS"; 
    Edit2->Text=IntToStr(max-min)+"mS"; 
    Edit3->Text=FloatToStr(suduc)+"m/s"; 
    Edit4->Text=FloatToStr(sudute)+"m/s"; 
    Edit5->Text=IntToStr(TIM3)+"mS"; 
    Edit6->Text=FloatToStr(sudupj)+"m/s"; 
    Edit7->Text=FloatToStr(y[511])+"mm"; 
    Edit8->Text=FloatToStr(y[511]-y[Tc])+"mm"; 
    Edit9->Text=FloatToStr(Umin)+"V"; 
} 
//--------------------------------------------------------------------------- 
 
void __fastcall TForm1::Button1Click(TObject *Sender) 
{ 
   Close(); 
} 
//--------------------------------------------------------------------------- 
 void gethdata()//得到H滤波器系数数据 
  { 
    int i,k=2; 
    filterlen=k; 
    float filter[2]={1/sqrt(2),1/sqrt(2)}; 
    for(i=0;i<30;i++) 
     h1[i]=0; 
    for(i=0;i<filterlen;i++) 
     h1[i]=filter[i]; 
     } 
     void getdata1(int DATA)//读取数据 
     { 
      int i; 
      float temp; 
      FILE *fp; 
      for(i=0;i<DATALEN;i++) 
      data[0][i]=0; 
      char *file_name="e:/测试信号/dianliuzs.txt"; 
      fp=fopen(file_name,"r"); 
      for(i=0;i<DATA;i++) 
        { 
          fscanf(fp,"%f", &temp); 
          data[0][i]=temp; 
          } 
         fclose(fp); 
      } 
       void getdata2(int DATA)//读取数据 
     { 
      int i; 
      float temp; 
      FILE *fp; 
      for(i=0;i<DATALEN;i++) 
      data[0][i]=0; 
      char *file_name="e:/测试信号/xchengzs.txt"; 
      fp=fopen(file_name,"r"); 
      for(i=0;i<DATA;i++) 
        { 
          fscanf(fp,"%f", &temp); 
          data[0][i]=temp; 
          } 
         fclose(fp); 
      } 
 
 
      float hh(int i,int m) //返回H算子周期化后的值 
      { 
        return h1[i+m]; 
        } 
      float gg(int i,int m)//返回G算子周期化后的值 
      { 
        char  flag; 
       if(i%2) flag=-1; 
       else flag=1; 
       return  hh(-1*i+1,m)*flag; 
       } 
       void hhh1(int level)//H算子周期化过程 
       { 
         int len,m,i,sign; 
         float temp; 
         len=DATALEN>>level; 
         m=filterlen/2; 
          for(sign=0;sign<len;sign++) 
          { 
           temp=0; 
           for(i=(-1*m+1);i<=m;i++) 
           { 
             if(((sign-i)%len)==0) 
             temp+=hh(i-1,m); 
             } 
             hhh[sign]=temp; 
             } 
           } 
         void ggg1(int level)//G算子周期化过程 
         { 
           int len,m,i,sign; 
           float temp; 
           len=DATALEN>>level; 
           m=filterlen/2; 
            for(sign=0;sign<len;sign++) 
             { 
               temp=0; 
               for(i=(-1*m+3);i<=(m+2);i++) 
                { 
                 if(((sign-i)%len)==0) 
                 temp+=gg(i-1,m); 
                 } 
                 ggg[sign]=temp; 
                 } 
             } 
           void codeh(int level,int flen,int len)//H算子作用 
           { 
             int i,i2,k,m,len1; 
             float temp1; 
             m=filterlen/2; 
             len1=len>>1; 
             for(i=0;i<len1;i++) 
             { 
              i2=2*i; 
              data[level][i]=0; 
              if(len>flen) 
               { 
                 for(k=i2-len;k<=i2-len+m;k++) 
                 { 
                   if((k>=0)&&(k<len)) 
                   if((temp1=hhh[(k-i2+len)%len])!=0) 
                     data[level][i]+=temp1*data[level-1][k]; 
                  } 
                  for(k=i2-m+1;k<=i2+m;k++) 
                  { 
                    if((k>=0)&&(k<len)) 
                    if((temp1=hhh[(k-i2+len)%len])!=0) 
                     data[level][i]+=temp1*data[level-1][k]; 
                     } 
                    for(k=i2+len-m+1;k<i2+len;k++) 
                    { 
                      if((k>=0)&&(k<len)) 
                      if((temp1=hhh[(k-i2+len)%len])!=0) 
                      data[level][i]+=temp1*data[level-1][k]; 
                      } 
                    } 
                  else 
                    { 
                      for(k=0;k<len;k++) 
                      { 
                       if((temp1=hhh[(k-i2+len)%len])!=0) 
                        data[level][i]+=temp1*data[level-1][k]; 
                        } 
                      } 
                    } 
                  } 
             void codeg(int level,int flen,int len)//G算子作用 
             { 
               int i,i2,k,m,len1; 
               float temp1,exchange ; 
               len1=len>>1; 
               m=flen/2; 
               for(i=0;i<len1;i++) 
               { 
                i2=2*i; 
                data[level][i+len1]=0; 
                if(len>flen) 
                 { 
                  for(k=i2-len;k<=i2-len+m+2;k++) 
                  { 
                    if((k>=0)&&(k<len)) 
                    if((temp1=ggg[(k-i2+len)%len])!=0) 
                    data[level][i+len1]+=temp1*data[level-1][k]; 
                    } 
                  for(k=i2-m+3;k<=i2+m+2;k++) 
                  { 
                    if((k>=0)&&(k<len)) 
                    if((temp1=ggg[(k-i2+len)%len])!=0) 
                    data[level][i+len1]+=temp1*data[level-1][k]; 
                    } 
 
                   for(k=i2+len-m+3;k<i2+len;k++) 
                  { 
                    if((k>=0)&&(k<len)) 
                    if((temp1=ggg[(k-i2+len)%len])!=0) 
                    data[level][i+len1]+=temp1*data[level-1][k]; 
                    } 
                    } 
 
                  else 
                   { 
                     for(k=0;k<len;k++) 
                    { 
                    if((temp1=ggg[(k-i2+len)%len])!=0) 
                    data[level][i+len1]+=temp1*data[level-1][k]; 
                    } 
                  } 
                } 
                 //??此部分程序是为了对应Matlab的输出结果,将首末互换,加负号 
                for(i=len1+1;i<2*len1;i++) 
                dataex[level][i]=data[level][i-1]; 
                dataex[level][len1]=data[level][2*len1-1]; 
                for(i=len1;i<2*len1;i++) 
                dataex[level][i]=-dataex[level][i]; 
             } 
 
             void stagek2(int level,int flen) 
             { 
               int i,j,len,start; 
               len=DATALEN>>(level-1); 
               hhh1(level-1); 
               ggg1(level-1); 
               codeh(level,flen,len); 
               codeg(level,flen,len); 
               } 
                void de2composek(int STAGE)//小波分解过程 
                { 
                  int i; 
                  for(i=1;i<STAGE;i++) 
                  stagek2(i,filterlen); 
 
                  } 
//以下部分为小波反变换部分 
 
      void codehrec(int level,int flen,int len)//H*算子作用 
           { 
             int i,k,k1,m,len1; 
             float temp1; 
             len1=len>>1; 
             m=flen/2; 
             for(i=0;i<len;i++) 
             { 
              data[level-1][i]=0; 
              if(len>flen) 
               { 
                 for(k1=(i-m);k1<(m+i);k1++) 
                 { 
                   k=k1/2; 
                   if((k>=0)&&(k<len1)&&((k1%2)==0)) 
                   if((temp1=hhh[(i-2*k+len)%len])!=0) 
                     data[level-1][i]+=temp1*data[level][k]; 
                  } 
                  for(k1=(len+i-m);k1<=(len+i);k1++) 
                  { 
                    k=k1/2; 
                   if((k>=0)&&(k<len1)&&((k1%2)==0)) 
                   if((temp1=hhh[(i-2*k+len)%len])!=0) 
                     data[level-1][i]+=temp1*data[level][k]; 
 
                     } 
                    for(k1=(i-len)+1;k1<(i-len+m);k1++) 
                    { 
                     k=k1/2; 
                     if((k>=0)&&(k<len1)&&((k1%2)==0)) 
                     if((temp1=hhh[(i-2*k+len)%len])!=0) 
                     data[level-1][i]+=temp1*data[level][k]; 
                      } 
                    } 
                  else 
                    { 
                      for(k=0;k<len1;k++) 
                      { 
                       if((temp1=hhh[(i-2*k+len)%len])!=0) 
                        data[level-1][i]+=temp1*data[level][k]; 
                        } 
                      } 
                    } 
                  } 
            void codegrec(int level,int flen,int len)//G*算子作用 
           { 
             int i,k,k1,m,len1; 
             float temp1; 
             len1=len>>1; 
             m=flen/2; 
             for(i=0;i<len;i++) 
             { 
              if(len>flen) 
               { 
                 for(k1=(len+i-m-2);k1<(len+i);k1++) 
                 { 
                   k=k1/2; 
                   if((k>=0)&&(k<len1)&&((k1%2)==0)) 
                   if((temp1=ggg[(i-2*k+len)%len])!=0) 
                     data[level-1][i]+=temp1*data[level][k+len1]; 
                  } 
                  for(k1=(i-m-2);k1<(i+m-2);k1++) 
                  { 
                    k=k1/2; 
                   if((k>=0)&&(k<len1)&&((k1%2)==0)) 
                   if((temp1=ggg[(i-2*k+len)%len])!=0) 
                     data[level-1][i]+=temp1*data[level][k+len1]; 
 
                     } 
                    for(k1=i-len+1;k1<(i+m-len-2);k1++) 
                    { 
                     k=k1/2; 
                     if((k>=0)&&(k<len1)&&((k1%2)==0)) 
                     if((temp1=ggg[(i-2*k+len)%len])!=0) 
                     data[level-1][i]+=temp1*data[level][k+len1]; 
                      } 
                    } 
                  else 
                    { 
                      for(k=0;k<len1;k++) 
                      { 
                       if((temp1=ggg[(i-2*k+len)%len])!=0) 
                        data[level-1][i]+=temp1*data[level][k+len1]; 
                        } 
                      } 
                    } 
                  } 
       void rec(int STAGE)  //小波反变换 
      { 
        int level,len,len1,j; 
         for (level=STAGE-1;level>0;level--) 
         { 
           len=DATALEN>>(level-1); 
           hhh1(level-1); 
           ggg1(level-1); 
           codehrec(level,filterlen,len); 
           codegrec(level,filterlen,len); 
           } 
         } 
 
 
      int tipan(int Start,int End, int  Yuzhi) 
      { 
          int i,Ty=0; 
          float index; 
           for(i=Start;i<End;) 
           { 
              if (data[0][i]>=Yuzhi) 
            { 
               index=i; 
              break; 
             } 
             i++; 
            } 
        for(i=index+1;i<=index+10;i++) 
          { 
             if(data[0][i]>Yuzhi) 
              continue; 
              else 
               { 
                 Ty=i; 
                  break ; 
                  } 
             } 
        if(Ty!=0) 
         tipan(Ty,End,Yuzhi); 
        else 
         return (index); 
         } 
   int Maxzhi(int level,int Start,int End) 
   { 
    float zhongjie; 
    int i; 
    zhongjie=data[level][Start]; 
    for (i=Start+1;i<End;i++) 
    { 
      if(data[level][i]>zhongjie) 
       zhongjie=data[level][i]; 
     } 
    for (i=Start;i<End;i++) 
     { 
       if(data[level][i]==zhongjie) 
       break; 
       } 
     return(i) ; 
     }