# C to matlab-sinusoids

• 02-03-2013
kiwi101
C to matlab-sinusoids
Hey All I have always seeked great advice on this website when I would porgram in C. Unfortunately for one of my classes we have started matlab programming.
Hi All,
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.

If you guys understand this code(which I am confident you do) please break it down for me.

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:
Code:

```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```
• 02-03-2013
kiwi101
I know I have to use f=4000 Hz and change 20/T to 25/T
• 02-03-2013
kiwi101
If you guys don't do matlab please direct me to a website as helpful as this that does.
• 02-03-2013
AndiPersti
Is your favourite search engine broken?

Bye, Andreas
• 02-03-2013
Click_here
You might have had more luck on the Tech/General Forum on this website, but it just so happens that I can help you :)

Code:

```/*  You can probably work out what is happening here... */ f = 22050;              %frequency of sinusoids T = 1/f;                    %period of sinusoids fs = 20/T;              %at least 20 samples per period /* Make a matrix starting at -T and ending at 2*T     with the resolution of fs     i.e. tt = [-T, -T+1/fs, -T+2/fs, ..., T-1/fs, T] */ tt = -T:(1/fs):2*T; %3 cycles starts at a negative time equal to -T /* For each point in the matrix tt, calculate a new matrix x1, x2 and then x3 */ x1 = 24*cos(2*pi*f*tt+8.8); x2 = 20*cos(2*pi*f*tt+2); x3 = x1+x2;   /* Display graphs 3x1 - This is graph 1*/ subplot(311); plot(tt, x1), grid on; title('x1'); axis tight /* Second plot */ subplot(312); plot(tt, x2), grid on; title('x2'); axis tight /* Third plot */ subplot(313); plot(tt, x3), grid on; title('x3'); xlabel('Time (sec)'); axis tight orient tall```
I've never heard "axis tight" before, but I'm guessing that it means keep x and y axis as small as possible -> I always use "axis auto", or "axis([xmin xmax ymin ymax])"

I've never used "orient tall" before either...

The trick to finding out about function which you don't know about it to type "help [function name]"

For example, help orient has a list of things -> one of them being:

"ORIENT TALL causes the current Figure window to map to the whole page in portrait orientation for subsequent PRINT operations."

enjoy
• 02-03-2013
Click_here
Also, Mod should put this in General Discussions or Tech thread.
• 02-04-2013
kiwi101
thank you so much. Okay so I changed some of my code to suit the question.
Question:
Code:

```Generate a time vector (tt) to cover a range of t that will exhibit approximately two cycles of the 4000 Hz sinusoids  Finally, make sure that you have at least 25 samples per period of the sinusoidal wave. In other words, when you use the colon operator to define the time vector, make the increment small enough to generate 25 samples per period```
my code
Code:

```f = 4e3;            % sinusoid freq T = 1/f;            % period (250 usec) fs = T/25;      % time step tt = -T:fs:T;    % time vector x1 = 20*cos(2*pi*f*tt-8.5); x2 = 24*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```
So the only thing that I don't get now is am I generating 25 samples per period?
because I am doing this:
Code:

` tt = -T:fs:T;`
but fs = 4000/25 = 160.
So I am doing 160 samples right?
I just want to make sure I have interpreted that part correctly so I can ask the teacher why he is doing that.

Thanks
• 02-05-2013
Click_here
Code:

`T=1/f`
And your sampling rate is defined as
Code:

`fs=T/25`
i.e. 1/25th of the signal's period
• 02-05-2013
Click_here
You are doing
Code:

`4000 * 25 (samples/second)`