%Demo done during Session 24 to illustrate
%the sidelobe cancellation and mainlobe widening
%effects achieved with a sine window relative
%to a rectangular window of the same length
clf
set(0,'defaultaxesfontsize',22);
clear
Nw=input('window length');
Mdel=(Nw-1)/2;
Nfft=input('FFT length');
freqomega=linspace(-pi,pi-2*pi/Nfft,Nfft);
rmphase=exp(j*Mdel*freqomega);
woff=pi/Nw;
coff=(Nw-1)/2;
wrec=ones(1,Nw);
wsin=cos(woff*((0:Nw-1)-coff));
whan=.5*wrec+.5*cos(2*woff*((0:Nw-1)-coff));
wham=.54*wrec+.46*cos(2*woff*((0:Nw-1)-coff));
xn=0:Nw-1;
plot(xn,wrec,'y','LineWidth',4);
axis([0 Nw-1 0 1])
xlabel('n')
hold 
plot(xn,wsin,'m','LineWidth',4);
plot(xn,whan,'c','LineWidth',4);
plot(xn,wham,'r','LineWidth',4);
legend('Rect','Sine','Hanning','Hamming');
title('Various Windows')
hold off
pause
mod=exp(j*(pi/Nw)*(0:Nw-1));
omega=linspace(-pi,pi,Nfft);
fsin=real(rmphase.*(fftshift(fft(wsin,Nfft))));
fprec=real(rmphase.*(fftshift(fft((mod.*wrec)/(2*j),Nfft))));
fnrec=real(rmphase.*(fftshift(-fft((conj(mod).*wrec)/(2*j),Nfft))));
absmin=min([min(fsin) min(fprec) min(fnrec) ]);
absmax=max([max(fsin) max(fprec) max(fnrec) ]);
plot(omega,fprec,'k','LineWidth',4);
axis([-pi pi absmin absmax])
xlabel('Omega (rps)')
ylabel('Magnitude ')
hold on
pause
plot(omega,fnrec,'m','LineWidth',4);
pause
plot(omega,fsin,'c','LineWidth',4);
%pause
%plot(omega,fham,'r','LineWidth',4);
legend('pi/M Offset','-pi/M Offset','Sine');
title('Depiction of Sidelobe Cancellation')
hold off

