transformation of a second order analog filter to a digital filter using the impulse invariance method

brachistochrone[{x1_, yp1_}, v0_] :=
Quiet[Module[{g=9.81,y1=-yp1, v1, fr,curve, normal,ρ = 0.05, φ0,φ1},
v1 = Sqrt[v0^2 + 2 g (y1)];

fr = FindRoot[Evaluate[{((v1^2 ((Sec[φ1])^2) (φ0-φ1+Cos[φ0] Sin[φ0]-Cos[φ1] Sin[φ1]))/(2 g))==x1,((v1^2 (-Cos[2 φ0]+Cos[2 φ1]) ((Sec[φ1])^2))/(4 g))==y1}], {φ0,1.5}, {φ1,-0.5}];
curve[φ_]={-v1^2/(2 g Cos[φ1]^2)(φ-φ0+Cos[φ+φ0]Sin[φ-φ0]),
  v1^2/(2 g Cos[φ1]^2)(Sin[φ+φ0]Sin[φ-φ0])}/.fr;
normal[φ_]=#/Sqrt[#.#]&[Reverse[D[curve[φ],φ]]{1, -1}];

Show[{ParametricPlot[Evaluate[curve[φ]],
Evaluate[{φ, φ0,φ1}/.fr],PlotStyle->{Thickness[.01]},AxesOrigin->{0,0}],
Graphics[{Thickness[.008],Table[Circle[curve[φ]+ρ normal[φ],ρ],
Evaluate[{φ, φ0,φ1,(φ1-φ0)/12}/.fr]]}]},
PlotRange -> {{0,3}, {1/2, -3}},ImageSize->{400,400},PlotLabel->Style["time = "<>ToString[NumberForm[(v1 (φ0-φ1)/(g Cos[φ1])/.fr), {4,3},NumberPadding->{"","0"}]],"Label",14],ImagePadding->{{20,20},{20,30}}]]];

