📄 dispfftdlg.cpp
字号:
// DispFFTDlg.cpp : implementation file
//
#include "stdafx.h"
#include "DispFFT.h"
#include "DispFFTDlg.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()
/////////////////////////////////////////////////////////////////////////////
// CDispFFTDlg dialog
CDispFFTDlg::CDispFFTDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDispFFTDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDispFFTDlg)
Num = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDispFFTDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDispFFTDlg)
DDX_Text(pDX, IDC_EDIT1, Num);
DDV_MinMaxInt(pDX, Num, 16, 2048);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDispFFTDlg, CDialog)
//{{AFX_MSG_MAP(CDispFFTDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDispFFTDlg message handlers
BOOL CDispFFTDlg::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
SetDlgItemInt(IDC_EDIT1, 512);
SetTimer(1, 100, 0);
return TRUE; // return TRUE unless you set the focus to a control
}
void CDispFFTDlg::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 CDispFFTDlg::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 CDispFFTDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
int my_rand(int M,int N)
{
//srand( (unsigned)time( NULL ) ); //撒种子
return (int)((double)rand()/(double)RAND_MAX*(N-M+1)+M);
}
#define MAX_POINT (2048+1)
double real[MAX_POINT], imag[MAX_POINT];
double res[MAX_POINT];
const double pp=3.141592654;
void CDispFFTDlg::OnButton1()
{
UpdateData();
OnButton2();
// TODO: Add your control notification handler code here
CString ss;
int width, heigh;
//
// 得到窗口的基本要素
//
CWnd *pWnd = GetDlgItem(IDC_STATIC_DISP);
CDC *pDC = pWnd->GetDC();
RECT rc;
pWnd->GetClientRect(&rc);
width = rc.right-rc.left; //宽
heigh = rc.bottom-rc.top; //高
// ss.Format("%d, %d", width, heigh);
// AfxMessageBox(ss);
int random = my_rand(0, 5);
//
// 模拟一个: y=sin(x)
//
int i =0;
double t = 0.0f;
while (1)
{
if (t > 0.6)
{
break;
}
real[i] = sin(2*pp*30*t) + sin(2*pp*120*t) + cos(2*pp*270*t)+ my_rand(0, random);
//real[i] = sin(2*pp*30*t) + sin(2*pp*120*t) + cos(2*pp*270*t);
imag[i] = 0.0f;
t += 0.001f;
i++;
}
// for (i =0; i<Num; i++)
// {
// //real[i] = sin(pp*i/(2.0*Num));
// real[i] = sin(2*pp*50*i);// + sin(2*pp*120*i);
// imag[i] = 0.0f;
// }
ss.Format("y=sin(x)");
pDC->TextOut(100, 100, ss);
//
// 把该函数的曲线画出来
//
// DrawMarker(pDC, real, Num, width, heigh, 0);
// DrawCurve(pDC, real, Num, width, heigh);
// AfxMessageBox("点击确定,画出FFT后数据");
//
//开始计算 FFT
//
extern void fft(double *xr,double *xi,int l,int inv) ;
fft(real, imag, Num, 0);
//
// 最后把FFT的结果显示
//
for(i=0;i<Num;i++)
{
res[i]=sqrt(pow(real[i],2) + pow(imag[i],2))/Num;
}
DrawMarker(pDC, res, Num, width, heigh, 1);
DrawCurve(pDC, res, Num, width, heigh);
pWnd->ReleaseDC(pDC);
}
//
// 找一组浮点数的最大
//
double CDispFFTDlg::GetMaxFloat(double *fData, int Num)
{
double fMax = fData[0];
int i = 0;
for (i =0; i<Num; i++)
{
if(fMax <fData[i])
fMax = fData[i];
}
return fMax;
}
//
// 画曲线
//
void CDispFFTDlg::DrawCurve(CDC *pDC, double *fData, int Num, int width, int heigh)
{
double fMax = GetMaxFloat(fData, Num);
int i = 0;
CPoint *pPoint = new CPoint[Num];
for (i =0; i<(Num/2); i++)
{
pPoint[i].x = i * width/((double)(Num/2));
pPoint[i].y = heigh - (fData[i] /fMax)*heigh;
if(i ==0)
{
pDC->MoveTo(0, heigh);
}
pDC->LineTo(pPoint[i]);
}
delete[] pPoint;
}
//
// 标刻度
//
void CDispFFTDlg::DrawMarker(CDC *pDC, double *fData, int Num, int width, int heigh, int left)
{
CString strDisp;
double fMax = GetMaxFloat(fData, Num);
CPoint *pPoint1 = new CPoint[Num+1];
CPoint *pPoint2 = new CPoint[Num+1];
//
// 标 x 轴
//
int i = 0;
for (i =0; i<=8; i++) //8个刻度
{
pPoint2[i].x = pPoint1[i].x = i * width/8.0;
pPoint1[i].y = heigh - 5;
pPoint2[i].y = heigh;
pDC->MoveTo(pPoint1[i]);
pDC->LineTo(pPoint2[i]);
//strDisp.Format("%d", i*(Num/8));
strDisp.Format("%d", (int)(1000.0*i/(2.0*8)));
pDC->TextOut(pPoint1[i].x-3, pPoint1[i].y+5, strDisp);
}
pDC->MoveTo(0, heigh); //水平轴
pDC->LineTo(width, heigh);
//
// 标 y 轴
//
if (left)
{
for (i =0; i<=8; i++)
{
pPoint1[i].x = 0;
pPoint2[i].x = 5;
pPoint2[i].y = pPoint1[i].y = heigh - i * heigh/8.0;
pDC->MoveTo(pPoint1[i]);
pDC->LineTo(pPoint2[i]);
if(i!=0)
{
strDisp.Format("%4.1f", i*fMax/8.0);
pDC->TextOut(pPoint1[i].x-18, pPoint1[i].y, strDisp);
}
}
}
else //标记在右边
{
for (i =0; i<=8; i++)
{
pPoint1[i].x = width;
pPoint2[i].x = width+5;
pPoint2[i].y = pPoint1[i].y = heigh - i * heigh/8.0;
pDC->MoveTo(pPoint1[i]);
pDC->LineTo(pPoint2[i]);
if(i!=0)
{
strDisp.Format("%4.1f", i*fMax/8.0);
pDC->TextOut(pPoint1[i].x-8, pPoint1[i].y, strDisp);
}
}
}
pDC->MoveTo(0, heigh); //垂直轴
pDC->LineTo(0, 0);
delete[] pPoint1;
delete[] pPoint2;
}
//
// 清屏
//
void CDispFFTDlg::OnButton2()
{
//
// 得到窗口的基本要素,并填充
//
CDC *pDC = GetDC();
RECT rc;
GetClientRect(&rc);
rc.right -= 100;
pDC->FillSolidRect(&rc, RGB(192, 192, 192));
ReleaseDC(pDC);
}
void CDispFFTDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
OnButton1();
CDialog::OnTimer(nIDEvent);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -