Tuesday, October 28, 2014

Analog Communications lab MATLAB Programs

%% Amplitude Modulation And Demodulation
% Name:
%
% R.No:
%% closing and clearing commands
close all
clear all
clc
%% Initialization
fs=8000; % sampling frequency
fm=20; % message signal frequency
fc=500; % carrier signal frequency
t=[0:.1*fs]/fs; % time index
%% Message signal Generation
Am=1; % message signal amplitude
m=Am*cos(2*pi*fm*t); % message signal
subplot(4,3,1:3);
plot(t,m);
xlabel('time-->'), ylabel('amplitude')
title('Modulating or Message signal(fm=20Hz)');
%% Carrier signal Generation
Ac=1; % carrier signal amplitude
c=Ac*cos(2*pi*fc*t); % carrier signal
subplot(4,3,4:6);
plot(t,c);
xlabel('time-->'), ylabel('amplitude')
title('Carrier signal(fc=500Hz)');
%% Under Modulation
ka=0.5; % modulation sensitivity
u=ka*Am; % modulation Index
s1=Ac*(1+u*cos(2*pi*fm*t)).*cos(2*pi*fc*t);%AM
subplot(4,3,7);
plot(t,s1);
xlabel('time-->'), ylabel('amplitude')
title('Under Modulated signal(ka.Am=0.5)');
%% Critically Modulated signal
Am=2; % message signal amplitude
ka=0.5; % modulation sensitivity
u=ka*Am; % modulation index
s2=Ac*(1+u*cos(2*pi*fm*t)).*cos(2*pi*fc*t);
subplot(4,3,8);
plot(t,s2);
xlabel('time-->'), ylabel('amplitude')
title('Critically Modulated signal(ka.Am=1)');
%% Over Modulated signal
Am=5; % message signal amplitude
ka=0.5; % modulation sensitivity
u=ka*Am; % modulation index
s3=Ac*(1+u*cos(2*pi*fm*t)).*cos(2*pi*fc*t);
subplot(4,3,9);
plot(t,s3);
xlabel('time-->'), ylabel('amplitude')
title('Over Modulated signal(ka.Am=2.5)');
%% Demodulation
% for under modulated signal
r1= s1.*c; %detection
[b a] = butter(1,0.01);% LPF design
mr1= filter(b,a,r1);% demodulation
subplot(4,3,10);
plot(t,mr1);
xlabel('time-->'), ylabel('amplitude')
title('Demodulated signal')

% for critically modulated signal
r2= s2.*c;%detection
[b a] = butter(1,0.01);% LPF design
mr2= filter(b,a,r2);% demodulation
subplot(4,3,11);
plot(t,mr2);
xlabel('time-->'), ylabel('amplitude')
title('Demodulated signal')

% for overmodulated signal
r3= s3.*c;%detection
[b a] = butter(1,0.01);% LPF design
mr3= filter(b,a,r3);% demodulation
subplot(4,3,12);
plot(t,mr3);
xlabel('time-->'), ylabel('amplitude')
title('Demodulated signal')


%% Pre emphasis and De emphasis
% Name:
%
% R.No:
%% closing and clearing commands
close all
clear all
clc
%% Initialization
f1=10;

for f=1:50
x(f)=(1/sqrt(1+(f1/f)^2));
f2(f)=f;
end
subplot(2,1,1);
plot(f2,x);
title('pre-emphasis filter response');

for f=1:50
y(f)=(1/sqrt(1+(f/f1)^2));
f3(f)=f;
end
subplot(2,1,2);
plot(f3,y);
title('de-emphasis filter response');


%% Time Division Multiplexing
% Name:
%
% R.No:
%% closing and clearing commands
close all;
clear all;
clc
%% Signal generation
x=0:.5:4*pi;     % signal taken upto 4pi
sig1=8*sin(x); % generate 1st sinusoidal signal
l=length(sig1);
sig2=8*triang(l);% Generate 2nd traingularSignal

%% Display of Both Signal
subplot(2,2,1);                         
plot(sig1);
title('Sinusoidal Signal');
ylabel('Amplitude--->');
xlabel('Time--->');

subplot(2,2,2);
plot(sig2);
title('Triangular Signal');
ylabel('Amplitude--->');
xlabel('Time--->');

subplot(2,2,3);
stem(sig1);
title('Sampled Sinusoidal Signal');
ylabel('Amplitude--->');
xlabel('Time--->');

