Hi everyone,
I am trying to plot a triangular wave for a specified time using the sawtooth function in Matlab. The user inputs values for T_amp, T_freq, and ft and the code then calculates the sawtooth and plots the function vs time, ft.
When plotted, the plot shows the sawtooth starting at -T_amp. I was wondering if there was a way to shift the function so that the sawtooth starts at 0, where the first point would be (0,0) instead of (0, -T_amp). If anyone could help me out with this, I would really appreciate it. Thanks!
tri = T_amp*sawtooth(T_freq*ft, .5);
plot(ft, tri;)

 채택된 답변

Ethan
Ethan 2020년 10월 16일

1 개 추천

The sawtooth function does not automatically account for the 2*pi required to plot with frequency. To add a phase shift, you can do the same as if it were a sign wave by adding or subtracting your phase:
tri = sawtooth(2*pi*T_freq*ft+(pi/2), .5);

추가 답변 (1개)

Jon
Jon 2020년 10월 15일
편집: Jon 2020년 10월 15일

1 개 추천

if you just want to shift it so that it starts at (0,0):
tri = T_amp*sawtooth(T_freq*ft, .5) + T_amp;
Note that with the above tri will range from 0 to 2*T_amp,
if you actually wanted it to range from 0 to T_amp you could use:
tri = T_amp/2*sawtooth(T_freq*ft, .5) + T_amp/2;

댓글 수: 6

Ethan
Ethan 2020년 10월 15일
Hi Jon,
That does shift the graph to start at (0,0), however it erases the negative component of the triangle wave. I am looking for a way to keep the triangle from going from -T_amp to T_amp, however starting at (0,0) instead of (0, -T_amp).
Ethan
Ethan 2020년 10월 15일
Hi Jon, I am trying to take the graph above and shift it to the right so that the first point is (0,0). The two solutions you provided had changed the graphs to all positive values ranging from 0 to T_amp or T_amp/2. If there was a way to shift the graph, without losing the positive and negative values, I would very much appreciate it. Thanks!
Jon
Jon 2020년 10월 15일
편집: Jon 2020년 10월 15일
Sorry, I misunderstood your question. So it sounds like you want to phase shift the sawtooth. You could do something like this:
% generate a few cycles of the sawtooth waveform
amp = 5;
theta = linspace(0,6*pi,1000);
tri = sawtooth(theta,0.5)*amp,
% now phase shift the waveform
thetaShift = theta - pi/2;
% just keep the values for positive time (angle)
iKeep = thetaShift >= 0;
theta = thetaShift(iKeep)
tri = tri(iKeep);
% plot the results
plot(theta,tri)
grid
Here I did it in terms of radians, since the sawtooth is defined to have a period of 2*pi it is clearer to see the phase shift. I think you can easily adapt this to do it in terms of your desired frequency and using units of time.
Ethan
Ethan 2020년 10월 15일
I actually just figured it out as I got your reply. The issue was I was missing a 2*pi in the sawtooth for the frequency, which I then added pi/2 to the signal to shift it. The equation then becomes:
tri = sawtooth(2*pi*T_freq*ft+(pi/2), .5);
which gives me a triangular wave that starts at (0,0), and ends at (ft(end),0).
Thanks for your help!
Jon
Jon 2020년 10월 15일
Oh even better, just feeding the sawtooth with the shifted angles
Jon
Jon 2020년 10월 16일
It would be good to either post your solution as an answer and then accept it, or accept this thread and let people find it in the comments just so that others who might have a similar question will know there is an answer available

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

제품

질문:

2020년 10월 15일

답변:

2020년 10월 16일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by