⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ffttestdlg.cpp

📁 vc和matlab混编,通过matcon调用matlab函数,使之能简单的完成复杂算法
💻 CPP
字号:
// FftTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FftTest.h"
#include "FftTestDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
#define ANALY_FFT   256

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFftTestDlg dialog

CFftTestDlg::CFftTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFftTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFftTestDlg)
	m_threshold = 0.0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFftTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFftTestDlg)
	DDX_Control(pDX, IDC_GRID, m_grid);
	DDX_Control(pDX, IDC_SLIDER1, m_slide);
	DDX_Text(pDX, IDC_THRESHOLD, m_threshold);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFftTestDlg, CDialog)
	//{{AFX_MSG_MAP(CFftTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_LOAD, OnBtnLoad)
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER1, OnReleasedcaptureSlider1)
	ON_BN_CLICKED(IDC_GRID, OnGrid)
	ON_BN_CLICKED(IDC_BTN_SMOOTH, OnBtnSmooth)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFftTestDlg message handlers

BOOL CFftTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_bGrid=true;
	m_grid.SetCheck(1);
	CRect Rect;
	m_hWndFft=GetDlgItem(IDC_FFT);
	MmAxisFft=winaxes(m_hWndFft->m_hWnd);
	m_hWndFft->GetClientRect(Rect);
	axes(CL(MmAxisFft));
	axesposition(Rect.TopLeft().x,Rect.TopLeft().y,Rect.Width(),Rect.Height());
	set(MmAxisFft,(CL(TM("Color")),TM("black")));
	title((CL(TM("TCD频谱分析"))));
	
	m_slide.SetLineSize(20);
	m_slide.SetRange(0,650,TRUE);
	m_slide.SetTicFreq(1);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CFftTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CFftTestDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CFftTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

static char   BASED_CODE szFilter[] ="TCD数据(*.tcd)|*.tcd||";
unsigned char /*nData[256],*/nTempData;
int nData[256];
int		  nCur;
void CFftTestDlg::OnBtnLoad() 
{
	// TODO: Add your control notification handler code here
	CFileDialog dlg(TRUE,"二进制数据文件", NULL,  OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, NULL );
	if(dlg.DoModal()==IDOK)
	{
		m_FileFft=dlg.GetFileName();
		if( (fpFft=fopen(m_FileFft,"r+"))==NULL) return;//二进制文件
		/* Attempt to read in 10 bytes: */
		for(int i=0;i<256;i++)
			fscanf(fpFft,"%d",&nData[i]);
		//fread(nData,sizeof(double),256,fpFft);
		MmDataFft=zeros(ANALY_FFT,1);

		m_slide.EnableWindow(true);	

		double sum=0.0;
		for(i=0;i<256;i++)
		{
			MmDataFft.r(i+1,1)=double(nData[i]);
			if(i>129)
				sum=sum+nData[i];
		}
		sum=sum/127;
		m_threshold=sum;
		UpdateData(false);

		cla();
		grid(TM("on"));
		m_bGrid=true;

		double bounddata[2]={0,260};
		Mm mbound;
		M_VECTOR(mbound,bounddata);
		axis(mbound);
		xlabel((CL(TM("频率点"))));
		ylabel((CL(TM("能量"))));
		title((CL(TM("TCD频谱分析"))));
		set(MmAxisFft,(CL(TM("Color")),TM("black")));
		plot( CL(MmDataFft));
		nCur=0;
	}

	
}

void CFftTestDlg::OnReleasedcaptureSlider1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	int currentpos=m_slide.GetPos();//滑尺所指当前圈数
	//fseek(fpFft,currentpos*256,SEEK_SET);	
	//fread(nData,sizeof(unsigned char),256,fpFft);
	for(int i=0;i<256;i++)
		fscanf(fpFft,"%d",&nData[i]);

	MmDataFft=zeros(ANALY_FFT,1);

	m_slide.EnableWindow(true);	
	double sum=0.0;
	for(i=0;i<256;i++)
	{
		MmDataFft.r(i+1,1)=nData[i];
		if(i>129)
			sum=sum+nData[i];
	}
	sum=sum/127;
    m_threshold=sum;
	UpdateData(false);
	cla();
	if(m_bGrid==true)
		grid(TM("on"));


	title((CL(TM("TCD频谱分析"))));
	set(MmAxisFft,(CL(TM("Color")),TM("black")));
	plot( CL(MmDataFft) );

	*pResult = 0;
}

void CFftTestDlg::OnGrid() 
{
	// TODO: Add your control notification handler code here
	if(m_grid.GetCheck()==1)
	{
		axes(CL(MmAxisFft));
		grid(TM("on"));
		m_bGrid=true;
	}
	else
	{
		axes(CL(MmAxisFft));
		grid(TM("off"));
		m_bGrid=false;
	}
}

void CFftTestDlg::OnBtnSmooth() 
{
	// TODO: Add your control notification handler code here
	int i;
	/*int temp=nData[0]; 
	for(int i=1;i<256;i++)
	{
		if(nData[i]-temp>50)
			nData[i]=temp+20;
		temp=nData[i];
	}*/
	double smooth[256];
	Jim_Smooth(256,nData,smooth);

	MmDataFft=zeros(ANALY_FFT,1);


	
	for(i=0;i<256;i++)
	{
		MmDataFft.r(i+1,1)=smooth[i];
	}
	cla();
	if(m_bGrid==true)
		grid(TM("on"));
	title((CL(TM("TCD频谱分析"))));
	set(MmAxisFft,(CL(TM("Color")),TM("black")));
	plot( CL(MmDataFft) );

	
}

void CFftTestDlg::Jim_Smooth(int n,int *y,double *yy)
{ 
	int i;
    if (n<5)
    { 
		for (i=0; i<=n-1; i++) 
			yy[i]=y[i];
	}
    else
    { 
		yy[0]=69.0*y[0]+4.0*y[1]-6.0*y[2]+4.0*y[3]-y[4];
        yy[0]=yy[0]/70.0;
        yy[1]=2.0*y[0]+27.0*y[1]+12.0*y[2]-8.0*y[3];
        yy[1]=(yy[1]+2.0*y[4])/35.0;
        for (i=2; i<=n-3; i++)
        { 
			yy[i]=-3.0*y[i-2]+12.0*y[i-1]+17.0*y[i];
            yy[i]=(yy[i]+12.0*y[i+1]-3.0*y[i+2])/35.0;
			if(yy[i]-0.0<-0.000001)
				yy[i]=0.0;

        }
        yy[n-2]=2.0*y[n-5]-8.0*y[n-4]+12.0*y[n-3];
        yy[n-2]=(yy[n-2]+27.0*y[n-2]+2.0*y[n-1])/35.0;
        yy[n-1]=-y[n-5]+4.0*y[n-4]-6.0*y[n-3];
        yy[n-1]=(yy[n-1]+4.0*y[n-2]+69.0*y[n-1])/70.0;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -