How to generate a sawtooth wave

조회 수: 149 (최근 30일)
Tze Chwen Lim
Tze Chwen Lim 2017년 12월 25일
답변: Sohan Das 2021년 11월 18일
I get the graph i want.But the amplitude is from -10 to 10. Can i know how can i change it to 0 to 10 and keep the same graph?
Here is what i tried so far: T=2*4; Fs=1000; dt = 1/Fs; t = 0:dt:T-dt; x =-10*sawtooth(2*pi*t);
plot(t,x) grid on

채택된 답변

Star Strider
Star Strider 2017년 12월 25일
편집: Star Strider 2017년 12월 25일
You only need to make 2 changes:
T=2*4;
Fs=1000;
dt = 1/Fs;
t = 0:dt:T-dt;
x = sawtooth(2*pi*t); % <— REMOVE THE ‘-10’ HERE
x = (x + 1)*5; % <— ADD THIS LINE
plot(t,x)
grid on
  댓글 수: 2
Tze Chwen Lim
Tze Chwen Lim 2017년 12월 26일
편집: Tze Chwen Lim 2017년 12월 26일
Thank you but can i know why i need to add that (x+1)*5? And the time need to be in ms so when i multiplies the time with 10^-3 , the graph goes wrong why would this happen?
Star Strider
Star Strider 2017년 12월 26일
My pleasure.
The default output amplitude of the sawtooth function is ±1, so adding 1 produces an amplitude of 0 to +2. Multiplying that by 5 produces the 0 to +10 amplitude you want.
Your time already appears to be in milliseconds because of the way you defined ‘dt’. You do not need to multiply it by anything.
You can label the x-axis as:
xlabel('Time (ms)')
or if you want to label it in seconds, this works:
plot(t,x)
grid on
xt = get(gca, 'XTick');
set(gca, 'XTick',xt, 'XTickLabel',xt*1E-3)
xlabel('Time (seconds)')
Here, you simply re-label the tick labels. Everything else remains the same.

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

추가 답변 (3개)

Boris Adum
Boris Adum 2021년 6월 12일
You can also write a function that would generate that wave. If you name your amplitude A, and the width of one tooth W then you can write two functions.
  • If you want the function to go from 0 to A
%define amplitude
A = 10; %set your own value
%define width of one tooth
W=5; %set your own value
syms x y;
y=A/W*(x-(W*fix(x/W)))
%plot the function over an interval
fplot(y,[0,3*W]) %set your own limits for ploting
  • If you want the function to go from -A to A
clear %clear old variables if needed
A=10; %amplitude limits of the sawtooth wave -A to A
W=5; %witdh/step of the sawtooth wave
syms x y;
y=((2*A)/W)*(x-(fix(x/W)*W))-A
fplot(y,[0,5*W]) %Plots five teeth

Sohan Das
Sohan Das 2021년 11월 18일
T=2*4;
Fs=1000;
dt = 1/Fs;
t = 0:dt:T-dt;
x = sawtooth(2*pi*t); % <— REMOVE THE ‘-10’ HERE
x = (x + 1)*5; % <— ADD THIS LINE
plot(t,x)
grid on

Sohan Das
Sohan Das 2021년 11월 18일
T=2*4;
Fs=1000;
dt = 1/Fs;
t = 0:dt:T-dt;
x = sawtooth(2*pi*t);
x = (x + 1)*5;
plot(t,x)
grid on

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by