(* ANIMAZIONE CONVOLUZIONE - GABRIEL HINE 30/11/2018 *)

Clear["Global`*"]
(*Funzione per graficare una funzione contenente delta di dirac NON TOCCARE*)
ArrowsDeltaFunction[eqn_,x_]:=Module[{xsubs,listDeltas,coefDeltas,locationDeltas},xsubs=(x/.Cases[eqn,DiracDelta[a__]:>Solve[a==0,x],Infinity]);locationDeltas=Flatten[xsubs];Arrow[Table[{{locationDeltas[[i]],0},{locationDeltas[[i]],( N[Integrate[eqn,{x,locationDeltas[[i]]-10^-18,locationDeltas[[i]]+10^-18}]])/. x->locationDeltas[[i]]}},{i,1,Length[locationDeltas]}]]]

(* definizioni NON TOCCARE*)
δ[x_] := DiracDelta[x];
Rect[x_,W_]:= UnitBox[x/W];
Tri  [x_,W_]:= UnitTriangle[x/W];

(* SI CALCOLI LA CONVOLUZIONE x(t) * h(t) *)
(*usare le rect per definire ciascun tratto (finestra) del segnale*)

h = - Rect[t+1/2,1] + Rect[t-1/2,1] 
x = Rect[t+3/2,1] - (t) Rect[t+1/2,1] +(t) Rect[t-1,2] + 2 Rect[t-5/2,1] 
(*Plot*)
Plot[h,{t,-2.5,3.5},Filling->Axis,AxesLabel->{"t","h(t)"}]
Plot[x,{t,-2.5,3.5},Filling->Axis,AxesLabel->{"t","x(t)"} ]

(*Convoluzione*)
y = Simplify[Convolve[x,h,t,τ]/.τ->t ]
Plot[y,{t,-3.5,4.5},Filling->Axis,AxesLabel->{"t","x(t)"} ]

(*Animazione*)
hh = h /.{t-> (τ-t)}
Animate[{Plot[{x,hh/.τ-> a },{t,-5,6},Filling->Axis],Plot[y*UnitStep[a-t],{t,-5,6},Filling->Axis,PlotRange->{-1.5,2.5}]},{a,-4,4}]