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

📄 communicationdlg.cpp

📁 本程序利用VC实现均匀量化和非均匀量化信噪比的仿真。并画出曲线
💻 CPP
字号:
// communicationDlg.cpp : implementation file
//

#include "stdafx.h"
#include "communication.h"
#include "communicationDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CCommunicationDlg dialog

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

void CCommunicationDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCommunicationDlg)

	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCommunicationDlg, CDialog)
	//{{AFX_MSG_MAP(CCommunicationDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCommunicationDlg message handlers

BOOL CCommunicationDlg::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
    //  we add the code here   在此添加程序
    // 获取绘制波形图的标签
	CWnd* pWnd = GetDlgItem(IDC_COORD);
	
	// 计算接受重绘波形的有效区域
	
    pWnd->GetClientRect(m_Rect);
	pWnd->ClientToScreen(&m_Rect);
	
	CRect rect;
	GetClientRect(rect);
	ClientToScreen(&rect);
	
	m_Rect.top -= rect.top;
	m_Rect.left -= rect.left;
	
	// 设置接受鼠标事件的有效区域
	
	m_Rect.bottom = m_Rect.top + 320;
	m_Rect.right = m_Rect.left + 570;
      
	


	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCommunicationDlg::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 CCommunicationDlg::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();
	}
    
	
	
	
	//TO ADD THE CODE   HERE


////////////////////////////////////////////////////////////////	
 //x轴的绘制//////////////////////////////////

    
	CString str;
	
	
	
	
	// 设备上下文
	CPaintDC dc(this);
	
	// 获取绘制坐标的文本框
	CWnd* pWnd = GetDlgItem(IDC_COORD);
	
	// 指针
	CDC* pDC = pWnd->GetDC();
	pWnd->Invalidate();
	pWnd->UpdateWindow();
	
	pDC->Rectangle(0,0,580,400);
	
	// 创建画笔对象
	CPen* pPenRed = new CPen;
	
	// 红色画笔
	pPenRed->CreatePen(PS_SOLID,1,RGB(255,0,0));



	
	// 创建画笔对象
	CPen* pPenBlue = new CPen;
	
	// 蓝色画笔
	pPenBlue->CreatePen(PS_SOLID,1,RGB(0,0, 255));



    CPen* pPenZ = new CPen;
	
	// 紫色画笔
	pPenZ->CreatePen(PS_SOLID,1,RGB(255,0,255));



    CPen* pPenQing = new CPen;
	
	// 蓝色画笔
	pPenQing->CreatePen(PS_SOLID,1,RGB(0,128, 128));


	
	// 创建画笔对象
	CPen* pPenBlack = new CPen;
	
	// 黑色画笔
	pPenBlack->CreatePen(PS_SOLID,1,RGB(0,0,0));


	
	// 选中当前红色画笔,并保存以前的画笔
	CGdiObject* pOldPen = pDC->SelectObject(pPenBlack);
	
	// 绘制坐标轴
	pDC->MoveTo(30,10);
	
	// 垂直轴
	pDC->LineTo(30,380); 
	pDC->MoveTo(30, 250);
	// 水平轴

	pDC->LineTo(570,250);
	
	// 写X轴刻度值
	str.Format("0");
	pDC->TextOut(10, 254, str);

	
	str.Format("32");
	pDC->TextOut(38, 254, str);


	str.Format("128");
	pDC->TextOut(62, 254, str);

	str.Format("256");
	pDC->TextOut(94, 254, str);

    str.Format("512");
	pDC->TextOut(148, 254, str);

    str.Format("1024");
	pDC->TextOut(266, 254, str);

    str.Format("2048");
	pDC->TextOut(510, 254, str);

     str.Format("输入的最大幅度a");
	pDC->TextOut(460, 220, str);
	
	// 绘制X轴刻度
	// 


			pDC->MoveTo(542, 250);
			pDC->LineTo(542, 256);

            pDC->MoveTo(286, 250);
			pDC->LineTo(286, 256);

           	pDC->MoveTo(158, 250);
			pDC->LineTo(158, 256);
			

        	pDC->MoveTo(94, 250);
			pDC->LineTo(94, 256);

        	pDC->MoveTo(62, 250);
			pDC->LineTo(62, 256);

        	pDC->MoveTo(38, 250);
			pDC->LineTo(38, 256);

        	pDC->MoveTo(30, 250);
			pDC->LineTo(30, 256);
	
	// 绘制X轴箭头
	pDC->MoveTo(565,245);
	pDC->LineTo(570,250);
	pDC->LineTo(565,255);


