NN = 100; (** number of  intervalle for [0,π], so we get NN+1 points! **) 
FF = 30; (** number of fourier coefficients **)

xPos[i_]:=i (π/NN);
HH := xPos[1]-xPos[0];

Let's choose some arbitrary initial condition for testing ...

u0=Table[Cos[ 2.2i/NN](2/NN)(Piecewise[{{i, i<NN/2}, {NN-i, True}}]), {i, 0,NN }];
ListPlot[{xPos[#],u0[[#+1]]}&/@Range[0,NN], Joined->True,PlotRange->{-1,1}]

The fourier coefficients are (i guess) 
(Subscript[b, n])= (∫_0^π)g(x) sin( nx ) dx, because we have a discrete function this simplifies to  
(Subscript[b, n])= (∑_(i=1)^N)(∫_(Subscript[x, i-1])^(Subscript[x, i]))((Subscript[g, i-1]) + ((Subscript[g, i])-(Subscript[g, i-1]))((x-(Subscript[x, i-1]))/((Subscript[x, i])-(Subscript[x, i-1]))))sin(n x)dx

The inner integral can be solved explicitly 
(where (Subscript[x, a])=(Subscript[x, i-1]), (Subscript[x, b])=(Subscript[x, i]) and  (Subscript[g, i-1])= g( (Subscript[x, a])), (Subscript[g, i])= g( (Subscript[x, b])) , h = (Subscript[x, b])-(Subscript[x, a])): 

FullSimplify[(∫_xa^xb)(ga+(gb-ga)*(((x-xa))/((xb-xa))))Sin[n x]dx, 
Reals, 
TransformationFunctions->Prepend[Function[x,x-#]&/@Flatten@Map[{#,-#}&,{xb-xa==h}/.Equal->Subtract],Automatic]
]

((ga h n Cos[n xa]-gb h n Cos[n xb]+(ga-gb) (Sin[n xa]-Sin[n xb]))/(h (n^2)))

We can (if we wish to do so) use this term to directly compute the fourier coefficients:

b = Table[
N[(2/π)(1/((n^2) HH))(∑_(i=1)^NN)(u0[[i]]  HH n Cos[n xPos[i-1]]-u0[[i+1]]  HH n Cos[n xPos[i]]+(u0[[i]]-u0[[i+1]]) (Sin[n xPos[i-1]]-Sin[n xPos[i]]))], 
{n, 1, FF}
];
b//BarChart

The solution is then:

Manipulate[
Plot[(∑_(n=1)^FF)b[[n]]Sin[n x]Cos[n t], {x, 0, π}, PlotRange->{-1,1}],
{t, 0, 10}
]

We can look at the solution for all times directly:

Plot3D[
(∑_(n=1)^FF)b[[n]]Sin[n x]Cos[n t], {x, 0, π}, {t, 0, 10}, 
AxesLabel->{x, t, u}, 
Mesh->{0, 11}
]