1.
Sampling Theorem
clc ; clear all; close all;
t=-10:.001:10;
T=4;
fm=1/T;
x=cos(2*pi*fm*t);
subplot(2,2,1);
plot(t,x);
xlabel('time');ylabel('x(t)');
title('continous time signal');
grid;
n1=-4:1:4;
fs1=1.6*fm;
fs2=2*fm;
fs3=8*fm;
x1=cos(2*pi*fm/fs1*n1);
subplot(2,2,2);
stem(n1,x1,'r');
xlabel('time');ylabel('x(n)');
title('discrete time signal with fs<2fm');
hold on
subplot(2,2,2);
plot(n1,x1)
grid;
n2=-5:1:5;
x2=cos(2*pi*fm/fs2*n2);
subplot(2,2,3);
stem(n2,x2,'r');
xlabel('time');ylabel('x(n)')
title('discrete time signal with fs=2fm')
hold on
subplot(2,2,3);
plot(n2,x2)
grid;
n3=-20:1:20;
x3=cos(2*pi*fm/fs3*n3);
subplot(2,2,4);
stem(n3,x3,'r');
xlabel('time');ylabel('x(n)')
title('discrete time signal with fs>2fm')
hold on
subplot(2,2,4);
plot(n3,x3)
grid;
2.PULSE CODE MODULATION
clc;
clear all;
close all;
n=input('enter n value for n bit pcm system:');
n1=input('enter number of samples in a period:');
L=2^n;
x=0:2*pi/n1:4*pi;
s=8*sin(x);
subplot(3,2,1)
plot(s);
title('analog signal');
ylabel('amplitude');
xlabel('time');
subplot(3,2,2)
stem(s);
grid on;
title('sampled signal');
ylabel('amplitude');
xlabel('time');
%quantization process
vmax=8;
vmin=-vmax;
del=(vmax-vmin)/L;
part=vmin:del:vmax;
code=vmin-(del/2):del:vmax+(del/2);
[ind,q]=quantiz(s,part,code);
L1=length(ind);
L2=length(q);
for i=1:L1
if(ind(i)~=0)
ind(i)=ind(i)-1;
q(i)=vmin+(del/2);
end
end
subplot(3,2,3)
stem(q);
grid on;
title('quantized signal');
ylabel('amplitude');
xlabel('time');
%encoding process figure
code=de2bi(ind,'left-msb');
k=1;
for i=1:L1
for j=1:n
coded(k)=code(i,j);
j=j+1;
k=k+1;
end
i=i+1;
end
subplot(3,2,4);
grid on;
stairs(coded);
axis([0 100 -2 3]);
title('encoded signal');
ylabel('amplitude');
xlabel('time');
%demodulation of pcm signal
qunt=reshape(coded,n,length(coded)/n);
index=bi2de(qunt','left-msb');
q=del*index+vmin+(del/2);
subplot(3,2,5);
grid on;
plot(q);
title('demodulated signal');
ylabel('amplitude');
xlabel('time');
Enter n value for n bit pcm system: 8
Enter number of samples in a period: 20
DIFFERENTIAL PULSE CODE MODULATION
clc; clear all; close all;
predictor = [0 1]; %y(k)=x(k-1);
partition = [-1:0.1:0.9];
codebook = [-1:0.1:1];
t = [0:pi/50:2*pi];
x = sin(4*t); % Original signal
% Quantize x using DPCM.
encodedx = dpcmenco(x,codebook,partition,predictor);
disp(encodedx)
% Try to recover x from the modulated signal.
decodedx = dpcmdeco(encodedx,codebook,predictor);
plot(t,x,t,decodedx,'--');
legend ('Original signal','Decoded signal','Location','NorthOutside');
FREQUENCY SHIFT KEYING
clc;
clear all;
close all;
fc1=input('enter the freq of 1st sine wave carrier:');
fc2=input('enter the freq of 2nd sine wave carrier:');
fp=input('enter the freq of period binary pulse(message):');
amp=input('enter the amplitude(for both carrier & binary pulse message):');
amp=amp/2;
t=0:0.001:1;
c1=amp.*sin(2*pi*fc1*t);
c2=amp.*sin(2*pi*fc2*t);
subplot(4,1,1);
plot(t,c1)
xlabel('time')
ylabel('amplitude')
title('carrier 1 wave')
subplot(4,1,2)
plot(t,c2)
xlabel('time')
ylabel('amplitude')
title('carrier 2 wave')
m=amp.*square(2*pi*fp*t)+amp;
subplot(4,1,3)
plot(t,m)
xlabel('time');
ylabel('amplitude');
title('binary message pulses')
for i=0:1000
if m(i+1)==0
m(i+1)=c2(i+1);
else
m(i+1)=c1(i+1);
end
end
subplot(4,1,4)
plot(t,m)
xlabel('time')
ylabel('amplitude')
title('modulated wave')
Enter the freq of 1st sine wave carrier: 50
Enter the freq of 2nd sine wave carrier: 30
Enter the freq of period binary pulse (message):5
Enter the amplitude (for both carrier & binary pulse message):2
PHASE SHIFT KEYING
clear;
clc;
b = input('Enter the Bit stream \n '); %b = [0 1 0 1 1 1 0];
n = length(b);
t = 0:.01:n;
x = 1:1:(n+1)*100;
for i = 1:n
if (b(i) == 0)
b_p(i) = -1;
else
b_p(i) = 1;
end
for j = i:.1:i+1
bw(x(i*100:(i+1)*100)) = b_p(i);
end
end
bw = bw(100:end);
sint = sin(2*pi*t);
st = bw.*sint;
subplot(3,1,1)
plot(t,bw)
grid on ;
axis([0 n -2 +2])
subplot(3,1,2)
plot(t,sint)
grid on ;
axis([0 n -2 +2])
subplot(3,1,3)
plot(t,st)
grid on ;
axis([0 n -2 +2])
Enter the Bit stream
[101101110011000100]
TIME DIVISION MULTIPLEXING
clc; close all; clear all; % Signal generation
x=0:0.4:6*pi; % signal taken up to 4pi
sig1=10*sin(x); % generate 1st sinusoidal signal
L=length(sig1);
sig2=8*triang(L); % Generate 2nd triangular Signal
figure
subplot(4,2,1); % Display of Both Signal
plot(sig1);
title('Sinusoidal Signal');
ylabel('Amplitude--->');
xlabel('Time--->');
subplot(4,2,2);
plot(sig2);
title('Triangular Signal');
ylabel('Amplitude--->');
xlabel('Time--->'); % Display of Both Sampled Signal
subplot(4,2,3);
stem(sig1);
title('Sampled Sinusoidal Signal');
ylabel('Amplitude--->');
xlabel('Time--->');
subplot(4,2,4);
stem(sig2);
title('Sampled Triangular Signal');
ylabel('Amplitude--->');
xlabel('Time--->');
L1=length(sig1);
L2=length(sig2);
for i=1:L1
sig(1,i)=sig1(i); % Making Both row vector to a matrix
sig(2,i)=sig2(i);
end % TDM of both quantize signal
tdmsig=reshape (sig,1,2*L1);
% Display of TDM Signal Figure
subplot(4,2,5);
stem(tdmsig);
title('TDM Signal');
ylabel('Amplitude--->');
xlabel('Time--->');
% Demultiplexing of TDM Signal
demux=reshape(tdmsig,2,L1);
for i=1:L1
sig3(i)=demux(1,i); % Converting The matrix into row vectors
sig4(i)=demux(2,i);
end % display of demultiplexed signal
subplot(4,2,6)
plot(sig3);
title('Recovered Sinusoidal Signal');
ylabel('Amplitude--->');
xlabel('Time--->');
subplot(4,2,7);
plot(sig4);
title('Recovered Triangular Signal');
ylabel('Amplitude--->');
xlabel('Time--->')
DELTA MODULATION
%% Delta Modulation (DM)
%delta modulation = 1-bit differential pulse code modulation (DPCM)
predictor = [0 1]; % y(k)=x(k-1)
%partition = [-1:.1:.9];codebook = [-1:.1:1];
step=0.2; %SFs>=2pifA
partition = [0];
codebook =[-1*step step]; %DM quantizer
t =[0:pi/12:4*pi];
x = 1.1*sin(3*pi*0.1*t); % Original signal, a sine wave
%t = [0:0.1:2*pi];x = 4*sin(t);
%x=exp(-1/3*t);
%x = sawtooth(3*t); % Original signal
% Quantize x(t) using DPCM.
encodedx = dpcmenco(x,codebook,partition,predictor);
% Try to recover x from the modulated signal.
decodedx = dpcmdeco(encodedx,codebook,predictor);
distor = sum((x-decodedx).^2)/length(x); % Mean square error
% plots
figure
subplot(2,2,1);
plot(t,x);
grid on;
xlabel('time');
title('original signal');
subplot(2,2,2);
stairs(t,10*codebook(encodedx+1),'--');
grid on;
xlabel('time');
title('DM output');
subplot(2,2,3);
plot(t,x);
hold;stairs(t,decodedx);grid on;xlabel('time');title('received signal');