![]() |
|
Code examples |
High Level Code generation in C, C++, Visual C++ |
![]() Create your own MFC Dialog Project SDI name:Sine You need a Timer with the code to calculate the sine value: void CSinusView::OnTimer(UINT nIDEvent) { // TODO: iTime++; if(iTime >= 800) iTime=800; arrSinus[iTime] = 80 * sin(2*3.14 * iTime/200); Invalidate(); CView::OnTimer(nIDEvent); } The Timer needs to be initialized: void CSinusView::OnInitialUpdate() { CView::OnInitialUpdate(); // TODO: SetTimer(1,50,0); } we need variables: declared as globals in the CPP file do not forget to include the "math.h" #include "math.h" #ifdef _DEBUG #define new DEBUG_NEW #endif int arrSinus[1000]; int iTime=0; now the code for the OnDraw function must be inserted void CSinusView::OnDraw(CDC* pDC) { CSinusDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; // TODO: #define xx0 30 #define yy0 100 pDC->TextOut(30,105,"SINUS LINE"); CString strx; strx.Format("Step = %d",iTime); pDC->TextOut(30,120,strx); pDC->MoveTo(xx0,yy0); pDC->LineTo(xx0+800,yy0); int xx=0; int yy; while(xx <= iTime) { yy = arrSinus[xx]; if(xx==0) pDC->MoveTo(xx0+xx,yy0-yy); else pDC->LineTo(xx0+xx,yy0-yy); xx++; } } ![]() For sinus line with third harmonic you must only change one line void CsinusView::OnTimer(UINT nIDEvent) { iTime++; if(iTime >= 800) iTime=800; arrSinus[iTime] = 90 * sin(2*3.14 * iTime/200) + 30 * sin(3*2*3.14 * iTime/200); Invalidate(); CView::OnTimer(nIDEvent); } the base frequency has an amplitude of 90, the 3th harmonic a amplitude of 30 the following line is with the 5th harmonic arrSinus[iTime] = 90 * sin(2*3.14 * iTime/200) + 30 * sin(5*2*3.14 * iTime/200); ![]() harmonics from 1st to 17th from the previous example you must only modify the OnTimer function void CSinusView::OnTimer(UINT nIDEvent) { iTime++; if(iTime >= 800) iTime=800; int xx=1; double amplitude; double dSinus; dSinus = 0; while(xx <= 17) { amplitude = 90.0 / xx; dSinus += amplitude * sin(xx * 2*3.14 * iTime/200); xx+=2; } arrSinus[iTime] = dSinus; Invalidate(); CView::OnTimer(nIDEvent); } ![]() harmonics from 1 to 99 change one line while(xx <= 99) ![]() declare some variables as global or as class variables double arrDtmf[1000]; int freq1=697,freq2=1209; #define PI 3.14159 the rest is all in the OnDraw function void CdtmfView::OnDraw(CDC* pDC) { CdtmfDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; CString strx; double dPeriod1,dPeriod2; dPeriod1 = 1000.0/freq1; dPeriod2 = 1000.0/freq2; strx.Format("Freq1 = %d Hz Freq2 = %dHz ",freq1,freq2); pDC->TextOut(50,10,strx); strx.Format("period1=%5.2fmsec period2=%5.2fmsec",dPeriod1,dPeriod2); pDC->TextOut(50,25,strx); #define xx0 30 #define xmax 950 #define yy0 150 #define ymax 100 pDC->MoveTo(xx0,yy0); pDC->LineTo(xx0+xmax,yy0); pDC->MoveTo(xx0,yy0); pDC->LineTo(xx0,yy0-ymax); pDC->LineTo(xx0,yy0+ymax); int index=0; pDC->MoveTo(xx0,yy0); while(index <= 950) // from 0 to 95,0 msec { // sin( 2 * pi * freq1 * t) time microseconds arrDtmf[index] = sin((2*PI*freq1*index)/10000); arrDtmf[index] += sin((2*PI*freq2*index)/10000); arrDtmf[index] /= 2; pDC->LineTo(xx0+index, yy0 - (int)(arrDtmf[index]*100)); index++; } } ![]() declare some variables as global or as class variables double sinb1; double cosa1; double arrHarmonics[300]; int harmonic; you must change the OnDraw from the previous example void CdtmfView::OnDraw(CDC* pDC) { CdtmfDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; CString strx; double dPeriod1,dPeriod2; dPeriod1 = 1000.0/freq1; dPeriod2 = 1000.0/freq2; strx.Format("Freq1 = %d Hz Freq2 = %dHz ",freq1,freq2); pDC->TextOut(50,10,strx); strx.Format("period1=%5.2fmsec period2=%5.2fmsec",dPeriod1,dPeriod2); pDC->TextOut(50,25,strx); #define xx0 30 #define xmax 950 #define yy0 100 #define ymax 50 pDC->MoveTo(xx0,yy0); pDC->LineTo(xx0+xmax,yy0); pDC->MoveTo(xx0,yy0); pDC->LineTo(xx0,yy0-ymax); pDC->LineTo(xx0,yy0+ymax); int index=0; pDC->MoveTo(xx0,yy0); while(index <= 950) // from 0 to 95,0 msec { arrDtmf[index] = sin((2*PI*freq1*index)/10000); arrDtmf[index] += sin((2*PI*freq2*index)/10000); arrDtmf[index] /= 2; pDC->LineTo(xx0+index, yy0 - (int)(arrDtmf[index]*ymax)); index++; } #define yy02 250 pDC->MoveTo(xx0,yy02); pDC->LineTo(xx0+xmax,yy02); pDC->MoveTo(xx0,yy02); pDC->LineTo(xx0,yy02-ymax); pDC->MoveTo(xx0,yy02); CalcSpectrum(); index=0; while(index < 300) { pDC->LineTo(xx0+index * 3 , yy02 - (arrHarmonics[index] * 200)); index++; } } you must insert a new function CalcSpectrum void CdtmfView::CalcSpectrum() { int index; harmonic = 1; while(harmonic < 300) { index=0; sinb1=0; cosa1=0; while(index < 950) { sinb1 += arrDtmf[index] * sin((harmonic * 2 * PI * index)/950); cosa1 += arrDtmf[index] * cos((harmonic * 2 * PI * index)/950); index++; } sinb1 *=2; sinb1 /= 950; cosa1 *=2; cosa1 /= 950; arrHarmonics[harmonic]=sqrt(sinb1*sinb1 + cosa1*cosa1); harmonic++; } } AMI Alternate Mark Inversion RZ Return to Zero NRZ Not Return to Zero BNZR Binary n (4) zero substitution ![]() I am using this program for exercises, the charakter of a source text line are converted to bits and than into the signal the students solve this, you can see the result in the next image The Kvalue range is from 1 to 22, the Kvalue is used as index into the source text line. ![]() download source and EXE for Visual C++ 6.0 ASK Amplitude Shift Key In this example we use the signal from RS232 as source for the modulation. The begin starts always with a Startbit followed by eight bits of one ASCII-Code, LSB (lowest significant bit) first and MSB (most significant bit) last. For example the character ‘A’ binary 01000001 will be send as 10000010, with Start- and Stopbit 0100000101. This ASK sends the carrier with logic input ONE and no carrier with logic input ZERO. This ASK modulation is also named OOK, ON OFF KEY ![]() FSK Frequency Shift Key The FSK uses two frequencies, a high frequency for logic input ZERO and a low frequency for logic input ONE. This type of modulation can be generated with a PLL (Phase Locked Loop) circuit. To demodulate the signal we can also use a PLL with a modified circuit. download source and EXE for Visual C++ 6.0 QAM Quadrature Amplitude Modulation QDPSK Quad Differential Phase Shift Key Modulation exercise to find out the source code download source and EXE for VisualC++.net 193KBytes This example demonstrates the theory of UMTS code generation. We use four vectors. Every bit from ASCII code is multiplied with his vector and added to the other results. In this manner we create the chips. Every chip contains the information of one bit of four ASCII bytes. download source and EXE for VisualC++.net 190KBytes ![]() dialog program to calculate the sine table for 3 phase inverter the number of points must be a multiply of six this table is a simple sine table used from µC 908HCMR32 to create the pulse train for the power stage a bridge with six Mosfets or IGBTs. other tables adds 3th harmonics, this gives more voltage ;******** sine table with 192 base points SINE_TABLE DB 128T,132T,136T,140T,145T,149T,153T,157T DB 161T,165T,169T,173T,177T,180T,184T,188T DB 192T,195T,199T,202T,205T,209T,212T,215T DB 218T,221T,223T,226T,229T,231T,234T,236T DB 238T,240T,242T,244T,245T,247T,248T,250T DB 251T,252T,253T,253T,254T,254T,255T,255T DB 255T,255T,255T,254T,254T,253T,253T,252T DB 251T,250T,248T,247T,245T,244T,242T,240T DB 238T,236T,234T,231T,229T,226T,223T,221T DB 218T,215T,212T,209T,205T,202T,199T,195T DB 191T,188T,184T,180T,177T,173T,169T,165T DB 161T,157T,153T,149T,145T,140T,136T,132T DB 128T,124T,120T,116T,111T,107T,103T,099T DB 095T,091T,087T,083T,079T,076T,072T,068T DB 064T,061T,057T,054T,051T,047T,044T,041T DB 038T,035T,033T,030T,027T,025T,022T,020T DB 018T,016T,014T,012T,011T,009T,008T,006T DB 005T,004T,003T,003T,002T,002T,001T,001T DB 001T,001T,001T,002T,002T,003T,003T,004T DB 005T,006T,008T,009T,011T,012T,014T,016T DB 018T,020T,022T,025T,027T,030T,033T,035T DB 038T,041T,044T,047T,051T,054T,057T,061T DB 065T,068T,072T,076T,079T,083T,087T,091T DB 095T,099T,103T,107T,111T,116T,120T,124T Create a dialogue window with a button and a relative message routine |