Creating a triangular waveform in Matlab

In summary: This has really helped me understand the FFT and how to interpret the results. Thanks!In summary, the conversation focused on creating a 512 element triangular waveform in Matlab and how to code it efficiently using indexing. The conversation also touched on calculating the FFT and plotting the magnitude and phase responses, with explanations on how to interpret the results.
  • #1
Evo8
169
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
  • #2
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:
  • #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 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,335
  • #4
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.
 
  • #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? :)
 
  • #6
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
 
  • #7
Thanks for the links and information ILS and Simon!
 

Related to Creating a triangular waveform in Matlab

1. How can I create a triangular waveform in Matlab?

To create a triangular waveform in Matlab, you can use the built-in function "sawtooth()". This function takes in parameters for frequency, amplitude, and phase to generate a triangular waveform.

2. What is the syntax for generating a triangular waveform in Matlab?

The syntax for generating a triangular waveform using the "sawtooth()" function is:

sawtooth(t, width)
Where "t" is the time vector and "width" is the duty cycle of the waveform.

3. How can I plot a triangular waveform in Matlab?

To plot a triangular waveform in Matlab, you can use the "plot()" function and pass in the output of the "sawtooth()" function as the y-axis values. You can also specify the x-axis values using the "t" time vector.

4. Can I customize the frequency and amplitude of a triangular waveform in Matlab?

Yes, you can customize the frequency and amplitude of a triangular waveform in Matlab by specifying the values for these parameters in the "sawtooth()" function. You can also use the "hold on" command to plot multiple waveforms with different frequencies and amplitudes on the same graph.

5. Are there any other methods for creating a triangular waveform in Matlab?

Yes, there are other methods for creating a triangular waveform in Matlab, such as using the "tripuls()" function or manually creating a vector of values for a triangular waveform. However, the "sawtooth()" function is the most commonly used and efficient method for generating a triangular waveform in Matlab.

Similar threads

Replies
7
Views
3K
Replies
2
Views
2K
Replies
6
Views
2K
Replies
4
Views
3K
Replies
2
Views
8K
Replies
4
Views
3K
Replies
7
Views
2K
Back
Top