Code examples

High Level Code generation in C, C++, Visual C++
draw sin2 line VisualC++ 6.0


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++;
        }
}
Draw sine line with 3th harmonic


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);
draw rectangle with sine lines


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)
2 draw DTMF signal with 2 sine lines

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++;
        }
}
5 Fourier analysis DTMF signal

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++;        
        }
}
3 Manchester AMI RZ NRZ
Manchester code
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 FSK modulation

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 QDPSK modulation

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
Create UMTS code
UMTS
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
4 Sine table with 192 points

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

insert the code below in this routine
to calculate the sine function we need to include the "math.h"
the variables like "char cBuffer[50000];" are declared as global
the zero line has the value 128, from 128 to 255 the sine is positive
under 128 the sine is negative
#include "math.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif char cBuffer[50000]; char *ptrx; #define CR 0x0D #define LF 0x0A void CCalcTabDlg::OnButCalc() { double pi=3.14159265359; double value; int ii; int SinusMaxTab = 192; int Umax = 127; int sinus; int MaxRow; ptrx = cBuffer; ptrx += sprintf(ptrx,";******** sine table with 192 base points %c%c",CR,LF); ptrx += sprintf(ptrx,"SINE_TABLE%c%c",CR,LF); MaxRow=0; ii=0; while(ii < (SinusMaxTab/2) ) { value = Umax * sin((2 * pi) /SinusMaxTab * ii ); sinus = int(value + 0.5) + 128 ; if(!MaxRow)ptrx += sprintf(ptrx,"%c%c DB %03dT",CR,LF,sinus); else ptrx += sprintf(ptrx,",%03dT",sinus); MaxRow++; if(MaxRow == 8) MaxRow=0; ii++; } ptrx += sprintf(ptrx,"%c%c",CR,LF); MaxRow=0; while(ii < SinusMaxTab ) { value = Umax * sin((2 * pi) /SinusMaxTab * ii ); sinus = int(value - 0.5) + 128; if(!MaxRow)ptrx += sprintf(ptrx,"%c%c DB %03dT",CR,LF,sinus); else ptrx += sprintf(ptrx,",%03dT",sinus); MaxRow++; if(MaxRow == 8) MaxRow=0; ii++; } ptrx += sprintf(ptrx,"%c%c",CR,LF); CString m_strOutfilename="sinus192.inc"; CFile m_file; if (!m_file.Open(m_strOutfilename,CFile::modeCreate|CFile::modeWrite,NULL)) { AfxMessageBox("Error to open file"); } else { m_file.Write(cBuffer,ptrx - cBuffer); } }