MATLAB code for Example 9.1.4, from Orfanidis
fs=10e3; % Sampling frequency T=1/fs; % Sampling period f1=2e3; % Frequencies of three sinusoids f2=2.5e3; f3=3e3; w = 0:pi/1000:pi; % Frequencies to calculate DFT figure(1) L = 10; % Define L n=0:L-1; x=cos(2*pi*f1*n*T)+cos(2*pi*f2*n*T)+cos(2*pi*f3*n*T); % Calcular x(n)*w(n) X=0; % Calculate X(w) for k=0:L-1 X = X + x(k+1)*exp(-j*w*(k)); end subplot(221) plot(w/pi,abs(X)) axis([0 1 0 50]) title('Rectangular Window -- L = 10') h = hamming(L); % Find Hamming window Xh=0; % Xh(w) = DFT [x(n)*h(n)] for k=0:L-1 Xh = Xh + x(k+1)*h(k+1)*exp(-j*w*(k)); end subplot(222) plot(w/pi,abs(Xh)) axis([0 1 0 50]) title('Hamming Window -- L = 10') L = 20; n=0:L-1; x=cos(2*pi*f1*n*T)+cos(2*pi*f2*n*T)+cos(2*pi*f3*n*T); X=0; for k=0:L-1 X = X + x(k+1)*exp(-j*w*(k)); end subplot(223) plot(w/pi,abs(X)) axis([0 1 0 50]) title('Rectangular Window -- L = 20') h = hamming(L); Xh=0; for k=0:L-1 Xh = Xh + x(k+1)*h(k+1)*exp(-j*w*(k)); end subplot(224) plot(w/pi,abs(Xh)) axis([0 1 0 50]) title('Hamming Window -- L = 20') figure(2) L = 40; n=0:L-1; x=cos(2*pi*f1*n*T)+cos(2*pi*f2*n*T)+cos(2*pi*f3*n*T); X=0; for k=0:L-1 X = X + x(k+1)*exp(-j*w*(k)); end subplot(221) plot(w/pi,abs(X)) axis([0 1 0 50]) title('Rectangular Window -- L = 40') h = hamming(L); Xh=0; for k=0:L-1 Xh = Xh + x(k+1)*h(k+1)*exp(-j*w*(k)); end subplot(222) plot(w/pi,abs(Xh)) axis([0 1 0 50]) title('Hamming Window -- L = 40') L = 100; n=0:L-1; x=cos(2*pi*f1*n*T)+cos(2*pi*f2*n*T)+cos(2*pi*f3*n*T); X=0; for k=0:L-1 X = X + x(k+1)*exp(-j*w*(k)); end subplot(223) plot(w/pi,abs(X)) axis([0 1 0 50]) title('Rectangular Window -- L = 100') h = hamming(L); Xh=0; for k=0:L-1 Xh = Xh + x(k+1)*h(k+1)*exp(-j*w*(k)); end subplot(224) plot(w/pi,abs(Xh)) axis([0 1 0 50]) title('Hamming Window -- L = 100')