You are on page 1of 6

LMS Channel eq

clc
clear all
close all
N = 750; %simulation length
M = 5; %channel length
T = 20; %number of independent trials
cir = zeros(1,2*M-1); %cascade impulse response
MSE_vec = zeros(T,N-3); %Mean Square Error vector
MSE_f=zeros(1,N-3); %final MSE vector
o_p=zeros(1,N); %output from the equalizer in Tth trial
for j = 1 : T
u = sign(randn(1,N)); %input signal
c = randn(M,1); %channel to be equalized
c = c / norm(c);
z = filter(c,1,u); %channel output
SNR = 30; %Signal to Noise Ratio
var_v = var(z) * 10^(-SNR/10); %additive noise to the channel output
v = var_v^0.5 * randn(1,N);
y = z + v; %input to the equalizer
%-----------------------------LMS channel equalization-----------------------
%
w = zeros(M,1);
y_d = zeros(1,M);
step =.12;
MSE=0;
for k = 4 : N
y_d = [y(k) y_d(1:M-1)];
e = u(k-3) - y_d * w;
w = w + step * y_d' * e ;
if j==T
o_p(1,k)=y_d * w;
end
MSE=MSE+(abs(e).^2);
MSE_avg=MSE/(k-3);
MSE_vec(j,:)=[MSE_avg MSE_vec(j,1:N-4)];%mse is calculated row wise,
%MSE_vec(j,1:N-4)(j=row no,elements
%are from 1st to N-4
end
MSE_vec(j,:)=fliplr(MSE_vec(j,:));%rows are inverted from left to right
q=[4:1:N];
figure(2);
plot(q,MSE_vec(j,:));
title('mean square error for different trial');
xlabel('No of iterations ');
ylabel('MSE');
hold on;
cir = cir + conv(w,c)';
end
hold off
figure(1);
subplot(2,1,1);
plot(q,MSE_vec(j,:));
title('MSE in Tth trial');
xlabel('No of iterations');
ylabel('MSE');
hold on;
o_p_b=sign(o_p); %converting to binary(+1/-1)
n=[1:N];
plot(n,v); %noise in the final trial
hold off;
temp=ones(T,1);
for j=1:N-3
MSE_f(1,j)=MSE_f(1,j)+((MSE_vec(:,j)'*temp)/T);
end
subplot(2,1,2);
plot(q,MSE_f);
title('MSE in final trial');
xlabel('No of iterations');
ylabel('MSE');
%-----------------------FOR SIGNALS IN FINAL TRIAL-------------------------%
figure(3);
subplot(3,1,1);
plot(c);
title('channel impulse response');
xlabel('time');
ylabel('magnitude');
subplot(3,1,2);
plot(w);
title('equalizer impulse response');
xlabel('time');
ylabel('magnitude');
subplot(3,1,3);
stem(cir/T);
title('cascade channel-equalizer impulse response');
ylabel('magnitude');
xlabel('taps');
figure(4);
subplot(2,2,1);
n_i=n(4:N);
stem(n_i,u(1:N-3));
xlabel('time');
title('input signal');
ylabel('magnitude');
subplot(2,2,2);
stem(n,z);
xlabel('time');
title('output after passing through channel');
ylabel('magnitude');
subplot(2,2,3);
n_o=n(1:N-3);
stem(n_o,o_p(1:N-3));
xlabel('time');
title('Final output');
ylabel('magnitude');
subplot(2,2,4);
stem(n_o,o_p_b(1:N-3));
xlabel('time');
title('Binary final output');
ylabel('magnitude');
16-QAM
% symbol error rate for 16-QAM modulation
clear
N = 2*10^5; % number of symbols
alpha16qam = [-3 -1 1 3]; % 16-QAM alphabets
Es_N0_dB = [0:20]; % multiple Es/N0 values
ipHat = zeros(1,N);
for ii = 1:length(Es_N0_dB)
ip = randsrc(1,N,alpha16qam) + j*randsrc(1,N,alpha16qam);
s = (1/sqrt(10))*ip; % normalization of energy to 1
n = 1/sqrt(2)*[randn(1,N) + j*randn(1,N)]; % white guassian noise, 0dB
variance

y = s + 10^(-Es_N0_dB(ii)/20)*n; % additive white gaussian noise

% demodulation
y_re = real(y); % real part
y_im = imag(y); % imaginary part

ipHat_re(find(y_re< -2/sqrt(10))) = -3;


ipHat_re(find(y_re > 2/sqrt(10))) = 3;
ipHat_re(find(y_re>-2/sqrt(10) & y_re<=0)) = -1;
ipHat_re(find(y_re>0 & y_re<=2/sqrt(10))) = 1;

ipHat_im(find(y_im< -2/sqrt(10))) = -3;


ipHat_im(find(y_im > 2/sqrt(10))) = 3;
ipHat_im(find(y_im>-2/sqrt(10) & y_im<=0)) = -1;
ipHat_im(find(y_im>0 & y_im<=2/sqrt(10))) = 1;
ipHat = ipHat_re + j*ipHat_im;
nErr(ii) = size(find([ip- ipHat]),2); % couting the number of errors
end

simBer = nErr/N;
theoryBer = 3/2*erfc(sqrt(0.1*(10.^(Es_N0_dB/10))));
close all
figure
semilogy(Es_N0_dB,theoryBer,'b.-','LineWidth',2);
hold on
semilogy(Es_N0_dB,simBer,'mx-','Linewidth',2);
axis([0 20 10^-5 1])
grid on
legend('theory', 'simulation');
xlabel('Es/No, dB')
ylabel('Symbol Error Rate')
title('Symbol error probability curve for 16-QAM modulation')
OFDM

clear all
nFFT = 64; % fft size
nDSC = 52; % number of data subcarriers
nBitPerSym = 52; % number of bits per OFDM symbol (same as the number of
subcarriers for BPSK)
nSym = 10^4; % number of symbols
EbN0dB = [0:10]; % bit to noise ratio
EsN0dB = EbN0dB + 10*log10(nDSC/nFFT) + 10*log10(64/80); % converting to
symbol to noise ratio
for ii = 1:length(EbN0dB)
% Transmitter
ipBit = rand(1,nBitPerSym*nSym) > 0.5; % random 1's and 0's
ipMod = 2*ipBit-1; % BPSK modulation 0 --> -1, 1 --> +1
ipMod = reshape(ipMod,nBitPerSym,nSym).'; % grouping into multiple
symbolsa
% Assigning modulated symbols to subcarriers from [-26 to -1, +1 to +26]
xF = [zeros(nSym,6) ipMod(:,[1:nBitPerSym/2]) zeros(nSym,1)
ipMod(:,[nBitPerSym/2+1:nBitPerSym]) zeros(nSym,5)] ;
% Taking FFT, the term (nFFT/sqrt(nDSC)) is for normalizing the power of
transmit symbol to 1
xt = (nFFT/sqrt(nDSC))*ifft(fftshift(xF.')).';
% Appending cylic prefix
xt = [xt(:,[49:64]) xt]; % Concatenating multiple symbols to form a long
vector
xt = reshape(xt.',1,nSym*80);
% Gaussian noise of unit variance, 0 mean
nt = 1/sqrt(2)*[randn(1,nSym*80) + j*randn(1,nSym*80)];
% Adding noise, the term sqrt(80/64) is to account for the wasted energy
due to cyclic prefix
yt = sqrt(80/64)*xt + 10^(-EsN0dB(ii)/20)*nt;
% Receiver
yt = reshape(yt.',80,nSym).'; % formatting the received vector into
symbols
yt = yt(:,[17:80]); % removing cyclic prefix
% converting to frequency domain
yF = (sqrt(nDSC)/nFFT)*fftshift(fft(yt.')).';
yMod = yF(:,[6+[1:nBitPerSym/2] 7+[nBitPerSym/2+1:nBitPerSym] ]);
% BPSK demodulation +ve value --> 1, -ve value --> -1
ipModHat = 2*floor(real(yMod/2)) + 1;
ipModHat(find(ipModHat>1)) = +1;
ipModHat(find(ipModHat<-1)) = -1;
% converting modulated values into bits
ipBitHat = (ipModHat+1)/2;
ipBitHat = reshape(ipBitHat.',nBitPerSym*nSym,1).';
nErr(ii) = size(find(ipBitHat - ipBit),2); % counting the errors

end
simBer = nErr/(nSym*nBitPerSym);
theoryBer = (1/2)*erfc(sqrt(10.^(EbN0dB/10)));
close all; figure
semilogy(EbN0dB,theoryBer,'bs-','LineWidth',2);
hold on
semilogy(EbN0dB,simBer,'mx-','LineWidth',2);
axis([0 10 10^-5 1])
grid on
legend('theory', 'simulation');
xlabel('Eb/No, dB')
ylabel('Bit Error Rate')
title('Bit error probability curve for BPSK using OFDM')

Digital low pass filter using function remez

fs=2000;%the sample frequency40


rp=3;%passband ripple
rs=40;%stopband ripple
f=[500 600];%cutoff frequency
a=[1 0];%desired amplitude
dev=[(10^(rp/20)-1)/(10^(rp/20)+1) 10^(-rs/20)];
[n,fo,ao,w]=remezord(f,a,dev,fs);
%function remezord returns to parameter n and it stand for the order of
%filter
%when the number of frequency band is B, f,a,dev are vectors with 2B-2,B,B
%elements respectively.
b=remez(n,fo,ao,w);%The return number of function Remez is the order of
%n-order filter.
%fo,ao are vectors with 2B elements, represent boundary frequencies
%and amplitude in B frequencies band.
%w is a vector with B elements, represents weighted value of each
%frequency band.
figure(1)
freqz(b,1,1024,fs);%the characteristic diagram of the filter
f1=400;f2=700;%the frequencies of sines signal that needs filtered
t=0:1/fs:0.1;%define the time domain and steplength
s=sin(2*pi*f1*t)+sin(2*pi*f2*t);%signal before filtering
figure(2)
subplot(211)
plot(t,s);%plot the diagram before filtering
xlabel('time/s');ylabel('amplitude');
title('Time-domain diagram before filtering');
subplot(212)
Fs=fft(s,512);%transform to the frequency domain
AFs=abs(Fs);%the amplitude
f=(0:255)*fs/512;%frequency sampling
plot(f,AFs(1:256));%plot the frequency domain diagram before filtering
xlabel('frequency/Hz');ylabel('amplitude');
title('Frequency-domain diagram before filtering');
figure(3)
figure(3)
sf=filter(b,1,s);%use function filter
subplot(211)
plot(t,sf)%plot the diagram after filtering
xlabel('time/s');ylabel('amplitude');
title('Time-domain diagram after filtering')
subplot(212)
Fsf=fft(sf,512);AFsf=abs(Fsf);
f=(0:255)*fs/512;%frequency sampling
plot(f,AFsf(1:256))%plot the frequency domain diagram before filtering
xlabel('frequency/Hz');ylabel('amplitude');
title('Frequency-domain diagram after filtering');

Cyclic Coding

% Encoding & Decoding for (7,4) Cyclic code


clc;
clear all;
%Encoding
n=7;
k=4;
p=[1 1 0 ; 1 1 1; 0 0 1 ; 1 0 1]; % Parity Matix
d=[1 1 0 1]; % Message word
ik=eye(k);
g=cat(2,ik,p);disp('Generator Matrix:');disp(g);
g1=cyclpoly(n,k,'max');
gp=poly2sym(g1);
disp('Generator Polynomial:');disp(gp);
c1=mtimes(d,g);
c=mod(c1,2);
disp('The codeword for given message is:'); disp(c);
%Decoding
r=[1 0 0 1 1 1 0];disp('received word of 7 bit:');disp(r);
ink=eye(n-k);
h=cat(2,p',ink);
ht=h';disp('Transpose of parity check matrics :');disp(ht);
rp=poly2sym(r);
[qp,remp]=quorem(rp,gp);
disp('Syndrome polynomial:');disp(qp);
rem=sym2poly(remp);
s=mod(rem,2);disp('Syndrome :');disp(s);
if (s == 0)
disp('The received code is correct.');
else
disp('The received code is incorrect.');
row = 0;
for j=1:1:n
m=xor(s,ht(j,:));
if (m==0)
row = j;
break;
end
end
r(1,row) = ~r(1,row);
disp(r);
disp('Correct codeword is:');
end
disp('c=');
disp(r);

You might also like