www.pudn.com > fftanalysis > FrequencyShowDlg.cpp, change:2003-04-17,size:12943b


// FrequencyShowDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "FrequencyShow.h" 
#include "FrequencyShowDlg.h" 
 
#include "math.h" 
#include "SoundOut.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
CSoundOut	  SoundOut; 
void CALLBACK DspCallBack(int m_Toggle,DWORD pParam); 
CFile mfile; 
 
///////////////////////////////////////////////////////////////////////////// 
// 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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CFrequencyShowDlg dialog 
 
CFrequencyShowDlg::CFrequencyShowDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CFrequencyShowDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CFrequencyShowDlg) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
	m_bPlay=FALSE; 
	m_bPause=FALSE; 
} 
 
void CFrequencyShowDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CFrequencyShowDlg) 
		// NOTE: the ClassWizard will add DDX and DDV calls here 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CFrequencyShowDlg, CDialog) 
	//{{AFX_MSG_MAP(CFrequencyShowDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_FILE, OnFile) 
	ON_BN_CLICKED(IDC_PLAY, OnPlay) 
	ON_BN_CLICKED(IDC_STOP, OnStop) 
	ON_BN_CLICKED(IDC_PAUSE, OnPause) 
	ON_BN_CLICKED(IDC_FF, OnFf) 
	ON_BN_CLICKED(IDC_FFW, OnFfw) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CFrequencyShowDlg message handlers 
 
