Digitally sampling analogue waveforms in C++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 7 of 7

Thread: Digitally sampling analogue waveforms in C++

  1. #1
    Join Date
    Nov 2008

    Unhappy Digitally sampling analogue waveforms in C++

    First of all, I am a complete newbie to the world of C++ and programming in general, so please go easy on me!

    I have been set the task of creating a simple console program to investigate the effects of digitally sampling analogue waveforms.

    Basically, the sampling effect needs to be investigated using a single cycle of a cos wave (i.e. 0 - 360 degs).

    The program has to display the waveform by printing a character (i.e. '*') at the sampling point, one per row of output.

    The user should be able to enter an odd number of sampling points from a minimum of 3 to a maximum of 37, and should be able to set a scale factor. I.e if the user entered '5' for the number of points and '20' for the scale factor, something similar to this should be seen:

    Waveform analysis program

    Enter number of sample points between 3 and 37: 5

    Enter a scale factor between 20 and 40: 20

    Waveform plot:


    Run again? (Y or N): N

    End of Program

    {{{IGNORE the '---'s before each '*' in the waveform plot, i put these in so the '*'s remain in position when i post this!}}}

    Any help whatsoever, even a starting point, would be gratefully received!!!

    Thanks hugely in advance,


  2. #2
    Join Date
    Aug 2005
    San Diego, CA

    Lightbulb Re: Digitally sampling analogue waveforms in C++

  3. #3
    Join Date
    May 2002

    Re: Digitally sampling analogue waveforms in C++

    Very easy for experienced programmers. Show what you have done already - even if it doesn't work or it's just a start - and we will be glad to help.

  4. #4
    Join Date
    Nov 2008

    Re: Digitally sampling analogue waveforms in C++

    ok guys, sorry about the delay here.

    here is what i've got already. it's almost working fine now with the single problem that when the points are plotted, they just plot in a straight, veritcal line rather than offset at various points (to show the waveform) as they should be!

    any help would be gratefully received

    using namespace std;
    const double pi = acos(-1.0);
    class wave
    double pos;                // pos is position of plotted point
    int sp,sf;                    // sp and sf are sample points and scale factor
    double RA;                 // Radian Angle
    double DA;                 // Degree Angle
    int sposn;                  // centre position
    void plot(int sp);
    int read_sp();
    int read_sf();
    int main()
    char flag;              // flag is user query to run again
    wave w;
    int sp,sf;
    cout<<"\n\t\tCosine Wave Function";
    cout<<"\n Run Again? (y/n) : ";
    return 0;
    void wave::plot(int sp)
    for(int i=0; i<=360; i+=DA)
    for(int j=0;j<pos;j++)
    cout<<" ";
    int wave::read_sp(void)
    cout<<"\n Enter no. of sample points (must be odd & bet. 3-37) : ";
    return (sp);
    cout<<"\n\t ERROR!!";
    int wave::read_sf(void)
    cout<<"\n Enter the scale factor (20-40) : ";
    return (sf);
    cout<<"\n\t ERROR!!";
    Last edited by jp206; November 30th, 2008 at 08:46 AM. Reason: added comments

  5. #5
    Join Date
    May 2002

    Re: Digitally sampling analogue waveforms in C++

    In wave plot() change:

    RA = pi * DA / 180.0;


    RA = pi * i / 180.0;

  6. #6
    Join Date
    Nov 2008

    Re: Digitally sampling analogue waveforms in C++

    awesome thanks a lot! works a charm now.

  7. #7
    Join Date
    Nov 2011

    Re: Digitally sampling analogue waveforms in C++

    how can i implement it using the following code ?

    The output position, in terms of character spacing, of a sample point can be calculated as:
    position = CentrePosition + ScaleFactor x sin(radianAngle)
    where radianAngle = pi x degreeAngle / 180.0
    and pi = acos(-1.0)
    sin and acos are functions of the standard cmath library.
    The calculation of position should produce an integer value in the range 0  80 as 80 is the
    width in characters of the DOS console screen. The sin function accepts angles assumed to
    be in radians and returns values in the range -1.0  +1.0. CentrePosition,
    ScaleFactor and pi should be declared and initialised as constants with suitable values.
    Pseudocode fragments for other key sections of the program:
    Validating user input:
    set errorFlag = false
    display input prompt for noOfPoints
    if noOfPoints < 3 or noOfPoints > 31 or noOfPoints &#37; 2 = 0 then
    display error message
    set errorFlag = true
    end if
    until errorFlag = false
    Outputting the sampled waveform:
    set degreeAngle = 0
    for count = 0 to noOfPoints - 1
    calculate radianAngle
    calculate position
    output symbol into a field of width = position
    increment degreeAngle by 360.0 / noOfPoints - 1
    end for
    The actual C++ code equivalents of the above pseudocode fragments will be dealt with during
    Code fragment for obtaining the system date and time:
    char dateStr[255] = {'\0'};
    time_t timer = time(NULL);
    strcpy(dateStr, asctime(localtime(&timer)));
    After executing this code, the character array dateStr contains the date and time as defined
    by the computer's clock. It can subsequently be displayed on the console screen using cout.
    Required libraries for the assignment: <iostream>, <iomanip>, <cmath>, <cstring>


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

This is a survey!

HTML5 Development Center