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

📄 fftdlg.cpp

📁 用C++语言对1024点的FFT进行编程
💻 CPP
字号:
// FFTDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FFT.h"
#include "FFTDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
#include "math.h"
const double PI = 3.14159265358;
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()

/////////////////////////////////////////////////////////////////////////////
// CFFTDlg dialog

CFFTDlg::CFFTDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFFTDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFFTDlg)
	m_F = 0;
	m_N = 0;
	m_T = 0.0;
	m_addZero = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFFTDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFFTDlg)
	DDX_Control(pDX, IDC_SHOWDATA, m_showData);
	DDX_Text(pDX, IDC_F, m_F);
	DDV_MinMaxInt(pDX, m_F, 1, 250);
	DDX_Text(pDX, IDC_N, m_N);
	DDV_MinMaxInt(pDX, m_N, 1, 256);
	DDX_Text(pDX, IDC_T, m_T);
	DDX_Check(pDX, IDC_addZero, m_addZero);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFFTDlg, CDialog)
	//{{AFX_MSG_MAP(CFFTDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_INPUT, OnInput)
	ON_BN_CLICKED(IDC_about, Onabout)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFFTDlg message handlers

BOOL CFFTDlg::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_N = 32;
	m_F = 50;
	m_T = 0.002;
	UpdateData(FALSE);
	CStatic *pST=(CStatic *)GetDlgItem(IDC_Client1);
	pST->GetWindowRect(&m_rectClient1);
	ScreenToClient(&m_rectClient1);

	pST=(CStatic *)GetDlgItem(IDC_Client2);
	pST->GetWindowRect(&m_rectClient2);
	ScreenToClient(&m_rectClient2);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CFFTDlg::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 CFFTDlg::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
	{
		CPaintDC dc(this);
		//************************************绘图的准备
		dc.SelectStockObject(WHITE_BRUSH);
		dc.Rectangle(m_rectClient1);
		dc.Rectangle(m_rectClient2);
		CPen penNew, *penOld;
		
		int wild,high;
		wild=2;
		high=10;
		if(m_N>64)
		{
			wild=1;
			high=5;
		}

		penNew.CreatePen(PS_SOLID,wild,RGB(255,0,0));
		penOld=dc.SelectObject (&penNew);
		int xzu;
		//**************************************绘出正弦采样
		xzu=(m_rectClient1.BottomRight().x-m_rectClient1.TopLeft().x-20)/m_N;
		dc.MoveTo(m_rectClient1.TopLeft().x,m_rectClient1.BottomRight().y-45);
		dc.LineTo(m_rectClient1.BottomRight().x,m_rectClient1.BottomRight().y-45);
		{	for(int i=1;i<=m_N;i++)
		{
		dc.MoveTo(m_rectClient1.TopLeft().x+20+(i-1)*xzu,m_rectClient1.BottomRight().y-45);
		dc.LineTo(m_rectClient1.TopLeft().x+20+(i-1)*xzu,m_rectClient1.BottomRight().y-45-(int)(m_real[i]*20));
		}	}
		//***************************************绘出FFT		
		xzu=(m_rectClient2.BottomRight().x-m_rectClient2.TopLeft().x-20)/m_N;
		dc.MoveTo(m_rectClient2.TopLeft().x,m_rectClient2.BottomRight().y-20);
		dc.LineTo(m_rectClient2.BottomRight().x,m_rectClient2.BottomRight().y-20);
		for(int i=1;i<=m_N;i++)
		{
		dc.MoveTo(m_rectClient2.TopLeft().x+20+(i-1)*xzu,m_rectClient2.BottomRight().y-20);
		dc.LineTo(m_rectClient2.TopLeft().x+20+(i-1)*xzu,m_rectClient2.BottomRight().y-20-(int)(Last[i]*high));
		}

		dc.SelectObject(penOld);

		CDialog::OnPaint();
	}
}

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

void CFFTDlg::FFT(int n, int f, double t)//*******************FFT的算法
{
	int i,j,N,F,k;	
	double tr,ti,T;
	N=n;
	F=f;
	T=t;
	j=1;
	
	
	for(i=1;i<=N;i++)
	{		
		m_real[i]=x_real[i]=sin(PI*F*T*(i-1));		
		x_imag[i]=0;
	}
	//******************addzero
	if(m_addZero)
	{
		N=2*N;
		m_N=N;
		for(i=N/2+1;i<=N;i++)
		{
			x_real[i]=0;
			x_imag[i]=0;
		}
	}
	//*******************排序
	for(i=1;i<N;++i)
	{
		if(i<j)
		{
		tr=x_real[j];ti=x_imag[j];
		x_real[j]=x_real[i];
		x_imag[j]=x_imag[i];
		x_real[i]=tr;x_imag[i]=ti;
		k=N/2;
		while(k<j){j=j-k;k=k/2;}
		}
		else
		{
			k=N/2;
			while(k<j){j=j-k;k=k/2;}
		}
		j=j+k;
	}
	//***********************
	//fft
	int m,ir,l,ip,le,lv;

	double ur,ui,wr,wi,temp;
	m=0; 
	ir=N;
	while(ir>1)
	{ 
		m=m+1; 
		ir=ir/2;
	}
	for(l=1;l<=m;l++)
	{
		le=(long)pow(2,l); 
		lv=le/2;
		ur=1.0; 
		ui=0;
		wr=cos(PI/lv); 
		wi=sin(PI/lv);
		for(j=1;j<=lv;++j)
			{
				i=j;
				while(i<=N)
					{
						ip=i+lv;
						tr=x_real[ip]*ur-x_imag[ip]*ui;
						ti=x_imag[ip]*ur+x_real[ip]*ui;
						x_real[ip]=x_real[i]-tr;
						x_imag[ip]=x_imag[i]-ti;
						x_real[i]=x_real[i]+tr;
						x_imag[i]=x_imag[i]+ti;
						i=i+le;
					}
				temp=ur*wr-ui*wi;
				ui=ui*wr+ur*wi;
				ur=temp;
			}
	}
	//**********************
	for(i=1;i<=N;i++)
	{
		Last[i]=sqrt(x_real[i]*x_real[i]+x_imag[i]*x_imag[i]);
	}
}

void CFFTDlg::OnInput() //****************控件按钮输出
{
	if(!UpdateData())
		return;
	CINPUTDLG dlg;
	if(dlg.DoModal() == IDOK)
	{
		m_N=dlg.m_N;
		m_F=dlg.m_F;
		m_T=dlg.m_T;
		UpdateData(false);
	}
	FFT(m_N,m_F,m_T);

	m_showData.ResetContent();
	CString str;
	for(int i=1;i<=m_N;i++)
	{
		str.Format("x[%d] = %4f + j%4f",i,x_real[i],x_imag[i]);
		m_showData.AddString(str);
		str.Empty();
	}
	InvalidateRect(m_rectClient1);
	InvalidateRect(m_rectClient2);
}



void CFFTDlg::Onabout() 
{
	// TODO: Add your control notification handler code here
	MessageBox("Wxx制作!版权所有(R)","关于 FFT 程序",0);
}

⌨️ 快捷键说明

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