1.����ʱ��Դ����ô��
2.基于Matlab HDL Coder的矩阵8x8实矩阵QR分解
����ʱ��Դ����ô��
#include "stdafx.h"
#include "MFCFrame1.h"
#include "MFCFrame1Doc.h"
#include "MFCFrame1View.h"
#include "PointDialog.h"
#include "math.h"
GLUquadricObj *objCylinder = gluNewQuadric();
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMFCFrame1View
IMPLEMENT_DYNCREATE(CMFCFrame1View, CView)
BEGIN_MESSAGE_MAP(CMFCFrame1View, CView)
//{ { AFX_MSG_MAP(CMFCFrame1View)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_SIZE()
ON_COMMAND(IDM_ZIXUAN, OnZixuan)
ON_WM_TIMER()
ON_COMMAND(IDM_ChangDirect, OnChangDirect)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMFCFrame1View construction/destruction
CMFCFrame1View::CMFCFrame1View()
{
// TODO: add construction code here
this->m_GLPixelIndex = 0;
this->m_hGLContext = NULL;
Angle1=0.0;
Angle2=.0;
Timer=0;
x=0.0;
z=0.0;
juli=.0;
}
CMFCFrame1View::~CMFCFrame1View()
{
}
BOOL CMFCFrame1View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style |= (WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMFCFrame1View drawing
/////////////////////////////////////////////////////////////////////////////
// CMFCFrame1View printing
BOOL CMFCFrame1View::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMFCFrame1View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMFCFrame1View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMFCFrame1View diagnostics
#ifdef _DEBUG
void CMFCFrame1View::AssertValid() const
{
CView::AssertValid();
}
void CMFCFrame1View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMFCFrame1Doc* CMFCFrame1View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMFCFrame1Doc)));
return (CMFCFrame1Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMFCFrame1View message handlers
BOOL CMFCFrame1View::SetWindowPixelFormat(HDC hDC)
{
PIXELFORMATDESCRIPTOR pixelDesc=
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|
PFD_DOUBLEBUFFER|PFD_SUPPORT_GDI,
PFD_TYPE_RGBA,
,
0,0,0,0,0,0,
0,
0,
0,
0,0,0,0,
,
0,
0,
PFD_MAIN_PLANE,
0,
0,0,0
};
this->m_GLPixelIndex = ChoosePixelFormat(hDC,&pixelDesc);
if(this->m_GLPixelIndex==0)
{
this->m_GLPixelIndex = 1;
if(DescribePixelFormat(hDC,this->m_GLPixelIndex,sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0)
{
return FALSE;
}
}
if(SetPixelFormat(hDC,this->m_GLPixelIndex,&pixelDesc)==FALSE)
{
return FALSE;
}
return TRUE;
}
int CMFCFrame1View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
HWND hWnd = this->GetSafeHwnd();
HDC hDC = ::GetDC(hWnd);
if(this->SetWindowPixelFormat(hDC)==FALSE)
{
return 0;
}
if(this->CreateViewGLContext(hDC)==FALSE)
{
return 0;
}
return 0;
}
BOOL CMFCFrame1View::CreateViewGLContext(HDC hDC)
{
this->m_hGLContext = wglCreateContext(hDC);
if(this->m_hGLContext==NULL)
{ //创建失败
return FALSE;
}
if(wglMakeCurrent(hDC,this->m_hGLContext)==FALSE)
{ //选为当前RC失败
return FALSE;
}
return TRUE;
}
void CMFCFrame1View::OnDestroy()
{
CView::OnDestroy();
// TODO: Add your message handler code here
if(wglGetCurrentContext()!=NULL)
{
wglMakeCurrent(NULL,NULL);
}
if(this->m_hGLContext!=NULL)
{
wglDeleteContext(this->m_hGLContext);
this->m_hGLContext = NULL;
}
}
void CMFCFrame1View::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
GLsizei width,height;
GLdouble aspect;
width = cx;
height = cy;
if(cy==0)
{
aspect = (GLdouble)width;
}
else
{
aspect = (GLdouble)width/(GLdouble)height;
}
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(.0,aspect,5.0,.0);
}
void CMFCFrame1View::OnDraw(CDC* pDC)
{
CMFCFrame1Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CPaintDC dc(this);
glClearColor(1.0,1.0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,.0,0.0,0.0,0.0,0.0,1.0,0.0);
glRotatef(-.0,1.0,0.0,0.0);/*返回原坐标*/
glTranslatef(-3.0,0.0,0.0);
SwapBuffers(dc.m_ps.hdc);
glDrawBuffer (GL_BACK);
glFlush();
}
void CMFCFrame1View::OnZixuan()
{
// TODO: Add your command handler code here
Timer=1;
SetTimer(1,,NULL);
}
void CMFCFrame1View::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CPaintDC dc(this);
if (Timer==1)
{ Angle1=Angle1-1;
Angle2=Angle2-1;
glClearColor(1.0,1.0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);/**/
glLoadIdentity();
gluLookAt(0.0,.0,0.,0.0,0.0,0.0,0.0,1.0,0.0);
glPushMatrix();
glColor3f(0.7,0.7,0.7);
glTranslatef(0.0,0.0,1.1);
glRotatef(-.0,1.0,0.0,0.0);
glutSolidCone(5.0,0.0,.0,.0); /*底盘*/
glPopMatrix();
glPushMatrix();
glColor3f(0.0,0.0,0.0);
glTranslatef(3.9,0.,1.0); /*刻度*/
glRotatef(.0,0.0,1.0,0.0);
gluCylinder(objCylinder, 0., 0., 0.8, , 9);
glPopMatrix();
glPushMatrix();
glColor3f(0.0,0.0,0.0);
glTranslatef(-4.7,0.,1.0); /*刻度*/
glRotatef(.0,0.0,1.0,0.0);
gluCylinder(objCylinder,0., 0., 0.8, , 9);
glPopMatrix();
glPushMatrix();
glColor3f(0.0,0.0,0.0);
glTranslatef(0.0,0.,-2.9); /*刻度*/
glRotatef(.0,0.0,1.0,0.0);
gluCylinder(objCylinder,0., 0., 0.8, , 9);
glPopMatrix();
glPushMatrix();
glColor3f(0.0,0.0,0.0);
glTranslatef(0.0,0.,5.8); /*刻度*/
glRotatef(.0,0.0,1.0,0.0);
gluCylinder(objCylinder,0., 0.,0.8, , 9);
glPopMatrix();
glPushMatrix();
glColor3f(0.0,1.0,0.0);
glRotatef(.0,0.0,1.0,0.0);
glTranslatef(-0.,0.,0.7); /*时针*/
glRotatef(Angle1/,0.0,1.0,0.0);
gluCylinder(objCylinder, 0., 0., 2.5, , 9);
glPopMatrix();
glPushMatrix();
glColor3f(1.0,0.0,0.0);
glTranslatef(0.0,0.,1.0); /*分针*/
glRotatef(Angle2/,0.0,1.0,0.0);
gluCylinder(objCylinder, 0., 0., 3.5, , 9);
glPopMatrix();
glPushMatrix();
glColor3f(0.0,0.0,0.5);
glTranslatef(0.0,0.,1.0); /*秒针*/
glRotatef(Angle1,0.0,1.0,0.0);
gluCylinder(objCylinder, 0., 0., 4.5, , 9);
glPopMatrix();
SwapBuffers(dc.m_ps.hdc);
glDrawBuffer (GL_BACK);
glFlush();
}
else if(Timer==2)
{ glClearColor(1.0,1.0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT);
if (juli>.0)
{
glMatrixMode(GL_MODELVIEW);/*建立了从世界坐标系到观察坐标系的转换矩阵*/
glLoadIdentity();
gluLookAt(0.0,8.0,juli,0.0,0.0,0.0,0.0,1.0,0.0);
juli=juli-0.1;
glPushMatrix();
glColor3f(0.0,0.0,0.0);
glRotatef(-.0,1.0,0.0,0.0);
glutWireCone(.0,0.0,.0,.0); /*画高度为0的圆锥*/
glPopMatrix();
glPushMatrix();
glColor3f(1.0,0.0,1.0);
glLineWidth(4.0);
glTranslatef(4.0,1.0,0.0);
glutWireOctahedron(); /*画八面体*/
glLineWidth(1.0);
glPopMatrix();
glPushMatrix();
glColor3f(1.0,0.0,1.0);
glTranslatef(0.0,1.1,0.0);
glRotatef(Angle2,0.0,1.0,0.0);
gluCylinder(objCylinder, 1.0, 1.0, .0, , 9); /*画壶*/
glPopMatrix();
}
else if(juli<=.0)
{
Angle2=Angle2+0.;
if (Angle2==.0)
Angle2=0.0;
glMatrixMode(GL_MODELVIEW);/*建立了从世界坐标系到观察坐标系的转换矩阵*/
glLoadIdentity();
x=.0*sin(Angle2);
z=.0*cos(Angle2);
gluLookAt(x,5.0,z,0.0,0.0,0.0,0.0,1.0,0.0);
glPushMatrix();
glColor3f(0.0,0.0,0.0);
glRotatef(-.0,1.0,0.0,0.0);
glutWireCone(.0,0.0,.0,.0); /*画高度为0的圆锥*/
glPopMatrix();
glPushMatrix();
glColor3f(1.0,0.0,1.0);
glLineWidth(4.0);
glTranslatef(4.0,1.0,0.0);
glutWireOctahedron(); /*画八面体*/
glLineWidth(1.0);
glPopMatrix();
glPushMatrix();
glColor3f(1.0,0.0,1.0);
glTranslatef(0.0,1.1,0.0);
gluCylinder(objCylinder, 1.0, 1.0, .0, , 9);
glPopMatrix();
}
SwapBuffers(dc.m_ps.hdc);
glDrawBuffer (GL_BACK);
glFlush();
}
CView::OnTimer(nIDEvent);
}
void CMFCFrame1View::OnChangDirect()
{
// TODO: Add your command handler code here
Timer=2;
SetTimer(1, ,NULL);
}
基于Matlab HDL Coder的8x8实矩阵QR分解
本文阐述了基于Givens旋转的QR分解原理在实际矩阵操作中的应用,特别是时钟针对8x8实矩阵的分解。在计算机视觉和机器学习领域,源码用矩QR分解是阵时钟源重要的矩阵求逆方法,能将矩阵分解为一个正交矩阵Q与一个上三角矩阵R的码用乘积。分解方法主要有三种:Householder transformations、矩阵软文代发源码Givens rotation和Gram-Schmidt orthogonalization。时钟Matlab的源码用矩qr()函数使用的是Householder transformations方法,本文中选择Givens rotation方法进行8x8实矩阵的阵时钟源分解。
实现8x8实矩阵QR分解的码用关键是利用Givens旋转的性质,即计算两个向量之间的矩阵夹角sin和cos函数。在设计中,时钟采用DSP HDL工具包中的源码用矩SGVision源码优化CORDIC模块(Complex to Magnitude-Angle HDL Optimized)来计算Givens函数。由于G矩阵只有四个非零值,阵时钟源因此在嵌套循环中,码用Q和R的每次更新仅涉及两行/列,其余保持不变。通过每次读取需要更新的行/列与2x2的sub G矩阵相乘来完成更新过程。
接下来进行的automator源码是控制模块设计,包括QR分解的外部接口,如U_col_?、V_col_?、start_pulse、start_svd、R_rd_ext、mlocate源码Q_rd_ext、R_col、Q_row和qrd_done等。FSM(Finite State Machine)用于控制整个分解过程,从idle状态开始,装载矩阵U和单位矩阵V,ltrace源码然后发送start_svd信号启动分解。接着进入read状态读取Givens矩阵所需的R分量,更新Q和R矩阵,直至循环完成回到idle状态等待下一轮分解。
嵌套计数器设计相对简单,主要关注点在于Q和R的存储方式,分别在8个RAM中按行和列存储。整体设计的性能评估显示,完成一次8x8矩阵分解需要次循环,大约个时钟周期。设计的不足之处在于一些触发器的添加,这是由于对HDL Coder模块功能理解的局限性。
总结来说,本文通过实例阐述了基于Givens旋转的8x8实矩阵QR分解的实现,从原理、设计到控制模块,以及性能评估,展现了矩阵分解在计算机视觉和机器学习应用中的重要性。通过使用HDL Coder和DSP HDL工具包,作者强调了这种工具在算法实现和硬件设计中的便利性,有助于初学者建立硬件思维,提高开发效率。