Creating a triangular waveform in Matlab

  • Thread starter Thread starter Evo8
  • Start date Start date
  • Tags Tags
    Matlab Waveform
Click For Summary

Discussion Overview

The discussion revolves around creating a triangular waveform in Matlab, specifically focusing on coding techniques to generate the waveform, as well as analyzing its Fast Fourier Transform (FFT) to understand its frequency and phase characteristics. The scope includes coding, signal processing, and analysis of waveforms.

Discussion Character

  • Homework-related
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant presents a piece of code to create a triangular waveform using piecewise definitions for different ranges of k.
  • Another participant suggests a more efficient indexing method to create the waveform and explains how to repeat the waveform to achieve the desired length.
  • A participant expresses gratitude for the cleaner code and shares their attempt to calculate the FFT and plot the magnitude and phase of the waveform.
  • Discussion includes observations about the expected frequency spikes in the FFT due to the periodic nature of the waveform and the implications of truncating the data to 256 samples.
  • Participants discuss the relevance of phase jumps observed in the phase plot and reference previous discussions for clarification.
  • One participant questions the interpretation of the smaller spikes in the FFT magnitude plot and seeks clarification on the relevance of phase jumps.

Areas of Agreement / Disagreement

Participants generally agree on the coding approach and the expected characteristics of the FFT. However, there are unresolved questions regarding the interpretation of the phase plot and the relevance of certain features in the FFT analysis.

Contextual Notes

Some assumptions about the measurement interval and the relationship between the waveform and its FFT characteristics are not fully explored. The discussion also references external threads and links that may provide additional context but are not detailed within this thread.

Who May Find This Useful

Individuals interested in signal processing, Matlab programming, and the analysis of waveforms through FFT may find this discussion beneficial.

Evo8
Messages
168
Reaction score
0

Homework Statement



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.

Homework Equations


N/A


The Attempt at a Solution


Ok so I am 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!

 
Physics news on Phys.org
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:
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 I am not really sure what I am 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,
 

Attachments

  • L4_5.jpg
    L4_5.jpg
    34.6 KB · Views: 1,405
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.
 
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? :)
 
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
 
Thanks for the links and information ILS and Simon!
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K