
T = 1;
w0 = 2*Pi*T;
signal[t_] := 1 + 2*Cos[w0*t] + 0.5*Cos[2*w0*t] + 1*Sin[3*w0*t] + 0.2*Cos[10*w0*t];
                
Plot[signal[t], {t, 0, 1},AxesLabel->{"t","s"}]

npoints = 50; (* nombre de points *)
te = T/npoints; (* temps d'echantillonnage *)
temps[k_] := (k - 1)*te;
echantillons = Table[signal[temps[k]], {k, npoints}];
                

coefFourier = Fourier[echantillons, FourierParameters -> {-1, -1}];
frequences[k_] := (k - 1)/T;
raies = Table[Line[{{frequences[k], 0}, {frequences[k], Abs[coefFourier[[k]]]}}], {k, npoints}];           
                
Show[Graphics[{RGBColor[1,0,0],raies},Frame->True,PlotRange->{{0,npoints/T},{0,1}},
			AspectRatio->0.6,FrameLabel->{"f","|Sn|"}]]

raiesSpectre[npoints_]:=Module[{te,temps,echantillons,frequences},
	te = T/npoints;
	temps[k_] := (k - 1)*te;
	echantillons = Table[signal[temps[k]], {k, npoints}];
	coefFourier = Fourier[echantillons, FourierParameters -> {-1, -1}];
	frequences[k_] := (k - 1)/T;
	Return[Table[Line[{{frequences[k], 0}, {frequences[k], Abs[coefFourier[[k]]]}}],
						{k, npoints}]];
]
                

npoints=15;
raies = raiesSpectre[npoints];
                
Show[Graphics[{RGBColor[1,0,0],raies},Frame->True,PlotRange->{{0,npoints/T},{0,1}},
			AspectRatio->0.6,FrameLabel->{"f","|Sn|"}]]

npoints=21;
raies = raiesSpectre[npoints];
                
Show[Graphics[{RGBColor[1,0,0],raies},Frame->True,PlotRange->{{0,npoints/T},{0,1}},
			AspectRatio->0.6,FrameLabel->{"f","|Sn|"}]]

T=1
signal[t_]:=If[t<T/2,1,-1];     
                

npoints=100;
raies = raiesSpectre[npoints];
                
Show[Graphics[{RGBColor[1,0,0],raies},Frame->True,PlotRange->{{0,npoints/T},{0,1}},
			AspectRatio->0.6,FrameLabel->{"f","|Sn|"}]]

indices=Table[2*p-1,{p,1,20}];
precision=Table[Abs[coefFourier[[2]]]/Abs[coefFourier[[2*p]]]/(2*p-1),{p,1,20}];
            

npoints=500;
raies = raiesSpectre[npoints];
indices=Table[2*p-1,{p,1,20}];
precision=Table[Abs[coefFourier[[2]]]/Abs[coefFourier[[2*p]]]/(2*p-1),{p,1,20}];
            
