Understanding code - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Understanding code

Posts: 2Member
Hi All,

Okay so I am a C programmer who is now programming in MATLAB. For our first college asignment I have to plot 3 sinusoid graphs of 4000 Hz with 25 samples per period. I also have to start from -T. The thing is I have the code. The whole class has the code i just want to understand it better.
Things I don't understand in the code:
* what is the point of fs
*i dont get how 3 graphs result from this

the x1-x3 functions are defined by our teacher.

Thank you:

code:
[b]
f = 22050; %frequency of sinusoids
T = 1/f; %period of sinusoids
fs = 20/T; %at least 20 samples per period
tt = -T:(1/fs):2*T; %3 cycles starts at a negative time equal to -T
x1 = 24*cos(2*pi*f*tt+8.8);
x2 = 20*cos(2*pi*f*tt+2);
x3 = x1+x2;

subplot(311);
plot(tt, x1), grid on;
title('x1');
axis tight
subplot(312);
plot(tt, x2), grid on;
title('x2');
axis tight
subplot(313);
plot(tt, x3), grid on;
title('x3');
xlabel('Time (sec)');
axis tight
orient tall
[/b]

• Posts: 3Member
Hi,

When I paste your code into my Matlab I get everything like I should.
x3 = x1+x2; means something like (in C):
suppose that x1, x2 and x3 has the same elements in table,
and it is like 25 (don't know how to get number of elements in table in C, in Matlab you do length(x1) :P)

for (i=1;i<=25;i++)
{
x3(i) = x1(i) + x2(i);
}

If you change fs into higher value, you will get finer charts.
Try enter fs = 5/Tp and see difference It means how many points in 1 second of simulation will be in chart.

Best regards,
Marcin