SIMULATION CODES of DC
SIMULATION CODES of DC
clc;
clear all;
% Set frequency & time vector (random process)
w0=2*pi*1/3; % Frequency is 1/3hz
t=0:1/10:10; %time runs from 0 to 10 seconds with 0.1sec increments
for k=1:4;
Th=2*pi*rand(1);
plot(t, sin(w0*t+Th));
hold all;
end
hold off;
grid on;
axis ([0 10 -2 2])
title ('Realization of process x(t)');
xlabel ('t(sec)');
ylabel ('volt');
Output:
Name of Student: Roll No:
clear;
clc;
N=10000000; %Number of input bits
EbN0dB = -5:1:27;
data=randn(1,N)>=0; % Generating a uniformly distributed random 1s and 0s
bpskModulated = 2*data-1; % Mapping 0->-1 and 1->1
M=2; % Number of Constellation points M=2^k for BPSK k=1
Rm=log2(M); % Rm=log2(M) for BPSK M=2
Rc=1; %Number of Constellation points M=2^k for BPSK k=1
BER = zeros(1,length(EbN0dB)); %Place holder for BER values for each Eb/N0
index=1;
for k=EbN0dB,
%Adding noise with variance according to the required Eb/N0
EbN0 = 10.^(k/10); %Converting Eb/N0 dB value to linear scale
noiseSigma = sqrt(1./(2*Rm*Rc*EbN0)); %Standard deviation for AWGN Noise
noise = noiseSigma*randn(1,length(bpskModulated));
received = bpskModulated + noise;
estimatedBits=(received>=0);
BER(index) = sum(xor(data,estimatedBits))/length(data);
index=index+1;
end
for
plotHandle=plot(EbN0dB,log10(BER),'r--');
set(plotHandle,'LineWidth',1.5);
title('SNR per bit (Eb/N0) Vs BER Curve for BPSK Modulation Scheme');
xlabel('SNR per bit (Eb/N0) in dB');
ylabel('Bit Error Rate (BER) in dB');
grid on;
hold on;
theoreticalBER = 0.5*erfc(sqrt(10.^(EbN0dB/10)));
plotHandle=plot(EbN0dB,log10(theoreticalBER),'k*');
set(plotHandle,'LineWidth',1.5);
legend('Simulated','Theoretical');
grid on;
Output:
Name of Student: Roll No:
Title of Experiment: Simulation of Entropy
hx=0;
px=0:.01:1;
for i=1:length(px)
q=1-px(i);
hx(i)=-px(i)*log2(px(i))-q*log2(q);
end
plot(px,hx);
grid on;
xlabel('Probability p(x)'); ylabel('Entropy H(x)');
Output:
Name of Student: Roll No:
Title of Experiment: Simulation of Linear Block Codes
%Identity Matrix
ik=eye(k);
% Encoding
disp('Linear Block Codes : Encoding');
disp('Generator Matrix:');
%Matrix Multiplication
c1=mtimes(d,g);
% Decoding
disp('Linear Block Codes : Decoding');
r=input('Enter Received bit:');
disp('Transpose of parity matrix:');
pt=p.';
disp(pt);
ilp=eye(lp);
disp('Parity check matrix:');
h=cat(2,pt,ilp);
disp(h);
disp('Transpose parity check matrix:');
ht=h.';
disp(ht);
s1=mtimes(r,ht);
disp('Syndrome matrix:');
s=mod(s1,2);
disp(s);
if (s == 0)
disp('The received code is correct.');
else
for i=1:n
m=xor(s,ht(i,:));
if (m==0)
row =i;
break ;
end
end
r(1,row) = ~r(1,row);
disp('Correct codeword is:');
disp('c=');
disp(r);
end;
OUTPUT:
clear all;
clc;
n=input('Enter no. of inputs:');
p=input('Enter probabilities of messages:');
symbols=[1:n];
[dict,avglen]=huffmandict(symbols,p);
disp('Huffman Dicitionary :');
disp(dict);
disp('Huffman bit length :');
disp(avglen);
temp=dict;
for i=1:length(temp)
temp{i,2}=num2str(temp{i,2});
end
disp(temp);