原创 Labwindows/cvi8.5学习日志(42)----谐波分析

2010-8-2 20:47 2315 8 8 分类: 软件与OS

417616c8-d551-4ad8-a5a7-45d4c52f4a5f.jpg


//*****************************************************************************
// Labwindows/cvi8.5  虚拟简易示波器
// szlihongtao
// 2010-08-02
//***************************************************************************** 
#include <utility.h>
#include <ansi_c.h>
#include <analysis.h>
#include <cvirte.h>
#include <userint.h>
#include "sample12.h"
//**********************************************************************************************
#define   NUM_CYC   2      // 2个周波
#define   SAM_CYC   128      // 每个周波占用128点
#define   SAM_TOTAL   (NUM_CYC*SAM_CYC) // 总的采样点数
//**********************************************************************************************
 static int panelHandle;
 static double wave_v[SAM_TOTAL], wave_i[SAM_TOTAL];


 static double Amp_U1,Amp_U2,Amp_U3,Amp_U4,Amp_U5;
 static double Amp_I1,Amp_I2,Amp_I3,Amp_I4,Amp_I5;


 static double Phase_U1,Phase_U2,Phase_U3,Phase_U4,Phase_U5;
 static double Phase_I1,Phase_I2,Phase_I3,Phase_I4,Phase_I5;


 static int samples=SAM_TOTAL;    // 总点数


 static double Img_v[SAM_TOTAL],Phase_v[SAM_TOTAL],Mag_v[SAM_TOTAL];
 static double Img_i[SAM_TOTAL],Phase_i[SAM_TOTAL],Mag_i[SAM_TOTAL];
