Thread: C to matlab-sinusoids

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    210

    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

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    210
    I know I have to use f=4000 Hz and change 20/T to 25/T

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    210
    If you guys don't do matlab please direct me to a website as helpful as this that does.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Is your favourite search engine broken?

    Bye, Andreas

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    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
    Fact - Beethoven wrote his first symphony in C

  6. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Also, Mod should put this in General Discussions or Tech thread.
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    210
    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

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Your period is defined as
    Code:
    T=1/f
    And your sampling rate is defined as
    Code:
    fs=T/25
    i.e. 1/25th of the signal's period
    Fact - Beethoven wrote his first symphony in C

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    You are doing
    Code:
    4000 * 25 (samples/second)
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matlab help
    By cyberfish in forum Tech Board
    Replies: 1
    Last Post: 03-07-2011, 06:53 AM
  2. C++ to matlab
    By loga in forum C++ Programming
    Replies: 1
    Last Post: 02-10-2008, 01:33 AM
  3. Matlab vs. R
    By ch147 in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 11-14-2007, 11:57 AM
  4. C/C++ and Matlab?
    By darkwalk in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2006, 03:00 AM
  5. Matlab to C/C++
    By jisarrar in forum C Programming
    Replies: 2
    Last Post: 01-31-2006, 07:01 AM