9
$\begingroup$

I'm looking for a way to plot the waveform with a flat top and dotted line of a sine as in the second or third curves.
How can I do it?

Enter image description here

$\endgroup$
18
$\begingroup$

enter image description here

gap = 1/4;

pw0 = Piecewise[{{ 2/3 Sin[ x], 0 <= x <= 2 π}}, Undefined];

pw1 = Piecewise[{{gap + Sin[ x - π/2], 2 π + π/2 <= x <= 4 π + π /2},
    {-gap + Sin[ x - π], 5 π <= x <= 7 π}}, Undefined];

Text, line and arrow primitives to annotate the plot:

annotations = {Text[Style["0", 14], Offset[{-15, 0}, {0, 0}], {1, 0}],
   Text[Style["+Vcc", 14], Offset[{-10, 10}, {0, 1}], {1, 0}],
   Text[Style["-Vcc", 14], Offset[{-10, -10}, {0, -1}], {1, 0}],
   Text[Style["Input\nSignal", 14], Offset[{0, -10}, {π/2, 0}], {0, 1}],
   Text[Style["ωt", 14], Offset[{-15, 15}, {2 π, 0}], {1, -1}], 
   Text[Style["Bias level\nto High", 14], Offset[{0, 5}, {4 π, gap}], {0, -1}],
   Text[Style["Bias level\nto Low", 14], Offset[{0, -5}, {5 π + π/2, -gap}], {0, 1}],
   Text[Style["Positive\nHalf Clipped", 14], 
      Offset[{-10, 2}, {3 π + π/2, 1}], {-1, -1}],
   Text[Style["Negative\nHalf Clipped", 14], Offset[{10, -2}, {6 π, -1}], {1, 1}],
   Text[Style["Distorted Output\nSignal", 14], {6 π, 2/3}, {-1, 0}],
   Text[Style["0v", 14], {7 π, 0}, {1, -1}],
   Gray, Arrowheads[.03], 
   Arrow[{Offset[{-30, 10}, {2 π, 0}], Offset[{10, 10}, {2 π, 0}]}],
   Arrowheads[{-.02, .02}], Arrow[{{4 π, 0}, {4 π, gap}}],
   Arrow[{{5 π + π/2, 0}, {5 π + π/2, -gap}}],
   Line[{Offset[{-20, 0}, {2 π + π/2, gap}], Offset[{20, 0}, {4 π + π/2, gap}]}],
   Line[{Offset[{-20, 0}, {5 π, -gap}], Offset[{20, 0}, {7 π, -gap}]}]};

Use annotations as Epilog in Plot:

Plot[{pw0, pw1, Clip[pw1, {-1, 1}]}, {x, 0, 7 π + π/2},
 PlotStyle -> {Directive[CapForm["Round"], AbsoluteThickness[5]],
   Directive[Gray, Dashed],
   Directive[CapForm["Round"], AbsoluteThickness[5], RGBColor[0.2, 0.4, 0.2]]},
 GridLines -> {None, {-1, 1}},
 GridLinesStyle -> Directive[Gray, Dashing[Large]],
 ImageSize -> 700,
 Ticks -> False,
 AxesStyle -> {Arrowheads[Large], Automatic},
 Epilog -> annotations, 
 PlotRangeClipping -> False,
 ImagePadding -> {{50, 10}, {10, 10}}]

picture above

$\endgroup$
1
  • $\begingroup$ really impressive! Other people also gave nice answers which is simple too. $\endgroup$
    – hana
    Jan 10 at 1:50
9
$\begingroup$
Plot[{#, Clip[#, {-0.8, 0.8}]}
   , {x, 0, 2 \[Pi]}
   , PlotStyle -> {{Dashed, Gray}, {Opacity[1], Thick, Darker@Cyan}}
   ] &@Sin[x]

enter image description here


You can change opacity to make the other dotted waveform more visible.


EDIT-1

The Clip function has a third argument that makes it more versatile for plotting waveforms for clipping and clamping circuits. You can choose another function during clipped phases. As a simpler demonstration:

Plot[{#, Clip[#, {-0.8, 0.8}, {-0.3, 0.5}]}
   , {x, 0, 2 \[Pi]}
   , Exclusions -> None
   , PlotStyle -> {{Dashed, Gray}, {Opacity[1], Thick, Darker@Cyan}}
   ] &@Sin[x]

enter image description here

$\endgroup$
8
$\begingroup$

Edit

Plot[Sin[2 x], {x, 0, 2 Pi}, Mesh -> {{-.8, .8}}, 
  MeshFunctions -> {#2 &}, 
  MeshShading -> {Dotted, 
    Directive[AbsoluteThickness[3], Darker@Cyan]}, 
  MeshStyle -> Directive[PointSize[Tiny], Blue]] /. {Dotted, 
   Line[a_]} :> {{Dotted, Line[a]}, {Red, Line[{First[a], Last[a]}]}}

enter image description here

Original

Show[Plot[Sin[2 x], {x, 0, 2 Pi}, PlotRange -> {{-.8, .8}}, 
  ClippingStyle -> {Red, Brown}, PlotStyle -> Green], 
 Plot[Sin[2 x], {x, 0, 2 Pi}, 
  RegionFunction -> Function[{x, y}, Abs@y > .8], 
  PlotStyle -> {Gray, Dotted}], PlotRange -> All]
$\endgroup$
6
$\begingroup$

Some ideas

Show[
 Plot[Min[Sin[x], 0.8], {x, 0, Pi}, PlotStyle -> Thick],
 Plot[Sin[x], {x, ArcSin[0.8], Pi - ArcSin[0.8]}, PlotStyle -> Dashed],
 PlotRange -> All]
$\endgroup$
2
  • $\begingroup$ Couldn't you repeat the trick in the first line? Plot Min[Sin[x], 0.8] with Thick and Max[Sin[x], 0.8] with Dashed. Then you could do everything in one Plot call if you want $\endgroup$ Jan 9 at 20:15
  • $\begingroup$ @AccidentalTaylorExpansion With Plot[{Min[Sin[x], 0.8], Sin[x]}, {x, 0, Pi}, PlotStyle -> {Thick, Dashed}] I see the dashes are visible throughout the plot, so the second plot in my answer only plots dashes where they should be seen. $\endgroup$ Jan 9 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.