Sine Wave Generation

Discussion in 'Programmer's Corner' started by kaleanae, Nov 8, 2007.

  1. kaleanae

    Thread Starter New Member

    Nov 8, 2007
    1
    0
    Hi!
    I was wondering if someone knows how to generate many sine waves at once (I also need to add them), for example, to generate 2 I use:

    s_1 = (exp(j*2*pi*f_1*t) - exp(-j*2*pi*f_1*t))/(2*j);
    s_2 = imag(exp(j*2*pi*f_2*t));

    % plot the 2 and 4 Hz waves together in the same panel
    figure
    subplot(2,1,1)
    plot(t,s_1, 'b-')
    hold on
    plot(t, s_2, 'r--')
    ylabel('Amplitude')

    % Plot the sum
    subplot(2,1,2)
    plot(t, s_1+s_2)
    ylabel('Amplitude')
    xlabel('Time (s)')
    How can I do this without using Hold on.

    Thank you
     
  2. cheddy

    Active Member

    Oct 19, 2007
    87
    0
    what language is that?
     
  3. Papabravo

    Expert

    Feb 24, 2006
    10,152
    1,793
    The ever popular, but ruinously expensive Matlab.
    Go for Scilab as a freeware Matlab clone.

    Why are you using the complex exponential? The ordinary sin function isn't good enough for you?

    You just load the rows(columns) of a matrix with calls to the sin function where the argument depends on either the row or coulmn index. Heck you could even use a random number generator to pick the frequency of the sin function. When you're done you can use row or column sums as appropriate to add them together.
     
  4. Salgat

    Active Member

    Dec 23, 2006
    215
    1
    I love your brutal honesty Papabravo, I both hate and adore you simultaneously. Anyways, does that clone use identical sytax as Matlab?
     
  5. Papabravo

    Expert

    Feb 24, 2006
    10,152
    1,793
    It is identical in many cases. They do have features that were not in Matlab the last time I looked like symbolic polynomial manipulation. For the price it is really hard not to take it for a test drive.

    Also you have to know what you are looking for since the help apparently doesn't have a search feature.

    I guess brutal honesty works both ways -- Thanks for that.
     
  6. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    145
    The (free) Matlab clone that most closely resembles Matlab syntax is Octave - it is designed to be that way. There are some very subtle differences, however they are corrections to some of the more illogical Matlab constructs, therefore the experienced programmer should find it *easier* to use. Another one that is worth considering is FreeMat.

    Dave
     
  7. Dave

    Retired Moderator

    Nov 17, 2003
    6,960
    145
    I agree with Papabravo's suggestion - use the sin function.

    Declare your time variable t (I have done this arbitrarily below), and the pass that as an argument to your sin function:

    Code ( (Unknown Language)):
    1. t = [1:0.1:100]; % 991 time points at 0.1 resolution
    2. s_1 = sin(2*pi*f_1*t);
    3. s_2 = sin(2*pi*f_2*t);
    4. s_3 = s_1 + s_3
    You can have as many, and add as many, sine waves as you want using the above syntax.

    You will need to use hold on to plot multiple graphs on the same axis.

    Dave
     
Loading...