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

📄 dialogconvolution.cpp

📁 fft and convolution
💻 CPP
字号:
// DialogConvolution.cpp : implementation file
//

#include "stdafx.h"
#include "fft2.h"
#include "DialogConvolution.h"
#include "windows.h"

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

/////////////////////////////////////////////////////////////////////////////
// DialogConvolution dialog


DialogConvolution::DialogConvolution(CWnd* pParent /*=NULL*/)
	: CDialog(DialogConvolution::IDD, pParent)
{
	//{{AFX_DATA_INIT(DialogConvolution)
	m_bi = 0;
	//}}AFX_DATA_INIT
}


void DialogConvolution::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(DialogConvolution)
	DDX_Control(pDX, IDC_SLIDERBIL, m_bili);
	DDX_Control(pDX, IDC_SCROLLBAR_RATIOH, m_ratioh);
	DDX_Control(pDX, IDC_SCROLLBAR_RATIOX, m_ratiox);
	DDX_Control(pDX, IDC_SLIDER_NUMH, m_NUMH);
	DDX_Control(pDX, IDC_SLIDER_NUMX, m_NUMX);
	DDX_Control(pDX, IDC_COMBO_WAVESTYLEX, m_wavestylex);
	DDX_Control(pDX, IDC_COMBO_WAVESTYLEH, m_wavestyleh);
	DDX_Slider(pDX, IDC_SLIDERBIL, m_bi);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(DialogConvolution, CDialog)
	//{{AFX_MSG_MAP(DialogConvolution)
	ON_WM_PAINT()
	ON_CBN_SELCHANGE(IDC_COMBO_WAVESTYLEX, OnSelchangeComboWavestylex)
	ON_CBN_SELCHANGE(IDC_COMBO_WAVESTYLEH, OnSelchangeComboWavestyleh)
	ON_WM_HSCROLL()
	ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
	ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// DialogConvolution message handlers
void DialogConvolution::DrawElement(int nID)
{
		int i;
	CWnd *pWnd=GetDlgItem(nID);//
	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);
	pDC->LineTo(0,-160);
	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);
	}
	//-
	for(i=1;i<16;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);
	}

	pDC->MoveTo(0,-80);
	pDC->LineTo(510,-80);
	
    
	pDC->SelectObject(&OldPen);

	pWnd->ReleaseDC(pDC);
}

void DialogConvolution::mdrawlins(int x0, int y0, int x1, int y1, int a,int nID)
{
	CWnd *pWnd=GetDlgItem(nID);//
	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-95);
	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-95);
	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 DialogConvolution::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here

	Numx=4;
	Numh=2;
	datax = new fushu[Numx];
	datafordrawx = new fushu[Numx];
	datah = new fushu[Numh];
	datafordrawh = new fushu[Numh];
	datay= new fushu[Numx+Numh];//////////////////////////??Numx=Numh
	//power = new double[Num];
	
	
	//for(int i=0;i<Num;i++)
	//power[i]=0.0;
	m_NUMX.SetRange(1,9);
	m_NUMH.SetRange(1,9);
	m_ratiox.SetScrollRange(1,10);
	m_ratioh.SetScrollRange(1,10);
	
	m_ratiox.SetScrollPos(10);
	m_ratioh.SetScrollPos(10);
	m_bili.SetRange(1,150);
	m_bili.SetPos(1);
	m_bi=1;
	//m_numvaluex=256;
	m_NUMX.SetPos(2);
	m_NUMH.SetPos(1);

	wavestyleh=wavestylex="正弦波";

	contype=1;
	 

	ratiox=1;
	ratioh=1;
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void DialogConvolution::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(IDC_DRAWBOX_X);
		DrawElement(IDC_DRAWBOX_H);
		DrawElement(IDC_DRAWBOX_Y);




	 Drawwave();
		CDialog::OnPaint();
	}	
	// Do not call CDialog::OnPaint() for painting messages
}

void DialogConvolution::OnSelchangeComboWavestylex() 
{
	// TODO: Add your control notification handler code here
	int nIndex=m_wavestylex.GetCurSel();

	if(nIndex !=CB_ERR)
	{
		CString str;
		m_wavestylex.GetLBText(nIndex,str);

	
		wavestylex=str;
	
		FormWave(str,1);
		FormWave(wavestyleh,2);
		Compute();
		Drawwave();
		
		
	}
	
}

void DialogConvolution::OnSelchangeComboWavestyleh() 
{
	// TODO: Add your control notification handler code here
		int nIndex=m_wavestyleh.GetCurSel();

	if(nIndex !=CB_ERR)
	{
		CString str;
		m_wavestyleh.GetLBText(nIndex,str);

	
		wavestyleh=str;
		FormWave(str,2);
		FormWave(wavestylex,1);//重建datax
		Compute();
		Drawwave();
		
		
	}
	
}

