sf[n_,x_]:=Pi/2-4/Pi Sum[Cos[k x]/k^2,{k,1,n,2}]

sg[n_,x_]:=4/Pi Sum[Sin[k x]/k,{k,1,n,2}]

g1=Plot[sf[10,x],{x,-3Pi,3Pi}]
g2=Plot[sg[10,x],{x,-3Pi,3Pi}]

f[x_]:=Abs[x]

fp[x_]:=Abs[Mod[x,2Pi,-Pi]]

Plot[fp[x],{x,-3Pi,3Pi}]

1/Pi Integrate[f[x]Cos[n x],{x,-Pi,Pi}]

a[n_]:=Simplify[1/Pi Integrate[f[x]Cos[n x],{x,-Pi,Pi}],Element[n,Integers]]

ap[n_]:=Simplify[1/Pi Integrate[fp[x]Cos[n x],{x,-Pi,Pi}],Element[n,Integers]]

a[n]

Timing[Table[a[n],{n,0,10}]]

g[x_]:=Sign[x]

Plot[g[Mod[x,2Pi,-Pi]],{x,-3Pi,3Pi}]

b[n_]:=Simplify[1/Pi Integrate[g[x]Sin[n x],{x,-Pi,Pi}],Element[n,Integers]]

b[n]

Timing[Table[b[n],{n,1,10}]]

Manipulate[Plot[sg[n,x],{x,-Pi,Pi},PlotPoints->200],{n,1,100,1}]

(* Timing[Table[ap[n],{n,0,10}]] *)