ExpSine

block ExpSine "Generate exponentially damped sine signal"
    import Modelica.Constants.pi;

    parameter Real amplitude = 1 "Amplitude of sine wave"
        annotation (Dialog(groupImage = "modelica://Modelica/Resources/Images/Blocks/Sources/ExpSine.png"));
    parameter SIunits.Frequency freqHz(start = 2) "Frequency of sine wave";
    parameter SIunits.Angle phase = 0 "Phase of sine wave";
    parameter SIunits.Damping damping(start = 1) "Damping coefficient of sine wave";

    extends Interfaces.SignalSource;
equation
    y = offset + (if time < startTime then 0 else amplitude * Modelica.Math.exp(-(time - startTime) * damping) * Modelica.Math.sin(2 * pi * freqHz * (time - startTime) + phase));

    annotation (
        Icon(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Line(
                    points = {
                        {-80, 68}, 
                        {-80, -80}},
                    color = {192, 192, 192}), 
                Polygon(
                    points = {
                        {-80, 90}, 
                        {-88, 68}, 
                        {-72, 68}, 
                        {-80, 90}},
                    lineColor = {192, 192, 192},
                    fillColor = {192, 192, 192},
                    fillPattern = FillPattern.Solid), 
                Line(
                    points = {
                        {-90, 0}, 
                        {68, 0}},
                    color = {192, 192, 192}), 
                Polygon(
                    points = {
                        {90, 0}, 
                        {68, 8}, 
                        {68, -8}, 
                        {90, 0}},
                    lineColor = {192, 192, 192},
                    fillColor = {192, 192, 192},
                    fillPattern = FillPattern.Solid), 
                Line(
                    points = {
                        {-80, 0}, 
                        {-75.2, 32.3}, 
                        {-72, 50.3}, 
                        {-68.7, 64.5}, 
                        {-65.5, 74.2}, 
                        {-62.3, 79.3}, 
                        {-59.1, 79.6}, 
                        {-55.9, 75.3}, 
                        {-52.7, 67.1}, 
                        {-48.6, 52.2}, 
                        {-43, 25.8}, 
                        {-35, -13.9}, 
                        {-30.2, -33.7}, 
                        {-26.1, -45.9}, 
                        {-22.1, -53.2}, 
                        {-18.1, -55.3}, 
                        {-14.1, -52.5}, 
                        {-10.1, -45.3}, 
                        {-5.23, -32.1}, 
                        {8.44, 13.7}, 
                        {13.3, 26.4}, 
                        {18.1, 34.8}, 
                        {22.1, 38}, 
                        {26.9, 37.2}, 
                        {31.8, 31.8}, 
                        {38.2, 19.4}, 
                        {51.1, -10.5}, 
                        {57.5, -21.2}, 
                        {63.1, -25.9}, 
                        {68.7, -25.9}, 
                        {75.2, -20.5}, 
                        {80, -13.8}},
                    smooth = Smooth.Bezier), 
                Text(
                    extent = {
                        {-147, -152}, 
                        {153, -112}},
                    textString = "freqHz=%freqHz")}),
        Diagram(
            coordinateSystem(
                preserveAspectRatio = true,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Line(
                    points = {
                        {-86, -90}, 
                        {-86, 84}},
                    color = {95, 95, 95}), 
                Polygon(
                    points = {
                        {-86, 98}, 
                        {-90, 82}, 
                        {-82, 82}, 
                        {-86, 98}},
                    lineColor = {95, 95, 95},
                    fillColor = {95, 95, 95},
                    fillPattern = FillPattern.Solid), 
                Line(
                    points = {
                        {-99, -40}, 
                        {85, -40}},
                    color = {95, 95, 95}), 
                Polygon(
                    points = {
                        {97, -40}, 
                        {81, -35}, 
                        {81, -44}, 
                        {97, -40}},
                    lineColor = {95, 95, 95},
                    fillColor = {95, 95, 95},
                    fillPattern = FillPattern.Solid), 
                Line(
                    points = {
                        {-50, 0}, 
                        {-46.1, 28.2}, 
                        {-43.5, 44}, 
                        {-40.9, 56.4}, 
                        {-38.2, 64.9}, 
                        {-35.6, 69.4}, 
                        {-33, 69.6}, 
                        {-30.4, 65.9}, 
                        {-27.8, 58.7}, 
                        {-24.5, 45.7}, 
                        {-19.9, 22.5}, 
                        {-13.4, -12.2}, 
                        {-9.5, -29.5}, 
                        {-6.23, -40.1}, 
                        {-2.96, -46.5}, 
                        {0.302, -48.4}, 
                        {3.57, -45.9}, 
                        {6.83, -39.6}, 
                        {10.8, -28.1}, 
                        {21.9, 12}, 
                        {25.8, 23.1}, 
                        {29.7, 30.5}, 
                        {33, 33.3}, 
                        {36.9, 32.5}, 
                        {40.8, 27.8}, 
                        {46, 16.9}, 
                        {56.5, -9.2}, 
                        {61.7, -18.6}, 
                        {66.3, -22.7}, 
                        {70.9, -22.6}, 
                        {76.1, -18}, 
                        {80, -12.1}},
                    color = {0, 0, 255},
                    thickness = 0.5,
                    smooth = Smooth.Bezier), 
                Text(
                    extent = {
                        {-80, 17}, 
                        {-57, -3}},
                    textString = "offset"), 
                Text(
                    extent = {
                        {-72, -43}, 
                        {-25, -53}},
                    textString = "startTime"), 
                Text(
                    extent = {
                        {77, -53}, 
                        {101, -64}},
                    textString = "time"), 
                Text(
                    extent = {
                        {-89, 99}, 
                        {-49, 82}},
                    textString = "y"), 
                Line(
                    points = {
                        {-50, 0}, 
                        {18, 0}},
                    color = {95, 95, 95}), 
                Line(
                    points = {
                        {-50, 0}, 
                        {-86, 0}},
                    color = {0, 0, 255},
                    thickness = 0.5), 
                Line(
                    points = {
                        {-50, 77}, 
                        {-50, 0}},
                    color = {95, 95, 95}), 
                Line(
                    points = {
                        {18, -1}, 
                        {18, 76}},
                    color = {95, 95, 95}), 
                Line(
                    points = {
                        {18, 73}, 
                        {-50, 73}},
                    color = {95, 95, 95}), 
                Text(
                    extent = {
                        {-42, 83}, 
                        {9, 74}},
                    textString = "1/freqHz"), 
                Polygon(
                    points = {
                        {-50, 73}, 
                        {-41, 75}, 
                        {-41, 71}, 
                        {-50, 73}},
                    lineColor = {95, 95, 95},
                    fillColor = {95, 95, 95},
                    fillPattern = FillPattern.Solid), 
                Polygon(
                    points = {
                        {18, 73}, 
                        {10, 75}, 
                        {10, 71}, 
                        {18, 73}},
                    lineColor = {95, 95, 95},
                    fillColor = {95, 95, 95},
                    fillPattern = FillPattern.Solid), 
                Line(
                    points = {
                        {-50, -61}, 
                        {-19, -61}},
                    color = {95, 95, 95}), 
                Polygon(
                    points = {
                        {-18, -61}, 
                        {-26, -59}, 
                        {-26, -63}, 
                        {-18, -61}},
                    lineColor = {95, 95, 95},
                    fillColor = {95, 95, 95},
                    fillPattern = FillPattern.Solid), 
                Text(
                    extent = {
                        {-51, -63}, 
                        {-27, -75}},
                    textString = "t"), 
                Text(
                    extent = {
                        {-83, -69}, 
                        {100, -96}},
                    textString = "amplitude*exp(-damping*t)*sin(2*pi*freqHz*t+phase)"), 
                Line(
                    points = {
                        {-50, 0}, 
                        {-50, -40}},
                    color = {95, 95, 95}), 
                Line(
                    points = {
                        {-50, -54}, 
                        {-50, -72}},
                    color = {95, 95, 95}), 
                Line(
                    points = {
                        {-15, -77}, 
                        {-1, -48}},
                    color = {95, 95, 95})}),
        Documentation(info = "<html>\n<p>\nThe Real output y is a sine signal with exponentially changing amplitude:\n</p>\n\n<p>\n<img src=\"modelica://Modelica/Resources/Images/Blocks/Sources/ExpSine.png\"\n     alt=\"ExpSine.png\">\n</p>\n</html>"));
end ExpSine;