Repeating a Waveform in MATLAB

4 views (last 30 days)
Hasan Haider
Hasan Haider on 18 May 2019
Answered: Soumya on 21 Feb 2025
I need to create a sine wave with a certain firing angle in the positive and negative cycles (both firing angles differ), and also different clipping values in the positive and negative cycles.
I did it for one cycle basically, using the following code:
clear
clc
com0 = 'Insert the frequency of the sine waveform - ';
f = input(com0);
com1 = 'Insert the time at which the positive cycle starts - ';
t1 = input(com1);
com2 = 'Insert the time at which the negative cycle starts - ';
t2 = input(com2);
com3 = 'Insert the clipping value desired during positive cycle - ';
c1 = input(com3);
com4 = 'Insert the clipping value desired during negative cycle - ';
c2 = input(com4);
T = 1/f; %Period of the Signal
t3 = (asin(c1))/(2*pi*f);
t4 = (asin(c2))/(2*pi*f);
if t3 < T/4
tx = ((T/2)-t3):0.00001:(T/2);
x = sin(2*pi*f*tx);
end
if t3 >= T/4
tx = t3:0.00001:(T/2);
x = sin(2*pi*f*tx);
end
ty = t2:0.00001:T;
if t4 < (3*T)/4
ty = (t4+T):0.00001:T;
y = sin(2*pi*f*ty);
end
if t4 >= (3*T)/4
ty = t4:0.00001:T;
y = sin(2*pi*f*ty);
end
line(xlim(), [0,0], 'LineWidth', 1, 'Color', 'k');
hold on
plot(tx,x)
plot(ty,y)
grid
plot([t1 t1],[0 c1])
if t3 < T/4
plot([t1 ((T/2)-t3)],[c1 c1])
end
if t3 >= T/4
plot([t1 t3],[c1 c1])
end
plot([t2 t2],[0 c2])
if t4 < (3*T)/4
plot([t2 (t4+T)],[c2 c2])
end
if t4 >= (3*T)/4
plot([t2 t4],[c2 c2])
end
xlim([0 T])
hold off
How can I repeat the same waveform for at least two more periods (or cycles)?

Answers (1)

Soumya
Soumya on 21 Feb 2025
Hi Hasan,
To repeat the waveform that you have designed for any desired number of periods (or cycles), you will need to make a few modifications to your code:
1.Define a variable to specify the number of cycles you want to plot:
com5 = 'Insert the number of cycles to plot - ';
numCycles = input(com5);
2. Create a “for” loop which iterates over the number of cycles you have defined. A time shift “shift = k * T” has to be created to ensure that each cycle starts at the correct time position on the plot:
for k = 0:(numCycles - 1)
shift = k * T;
3. Within the loop, we can shift the sine wave segments for each iteration. The “line” function draws a horizontal line from “x = shift” to “x = shift + T at y = 0”, with a specified line width and color:
line([shift shift+T], [0, 0], 'LineWidth', 1, 'Color', 'k');
plot(tx + shift, x, 'b')
plot(ty + shift, y, 'b')
4.The conditional statements that you used to determine the endpoints of the clipped waveform segments should also be shifted:
plot([t1 t1] + shift, [0 c1], 'r')
if t3 < T/4
plot([t1 ((T/2)-t3)] + shift, [c1 c1], 'r')
else
plot([t1 t3] + shift, [c1 c1], 'r')
end
plot([t2 t2] + shift, [0 c2], 'r')
if t4 < (3*T)/4
plot([t2 (t4+T)] + shift, [c2 c2], 'r')
else
plot([t2 t4] + shift, [c2 c2], 'r')
end
5.Set the x-axis limits to “xlim([0 numCycles*T])” to cover the entire duration of all plotted cycles.
This is the plot generated by applying the changes to the code, displaying the waveform over three periods (or cycles):
I hope this helps!

Categories

Find more on Performance and Memory in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!