//**********************************************************************************************
int main (int argc, char *argv[])
{
 if (InitCVIRTE (0, argv, 0) == 0)


  return -1; /* out of memory */
 if ((panelHandle = LoadPanel (0, "sample12.uir", PANEL)) < 0)
  return -1;
 DisplayPanel (panelHandle);
 RunUserInterface ();
 DiscardPanel (panelHandle);
 return 0;
}
//**********************************************************************************************
int CVICALLBACK GenerateWave (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 double f,wave_temp[SAM_TOTAL];


 switch (event)
 {
  case EVENT_COMMIT:
   GetCtrlVal (panelHandle,PANEL_AMP_V1 ,&Amp_U1);
   GetCtrlVal (panelHandle,PANEL_AMP_V2 ,&Amp_U2);
   GetCtrlVal (panelHandle,PANEL_AMP_V3 ,&Amp_U3);
   GetCtrlVal (panelHandle,PANEL_AMP_V4 ,&Amp_U4);
   GetCtrlVal (panelHandle,PANEL_AMP_V5 ,&Amp_U5);


   GetCtrlVal (panelHandle,PANEL_AMP_I1 ,&Amp_I1);
   GetCtrlVal (panelHandle,PANEL_AMP_I2 ,&Amp_I2);
   GetCtrlVal (panelHandle,PANEL_AMP_I3 ,&Amp_I3);
   GetCtrlVal (panelHandle,PANEL_AMP_I4 ,&Amp_I4);
   GetCtrlVal (panelHandle,PANEL_AMP_I5 ,&Amp_I5);


   GetCtrlVal (panelHandle,PANEL_PHASE_V1 ,&Phase_U1);
   GetCtrlVal (panelHandle,PANEL_PHASE_V2 ,&Phase_U2);
   GetCtrlVal (panelHandle,PANEL_PHASE_V3 ,&Phase_U3);
   GetCtrlVal (panelHandle,PANEL_PHASE_V4 ,&Phase_U4);
   GetCtrlVal (panelHandle,PANEL_PHASE_V5 ,&Phase_U5);


   GetCtrlVal (panelHandle,PANEL_PHASE_I1 ,&Phase_I1);
   GetCtrlVal (panelHandle,PANEL_PHASE_I2 ,&Phase_I2);
   GetCtrlVal (panelHandle,PANEL_PHASE_I3 ,&Phase_I3);
   GetCtrlVal (panelHandle,PANEL_PHASE_I4 ,&Phase_I4);
   GetCtrlVal (panelHandle,PANEL_PHASE_I5 ,&Phase_I5);
//--------------------------------------------------------------------------------
      f=1.0/SAM_CYC;


      /* 产生电压正弦波曲线函数*/
      SineWave (samples, Amp_U1,  f,  &Phase_U1, wave_v);// 基波数据


      SineWave (samples, Amp_U2,  2*f,  &Phase_U2, wave_temp);
      Add1D (wave_v,wave_temp,samples,wave_v) ;


      SineWave (samples, Amp_U3,  3*f,  &Phase_U3, wave_temp);
      Add1D (wave_v,wave_temp,samples,wave_v) ;


      SineWave (samples, Amp_U4,  4*f,  &Phase_U4, wave_temp);
      Add1D (wave_v,wave_temp,samples,wave_v) ;


      SineWave (samples, Amp_U5,  5*f,  &Phase_U5, wave_temp);
      Add1D (wave_v,wave_temp,samples,wave_v) ;
//--------------------------------------------------------------------------------
      DeleteGraphPlot (panelHandle, PANEL_GRAPH, -1, VAL_IMMEDIATE_DRAW);
      PlotY (panelHandle, PANEL_GRAPH, wave_v, samples , VAL_DOUBLE,
       VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_BLACK);
//---------------------------------------------------------------------------------------------
       /* 产生电流正旋曲线函数*/
      SineWave (samples, Amp_I1,  f,  &Phase_I1, wave_i);// 基波数据


      SineWave (samples, Amp_I2,  2*f,  &Phase_I2, wave_temp);
      Add1D (wave_i,wave_temp,samples,wave_i) ;


      SineWave (samples, Amp_I3,  3*f,  &Phase_I3, wave_temp);
      Add1D (wave_i,wave_temp,samples,wave_i) ;


      SineWave (samples, Amp_I4,  4*f,  &Phase_I4, wave_temp);
      Add1D (wave_i,wave_temp,samples,wave_i) ;


      SineWave (samples, Amp_I5,  5*f,  &Phase_I5, wave_temp);
      Add1D (wave_i,wave_temp,samples,wave_i) ;
//--------------------------------------------------------------------------------
      PlotY (panelHandle, PANEL_GRAPH, wave_i, samples , VAL_DOUBLE,
      VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED);


   SetCtrlAttribute (panelHandle,PANEL_PLOT ,ATTR_DIMMED,1);
   SetCtrlAttribute (panelHandle,PANEL_COMMANDBUTTON ,ATTR_DIMMED,0);
     break;
  }
 return 0;
}
//**********************************************************************************************
double get_amp(double in)
{
 return (in*2.0/samples);
}
//**********************************************************************************************
double get_phase(double in)
{
 return (in*180.0/3.1415926+90);
}
//**********************************************************************************************
/*电压和电流谐波分析*/
//**********************************************************************************************
int CVICALLBACK Analysis (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 int i ;
 double amp,phase;


 switch (event)
 {
  case EVENT_COMMIT:
  {
       for (i=0;i<samples;i++)        // 虚部清0
        Img_v=0;


     FFT (wave_v,Img_v,samples);      // FFT计算
    ToPolar1D(wave_v,Img_v,samples, Mag_v,Phase_v);  // 极坐标转换,Mag-幅值,Phase-相位


//---------------------------------------------------------------------------------------------
      amp=get_amp(Mag_v[NUM_CYC*1]);      // 计算幅值
      SetCtrlVal(panelHandle,PANEL_AMP_V1_2,amp);
   
      amp=get_amp(Mag_v[NUM_CYC*2]);
      SetCtrlVal(panelHandle,PANEL_AMP_V2_2,amp);
   
      amp=get_amp(Mag_v[NUM_CYC*3]);
      SetCtrlVal(panelHandle,PANEL_AMP_V3_2,amp);
   
      amp=get_amp(Mag_v[NUM_CYC*4]);
      SetCtrlVal(panelHandle,PANEL_AMP_V4_2,amp);
   
      amp=get_amp(Mag_v[NUM_CYC*5]);
      SetCtrlVal(panelHandle,PANEL_AMP_V5_2,amp);
//---------------------------------------------------------------------------------------------
      phase=get_phase(Phase_v[NUM_CYC*1]);      // 计算相位   
      SetCtrlVal(panelHandle,PANEL_PHASE_V1_2,phase);  // 显示范围为-90-----270
   
      phase=get_phase(Phase_v[NUM_CYC*2]);
      SetCtrlVal(panelHandle,PANEL_PHASE_V2_2,phase);
   
      phase=get_phase(Phase_v[NUM_CYC*3]);
      SetCtrlVal(panelHandle,PANEL_PHASE_V3_2,phase);
   
      phase=get_phase(Phase_v[NUM_CYC*4]);
      SetCtrlVal(panelHandle,PANEL_PHASE_V4_2,phase);
   
      phase=get_phase(Phase_v[NUM_CYC*5]);
      SetCtrlVal(panelHandle,PANEL_PHASE_V5_2,phase);
//---------------------------------------------------------------------------------------------   
   
    for (i=0;i<samples;i++)        // 虚部清0
            Img_i=0;


       FFT (wave_i,Img_i,samples);    // FFT计算
       ToPolar1D (wave_i,Img_i,samples,Mag_i,Phase_i);
 //---------------------------------------------------------------------------------------------
      amp=get_amp(Mag_i[NUM_CYC*1]);       // 计算幅值        
      SetCtrlVal(panelHandle,PANEL_AMP_I1_2,amp);
   
      amp=get_amp(Mag_i[NUM_CYC*2]);
      SetCtrlVal(panelHandle,PANEL_AMP_I2_2,amp);
   
      amp=get_amp(Mag_i[NUM_CYC*3]);
      SetCtrlVal(panelHandle,PANEL_AMP_I3_2,amp);
   
      amp=get_amp(Mag_i[NUM_CYC*4]);
      SetCtrlVal(panelHandle,PANEL_AMP_I4_2,amp);
   
      amp=get_amp(Mag_i[NUM_CYC*5]);
      SetCtrlVal(panelHandle,PANEL_AMP_I5_2,amp);
//---------------------------------------------------------------------------------------------
      phase=get_phase(Phase_i[NUM_CYC*1]);       // 计算相位  
      SetCtrlVal(panelHandle,PANEL_PHASE_I1_2,phase);
   
      phase=get_phase(Phase_i[NUM_CYC*2]);
      SetCtrlVal(panelHandle,PANEL_PHASE_I2_2,phase);
   
      phase=get_phase(Phase_i[NUM_CYC*3]);
      SetCtrlVal(panelHandle,PANEL_PHASE_I3_2,phase);
   
      phase=get_phase(Phase_i[NUM_CYC*4]);
      SetCtrlVal(panelHandle,PANEL_PHASE_I4_2,phase);
   
      phase=get_phase(Phase_i[NUM_CYC*5]);
      SetCtrlVal(panelHandle,PANEL_PHASE_I5_2,phase);


   SetCtrlAttribute (panelHandle,PANEL_PLOT ,ATTR_DIMMED,0);
   SetCtrlAttribute (panelHandle,PANEL_COMMANDBUTTON ,ATTR_DIMMED,1);


      break;
     }
 }
 return 0;
}
 //**********************************************************************************************
int CVICALLBACK Close (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
   QuitUserInterface (0);
   break;
 }
 return 0;
}
//**********************************************************************************************
//**********************************************************************************************
//**********************************************************************************************


 


 


 


 


 


 


 


 


 


 

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
8
关闭 站长推荐上一条 /3 下一条