Sine Wave Generation

Thread Starter

kaleanae

Joined Nov 8, 2007
1
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
 

Papabravo

Joined Feb 24, 2006
21,227
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.
 

Salgat

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

Papabravo

Joined Feb 24, 2006
21,227
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.
 

Dave

Joined Nov 17, 2003
6,969
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
 

Dave

Joined Nov 17, 2003
6,969
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
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:

Rich (BB code):
t = [1:0.1:100]; % 991 time points at 0.1 resolution
s_1 = sin(2*pi*f_1*t);
s_2 = sin(2*pi*f_2*t);
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
 
Top