Convolution with Step Function “Good Kernel”

K[x_,n_]:=n UnitBox[x/(2Pi/n)] (* equals n on interval [-Pi/n,Pi/n] *)

Manipulate[Plot[K[x,n],{x,-Pi,Pi},PlotRange->{-1,30},PlotPoints->4000],{n,1,30,1}]

f[x_]:=20UnitBox[x-1]+Sin[100x]
Plot[f[x],{x,-Pi,Pi},PlotPoints->10000,PlotRange->All]

Manipulate[Plot[{f[y],K[Mod[x-y+Pi,2Pi]-Pi,2]},{y,-Pi,Pi},PlotRange->{-1,21},PlotPoints->4000],{x,-Pi,Pi}]

(* UnitBox[x/(2Pi)] keeps integral on interval [-Pi,Pi] *)

conv2=1/(2Pi)Convolve[K[x,2]UnitBox[x/(2Pi)],f[x]UnitBox[x/(2Pi)],x,y];(* very slow! *)

Plot[{conv2,f[y]},{y,-Pi,Pi}]

Manipulate[Plot[{f[y],K[x-y,10]},{y,-Pi,Pi},PlotRange->{-1,21},PlotPoints->4000],{x,-Pi,Pi}]

conv10=1/(2Pi)Convolve[K[x,10]UnitBox[x/(2Pi)],f[x]UnitBox[x/(2Pi)],x,y];

Plot[{conv10,f[y]},{y,-Pi,Pi}]

Manipulate[Plot[{f[y],K[x-y,50]},{y,-Pi,Pi},PlotRange->{-1,51},PlotPoints->4000],{x,-Pi,Pi}]

conv50=1/(2Pi)Convolve[K[x,50]UnitBox[x/(2Pi)],f[x]UnitBox[x/(2Pi)],x,y];

Plot[conv50,{y,-Pi,Pi},PlotRange->All,PlotPoints->2000]

Plot[{conv50,f[y]},{y,-Pi,Pi},PlotRange->All]