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

📄 ppdlg.cpp

📁 基于VC编写正弦信号的傅立叶变换程序
💻 CPP
字号:
// PPDlg.cpp : implementation file
//

#include "stdafx.h"
#include "fft.h"
#include "PP.h"
#include "PPDlg.h"
#include "math.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

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()

/////////////////////////////////////////////////////////////////////////////
// CPPDlg dialog

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

void CPPDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPPDlg)
	DDX_Text(pDX, IDC_EDITF, m_F);
	DDV_MinMaxUInt(pDX, m_F, 1, 40000);
	DDX_Text(pDX, IDC_EDITFS, m_FS);
	DDV_MinMaxUInt(pDX, m_FS, 1, 40000);
	DDX_Text(pDX, IDC_AMPLITUDE, m_Amplitude);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPPDlg, CDialog)
	//{{AFX_MSG_MAP(CPPDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BDISPLAY, OnBdisplay)
	ON_BN_CLICKED(IDC_BFFT, OnBfft)
	ON_BN_CLICKED(IDC_BSAMPLE, OnBsample)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPPDlg message handlers

BOOL CPPDlg::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_F=8;
	m_FS=256;
	m_Amplitude=1.00;
	UpdateData(false);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CPPDlg::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 CPPDlg::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 CPPDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CPPDlg::OnBdisplay() 
{
	// TODO: Add your control notification handler code here
	double temp;
	Invalidate(); 
    UpdateWindow(); 
	UpdateData(true);
	npp = float(m_FS)/m_F;
	n_sample=2*m_FS;

	for(int i=1;i<=20;i++)
	{
		temp=pow(2,i);
		if(temp>=n_sample)
		{
			n_sample=temp;
			break;
		}
	}

	pXIN = new COMPX[n_sample];

	CWnd* pWnd=GetDlgItem(IDC_SHOW_SINE);
	CDC* pDC=pWnd->GetDC();
    
	CPen* pPenRed=new CPen;
	pPenRed->CreatePen(PS_SOLID,1,RGB(255,0,0));

	CPen* pPenGreen=new CPen;
	pPenGreen->CreatePen(PS_DOT,1,RGB(0,255,0));

	CGdiObject* pOldPen=pDC->SelectObject(pPenRed);

    pDC->SetTextColor(RGB(0,255,0));
	for(int m=0;m<=500;m+=50)
	{
		str.Format("%d",m);
		pDC->TextOut(10+m,145,str);
	}

	pDC->MoveTo(10,10);      
	pDC->LineTo(10,280);
	pDC->MoveTo(10,145);
	pDC->LineTo(540,145);

	for(i=0;i<n_sample;i+=5)
	{
		if((i&1)==0)
		{
			pDC->MoveTo(i+10,145);
			pDC->LineTo(i+10,149);
		}
		else
		{
			pDC->MoveTo(i+10,145);
			pDC->LineTo(i+10,147);
		}
	}

    pDC->MoveTo(535,140);
	pDC->LineTo(540,145);
	pDC->LineTo(535,150);
	pDC->TextOut(535,120,"n");

	pDC->MoveTo(10,10);
	pDC->LineTo(5,15);
	pDC->MoveTo(10,10);
	pDC->LineTo(15,15);
	pDC->TextOut(15,0,"A");

	sine_generate(pXIN,n_sample,npp,m_Amplitude);
	pDC->MoveTo(10, 145);
	for(i=0;i<n_sample;i++)
		pDC->LineTo(10 + i, 145 - pXIN[i].real *40);

	pOldPen=pDC->SelectObject(pPenGreen);
	for(i=-3;i<=3;i++)
	{
		if(i==0) continue;
		int doty=145+i*40;
		pDC->MoveTo(10,doty);
		pDC->LineTo(540,doty);
		str.Format("%d",-i);
		pDC->TextOut(0,doty-8,str);
	}
	pDC->SelectObject(pOldPen);
    delete pPenRed;
}

void CPPDlg::OnBfft() 
{
	// TODO: Add your control notification handler code here
	//Invalidate(); 
    //UpdateWindow(); 
	int max1,max2;
	int k_max1,k_max2;
	double temp;
	
	CWnd* pWnd=GetDlgItem(IDC_SHOW_PP);
	CDC* pDC=pWnd->GetDC();

	CPen* pPenBlue=new CPen;
	pPenBlue->CreatePen(PS_SOLID,1,RGB(0,0,255));
	CGdiObject* pOldPen=pDC->SelectObject(pPenBlue);

	pDC->SetTextColor(RGB(0,255,0));
	for(int m=0;m<=500;m+=50)
	{
		str.Format("%d",m);
		pDC->TextOut(10+m,280,str);
	}


	pDC->MoveTo(10,10);      

	pDC->LineTo(10,280);

	pDC->MoveTo(10,280);

	pDC->LineTo(540,280);

	for(int i=0;i<512;i+=5)
	{
		if((i&1)==0)
		{
			pDC->MoveTo(i+10,280);
			pDC->LineTo(i+10,284);
		}
		else
		{
			pDC->MoveTo(i+10,280);
			pDC->LineTo(i+10,282);
		}
	}

    pDC->MoveTo(535,275);
	pDC->LineTo(540,280);
	pDC->LineTo(535,285);
	pDC->TextOut(535,260,"k");

	pDC->MoveTo(10,10);
	pDC->LineTo(5,15);
	pDC->MoveTo(10,10);
	pDC->LineTo(15,15);
	
	FFT(pXIN,n_sample);
	pDC->MoveTo(10, 280);

	for(i=0;i<n_sample;i++)
	{
		temp=sqrt(pXIN[i].real*pXIN[i].real+pXIN[i].imag*pXIN[i].imag);
		if(i<n_sample/2 && temp>3.14)
		{
			k_max1=i;
			max1=temp;
		}
		if(i>n_sample/2 && temp>3.14)
		{
			k_max2=i;
			max2=temp;
		}
		pDC->LineTo(10 + i,280 - temp*0.4);
	}
	
	str.Format("(%d,%d)",k_max1,max1);
	pDC->TextOut(k_max1-15,260-max1*0.4,str);

	str.Format("(%d,%d)",k_max2,max2);
	pDC->TextOut(k_max2-15,260-max2*0.4,str);
	pDC->SelectObject(pOldPen);
	delete pPenBlue;
}

void CPPDlg::OnBsample() 
{
	// TODO: Add your control notification handler code here
	CWnd* pWnd=GetDlgItem(IDC_SHOW_SINE);
	CDC* pDC=pWnd->GetDC();

	CPen* pPenGreen=new CPen;
	pPenGreen->CreatePen(PS_SOLID,1,RGB(0,200,0));
	CGdiObject* pOldPen=pDC->SelectObject(pPenGreen);
	
	for(int i=0;i<n_sample;i++)
	{
		pDC->MoveTo(10 + i, 145);
		pDC->LineTo(10 + i, 145 - pXIN[i].real *40);
	}

	pDC->SelectObject(pOldPen);
	delete pPenGreen;
}

⌨️ 快捷键说明

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