Manipulate[
process[ξ,ωn,T],
Panel[Labeled[Grid[{
{Text["ξ"],Control[{{ξ,.07,""},0,2,.1,Appearance->"Labeled" ,ImageSize->Small}]},
{Text["(Subscript[ω, n]) (rad/sec)"],Control[{{ωn,1,""},0.01,Pi,.01,Appearance->"Labeled" ,ImageSize->Small}]},
{Text@Row[{Style["T",Italic], "(sec)"}],Control[{{T,1,""},0.1,10,.1,Appearance->"Labeled" ,ImageSize->Small}]}
},Spacings->{0,0}],Column[{Text@Style["second order system specification",11],Text@Row[{Style["y''(t)",Italic]," + ξ (Subscript[ω, n])",Style[" y'(t)",Italic], " + (Subscript[ω, n]^2)",Style[" y(t)",Italic]," = δ(t)"}]},Center],
{{Top,Center}},Spacings->{0,1}
],FrameMargins->7,ImageSize->260],
Delimiter,
Panel[Grid[{{Dynamic[If[ξ>0,Text@Style[Row[{Style["y''(t)",Italic]," + ",ξ ωn,Style[" y'(t)",Italic], " + ",ωn^2,Style[" y(t)",Italic]," = δ(t)"}],12],Text@Style[Row[{Style["y''(t)",Italic]," + ",ωn^2,Style[" y(t)",Italic]," = δ(t)"}],11]]]}},Spacings->{0,0},Alignment->Center,ItemSize->30,Frame->None],FrameMargins->7,ImageSize->260
],Panel[Grid[{{Labeled[Control[{{maxy,1,""},0.1,10,.1,
ImageSize->Small,
ControlType->VerticalSlider,Enabled->Dynamic@TrueQ[fixScale==0]}],Text@Column[{Style["y(t)",Italic]," scale"},Alignment->Center],{{Top,Center}},Spacings->{0,0}],
Dynamic[Plot[If[PossibleZeroQ[ξ-1.],t Exp[-t ωn],-(((E^(t (-ξ ωn-(Sqrt[(-1+(ξ^2)) ((ωn)^2)]))))-(E^(t (-ξ ωn+(Sqrt[(-1+(ξ^2)) ((ωn)^2)])))))/(2 (Sqrt[(-1+(ξ^2)) ((ωn)^2)])))],{t,0,If[TrueQ[fixScale==1],50,maxt]},
ImageSize->{218},
ImageMargins->0,
ImagePadding->{{20,33},{10,20}},
AspectRatio->.6,
PlotRange->{{0,If[TrueQ[fixScale==1],Automatic,maxt]},If[TrueQ[fixScale==1],All,{-maxy,maxy}]},
AxesLabel->{Text@Row[{Style["t",Italic]," (sec)"}],Text@Style["y(t)",Italic]},
PlotLabel->Text@Column[{Style["impulse response",11],If[ξ<1,Style[" (underdamped)",11],If[ξ>1,Style[" (overdamped)",11],Style[" (critcally damped)",11]]]},Alignment->Center],
AxesOrigin->{0,0},
TicksStyle -> Directive[8],PlotStyle->Red]]},
{Control[{{maxt,50,Text["time scale"]},0.1,100,.1,Appearance->"Labeled" ,ImageSize->Small,Enabled->Dynamic@TrueQ[fixScale==0]}],SpanFromLeft},{Control[{{fixScale,1,Text["use automatic scale"]},{0,1} ,ControlType->Checkbox,ImageSize->Small}],SpanFromLeft}
},Alignment->Center,Spacings->{0,0},Frame->None],
FrameMargins->5,ImageSize->260
],
Panel[Grid[{
{Dynamic[Plot[0,{t,.5,-13 },ImageSize->{150},ImageMargins->0,ImagePadding->{{15,5},{5,20}},
AspectRatio->1.6,
PlotRange->{{-13,.5},{-3.5,3.5}},
AxesOrigin->{0,0},
Ticks->{{-12,-6},{-3,3}},
TicksStyle -> Directive[8],PlotLabel->Text@Row[{Style["H(s)",Italic,11],Style[" poles",11]}],
AxesLabel->{None,Text@Row[{Style["j",Italic],"ω"}]},Epilog->{{Thin,Red,Line[{{0,0},If[ξ<1,{-ωn ξ,ωn(Sqrt[ 1-(ξ^2)])},If[ξ>1,{-ωn ξ+ωn(Sqrt[ (ξ^2)-1]),0},{0,ωn}]]}]},{Thin,Red,Line[{{0,0},If[ξ<1,{-ωn ξ,-ωn(Sqrt[ 1-(ξ^2)])},If[ξ>1,{-ωn ξ-ωn(Sqrt[ (ξ^2)-1]),0},{0,-ωn}]]}]},Text[Style["\[Cross]",Bold,16],If[ξ<1,{-ωn ξ,ωn(Sqrt[ 1-(ξ^2)])},If[ξ>1,{-ωn ξ+ωn(Sqrt[ (ξ^2)-1]),0},{0,ωn}]],{0,0}],Text[Style["\[Cross]",Bold,16],If[ξ<1,{-ωn ξ,-ωn(Sqrt[ 1-(ξ^2)])},If[ξ>1,{-ωn ξ-ωn(Sqrt[ (ξ^2)-1]),0},{0,-ωn}]],{0,0}]}]],Dynamic[Plot[0,{t,-1.1,1.1},ImageSize->{150},
ImageMargins->0,
ImagePadding->{{1,1},{5,10}},
AspectRatio->1,
PlotRange->All,
AxesOrigin->{0,0},
PlotLabel->Text@Row[{Style["H(z)",Italic,11],Style[" poles",11]}],
Ticks->None,Epilog->{Circle[{0,0},1],Text[Style["\[Cross]",Bold,16],If[ξ<1,{Exp[-ωn ξ T]Cos[ωn T (Sqrt[1-(ξ^2)])],Exp[-ωn ξ T]Sin[T ωn(Sqrt[1-(ξ^2)])]},If[ξ>1,{Exp[-ωn ξ T+ωn T (Sqrt[ -1+(ξ^2)])],0},{Exp[ωn T],0}]],{0,0}],Text[Style["\[Cross]",Bold,16],If[ξ<1,{Exp[-ωn ξ T]Cos[-T ωn(Sqrt[1-(ξ^2)])],Exp[-T ωn ξ]Sin[-T ωn(Sqrt[1-(ξ^2)])]},If[ξ>1,{Exp[-ωn ξ T-ωn T (Sqrt[ -1+(ξ^2)])],0},{Exp[-ωn T],0}]],{0,0}],
{Thin,Red,Line[{{0,0},If[ξ<1,{Exp[-ωn ξ T]Cos[T ωn(Sqrt[1-(ξ^2)])],Exp[-T ωn ξ]Sin[T ωn(Sqrt[1-(ξ^2)])]},If[ξ>1,{Exp[-T ωn ξ+T ωn(Sqrt[ -1+(ξ^2)])],0},{Exp[T ωn],0}]]}]},{Thin,Red,Line[{{0,0},If[ξ<1,{Exp[-T ωn ξ]Cos[-T ωn(Sqrt[1-(ξ^2)])],Exp[-T ωn ξ]Sin[-T ωn(Sqrt[1-(ξ^2)])]},If[ξ>1,{Exp[-T ωn ξ-T ωn(Sqrt[ -1+(ξ^2)])],0},{Exp[-T ωn],0}]]}]}
}]]}},
Alignment->Center,
Spacings->{0,0},Frame->None,ItemSize->16],FrameMargins->1,ImageSize->260
],

{{t,0},ControlType->None},
{{maxy,1},ControlType->None},
{{maxt,50},ControlType->None},
{{fixScale,1},ControlType->None},
ContinuousAction->False,
SynchronousUpdating->False,
AutorunSequencing->{1,2,3},
ControlPlacement->Left,
Initialization:> 
{

str[expr_]:=Module[{},StringReplace[ToString[expr,FormatType->TraditionalForm],c:LetterCharacter~~"$"~~DigitCharacter..:>c]
];

process[ξ_,ωn_,Ts_]:=Module[{y,H,s,Ω,plotOptions,n,hz,z,dtft,t,ω,p1,p2,p3,T,yString,tfLaplace,laplace,polesAnalog,tfFourier,firstPoleString,secondPoleString,tfLaplaceString,ztfString,ps1,ps2,absMag},

plotOptions={ImageSize->{330},ImageMargins->0,ImagePadding->{{40,55},{25,30}},
AspectRatio->.45,Exclusions->None,PlotRange->All};

ps1=Chop[-ωn ξ-(Sqrt[((ωn)^2) (-1+(ξ^2))])]; ps2=Chop[ -ωn ξ+(Sqrt[((ωn)^2) (-1+(ξ^2))])];
tfLaplace=Simplify[(1/((s-ps1)(s-ps2)))];

tfFourier=tfLaplace/.s->I Ω;
absMag=ComplexExpand[Abs[tfFourier]];

p2=Plot[20 Log[10,absMag],{Ω,0,2Pi/Ts},AxesOrigin->{0,0},Evaluate[plotOptions],AxesLabel->{Row[{Style["Ω ",10],Style["(rad/sec)",9]}],"20 Log|(Subscript[H, a])(jΩ)|"},PlotLabel->Text@Grid[{
{Style["H(s)",12],Style[str[Chop[tfLaplace]],16],SpanFromLeft},{"poles",str[ps1],str[ps2]}},Frame->All]];

f1=HoldForm[(z/(z-Exp[ps1 Ts]))];
f1=ReplacePart[f1,Extract[f1,{1,2,1,2,2,1},Identity],{1,2,1,2,2,1}];
f2=HoldForm[(z/(z-Exp[ps2 Ts]))];
f2=ReplacePart[f2,Extract[f2,{1,2,1,2,2,1},Identity],{1,2,1,2,2,1}];
hz=ReleaseHold[f1]+ReleaseHold[f2];

dtft=hz/.z->Exp[I ω];
absMag=ComplexExpand[Abs[dtft]];
p3=Plot[20 Log[10,absMag],{ω,0.001,2Pi},Evaluate[plotOptions],AxesOrigin->{0,0},
AxesLabel->{Column[{Row[{Style["ω = Ω ",10],Style["T",Italic]}],Style[" (rad)",9]},Center],"20 Log|H((e^(jω)))|"},
Ticks->{{0,Pi/2,Pi,3/2 Pi,2Pi},Automatic},
PlotLabel->Text@Grid[{
{Style["H(z)",12],Style[str[f1+f2],14],SpanFromLeft},{"poles",Chop[Exp[ps1 Ts]],Chop[Exp[ps2 Ts]]}},Frame->All]];

Grid[
{{p2},{p3}},
Alignment->Center,Spacings->{0,1},Frame->None
]]}
]

Digital filter demo

Add later

Contributed by: Nasser M. Abbasi