subplot(2,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
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
figure
subplot(2,1,1)
xlabel('Time--->');
%Display of Both Sampled Signal
plot(sig3);
title('Recovered Sinusoidal Signal');
ylabel('Amplitude--->');
xlabel('Time--->');
subplot(2,1,2)
plot(sig4);
title('Recovered Triangular Signal');
ylabel('Amplitude--->');
xlabel('Time--->');


%% DSBSC Modulation And Demodulation% Name:
%
% R.No:
%% closing and clearing commands
close all
clear all
clc
%% Initialization
Vm= 1; % message signal amplitude
Vc= 1; % carrier signal amplitude
fm = 20;%message signal frequency
fc= 500; %carrier signal frequency
fs=2*fc; % sampling frequency
t =0:inv(fs):2*pi; %time index
m_t = Vm*sin(2*pi*(fm/fs)*t);% message signal
subplot(4,1,1);
plot(t,m_t);
xlabel('time-->'), ylabel('amplitude')
title('message signal')
c_t = Vc*sin(2*pi*(fc/fs)*t); % carrier signal
subplot(4,1,2);
plot(t,c_t);
xlabel('time-->'), ylabel('amplitude')
title('carrier signal')
s_t = m_t.*c_t; %DSB-SC signal
subplot(4,1,3);
hold on;
plot(t,s_t);
plot(t,m_t,'r:');
plot(t,-m_t,'r:');
hold off;
xlabel('time-->'), ylabel('amplitude')
title('DSB-SC modulated signal')
%% Demodulated signal
r = s_t.*c_t;% detection
[b a] = butter(1,0.01);%LPF design
mr= filter(b,a,r);%demodulation
subplot(4,1,4);
plot(t,mr);
xlabel('time-->'), ylabel('amplitude')
title('Demodulated signal')

%% SSB-SC Modulation And Demodulation
% Name:
%
% R.No:
%% closing and clearing commands
close all
clear all
clc
%% Initialization
fs=8000; % sampling frequency
fm=20;%message signal frequency
fc=50;%carrier signal frequency
Am=1;% message signal amplitude
Ac=1;% carrier signal amplitude
t=[0:.1*fs]/fs;%time index
%% message signal generation
m1=Am*cos(2*pi*fm*t);% message signal 1
subplot(4,2,1);
plot(t,m1);
xlabel('time-->'), ylabel('amplitude')
title('Message Signal 1');

m2=Am*sin(2*pi*fm*t);% Message signal 2
subplot(4,2,2)
plot(t,m2);
xlabel('time-->'), ylabel('amplitude')
title('Message Signal 2');
%% carrier signal generation
c1=Ac*cos(2*pi*fc*t);% carrier signal 1
subplot(4,2,3)
plot(t,c1)
xlabel('time-->'), ylabel('amplitude')
title('Carrier Signal 1');

c2=Ac*sin(2*pi*fc*t);% carrier signal 2
subplot(4,2,4)
plot(t,c2)
xlabel('time-->'), ylabel('amplitude')
title('Carrier Signal 2');
%% SSB-SC generation
Susb=0.5*m1.*c1-0.5*m2.*c2;
subplot(4,2,5);
plot(t,Susb);
xlabel('time-->'), ylabel('amplitude')
title('SSB-SC Signal with USB');

Slsb=0.5*m1.*c1+0.5*m2.*c2;
subplot(426)
plot(t,Slsb);
xlabel('time-->'), ylabel('amplitude')
title('SSB-SC Signal with LSB');
%% Demodulation
r1 = Susb.*c1;% Detection
[b a] = butter(1,0.0001,'low');%LPF design
mr1= filter(b,a,r1);% demodulation
subplot(4,2,7);plot(t,mr1);
xlabel('time-->'), ylabel('amplitude')
title('Demodulated signal 1')

r2 = Slsb.*c2;% Detection
[b a] = butter(1,0.0001,'low');%LPF design
mr2= filter(b,a,r2);% demodulation
subplot(4,2,8);plot(t,mr2);
xlabel('time-->'), ylabel('amplitude')
title('Demodulated signal 2')


%% Frequency Modulation And Demodulation
% Name:
%
% R.No:
%% closing and clearing commands
close all
clear all
clc
%% Initialization
%fm=35HZ,fc=500HZ,Am=1V,Ac=1V,B=10
fs=10000; % sampling frequency
Ac=1;% carrier signal amplitude
Am=1;% message signal amplitude
fm=35;%message signal frequency
fc=500;%carrier signal frequency
B=10;
t=(0:.1*fs)/fs;%time index
%% MESSAGE SIGNAL GENERATION
m_t=Am*cos(2*pi*fm*t);
subplot(4,1,1);
plot(t,m_t);
title('Modulating or Message signal(fm=35Hz)');
%% carrier signal generation
c_t=Ac*cos(2*pi*fc*t);
subplot(4,1,2);
plot(t,c_t);
title('Carrier signal(fm=500Hz)');
%% Frequency modulated signal generation
s_t=Ac*cos(2*pi*fc*t+B*sin(2*pi*fm*t));
subplot(4,1,3);
plot(t,s_t);
title('Frequency Modulated signal');
%% Demodulated signal generation
d=demod(s_t,fc,fs,'fm');
subplot(4,1,4);
plot(t,d);
title('demodulated signal');

%% Pulse Amplitude Moulation And Demodulation
% Name:
%
% R.No:
%% ALGORITHM:
% STEP1:    Start
% STEP2:    define modulating and carrier frequency.
% STEP3:    define time axis
% STEP4:    define modulating signal and carrier signal
% STEP5:    modulate the signals using FFT Algorithm to get PAM signal
% STEP6:    demodulate the signal using IFFT Algorithm to get back the original signal.
% STEP7: Stop
%% PROGRAM:
%% clearing and closing commands
close all
close all
clc
%% Initialization
t=0:1/6000:((10/1000)-(1/6000));% time index
xa=sin(2*pi*100*abs(t));
subplot(3,1,1)
plot(xa);
grid

Ts=32;
x=sin(2*pi*600*(Ts*t));

X=fft(xa,abs(x));
subplot(3,1,2);
stem(t, abs(X));
grid

Y=ifft(xa,X);
subplot(3,1,3)
plot(t,abs(Y))


%% PPM Generation And Demodulation
% Name:
%
% R.No:
%    Choose the sampling frequency fs and modulating frequency f1 such that Nyquist criteria are satisfied.
%    Generate the message signal using f1 andfs .
%    Modulate the message signal using the carrier frequency.
%    FFT is applied to the modulated signal to get frequency spectrum.
%    Demodulate the modulated signal using the same carrier frequency.
%    Plot the graphs for the original message signal, modulated, frequency spectrum and demodulated signal.
%% clearing and closing commands
close all;   
clear all
clc
%% Initialization
fc=100; % carrier signal frequency
fs=1000; % sampling frequency
        f1=80;%f2=300
t=0:1/fs:((2/f1)-(1/fs));
x1=0.4*cos(2*pi*f1*t)+0.5;
        %x2=0.2*(cos(2*pi*f1*t)+cos(2*pi*f2*t))+0.5 ;
subplot(4,2,1)
plot(x1)
title('original msg signal')
       y1=modulate(x1,fc,fs,'ppm')
subplot(4,2,2)
plot(y1)
axis([0 50 -0.2 1.2])
title('ppm one of f1,fc=1000,f1=80 ')
       fx1=abs(fft(y1,1024))
       fx1=[fx1(512:1024) fx1(1:513)]
       f=[(511*fs/1024):(fs/1024):(512*fs/1024)]
subplot(4,2,3)
plot(fx1)
title('freq des ppm signal tone,fc=1000')
       x1_recov = demod(y1,fc,fs,'ppm')
subplot(4,2,4)
plot(x1_recov)
title('time domain recovered signal')


%% PWM MODULATION AND DEMODULATION
% Name:
%
% R.no:
%% clearing and closing commands
clear all
close all
clc
%% Initialization
fc=1000; % carrier signal frequency
fs=10000; % sampling frequency
f1=200;
t=0:1/fs:((2/f1)-(1/fs));

x1=0.4*cos(2*pi*f1*t)+0.5;
subplot(421);
plot(x1);
title('original signal tone mesage,f1=500,fs=10000')
%% modulation
y1=modulate(x1,fc,fs,'pwm');
subplot(422);
plot(y1);
axis([0 500 -0.2 1.2]);
title('PWM')
%% demodulation
x1_recov=demod(y1,fc,fs,'pwm');
subplot(423);
plot(x1_recov);
title('time domain recoverd signal tone,f1=200')