1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Creating a triangular waveform in Matlab

  1. Jan 19, 2012 #1
    1. The problem statement, all variables and given/known data

    Create 512 element triangular waveform (In Matlab)


    x(k)= k 0≤k≤7
    8-k 8≤k≤23
    -32+k 24≤k≤31

    For 0≤k≤31

    repeating every 32 elements.

    2. Relevant equations
    N/A


    3. The attempt at a solution
    Ok so im confused on how to code this.

    This is what I have so far.

    k1=0:7;
    k2=8:23;
    k3=8-k2;
    k4=24:31;
    k5=-32+k4;
    K=[k1,k3,k5];


    Im not sure if this is the best way though. Or if this is correct.

    How do I get it to repeat every 32 elements?

    Thanks for any assistance!
    1. The problem statement, all variables and given/known data



    2. Relevant equations



    3. The attempt at a solution
    1. The problem statement, all variables and given/known data



    2. Relevant equations



    3. The attempt at a solution
     
  2. jcsd
  3. Jan 20, 2012 #2

    Simon Bridge

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member
    2016 Award

    You can range-index inside vectors eg.

    k=0:31;
    z = [k(1:8) 8-k(9:24) k(25:32)-32];

    This assignment is an indexing exercize - since z is 32 elements long, and you want x to be 512 elements long - repeating z, then you could just do:

    x=[z z z z z z z z z z z z z z z z];

    which is pretty apt.
    You will be expected to look for some indexing method perhaps like

    x(1:16)=z;

    only that won't work - go through your notes for the method to stick a vector in an indexed position.
     
    Last edited: Jan 20, 2012
  4. Jan 21, 2012 #3
    Hi Simon!

    Thanks for the info. This makes the code much cleaner. Plus if I want to change something it will be much easier to implement.

    After creating this array i am trying to attempt to calculate the fft and plot the magnitude and phase.

    I have done this (I think)

    Here is my code:
    k=0:31;
    Z=[k(1:8) 8-k(9:24) k(25:32)-32];
    X=[Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z Z];
    fft(X);
    magX=abs(X);
    phaX= angle(X);
    magX=magX(1:256);
    phaX=phaX(1:256);
    subplot(2,1,1); plot(magX); grid
    xlabel('Frequency'); ylabel('Magnitude');
    title('Magnitude Response');
    subplot(2,1,2); plot(phaX); grid

    attachment.php?attachmentid=42929&d=1327170826.jpg

    I think this is correct. However Im not really sure what im looking at. Can anyone give me a little bit of an idea of what to look for when analyzing a plot of an FFT magnitude and phase response?

    Thanks,
     

    Attached Files:

    • L4_5.jpg
      L4_5.jpg
      File size:
      35.5 KB
      Views:
      578
  5. Jan 21, 2012 #4

    I like Serena

    User Avatar
    Homework Helper

    In one measurement interval you have 256 samples.

    You have a wave with 8 periods of 32 samples each in that interval.

    A wave with 8 periods means that you can expect a spike in the frequency magnitude at a frequency of 256/8=32 and there is.
    Typically you also get spikes at multiples of this frequency.

    In your phase diagrams you see jumps up and down of pi.
    I believe in your other thread we found that such a jump is not relevant.
     
  6. Jan 21, 2012 #5
    Hi I Like Serena,

    Thanks for your response.

    I see the 8 larger spikes I assume since I've truncated the data to 256 that these are the 8 periods showing up? Are the smaller spike the multiples at lower amplitudes?

    As for the phase plot I'm not sure What other thread you are referring too. Why are these not relevant?

    Have I been sleep posting? :)
     
  7. Jan 21, 2012 #6

    I like Serena

    User Avatar
    Homework Helper

    Check this post of yours: https://www.physicsforums.com/showpost.php?p=3701217&postcount=10


    Typically a wave is constructed of a sine and a number of sines with a multiple of the frequency as you can see here:
    400px-Periodic_identity_function.gif


    Furthermore, since your wave fits exactly in your measurement interval it wraps around causing multiples of your frequency and dividers of your frequency.

    If your wave does not fit exactly in your measurement interval it looks quite different.
    more like this:
    http://www.facstaff.bucknell.edu/mastascu/elessonshtml/Freq/Freq4Note8MatlabExample.htm
     
  8. Jan 21, 2012 #7
    Thanks for the links and information ILS and Simon!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Creating a triangular waveform in Matlab
Loading...