How do I draw PWM graph?

I can not figure out how to use tikzpicture but I want to draw this (not exactly this because this is a crude schematic made in paint i.e. bars are equally tall and distance between red line are the same).

• How do I even go about it?

• How can I improve the code to create longer pulses instead of bars?

• How can I get the red lines to represent cycle lengths properly?

• How can I match the blue lines with the cycle lengths properly?

With help from js bibra, I have managed to supplement a minimum code base. I have also changed my questions.

\documentclass{article}
\usepackage{siunitx}
\usepackage{tikz}
\usetikzlibrary{calc,matrix, arrows.meta, positioning,
decorations,decorations.markings, math}

\begin{document}
\begin{tikzpicture}

%Axes
\draw[->] (-0.2,0) -- (20,0) node[right] {$t$};
\draw[->] (0,-4) -- (0,4) node[above] {$v$};

%Cycle separator
\foreach \x in {1.38,2.57,...,20.23}{\draw[-, red] (\x,-2.5) -- (\x,2.5)
node[above] {\x};}

%PWM
\foreach \x in {0.2,1.4,...,7.4} { \draw[black] (\x,0)--(\x,2)--(\x+0.2,2)--
(\x+0.2,0); }
\foreach \x in {1.7,2.9,...,6.9} { \draw[black] (\x,0)--(\x,2)--(\x+0.2,2)--
(\x+0.2,0); }
\foreach \x in {3.2,4.4,...,6.1} { \draw[black] (\x,0)--(\x,2)--(\x+0.2,2)--
(\x+0.2,0); }
\draw[black] (4.7,0)--(4.7,2)--(4.7+0.2,2)--(4.7+0.2,0);

\foreach \x in {8.5,9.4,...,16.4} { \draw[black] (\x,0)--(\x,-2)--
(\x+0.2,-2)--(\x+0.2,0); }
\foreach \x in {10.7,11.9,...,15.9} { \draw[black] (\x,0)--(\x,-2)--
(\x+0.2,-2)--(\x+0.2,0); }
\foreach \x in {11.2,12.4,...,14.1} { \draw[black] (\x,0)--(\x,-2)--
(\x+0.2,-2)--(\x+0.2,0); }
\draw[black]  (12.7,0)--(12.7,-2)--(12.7+0.2,-2)--(12.7+0.2,0);

%Average
\draw[blue] (0,0.5)--(1.38,0.5);
\draw[blue] (1.38,1)--(2.57,1);
\draw[blue] (2.57,1.5)--(3.95,1.5);
\draw[blue] (3.95,2)--(5.338,2);

\end{tikzpicture}
\end{document}

• Although this is a PWM diagram (for the non electronic people here: is used to recreate an almost-sinusoidal wave if you filter it), it has nothing to do with circuitikz. Going to change tags. – Rmano Jun 24 at 7:37
• Welcome to TeX.SX! Try to read the tutorials on pgfplots manual and show your attempt. – Rmano Jun 24 at 7:38
• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. We would be happy to refer to you with a significant username ;) – Alessandro Cuttin Jun 24 at 8:52
• I have now tried pgfplots but it seems to be a plotting package but I don't see how I can use it properly – asasasasas Jun 24 at 18:16
• I have now added my attempt with a minimum working code sample. – asasasasas Jun 25 at 15:08

Just a proof of concept and not a complete answer with Tikz

Seems simple to me --disect the PWM into rectangles of 0.2 unit width--height of 2 units--each rectangle would be having a coordinate --say 1,0--1,2--2,2--2,0--this could be replaced with --x,0--x,2--x+1,2--x+1,0--where x is a recurring number

Similarly for the negative side pulse--x,0--x,-2--x+1,-2--x+1,0

Similarly the red verticals can be placed for calculating the duty cycle etc with the help of foreach

I have kept the pulse width as 0.2 you may like to vary it to your choice in (\x,0)--(\x,2)--(\x+0.2,2)--(\x+0.2,0)

Each pulse starts at intervals of 0.4units which you can again vary to your choice in {0.4,0.8,...,4}

Hope it makes some sense -- off course intagrating the sine wave with the PWM can also be done and is solved here--https://tex.stackexchange.com/questions/255081/creating-a-pulse-width-modulator-in-tikz

\documentclass{article}
\usepackage{siunitx}
\usepackage{tikz}
\usetikzlibrary{calc,matrix, arrows.meta, positioning, decorations,decorations.markings, math}

\begin{document}
\begin{tikzpicture}

%Axes
\draw[->] (-0.2,0) -- (12,0) node[right] {$t$};
\draw[->] (0,-1.2) -- (0,4) node[above] {$v$};

%PWM
\foreach \x in {0.4,0.8,...,4} { \draw[blue] (\x,0)--(\x,2)--(\x+0.2,2)--(\x+0.2,0); }
\foreach \x in {4.4,4.8,5.2} { \draw[red] (\x,0)--(\x,-2)--(\x+0.2,-2)--(\x+0.2,0); }
\end{tikzpicture}
\end{document}


EDIT Added the verticals in black color and sine wave clipping in green color

\documentclass{article}
\usepackage{siunitx}
\usepackage{tikz}
\usetikzlibrary{calc,matrix, arrows.meta, positioning,
decorations,decorations.markings, math}

\begin{document}
\begin{tikzpicture}
%Axes
\draw[->] (-0.2,0) -- (8,0) node[right] {$t$};
\draw[->] (0,-1.2) -- (0,4) node[above] {$v$};

%PWM
\foreach \x in {0.4,0.8,...,4} { \draw[blue] (\x,0)--(\x,2)--(\x+0.2,2)--
(\x+0.2,0); }
\foreach \x in {4.4,4.8,5.2} { \draw[red] (\x,0)--(\x,-2)--(\x+0.2,-2)--
(\x+0.2,0); }
\foreach \x in {0.7, 1.5, 2.7,4.1} { \draw[black,thick] (\x,2.2)--(\x,-2.2);
}

{ \draw[green,thick]  (0,0.4)--(0.7,0.4)
(0.7,1.2)--(1.5,1.2)
(1.5,2)--(2.7,2); }

\end{tikzpicture}
\end{document}

• This helped me a lot. I have edited my post. – asasasasas Jun 25 at 15:07