////////////////////////////////////////////////////////////////	
     //y轴的绘制//////////////////////////////////
    int i;

	// 绘制y轴箭头
	pDC->MoveTo(30,10);
	pDC->LineTo(25,15);
	pDC->MoveTo(30,10);
	pDC->LineTo(35,15);

// 绘制y轴刻度
	for(i=0;i<9;i++)
	{
      
    pDC->MoveTo(30,370-40*i);
	pDC->LineTo(23,370-40*i);


	}

   	
   // 写 y轴刻度值
	
	str.Format("-30");
	pDC->TextOut(5, 370, str);

    str.Format("-20");
	pDC->TextOut(5, 330, str);

    str.Format("-10");
	pDC->TextOut(5, 290, str);



    str.Format("10");
	pDC->TextOut(10, 210, str);


    str.Format("20");
	pDC->TextOut(10, 170, str);

    str.Format("30");
	pDC->TextOut(10, 130, str);


   str.Format("40");
	pDC->TextOut(10, 90, str);

    str.Format("50");
	pDC->TextOut(10, 50, str);

   
    
   
    str.Format("信号量噪比(SQNR)-dB");
	pDC->TextOut(40, 10, str);





/////////////////////显示均匀量化波形//////////////////////////////////////////	

    pDC->SelectObject(pPenBlue);   //用蓝色曲线
    
	double a1,snr;  //a1,snr为实际值,snr单位为dB
    int a,b;    //a1----a,b1-----snr 对应的在屏幕中的坐标
    int m1;//为量化级数   
/////////////////////////////////////
	
	//////  显示M为256时的波形

	m1=256;             //设置初始起点
    a1=4 ;
	snr=::snruniform(a1,m1);  
    a=int(a1/4+30);
	b=int(250-snr*4);
	pDC->MoveTo (a,b);

	for(i=5;i<2048;i++)
	{
      a1=i;
	  snr=::snruniform(a1,m1);
      a=int(a1/4+30);
	  b=int(250-snr*4);
	  pDC->LineTo (a,b);

	}
    str.Format("M=256");
	pDC->TextOut((a-15), (b-20), str);   //显示标注

//////////////////////////////////////////////

/////////////////////////////////////
	
	//////  显示M为64时的波形

    pDC->SelectObject(pPenRed);

    m1=64;             //设置初始起点
    a1=4 ;
	snr=::snruniform(a1,m1);  
    a=int(a1/4+30);
	b=int(250-snr*4);
	pDC->MoveTo (a,b);

	for(i=5;i<2048;i++)
	{
      a1=i;
	  snr=::snruniform(a1,m1);
      a=int(a1/4+30);
	  b=int(250-snr*4);
	  pDC->LineTo (a,b);

	}
    str.Format("M=64");  //显示标注
	pDC->TextOut((a-15), (b+5), str);

////////////////////////////////////////////////////
	
	//////  显示M为128时的波形

    pDC->SelectObject(pPenQing);
    m1=128;             //设置初始起点
    a1=4 ;
	snr=::snruniform(a1,m1);  
    a=int(a1/4+30);
	b=int(250-snr*4);
	pDC->MoveTo (a,b);

	for(i=5;i<2048;i++)
	{
      a1=i;
	  snr=::snruniform(a1,m1);
      a=int(a1/4+30);
	  b=int(250-snr*4);
	  pDC->LineTo (a,b);

	}

    str.Format("M=128");  //显示标注
	pDC->TextOut((a-15), (b+5), str);

/////////////////////////////////////////////////////////////////////////////////
  ////绘制A律十三折线的信号量化噪声功率比  SNR
     

   

    pDC->SelectObject(pPenZ);
                //设置初始起点
    a1=3 ;
	snr=::snralaw(a1);;  
    a=int(a1/4+30);
	b=int(250-snr*4);
	pDC->MoveTo (a,b);

	for(i=4;i<2048;i++)
	{
      a1=i;
	  snr=::snralaw(a1);
      a=int(a1/4+30);
	  b=int(250-snr*4);
	  pDC->LineTo (a,b);

	}
    a1=800 ;
	snr=::snralaw(a1);;  
    a=int(a1/4+30);
	b=int(250-snr*4);

    str.Format("A-law");  //显示标注
	pDC->TextOut(a, (b-20), str);


    
		// 恢复以前的画笔
	pDC->SelectObject(pOldPen);	
	
	// 删除新的画笔
	delete pPenRed;
	delete pPenBlue;
	delete pPenBlack;
    delete pPenZ;
	delete pPenQing;


}

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

void CCommunicationDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	
}




⌨️ 快捷键说明

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