Simple Sine Wave Array

Discussion in 'C++' started by Phil Newman, Nov 2, 2005.

  1. Phil Newman

    Phil Newman Guest

    I'm trying to implement a basic sinewave signal using arrays, but I'm
    having difficulty (I'm a complete beginner!)

    This is what I have so far:

    using namespace std;

    double signal(double sinewave, double x);

    int main()
    {

    double x, sinewave, output;

    cout<<endl<<"Please enter a value, x"<<endl<<endl;
    cin>>x;

    output = signal(sinewave,x);
    cout<<endl<<"Sin(x) = "<<output<<endl<<endl;

    return 0;
    }

    double signal(double sinewave, double x)
    {

    sinewave = sin(x);
    return sinewave;
    }

    I want to swap a single value x, for an array of 100 points.

    Can anyone help me with this?

    Later, I'll need to change the frequency of the signal, but this
    doesn't matter now? Also, I'll want to plot the signal, but I think
    this is advanced.

    If anyone can help, I would really appreciated it!

    Phil Newman
     
    Phil Newman, Nov 2, 2005
    #1
    1. Advertising

  2. Phil Newman

    mlimber Guest

    Phil Newman wrote:
    > I'm trying to implement a basic sinewave signal using arrays, but I'm
    > having difficulty (I'm a complete beginner!)
    >
    > This is what I have so far:
    >
    > using namespace std;
    >
    > double signal(double sinewave, double x);
    >
    > int main()
    > {
    >
    > double x, sinewave, output;
    >
    > cout<<endl<<"Please enter a value, x"<<endl<<endl;
    > cin>>x;
    >
    > output = signal(sinewave,x);
    > cout<<endl<<"Sin(x) = "<<output<<endl<<endl;
    >
    > return 0;
    > }
    >
    > double signal(double sinewave, double x)
    > {
    >
    > sinewave = sin(x);
    > return sinewave;
    > }
    >
    > I want to swap a single value x, for an array of 100 points.
    >
    > Can anyone help me with this?
    >
    > Later, I'll need to change the frequency of the signal, but this
    > doesn't matter now? Also, I'll want to plot the signal, but I think
    > this is advanced.
    >
    > If anyone can help, I would really appreciated it!
    >
    > Phil Newman


    Try this:

    #include <iostream>
    #include <vector>
    #include <cmath>

    using namespace std;

    void FillSin( vector<double>& v )
    {
    static const double PI = 4*atan(1.0);
    for( int n=0; n < output.size(); ++n )
    {
    v[ n ] = sin( 2*PI / n );
    }
    }

    int main()
    {
    vector<double> output( 100 );
    FillSin( output );
    for( int n=0; n < output.size(); ++n )
    {
    cout << output[n] << '\n';
    }
    return 0;
    }

    Cheers! --M
     
    mlimber, Nov 2, 2005
    #2
    1. Advertising

  3. mlimber wrote:
    > Phil Newman wrote:
    >
    >>I'm trying to implement a basic sinewave signal using arrays, but I'm
    >>having difficulty (I'm a complete beginner!)
    >>
    >>This is what I have so far:
    >>
    >> [snip]
    >>I want to swap a single value x, for an array of 100 points.
    >>
    >>Can anyone help me with this?
    >>
    >>Later, I'll need to change the frequency of the signal, but this
    >>doesn't matter now? Also, I'll want to plot the signal, but I think
    >>this is advanced.
    >>
    >>If anyone can help, I would really appreciated it!
    >>
    >>Phil Newman

    >
    >
    > Try this:
    >
    > #include <iostream>
    > #include <vector>
    > #include <cmath>
    >
    > using namespace std;
    >
    > void FillSin( vector<double>& v )
    > {
    > static const double PI = 4*atan(1.0);
    > for( int n=0; n < output.size(); ++n )
    > {
    > v[ n ] = sin( 2*PI / n );


    Division by Zero !!

    > }
    > }
    >
    > int main()
    > {
    > vector<double> output( 100 );
    > FillSin( output );
    > for( int n=0; n < output.size(); ++n )
    > {
    > cout << output[n] << '\n';
    > }
    > return 0;
    > }


    Stefan
    --
    Stefan Naewe
    naewe.s_AT_atlas_DOT_de
     
    =?ISO-8859-1?Q?Stefan_N=E4we?=, Nov 2, 2005
    #3
  4. Phil Newman

    Phil Newman Guest

    Thanks. Unfortunately, the code won't compile. i get an error saying:


    In function 'void FillSin(std::vector<double, std::allocator<double>
    >&)':


    'output' undeclared

    Can you help there?

    Thanks,

    Phil
     
    Phil Newman, Nov 2, 2005
    #4
  5. [...]

    > #include <iostream>
    > #include <vector>
    > #include <cmath>
    >
    > using namespace std;
    >
    > void FillSin( vector<double>& v )
    > {
    > static const double PI = 4*atan(1.0);
    > for( int n=0; n < output.size(); ++n )

    ^^^

    this wont compile
    you mean v, not output

    > {
    > v[ n ] = sin( 2*PI / n );
    > }


    {} are optional
    matter of personal preference

    > }
    >
    > int main()
    > {
    > vector<double> output( 100 );
    > FillSin( output );
    > for( int n=0; n < output.size(); ++n )
    > {
    > cout << output[n] << '\n';
    > }
    > return 0;
    > }


    I have not used C++ for ages but I recall that one could use
    std::transform on a vector
    something like
    #include <functional>
    or
    #include <algorithm>
    std::transform(v.begin(), v.end(), std::sin)

    and see the result
    std::copy(v.begin(), s.end(), std::eek:stream<int>(std::cout, " : "));


    hth, Daniel

    ps: well about ploting C++ has nothing built-in, nothing standard so to
    say, you will have to use yours op libraries
    nowdays I code in python there you could use Tk or ploting modules
    matplotlib, scipy and Numeric
     
    =?ISO-8859-1?Q?Daniel_Sch=FCle?=, Nov 2, 2005
    #5
  6. Phil Newman

    Marcus Kwok Guest

    > mlimber <> wrote:
    >> #include <iostream>
    >> #include <vector>
    >> #include <cmath>
    >>
    >> using namespace std;
    >>
    >> void FillSin( vector<double>& v )
    >> {
    >> static const double PI = 4*atan(1.0);
    >> for( int n=0; n < output.size(); ++n )
    >> {
    >> v[ n ] = sin( 2*PI / n );
    >> }
    >> }
    >>
    >> int main()
    >> {
    >> vector<double> output( 100 );
    >> FillSin( output );
    >> for( int n=0; n < output.size(); ++n )
    >> {
    >> cout << output[n] << '\n';
    >> }
    >> return 0;
    >> }


    Phil Newman <> wrote:
    > Thanks. Unfortunately, the code won't compile. i get an error saying:
    >
    > In function 'void FillSin(std::vector<double, std::allocator<double>
    >>&)':

    >
    > 'output' undeclared


    Please quote the message you are replying to (I have pasted it in
    manually).

    In FillSin, either change it to:

    void FillSin( vector<double>& output )

    or in the for loop, change it to:

    for (int n = 0; n < v.size(), ++n)

    --
    Marcus Kwok
     
    Marcus Kwok, Nov 2, 2005
    #6
  7. Phil Newman

    Phil Newman Guest

    yay, thanks :) that worked nicely.

    cheers for the help,

    I get the feeling i'll be posting a few more questions on here!

    Phil
     
    Phil Newman, Nov 2, 2005
    #7
  8. Phil Newman

    mlimber Guest

    Stefan Näwe wrote:
    > mlimber wrote:
    > > Phil Newman wrote:
    > >
    > >>I'm trying to implement a basic sinewave signal using arrays, but I'm
    > >>having difficulty (I'm a complete beginner!)
    > >>
    > >>This is what I have so far:
    > >>
    > >> [snip]
    > >>I want to swap a single value x, for an array of 100 points.
    > >>
    > >>Can anyone help me with this?
    > >>
    > >>Later, I'll need to change the frequency of the signal, but this
    > >>doesn't matter now? Also, I'll want to plot the signal, but I think
    > >>this is advanced.
    > >>
    > >>If anyone can help, I would really appreciated it!
    > >>
    > >>Phil Newman

    > >
    > >
    > > Try this:
    > >
    > > #include <iostream>
    > > #include <vector>
    > > #include <cmath>
    > >
    > > using namespace std;
    > >
    > > void FillSin( vector<double>& v )
    > > {
    > > static const double PI = 4*atan(1.0);
    > > for( int n=0; n < output.size(); ++n )
    > > {
    > > v[ n ] = sin( 2*PI / n );

    >
    > Division by Zero !!


    Oops, sorry. I just wrote that off-the-cuff. It should be:

    for( int n=0; n < v.size(); ++n )
    {
    v[ n ] = sin( 2*PI / (n+1) );
    }

    Cheers! --M
     
    mlimber, Nov 2, 2005
    #8

  9. >
    > Try this:
    >
    > #include <iostream>
    > #include <vector>
    > #include <cmath>
    >
    > using namespace std;
    >
    > void FillSin( vector<double>& v )
    > {
    > static const double PI = 4*atan(1.0);
    > for( int n=0; n < output.size(); ++n )
    > {
    > v[ n ] = sin( 2*PI / n );



    The sine function has a period of 2*PI, so it should be:

    v[n] = sin(2*PI*n / v.size());

    for n = [ 0 ; v.size()-1 ]

    > }
    > }
    >
    > int main()
    > {
    > vector<double> output( 100 );
    > FillSin( output );
    > for( int n=0; n < output.size(); ++n )
    > {
    > cout << output[n] << '\n';
    > }
    > return 0;
    > }
    >
    > Cheers! --M
    >


    Cheers mate!

    -M
     
    Mogens Heller Jensen, Nov 2, 2005
    #9
  10. Phil Newman

    Phil Newman Guest

    Thanks, that works great.

    How would I be able to add white noise to the sinewave signal over 1000
    samples?

    Phil
     
    Phil Newman, Nov 3, 2005
    #10
  11. Phil Newman

    osmium Guest

    "Phil Newman" wrote:

    > How would I be able to add white noise to the sinewave signal over 1000
    > samples?


    Since no one has responded ....
    Here's what I would do but I can't certify anything.
    Decide how much noise you want and use rand() to generate a voltage in that
    range for a
    particular sample. For each sample add the two components to get signal
    plus noise. Make a second drawing from rand() to choose the polarity of the
    noise. You could speed that up, at the expense of clarity, by selecting a
    bit from a single drawing to define the polarity
     
    osmium, Nov 6, 2005
    #11
  12. Phil Newman

    ben Guest

    > I have not used C++ for ages but I recall that one could use
    > std::transform on a vector
    > something like
    > #include <functional>
    > or
    > #include <algorithm>


    It's in <algorithm>

    > std::transform(v.begin(), v.end(), std::sin)


    Then don't forget to fill the vector with increasing angles first.

    >
    > and see the result
    > std::copy(v.begin(), s.end(), std::eek:stream<int>(std::cout, " : "));


    Perhaps you mean:

    std::copy(
    v.begin(),
    v.end(),
    std::eek:stream_iterator<double>(std::cout, " : "));

    >
    >
    > hth, Daniel
    >
    > ps: well about ploting C++ has nothing built-in, nothing standard so to
    > say, you will have to use yours op libraries
    > nowdays I code in python there you could use Tk or ploting modules
    > matplotlib, scipy and Numeric


    Not so. You can plot on the text console with letters or punctuations,
    although not as good looking as most other means.

    Ben
     
    ben, Nov 6, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    4,739
    Jim Lewis
    Nov 28, 2005
  2. Xanax
    Replies:
    6
    Views:
    516
    Ashish
    Oct 6, 2003
  3. red floyd
    Replies:
    5
    Views:
    1,108
    Nathan Wijnia
    Jun 21, 2007
  4. rara10
    Replies:
    0
    Views:
    979
    rara10
    Jan 30, 2009
  5. Shiva
    Replies:
    2
    Views:
    1,718
    Chris Maryan
    Feb 2, 2009
Loading...

Share This Page