signal[t_]:=Cos[2*Pi*t]+0.5*Cos[2*2*Pi*t]+0.25*Sin[3*2*Pi*t]+0.125*Sin[4*2*Pi*t];
tmax=50.32563;
n=1000;
te=tmax/n;
fe=1/te (*frequence d'echantillonnage*) 
            

echant=Table[signal[(k-1)*te],{k,n}];
            
tfd=Fourier[echant,FourierParameters->{-1,-1}];
spectre=Table[{(k-1)/tmax,Abs[tfd[[k]]]},{k,n}]; 
            
ListPlot[spectre,Filling->Axis,PlotRange->{{0,5},Full},AxesLabel->{"f","A"}]
Wr[f_]:=Sinc[Pi*f];
            
Plot[20*Log[10,Abs[Wr[f]]],{f,-10,10},AxesLabel->{"f","dB"},
            PlotRange->{Full,{-100,0}}]
hamming[t_]:=Piecewise[{{0,t<0},{0,t>1}},0.54-0.5*Cos[2*Pi*t]];
Plot[hamming[t],{t,0,1}]
Wh=FourierTransform[hamming[t],t,f,FourierParameters->{0,-2*Pi}];
Plot[20*Log[10,Abs[Wh]],{f,-10,10},AxesLabel->{"f","dB"},PlotRange->{Full,{-100,0}}]
echantHamming = Table[echant[[k]]*hamming[k/n],{k,n}];
tfd=Fourier[echantHamming,FourierParameters->{-1,-1}];
spectre=Table[{(k-1)/tmax,Abs[tfd[[k]]]},{k,n}]; 
            
ListPlot[spectre,Filling->Axis,PlotRange->{{0,5},Full},AxesLabel->{"f","A"}]

Options[Spectre]={Fenetre->"hamming"};
Spectre[signal_,fe_,opts___]:=Module[{ne,fenetre,w,echant,tfd,spectre},
	fenetre = Fenetre/.{opts}/.Options[Spectre];
	ne=Length[signal];
	w["rectangulaire"][k_]:=1.0;
	w["hamming"][k_]:=0.54-0.46*Cos[2*Pi/ne*k];
	w["hanning"][k_]:=0.5-0.5*Cos[2*Pi/ne*k];
	w["blackman"][k_]:=0.42-0.5*Cos[2*Pi/ne*k]+0.08*Cos[4*Pi/ne*k];
	echant=Table[signal[[k+1]]*w[fenetre][k],{k,0,ne-1}];
	tfd=Fourier[echant,FourierParameters->{-1,-1}];
	spectre=Table[{fe*k/ne,Abs[tfd[[k]]]},{k,ne}];
	Return[spectre];
];
            
spectre=Spectre[echant,fe,Fenetre->"hanning"];
            
ListPlot[spectre,Filling->Axis,PlotRange->{{0,5},Full},AxesLabel->{"f","A"}]