void DialogConvolution::FormWave(CString wave,int which)
{
	CString wa=wave;
	if(which==1){
	if(wa=="正弦波")
	  FormSin(Numx,(float)ratiox,1,which);
	if(wa=="矩形波")
		FormRect(Numx,(float)ratiox,1.0,0,which);
	if(wa=="三角波")
		FormTri(Numx,which);
	}else if(which==2){
	if(wa=="正弦波")
	  FormSin(Numh,(float)ratioh,1,which);
	if(wa=="矩形波")
		FormRect(Numh,(float)ratioh,1.0,0,which);
	if(wa=="三角波")
		FormTri(Numh,which);
	
	}

}
void DialogConvolution::FormSin(int num,float nt,float amp,int which){

int n=num;
float t=nt;
float a=amp;
double xjiange=t*2*3.14159/(n-1);
double x=0.0;	

if(which==1){
	for(int i=0;i<n;i++)
	{
	//正弦波
	
		datax[i].image=0;
		
		datax[i].real=a*sin(x);	
		datafordrawx[i]=datax[i];
		x+=xjiange;

		
	}
}else if(which==2)
{
for(int i=0;i<n;i++)
	{
	//正弦波
	
		datah[i].image=0;
		
		datah[i].real=a*sin(x);	
		datafordrawh[i]=datah[i];
		x+=xjiange;

		
	}
}

}

void DialogConvolution::Compute(){
	
	fftmy myfft;
	CString str;

	if(contype==1){
	//BeginWaitCursor();	
    //myfft.test();
	//if(Numx==Numh){
	//myfft.convolution(datax,datah,datay,Numx);
	//}///////////////////////////??Numx=Numh
	//else{
	int time=Numx/Numh;
	int n=2*Numh;
	fushu *dx = new fushu[n];
	fushu *dh= new fushu[n];
	fushu *dy= new fushu[n];//
	fushu *dyy= new fushu[Numx+Numh];

	int i;
		fushu zero;
	zero.real=zero.image=0;
	for(i=0;i<Numx+Numh;i++)
	dyy[i]=zero;
	
	 i=0;

	 long time1=GetTickCount();
	 
	 
	for(int j=0;j<time;j++){
	for(i=0;i<n;i++)
	{
		if(i<Numh)
		{
			dx[i]=datax[i+j*Numh];
			dh[i]=datah[i];////////////focus
		}else {
		dx[i]=zero;
		dh[i]=zero;
		}
	}//for
	myfft.convolution(dx,dh,dy,n);
	for(int m=0;m<n;m++){
		dyy[m+j*Numh].image+=dy[m].image;
		dyy[m+j*Numh].real+=dy[m].real;///////////////don't forget
	}

	}//for j

	
	datay=dyy;

	//}//if(Numx==Numh)
	
	//Sleep(500);
	 long time2=GetTickCount();
	 long timecost=time2-time1;

	

	 str.Format("TIME COST: %ld ms",timecost);
	 GetDlgItem(IDC_STATICSHOW)->SetWindowText(str);


	}//	 if(contype==1){
	else{

		fushu *dyy= new fushu[Numx+Numh];
		//delete []datay;
		datay=dyy;
		 long time3=GetTickCount();
	
	myfft.convolutioncommon(datax,datah,datay,Numx,Numh);
	 long time4=GetTickCount();

	 long timecosts=time4-time3;

	 //CString str;
	 str.Format("TIME COST: %ld ms",timecosts);
	 GetDlgItem(IDC_STATICSHOW)->SetWindowText(str);
	
	}
 //	EndWaitCursor();

}
void DialogConvolution::Drawwave(){

    int xunite=512/Numx;
	int hunite=512/Numh;
	//int yunite=512/(Numx+Numh);
	int yunite=1;


	//CString str;
	for(int i=0;i<Numx;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*datafordrawx[i].real),1,IDC_DRAWBOX_X);
		}
    for( i=0;i<Numh;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*hunite,0,i*hunite,(int)(50*datafordrawh[i].real),1,IDC_DRAWBOX_H);
		}

	for( i=0;i<Numx+Numh-1;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*yunite,0,i*yunite,(int)(50*(datay[i].real/m_bi)),1,IDC_DRAWBOX_Y);
	
	}
		//CString str;
		//str.Format("%.2f %.2f %.2f ",datay[2].real,datay[2].real,datay[2].real);
		
}



void DialogConvolution::FormRect(int num,float ratio,float up,float down,int which)

