/*****************************************************************************
// Labwindows/cvi8.5 低通滤波器
// 改写自 Labwindows 自带的例程
// szlihongtao
// 2010-07-29
/*****************************************************************************
#include <cvirte.h> /* Needed if linking in external compiler; harmless otherwise */
#include <userint.h>
#include <math.h>
#include <analysis.h>
#include <ansi_c.h>
#include "sample3.h"
//*****************************************************************************
static int panelHandle;
static int typenn;
int num,status,seed;
//*****************************************************************************
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, "sample3.uir", PANEL)) < 0)
return -1;
DisplayPanel (panelHandle);
RunUserInterface ();
return 0;
}
//*****************************************************************************
int CVICALLBACK Filtertype (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
unsigned int f_disp0,f_disp1;
switch (event)
{
case EVENT_COMMIT:
{
GetCtrlVal (panelHandle, PANEL_RING, &typenn); // 选择滤波器类型
f_disp0=0;
f_disp1=0;
if (typenn==0)
{
f_disp0=1;
f_disp1=1;
}
else if (typenn==1)
{
f_disp0=0;
f_disp1=1;
}
else ;
SetCtrlAttribute (panelHandle, PANEL_ATTENUATION, ATTR_DIMMED, f_disp0);
SetCtrlAttribute (panelHandle, PANEL_RIPPLE, ATTR_DIMMED, f_disp1);
}
}
return 0;
}
//*****************************************************************************
int CVICALLBACK Tuichu (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
QuitUserInterface (0);
break;
}
return 0;
}
//*****************************************************************************
int CVICALLBACK Zhixing (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
//定义变量,分别表示滤波器纹波系数、衰减系数、截止频率,数组个数
int order;
double ripple,attenuation,samplefrequency,cutofffrequency;
int n=400;
//定义数组,x表示滤波前数组,y表示滤波后数组、datapoints表示白噪声干扰,numpoints表示正弦信号
double x[500],y[500],datapoints[500],numpoints[500];
switch (event)
{
case EVENT_COMMIT:
{
GetCtrlVal (panelHandle, PANEL_RING, &typenn); // 选择滤波器类型
WhiteNoise(n,0.2,++seed,datapoints); // 产生白噪声干扰信号
SinePattern(n,1.0,0.0,3.0,numpoints); // 产生正弦信号
/*
n=400
3个完整的周波,每个周波占用133.33点,
当取样频率为12KHz时候,可以理解为输入信号的频率为90Hz
*/
Add1D (datapoints, numpoints, n, x); // 白噪声和正弦信号迭加
// x表示滤波前数组
DeleteGraphPlot (panelHandle, PANEL_GRAPH1, -1, VAL_DELAYED_DRAW);
PlotY (panelHandle, PANEL_GRAPH1, x, n, VAL_DOUBLE, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1,VAL_BLACK);
GetCtrlVal (PANEL, PANEL_RIPPLE, &ripple); //获取滤波器波纹系数
GetCtrlVal (PANEL, PANEL_ATTENUATION, &attenuation);//获取滤波器衰减系数
GetCtrlVal (PANEL, PANEL_ORDER, &order); //获取滤波器阶数
GetCtrlVal (PANEL, PANEL_SAMPLEFRE, &samplefrequency);//获取采样频率
GetCtrlVal (PANEL, PANEL_CUTOFF, &cutofffrequency);//获取滤波器上限频率
/*
可以直接理解为RC滤波
*/
if (typenn==0)
{
//巴特沃斯低通滤波
Bw_LPF(x,n,samplefrequency,cutofffrequency,order,y);
}
if (typenn==1)
{
//切比雪夫低通滤波
Ch_LPF(x,n,samplefrequency,cutofffrequency,ripple,order,y);
}
if (typenn==2)
{
//椭圆低通滤波
Elp_LPF(x,n,samplefrequency,cutofffrequency,ripple,attenuation,order,y);
}
/*
3dB衰减,峰值变为转换以前的0.707
*/
DeleteGraphPlot (panelHandle, PANEL_GRAPH2, -1, VAL_DELAYED_DRAW);
PlotY (panelHandle, PANEL_GRAPH2, y, n, VAL_DOUBLE, VAL_THIN_LINE, VAL_NO_POINT, VAL_SOLID, 1, VAL_RED);
break;
}
}
return 0;
}
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
用户304799 2013-7-6 08:09