电子大神的日记本,供应链专家的功夫茶盘,在这里记录、分享与共鸣。

登录以开始

Labwindows/cvi8.5学习日志(56)----任意波形发生器

//******************************************************************************
// Labwindows/cvi8.5  任意波形生成器
// 练习了各种信号发生函数的使用方法
// szlihongtao
// 2010-09-08
// 参照 的<基于Labwindows/cvi的虚拟仪器设计与应用> page103
// 稍有改写
//******************************************************************************
#include <cvirte.h>  /* Needed if linking in external compiler; harmless otherwise */
#include <userint.h>
#include "sample1.h"   
#include <analysis.h>
//******************************************************************************
static int panelHandle,type=100,index=50,delay=30,width=10,seed=20;
static int n=2000,tablesize=100,interp,typenn;
double phase,waveTable[100], x[2000];
double amphase=1.0,f=0.001,amp=1.0,first=1.0,last=100.0;
//******************************************************************************
int main (int argc, char *argv[])
{
 if (InitCVIRTE (0, argv, 0) == 0) /* Needed if linking in external compiler; harmless otherwise */
  return -1; /* out of memory */
 if ((panelHandle = LoadPanel (0, "sample1.uir", PANEL)) < 0)
  return -1;
 DisplayPanel (panelHandle);
 RunUserInterface ();
 return 0;
}
//******************************************************************************
//启动开始按钮
int CVICALLBACK Kaishi (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
  {
   GetCtrlVal (panelHandle, PANEL_RING, &typenn);
            switch (typenn)
            {
                case 0:                  //正弦信号
                {
                    phase=0;
     
                    #if 0
                     SinePattern(type,amp,phase,1.5,waveTable);  // 1.5个周波
                    #else
      SineWave (type, amp, 1/66.6667, &phase, waveTable); // 每个周期占用66.6667点
                 #endif
                 // 以上2种语句写法实现的效果是一样的!

                 DeleteGraphPlot (panelHandle, PANEL_GRAPH, -1, VAL_DELAYED_DRAW);
     PlotY (panelHandle, PANEL_GRAPH, waveTable, type, VAL_DOUBLE,
        VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_BLACK);

     #if 1
           //获取相位信息
         GetCtrlVal (panelHandle, PANEL_ACTIVE, &phase);
         //获取插值方式
         GetCtrlVal (panelHandle, PANEL_INTERP, &interp);
         //任意波形函数

         n=500;   // 总共280个点
         amphase=3.66; // 峰值
            tablesize=67; // waveTable为存放单个周期内等间隔采样值的数值
             //  根据以上的语句,每个标准周波占用66.6667点
         f=1.0/250;    // 变化后的正弦波,每个周期占用250点
             // 所以这种现实500/250=2个周波

      //f=1.0/186; // 变化后的正弦波,每个周期占用186点
             // 所以这种现实280/186=1.51个周波
         ArbitraryWave(n,amphase,f,&phase,waveTable,tablesize,interp,x);
      
         DeleteGraphPlot (panelHandle, PANEL_GRAPH, -1, VAL_DELAYED_DRAW);
      
         //绘制图形
      PlotY (panelHandle, PANEL_GRAPH, x, n, VAL_DOUBLE,
          VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED);
     #endif
     return 0;
                    break;
    }
                case 1:                     //冲击信号
                {
     // 这个信号作为任意波信号函数的采样样本
                    Impulse(type,amp,50,waveTable);  // 数组程度type=100,,产生冲击信号的时刻为50
     
     n=1000;
     f=1/500.0;    // 产生2个脉冲,500个点为一个周期
                 break;
    }
                case 2:                    //脉冲信号
                {
     // 这个信号作为任意波信号函数的采样样本
                    Pulse(type,amp,delay,width,waveTable);
     
     n=200;
     f=1/100.0;    // 产生2个脉冲
                    break;
    }
                case 3:                     // 斜坡信号
                {
                 Ramp(100,first,last,waveTable);
     n=2000;
     f=1/200.0;    // 产生10个信号
                    break;
    }
                case 4:                     //均匀噪声信号
                     Uniform(type,seed,waveTable);
                    break;
                case 5:                     //高斯噪声信号
                    GaussNoise(type,0.1,seed,waveTable);
                    break;
                case 6:                    //锯齿波信号
                    GetCtrlVal (panelHandle, PANEL_ACTIVE, &phase);
                    SawtoothWave(type,amp,0.02,&phase,waveTable);
                    /*
                     f=0.02,采样频率为1/0.02=50,即每个周期占用50个点,
                     type=100,即数组包含了2个完整的波形
                     PlotY函数包含了2个waveTable,几最终的显示会有4个完整的锯齿波
                    */
                    break;
                case 7:                     // 三角波信号
                {
                  Triangle(type,amp,waveTable);
                    break;
    }
                case 8:                     // 方波信号
                {
                    GetCtrlVal (panelHandle, PANEL_ACTIVE, &phase);
     SquareWave (type, amp, 1.0/50, &phase, 20, waveTable);

                    /*
                     采样频率为1.0/50,即每个周期占用50个点,
                     type=100,即数组包含了2个完整的波形
                     PlotY函数包含了2个waveTable,最终的显示会有4个完整的锯齿波
                    */
                    break;
    }
   }

   
       GetCtrlVal (panelHandle, PANEL_ACTIVE, &phase);    //获取相位信息  
       GetCtrlVal (panelHandle, PANEL_INTERP, &interp);    //获取插值方式   
      
       ArbitraryWave(n,amphase,f,&phase,waveTable,tablesize,interp,x);//任意波形函数 
       DeleteGraphPlot (panelHandle, PANEL_GRAPH, -1, VAL_DELAYED_DRAW);
                    
    PlotY (panelHandle, PANEL_GRAPH, x, n, VAL_DOUBLE,
        VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED);  //绘制图形    
   break;
  }
  case EVENT_RIGHT_CLICK:
   break;
 }
 return 0;
}
//******************************************************************************
//启动结束按钮
//******************************************************************************
int CVICALLBACK Finish (int panel, int control, int event,
  void *callbackData, int eventData1, int eventData2)
{
 switch (event)
 {
  case EVENT_COMMIT:
   QuitUserInterface (0);
   break;
  case EVENT_RIGHT_CLICK:
   break;
 }
 return 0;
}

//******************************************************************************
//******************************************************************************
//******************************************************************************

博主
sz_lihongtao
sz_lihongtao's Blog
sz_lihongtao
点击跳转