{

	int n=num;
	float r=ratio;
	float u=up;
	float d=down;

	if(which==1){
	for(int i=0;i<n;i++){
if(i<(int)(r*n))
   {
	   datax[i].real=u;
       datax[i].image=0;

   }
   else{
	   datax[i].real=d;
	   datax[i].image=0;  

}
   datafordrawx[i]=datax[i];
}

	}else if(which==2){
	
		for(int i=0;i<n;i++){
if(i<(int)(r*n))
   {
	   datah[i].real=u;
       datah[i].image=0;

   }
   else{
	   datah[i].real=d;
	   datah[i].image=0;  

}
   datafordrawh[i]=datah[i];
}

	


	}

}



void DialogConvolution::FormTri(int num,int which){
int n=num;
if(which==1){
for(int i=0;i<n;i++){
if(i<n/2)
   {
	   datax[i].real=i*2.0/n;
       datax[i].image=0;

   }
   else{
	   datax[i].real=double(n+1-i)/(n/2-1);
	   datax[i].image=0;  

   }
   datafordrawx[i]=datax[i];

}
}else if(which == 2){

for(int i=0;i<n;i++){
if(i<n/2)
   {
	   datah[i].real=i*2.0/n;
       datah[i].image=0;

   }
   else{
	   datah[i].real=double(n+1-i)/(n/2-1);
	   datah[i].image=0;  

   }
   datafordrawh[i]=datah[i];

}

}
}

void DialogConvolution::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_NUMX)
	{
		int n=m_NUMX.GetPos();
		if(n>=m_NUMH.GetPos()){
		Numx=1;
		for(int i=1;i<=n;i++)
			Numx*=2;

		CString str;
		str.Format("%d",Numx);
		GetDlgItem(IDC_NOPX)->SetWindowText(str); 
			
		delete []datax;
		delete []datafordrawx;
		//delete []power;

		datax = new fushu[Numx];
		datafordrawx = new fushu[Numx];
		//power = new double[Numx];
		FormWave(wavestylex,1);
		FormWave(wavestyleh,2);
		Compute();
		
		Drawwave();
		}

	}else if(nID==IDC_SCROLLBAR_RATIOX)
	{
		int pos=m_ratiox.GetScrollPos();
		switch(nSBCode){
		case SB_LINELEFT:
			m_ratiox.SetScrollPos((pos-1)>0?(pos-1):1);
			break;
		case SB_LINERIGHT:
			m_ratiox.SetScrollPos((pos+1)<11?(pos+1):10);
			break;
		}
	
		ratiox=pos/10.0;
		FormWave(wavestylex,1);
		FormWave(wavestyleh,2);
		Compute();
		
		Drawwave();

		CString str;
		str.Format("%.1f",ratiox);
		GetDlgItem(IDC_PERX)->SetWindowText(str); 

	}else if(nID==IDC_SLIDER_NUMH){

		int n=m_NUMH.GetPos();
		if(n<=m_NUMX.GetPos()){
		Numh=1;
		for(int i=1;i<=n;i++)
			Numh*=2;

		CString str;
		str.Format("%d",Numh);
		GetDlgItem(IDC_NOPH)->SetWindowText(str); 
			
		delete []datah;
		delete []datafordrawh;
		//delete []power;

		datah = new fushu[Numh];
		datafordrawh= new fushu[Numh];
		//power = new double[Numx];
			FormWave(wavestylex,1);
		FormWave(wavestyleh,2);
		Compute();
		
		Drawwave();
		}

}else if(nID==IDC_SCROLLBAR_RATIOH){

int pos=m_ratioh.GetScrollPos();
		switch(nSBCode){
		case SB_LINELEFT:
			m_ratioh.SetScrollPos((pos-1)>0?(pos-1):1);
			break;
		case SB_LINERIGHT:
			m_ratioh.SetScrollPos((pos+1)<11?(pos+1):10);
			break;
		}
	
		ratioh=pos/10.0;
		FormWave(wavestylex,1);
		FormWave(wavestyleh,2);
		Compute();
		
		Drawwave();

		CString str;
		str.Format("%.1f",ratioh);
		GetDlgItem(IDC_PERH)->SetWindowText(str); 

}else if(nID==IDC_SLIDERBIL){

int pos=m_bili.GetPos();
m_bi=pos;
m_bili.SetPos(m_bi);
Drawwave();
CString str;
str.Format("比例尺 1:%d",m_bi);
GetDlgItem(IDC_STATICBILI)->SetWindowText(str); 

}

	
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

void DialogConvolution::OnRadio1() 
{
	// TODO: Add your control notification handler code here
	contype=1;
	
}

void DialogConvolution::OnRadio2() 
{
	// TODO: Add your control notification handler code here
	contype=0;

	
}

⌨️ 快捷键说明

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