📄 dialogfft.cpp
字号:
// DialogFFT.cpp : implementation file
//
#include "stdafx.h"
#include "fft2.h"
#include "DialogFFT.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// DialogFFT dialog
DialogFFT::DialogFFT(CWnd* pParent /*=NULL*/)
: CDialog(DialogFFT::IDD, pParent)
{
//{{AFX_DATA_INIT(DialogFFT)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void DialogFFT::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(DialogFFT)
DDX_Control(pDX, IDC_SCROLLBAR_RATIO, m_ratio);
DDX_Control(pDX, IDC_SLIDER_NUM, m_NUM);
DDX_Control(pDX, IDC_COMBO_WAVESTYLE, m_wavestyle);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(DialogFFT, CDialog)
//{{AFX_MSG_MAP(DialogFFT)
ON_WM_PAINT()
ON_CBN_SELCHANGE(IDC_COMBO_WAVESTYLE, OnSelchangeComboWavestyle)
ON_WM_HSCROLL()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// DialogFFT message handlers
void DialogFFT::DrawElement()
{
int i;
CWnd *pWnd=GetDlgItem(IDC_DRAWBOX_FFT);//
CDC *pDC=pWnd->GetDC();
::GetClientRect(pWnd->m_hWnd,conRect); //获得这个控件的矩形区域。
pDC->SelectStockObject(NULL_BRUSH); //将索引值为NULL_BRUSH的库存对象选进设备环境
//设置视窗原点
pDC->SetViewportOrg(conRect.left+20,conRect.bottom-15);
//判断成员变量的值,以便确定绘制对象的类型。
CPen Pen(PS_SOLID,1,RGB(0,150,150));
CPen *OldPen=pDC->SelectObject(&Pen);
pDC->MoveTo(0,0);
pDC->LineTo(510,0);
pDC->LineTo(510,-160*2);
pDC->LineTo(0,-160*2);
pDC->LineTo(0,0);
CPen Pen2(PS_DOT,1,RGB(0,150,150));;
pDC->SelectObject(&Pen2);
//||
for(i=0;i<50;i++)
{
pDC->MoveTo(10*i+10,0);
pDC->LineTo(10*i+10,-160*2);
}
//-
for(i=1;i<32;i++)
{
pDC->MoveTo(0,-10*i);
pDC->LineTo(510,-10*i);
}
CPen Pen1(PS_SOLID,1,RGB(0,150,150));;
pDC->SelectObject(&Pen1);
for(i=0;i<10;i++)
{
pDC->MoveTo(50*i,0);
pDC->LineTo(50*i,-160*2);
}
pDC->MoveTo(0,-80*2);
pDC->LineTo(510,-80*2);
pDC->MoveTo(0,-240);
pDC->LineTo(510,-240);
pDC->SelectObject(&OldPen);
pWnd->ReleaseDC(pDC);
}
void DialogFFT::mdrawlins(int x0, int y0, int x1, int y1, int a)
{
CWnd *pWnd=GetDlgItem(IDC_DRAWBOX_FFT);//
CDC *pDC=pWnd->GetDC();
::GetClientRect(pWnd->m_hWnd,conRect); //获得这个控件的矩形区域。
pDC->SelectStockObject(NULL_BRUSH); //将索引值为NULL_BRUSH的库存对象选进设备环境
//设置视窗原点
CPen *OldPen;
if(a==0)
{
pDC->SetViewportOrg(conRect.left+20,conRect.bottom-15-160);
CPen Pen(PS_SOLID,1,RGB(255,0,55));
OldPen=pDC->SelectObject(&Pen);
pDC->MoveTo(x0,y0);
pDC->LineTo(x1,y1);
}
if(a==1)
{
pDC->SetViewportOrg(conRect.left+20,conRect.bottom-15-240);
CPen Pen(PS_SOLID,1,RGB(0,0,0));
CPen *OldPen=pDC->SelectObject(&Pen);
pDC->Ellipse(x1-1,-y1-1,x1+1,-y1+1);
}
// Invalidate(false);
// pDC->Ellipse(x0-a,y1-a,x0+a,y1+a);
pDC->SelectObject(&OldPen);
pWnd->ReleaseDC(pDC);
}
BOOL DialogFFT::OnInitDialog()
{
CDialog::OnInitDialog();// 调用默认OnInitDialog函数
// TODO: Add extra initialization here
// CWnd* pWnd = GetDlgItem(IDC_DRAWBOX_FFT);
// pWnd->GetClientRect(m_MouseRect);
// pWnd->ClientToScreen(&m_MouseRect);
// GetClientRect(conRect);
//ClientToScreen(&conRect);
/////////////////////////////////////
Num=256;
data = new fushu[Num];
datafordraw = new fushu[Num];
power = new double[Num];
for(int i=0;i<Num;i++)
power[i]=0.0;
m_NUM.SetRange(1,9);
m_ratio.SetScrollRange(1,10);
m_ratio.SetScrollPos(10);
m_numvalue=256;
m_NUM.SetPos(8);
ratio=1;
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void DialogFFT::OnPaint()
{
// CPaintDC dc(this); // device context for painting
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
{
DrawElement();
Drawwave();
CDialog::OnPaint();
}
// Do not call CDialog::OnPaint() for painting messages
}
void DialogFFT::OnSelchangeComboWavestyle()
{
// TODO: Add your control notification handler code here
int nIndex=m_wavestyle.GetCurSel();
if(nIndex !=CB_ERR)
{
CString str;
m_wavestyle.GetLBText(nIndex,str);
wavestyle=str;
FormWave(str);
Compute();
Drawwave();
}
}
void DialogFFT::FormWave(CString wave)
{
CString wa=wave;
if(wa=="正弦波")
FormSin(Num,(float)ratio,1);
if(wa=="矩形波")
FormRect(Num,(float)ratio,1.0,0);
if(wa=="三角波")
FormTri(Num);
}
void DialogFFT::FormSin(int num,float nt,float amp){
int n=num;
float t=nt;
float a=amp;
double xjiange=t*2*3.14159/(n-1);
double x=0.0;
for(int i=0;i<n;i++)
{
//正弦波
data[i].image=0;
data[i].real=a*sin(x);
datafordraw[i]=data[i];
x+=xjiange;
}
}
void DialogFFT::Compute(){
fftmy myfft;
BeginWaitCursor();
myfft.FFTmy(data,Num);
myfft.fuabs(data,Num,power);
EndWaitCursor();
}
void DialogFFT::Drawwave(){
int xunite=512/Num;
for(int i=0;i<Num;i++)
{
//mdrawlin(i*xunite,0,i*xunite,(10*(int)(power[i]))<160?(10*(int)(power[i])):160,0);
mdrawlins(i*xunite,0,i*xunite,(int)(160*(power[i]/power[1])<=160?(int)160*(power[i]/power[1]):160),0);
mdrawlins(i*xunite,0,i*xunite,(int)(50*datafordraw[i].real),1);
}
}
void DialogFFT::FormRect(int num,float ratio,float up,float down)
{
int n=num;
float r=ratio;
float u=up;
float d=down;
for(int i=0;i<n;i++){
if(i<(int)(r*n))
{
data[i].real=u;
data[i].image=0;
}
else{
data[i].real=d;
data[i].image=0;
}
datafordraw[i]=data[i];
}
}
void DialogFFT::FormTri(int num){
int n=num;
for(int i=0;i<n;i++){
if(i<n/2)
{
data[i].real=i*2.0/n;
data[i].image=0;
}
else{
data[i].real=double(n+1-i)/(n/2-1);
data[i].image=0;
}
datafordraw[i]=data[i];
}
}
void DialogFFT::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
int nID=pScrollBar->GetDlgCtrlID();
if(nID==IDC_SLIDER_NUM)
{
int n=m_NUM.GetPos();
Num=1;
for(int i=1;i<=n;i++)
Num*=2;
CString str;
str.Format("%d",Num);
GetDlgItem(IDC_NOP)->SetWindowText(str);
delete []data;
delete []datafordraw;
delete []power;
data = new fushu[Num];
datafordraw = new fushu[Num];
power = new double[Num];
FormWave(wavestyle);
Compute();
Drawwave();
}else if(nID==IDC_SCROLLBAR_RATIO)
{
int pos=m_ratio.GetScrollPos();
switch(nSBCode){
case SB_LINELEFT:
m_ratio.SetScrollPos((pos-1)>0?(pos-1):1);
break;
case SB_LINERIGHT:
m_ratio.SetScrollPos((pos+1)<11?(pos+1):10);
break;
}
ratio=pos/10.0;
FormWave(wavestyle);
Compute();
Drawwave();
CString str;
str.Format("%.1f",ratio);
GetDlgItem(IDC_PER)->SetWindowText(str);
}
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -