How can I plot square waves with an increasing/decreasing linear pattern?

1 view (last 30 days)
Hello, I am trying to trace a triangle with a square wave. I have the square wave but I am having troubles increasing and decreasing it linearly to fit the shape of the triangle. It just stays at its original amplitude straight across.
%given
k_num=-45.6992;
h_num=60.3496;
%plotting lines of triangle
plot([k_num,0],[h_num,2*h_num],'m')
hold on
radius=sqrt((h_num.^2)+(k_num.^2));
newx=(k_num+radius);
plot([k_num,newx],[h_num,h_num])
plot([newx,(k_num-k_num)],[h_num,(h_num+h_num)])
%square wave
fnx = @(x,fn) ((h_num/2)*square(2*pi*fn*x)+(3*30.17));
ffs = 1000;
tp = k_num:1/ffs:newx;
pp = fnx(tp,.8);
plot(tp,pp)

Accepted Answer

Voss
Voss on 28 Jun 2022
%given
k_num=-45.6992;
h_num=60.3496;
%plotting lines of triangle
plot([k_num,0],[h_num,2*h_num],'m')
hold on
radius=sqrt((h_num.^2)+(k_num.^2));
newx=(k_num+radius);
plot([k_num,newx],[h_num,h_num])
plot([newx,(k_num-k_num)],[h_num,(h_num+h_num)])
%square wave
% fnx = @(x,fn) ((h_num/2)*square(2*pi*fn*x)+(3*30.17));
fnx = @(x,fn)square(2*pi*fn*x);
ffs = 1000;
tp = k_num:1/ffs:newx;
amplitude = interp1([k_num 0 newx], [0 h_num 0]/2 , tp);
offset = interp1([k_num 0 newx], [0 h_num 0]/2+h_num, tp);
pp = fnx(tp,0.8).*amplitude + offset;
plot(tp,pp)

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!