%fsedemo.m
% TJ Endres 10/95 & CRJ 10/95 and 4/96
% revised 6/12/96: SER formula accounting for achieved cursor value
clear
close all
chan=input('enter (as a column vector) T/2-spaced channel impulse response = ');
snrdb=input('enter (as a row vector) SNR (dB) range = ');
Lg=input('enter (as an integer) equalizer length = ');


%example 0: illustrative
%          chan=[.1 -.2 -.1 1 .5 -.1 -.3 -.1 .1]'
%          snrdb=20:30
%          Lg=7


%example 1: almost reflected roots
%           chan=[.1 .3 -.4 1 .5 -.3 -.2 .1 .1]'
%           snrdb=20:50
%           Lg=6

%example 2: zeros near unit circle
%           chan=[-.05 0.1 0.8 1.2 .4 1 .2 -.1 -.2 .2 .05 -.05]'
%           snrdb=20:50
%           Lg=9

%example 3: almost a pole
%           chan=[1 .6 .36 .216 .12 .07 .04]'
%           snrdb=20:50
%           Lg=9

SNR=10.^(snrdb/10);
M=64;                           %M-ary QAM


%initialize vectors
mse=100*ones(length(SNR),1);
SERqam=100*ones(length(SNR),1);
SERqam_ideal=100*ones(length(SNR),1);
condnum=100*ones(length(SNR),1);

for kk=1:length(SNR)
desirednorm=2*(1-1/SNR(kk));    %normalize channel
chan=(chan/norm(chan))*sqrt(desirednorm);

% even row decimation
Lc=length(chan);                % chan length
C=convmtx(chan,Lg);
Ce=C(1:2:Lc+Lg-1,:);
Pe=inv(Ce'*Ce + (1/SNR(kk))*eye(Lg));
Lde=ceil((Lg+Lc-1)/2);          % number of equations in mtx
Me=(eye(Lde)-Ce*Pe*Ce')^2 + (Ce*Pe'*Pe*Ce')*(1/SNR(kk));
[me, delaye]=min(diag(Me));

% odd row decimation
Co=C(2:2:Lc+Lg-1,:);
Po=inv(Co'*Co + (1/SNR(kk))*eye(Lg));
Ldo=floor((Lg+Lc-1)/2);         % number of equations in mtx
Mo=(eye(Ldo)-Co*Po*Co')^2 + (Co*Po'*Po*Co')*(1/SNR(kk));
[mo, delayo]=min(diag(Mo));

% choose between odd and even decimation
if me<=mo
h_desired=zeros(Lde,1);         % desired chan-eq combo
h_desired(delaye)=1;
gopt=Pe*Ce'*h_desired;          % MMSE equalizer taps
hhh1=conv(chan,gopt);
hhh=hhh1(1:2:length(hhh1));     % achieved response
hhh=hhh/max(abs(hhh));
condnum(kk)=cond(Ce'*Ce + (1/SNR(kk))*eye(Lg));
else

h_desired=zeros(Ldo,1);         % desired chan-eq combo
h_desired(delayo)=1;
gopt=Po*Co'*h_desired;          % MMSE equalizer taps
hhh1=conv(chan,gopt);
hhh=hhh1(2:2:length(hhh1));     % achieved response
hhh=hhh/max(abs(hhh));
condnum(kk)=cond(Co'*Co + (1/SNR(kk))*eye(Lg));
end  %if

% error rate performance
ng=(gopt'*gopt)/(max(abs(hhh))^2);  % eq noise gain
mse(kk)= (h_desired-hhh)'*(h_desired-hhh)+(1/SNR(kk))*ng;
SERpam=(1-1/sqrt(M))*erfc(sqrt((3/(M-1))*(1/2).*(1/mse(kk))));
SERqam(kk)=1-(1-SERpam).^2;

%ideal case
ctap=sqrt(desirednorm);         % ideal channel tap
mseideal=(1/ctap)^2*(1/SNR(kk));
SERpam_ideal=(1-1/sqrt(M))*erfc(sqrt((3/(M-1))*(1/2).*(1/mseideal)));
SERqam_ideal(kk)=1-(1-SERpam_ideal).^2;

end  %for

figure(1)
clg
plotpz(chan,1)
title('Zero Locations of T/2-Spaced Channel')
pause

figure(2)
clg
semilogy(snrdb,SERqam,'w-',snrdb,SERqam_ideal,'w:')
title(['Optimum Error Rate Performance; T/2 Eq taps = ',num2str(Lg)])
xlabel('SNR (dB) ')
ylabel('SER')
pause

figure(3)
clg
[hg,wg]=freqz(gopt,1,512,'whole');
[hc,wc]=freqz(chan,1,512,'whole');
subplot(211),plot(abs(hc),'w');
title('Magnitude Response of T/2-Spaced Channel')
subplot(212),plot(abs(hg),'w');
title('Magnitude Response of T/2-Spaced Equalizer')
pause

figure(4)
clg
[hc1,wc1]=freqz(hhh1,1,512,'whole');
[ho,wo]=freqz(hhh,1,512,'whole');
subplot(211),plot(abs(hc1),'w');
title('Magnitude Response of T/2-Spaced Chan-Eq. Combination')
subplot(212),plot(abs(ho),'w');
title('Magnitude Response of T-Spaced Chan-Equal. Combination')
pause

figure(5)
clg
subplot(211),stem(hhh1)
title('T/2-Spaced Combined Chan-Equal Time Response')
subplot(212),stem(hhh)
title('T-Spaced Combined Chan-Equal Time Response')
pause

figure(6)
clg
plot(snrdb,condnum,'w')
title('Condition Number of Channel Convolution Mtx vs. SNR')
xlabel('SNR (dB)')
ylabel('Condition Number')

*********************************************

Rick Johnson
School of Elec. Eng.
390 Frank H.T. Rhodes Hall
Cornell University
Ithaca, NY  14853   USA
phone (w/ answering machine): (607) 255-0429
fax: (607) 255-9072
johnson@anise.ee.cornell.edu

*********************************************