BOOL CFrequencyShowDlg::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) 
	{ 
		pSysMenu->RemoveMenu(4,MF_BYPOSITION); 
		pSysMenu->RemoveMenu(2,MF_BYPOSITION); 
		pSysMenu->RemoveMenu(0,MF_BYPOSITION); 
		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 
	CRect rect; 
	GetDlgItem(IDC_OSCOPE)->GetWindowRect(rect) ; 
	ScreenToClient(rect) ; 
	m_OScopeCtrl.Create(WS_VISIBLE | WS_CHILD, rect, this) ;  
	m_OScopeCtrl.SetScale(0,512,0,256,"",""); 
	m_OScopeCtrl.SetTitle("Y幅度 X频率") ; 
	m_OScopeCtrl.SetBackColor(RGB(0, 0, 64)) ; 
	m_OScopeCtrl.SetGridColor(RGB(192, 192, 255)) ; 
	m_OScopeCtrl.SetWaveColor(RGB(255, 255, 255)) ;	 
	 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CFrequencyShowDlg::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 CFrequencyShowDlg::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 CFrequencyShowDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
void CFrequencyShowDlg::OnFile()  
{ 
	// TODO: Add your control notification handler code here 
	CFileDialog dlg(TRUE,_T("WAV"),_T("*.WAV"),OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,_T("WAVE声音文件(*.WAV)|*.WAV|")); 
	if(IDOK==dlg.DoModal()) 
		m_FileName=dlg.GetPathName();		 
} 
 
void CFrequencyShowDlg::OnPlay()  
{ 
	// TODO: Add your control notification handler code here 
	if(m_bPlay) 
	{ 
		if(m_bPause) 
		{ 
			SoundOut.StartOutput(); 
			m_bPause=FALSE; 
			return; 
		} 
		else 
		{ 
			SoundOut.CloseOutput(); 
			mfile.Close(); 
			m_bPlay=FALSE; 
		} 
	} 
	long length_riff; 
	long length_fmt; 
	int  s_bit; 
	int  s_rate; 
	int  s_channel; 
	long d_rate; 
	long length_data; 
	 
	int i,j; 
	unsigned char htt[59]; 
	for(i=0;i<59;i++)	htt[i]=0; 
	if(mfile.Open(m_FileName,CFile::modeReadWrite|CFile::typeBinary,NULL)==FALSE) 
	{ 
		MessageBox("文件打开失败!"); 
		return ; 
	} 
	if(mfile.Read(htt,58)!=58) 
	{ 
		MessageBox("文件读写错误!"); 
		return ; 
	} 
	if(htt[0]!='R'||htt[1]!='I'||htt[2]!='F'||htt[3]!='F'||htt[8]!='W'||htt[9]!='A'||htt[10]!='V'||htt[11]!='E'||htt[12]!='f'||htt[13]!='m'||htt[14]!='t'||htt[15]!=32) 
	{ 
		MessageBox("文件格式错误!"); 
		return ; 
	} 
	length_riff=htt[4]+htt[5]*256+htt[6]*65536+htt[7]*16777216; 
	length_fmt=htt[16]+htt[17]*256; 
	if(length_fmt!=16||htt[20]!=1||htt[21]!=0) 
	{ 
		MessageBox("文件格式错误!"); 
		return ; 
	} 
	s_channel=htt[22]+htt[23]*256; 
	s_rate=htt[24]+htt[25]*256+htt[26]*65536+htt[27]*16777216; 
	d_rate=htt[28]+htt[29]*256+htt[30]*65536+htt[31]*16777216; 
	s_bit=htt[34]+htt[35]*256; 
	//含fact数据段 
	if(htt[38]=='f'&&htt[39]=='a'&&htt[40]=='c'&&htt[41]=='t') 
	{ 
		length_data=htt[46]+htt[47]*256+htt[48]*65536+htt[49]*16777216; 
		if(htt[46]!=htt[54]||htt[47]!=htt[55]||htt[48]!=htt[56]||htt[49]!=htt[57]) 
		{ 
			MessageBox("数据长度错误!"); 
			return ; 
		} 
		mfile.Seek(58,0); 
	} 
	//不含fact数据段 
	else if(htt[36]=='d'&&htt[37]=='a'&&htt[38]=='t'&&htt[39]=='a') 
	{ 
		length_data=htt[40]+htt[41]*256+htt[42]*65536+htt[43]*16777216; 
		mfile.Seek(44,0); 
	} 
	 
	SoundOut.CloseOutput();	 
	//准备声音输出 
	nChannel=s_channel; 
	nSampleRate=s_rate; 
	nBit=s_bit; 
	SoundOut.WaveInitFormat(s_channel,s_rate,s_bit); 
	mfile.ReadHuge(SoundOut.m_WaveBuffer[0],WAVEOUT_LENGTH); 
	mfile.ReadHuge(SoundOut.m_WaveBuffer[1],WAVEOUT_LENGTH); 
	SoundOut.m_Toggle=1; 
	 
	//设置波形窗口 
	m_OScopeCtrl.SetScale(0,(float)(nSampleRate/2000.0),0,256,"kHz",""); 
	m_OScopeCtrl.SetTitle("Y幅度 X频率") ; 
	for(i=0;i<5;i++) 
	for(j=0;j<256;j++) 
		tts[i][j]=0; 
 
	//启动线程 
	SoundOut.SetCallBack(DspCallBack,(DWORD)this); 
	SoundOut.OpenOutput(); 
	m_bPlay=TRUE; 
	m_bPause=FALSE; 
} 
 
void CFrequencyShowDlg::OnStop()  
{ 
	if(m_bPlay) 
	{ 
		SoundOut.CloseOutput(); 
		mfile.Close(); 
		m_bPlay=FALSE; 
		m_bPause=FALSE; 
	} 
} 
 
void CFrequencyShowDlg::OnPause()  
{ 
	if(m_bPlay) 
	{ 
		m_bPause=TRUE; 
		SoundOut.StopOutput(); 
	} 
} 
 
void CFrequencyShowDlg::OnFf()  
{ 
	mfile.Seek((nBit/8)*10*nChannel*nSampleRate,CFile::current); 
} 
 
void CFrequencyShowDlg::OnFfw()  
{ 
	mfile.Seek(-(nBit/8)*10*nChannel*nSampleRate,CFile::current);	 
} 
 
void CFrequencyShowDlg::OnCancel()  
{ 
	if(m_bPlay==TRUE) 
	{ 
		SoundOut.CloseOutput(); 
		mfile.Close(); 
	} 
	CDialog::OnCancel(); 
} 
 
void CFrequencyShowDlg::ShowFrequency() 
{ 
	complex ss1[512]; 
	complex	ss2[512]; 
	int		tt[256]; 
	int i,j,k,m; 
	if(nChannel==1) 
	{ 
		if(nBit==8) 
		{ 
			for(i=0;i<512;i++) 
			{ 
				ss2[i].Re=0; 
				ss2[i].Im=0; 
			} 
			for(i=0;i<WAVEOUT_LENGTH/512;i++) 
			{ 
				k=i*512; 
				for(j=0;j<512;j++) 
				{ 
					m=SoundOut.m_WaveBuffer[SoundOut.m_Toggle][j+k]; 
					ss1[j].Re=(float)m; 
					ss1[j].Im=0; 
				} 
				Fft(ss1,9); 
				for(j=0;j<256;j++) 
				{ 
					tts[0][j]=tts[1][j]; 
					tts[1][j]=tts[2][j]; 
					tts[2][j]=tts[3][j]; 
					tts[3][j]=tts[4][j]; 
					tts[4][j]=(int)((sqrt(ss1[j].Re*ss1[j].Re+ss1[j].Im*ss1[j].Im))); 
					tt[j]=(tts[0][j]+tts[1][j]+tts[2][j]+tts[3][j]+tts[4][j])/80; 
					if(tt[j]>255)	tt[j]=255; 
				} 
				m_OScopeCtrl.SetData(tt); 
			} 
		} 
		else if(nBit==16) 
		{ 
			for(i=0;i<512;i++) 
			{ 
				ss2[i].Re=0; 
				ss2[i].Im=0; 
			} 
			for(i=0;i<WAVEOUT_LENGTH/1024;i++) 
			{ 
				k=i*1024; 
				for(j=0;j<512;j++) 
				{ 
					m=*(SHORT *)(&SoundOut.m_WaveBuffer[SoundOut.m_Toggle][j*2+k]); 
					ss1[j].Re=(float)m; 
					ss1[j].Im=0; 
				} 
				Fft(ss1,9); 
				for(j=0;j<256;j++) 
				{ 
					tts[0][j]=tts[1][j]; 
					tts[1][j]=tts[2][j]; 
					tts[2][j]=tts[3][j]; 
					tts[3][j]=tts[4][j]; 
					tts[4][j]=(int)((sqrt(ss1[j].Re*ss1[j].Re+ss1[j].Im*ss1[j].Im))); 
					tt[j]=(tts[0][j]+tts[1][j]+tts[2][j]+tts[3][j]+tts[4][j])/80; 
					if(tt[j]>255)	tt[j]=255; 
				} 
				m_OScopeCtrl.SetData(tt); 
			} 
		} 
	} 
	else if(nChannel==2) 
	{ 
		if(nBit==8) 
		{ 
			for(i=0;i<512;i++) 
			{ 
				ss2[i].Re=0; 
				ss2[i].Im=0; 
				//ss3[i].Re=0; 
				//ss3[i].Im=0; 
			} 
			for(i=0;i<WAVEOUT_LENGTH/1024;i++) 
			{ 
				k=i*1024; 
				for(j=0;j<512;j++) 
				{ 
					m=SoundOut.m_WaveBuffer[SoundOut.m_Toggle][j*2+k]; 
					ss1[j].Re=(float)m; 
					ss1[j].Im=0; 
				} 
				Fft(ss1,9); 
				for(j=0;j<256;j++) 
				{ 
					tts[0][j]=tts[1][j]; 
					tts[1][j]=tts[2][j]; 
					tts[2][j]=tts[3][j]; 
					tts[3][j]=tts[4][j]; 
					tts[4][j]=(int)((sqrt(ss1[j].Re*ss1[j].Re+ss1[j].Im*ss1[j].Im))); 
					tt[j]=(tts[0][j]+tts[1][j]+tts[2][j]+tts[3][j]+tts[4][j])/80; 
					if(tt[j]>255)	tt[j]=255; 
				} 
				m_OScopeCtrl.SetData(tt); 
			} 
		} 
		else if(nBit==16) 
		{ 
			for(i=0;i<512;i++) 
			{ 
				ss2[i].Re=0; 
				ss2[i].Im=0; 
				//ss3[i].Re=0; 
				//ss3[i].Im=0; 
			} 
			for(i=0;i<WAVEOUT_LENGTH/2048;i++) 
			{ 
				k=i*2048; 
				for(j=0;j<256;j++) 
				{ 
					m=*(SHORT *)(&SoundOut.m_WaveBuffer[SoundOut.m_Toggle][j*4+k]); 
					ss1[j].Re=(float)m; 
					ss1[j].Im=0; 
				} 
				Fft(ss1,9); 
				for(j=0;j<256;j++) 
				{ 
					tts[0][j]=tts[1][j]; 
					tts[1][j]=tts[2][j]; 
					tts[2][j]=tts[3][j]; 
					tts[3][j]=tts[4][j]; 
					tts[4][j]=(int)((sqrt(ss1[j].Re*ss1[j].Re+ss1[j].Im*ss1[j].Im))); 
					tt[j]=(tts[0][j]+tts[1][j]+tts[2][j]+tts[3][j]+tts[4][j])/80; 
					if(tt[j]>255)	tt[j]=255; 
				} 
				m_OScopeCtrl.SetData(tt); 
			} 
		} 
	} 
} 
 
void CFrequencyShowDlg::Reverse(complex *a, int n) 
{ 
	int	nv2=n/2; 
	int nm1=n-1; 
	int i,j,k=0; 
	complex t; 
	i=j=1; 
	//按位逆序 
	for(i=1;i<=nm1;i++) 
	{ 
		if(i<j) 
		{ 
			t=a[j-1]; 
			a[j-1]=a[i-1]; 
			a[i-1]=t; 
		} 
		k=nv2; 
		while(k<j) 
		{ 
			j-=k; 
			k/=2; 
		} 
		j+=k; 
	} 
} 
 
void CFrequencyShowDlg::Fft(complex *a, int m) 
{ 
	complex u,w,t; 
	int le,le1,i,j,ip; 
	int n=(int)pow(2,m); 
	Reverse(a,n); 
	int l=1; 
	le=1; 
	while(l<=m) 
	{ 
		le1=le; 
		le=le<<1; 
		u.Re=1.0f; 
		u.Im=0.0f; 
		w.Re=(float)cos(PI/le1); 
		w.Im=-(float)sin(PI/le1); 
		j=1; 
		while(j<=le1) 
		{ 
			i=j; 
			while(i<=n) 
			{ 
				ip=i+le1; 
				//a*u 
				t.Re=(float)a[ip-1].Re*u.Re-a[ip-1].Im*u.Im; 
				t.Im=(float)a[ip-1].Re*u.Im+a[ip-1].Im*u.Re; 
				//a-t 
				a[ip-1].Re=(float)a[i-1].Re-t.Re; 
				a[ip-1].Im=(float)a[i-1].Im-t.Im; 
				//a+t 
				a[i-1].Re+=t.Re; 
				a[i-1].Im+=t.Im; 
				i+=le; 
			} 
			float temp=u.Re; 
			//u*w 
			u.Re=(float)u.Re*w.Re-u.Im*w.Im; 
			u.Im=(float)temp*w.Im+u.Im*w.Re; 
			j++; 
		} 
		l++; 
	} 
	for(i=0;i<n;i++) 
	{ 
		a[i].Re=a[i].Re/n; 
		a[i].Im=a[i].Im/n; 
	} 
} 
 
//WAVE播放回调函数 
#define PT_S ((CFrequencyShowDlg*)pParam)  
void CALLBACK DspCallBack(int m_Toggle,DWORD pParam) 
{ 
	if(PT_S->m_bPlay) 
	{ 
		DWORD fpos; 
		DWORD flen; 
		fpos=mfile.GetPosition(); 
		flen=mfile.GetLength(); 
		if(flen-fpos>=WAVEOUT_LENGTH) 
		{ 
			mfile.Read(SoundOut.m_WaveBuffer[m_Toggle],WAVEOUT_LENGTH); 
			PT_S->ShowFrequency(); 
		} 
		else 
		{ 
			SoundOut.CloseOutput(); 
			mfile.Close(); 
			PT_S->m_bPlay=FALSE; 
		} 
	} 
}