0% found this document useful (0 votes)
574 views

Principles of Communication Engineering Practical

The document contains MATLAB code to generate and plot various signals including impulse, unit step, ramp, and square wave signals. It also contains code to calculate and plot the power spectral density of a signal containing multiple frequencies. The code generates time vectors, defines signals as functions of time, and plots the signals individually or overlaid with sine waves for comparison. It also calculates the power of a sample multi-frequency signal and plots its power spectral density.

Uploaded by

Techniwork
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
574 views

Principles of Communication Engineering Practical

The document contains MATLAB code to generate and plot various signals including impulse, unit step, ramp, and square wave signals. It also contains code to calculate and plot the power spectral density of a signal containing multiple frequencies. The code generates time vectors, defines signals as functions of time, and plots the signals individually or overlaid with sine waves for comparison. It also calculates the power of a sample multi-frequency signal and plots its power spectral density.

Uploaded by

Techniwork
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Question no 1.

AM
clc;
clear all;
close all;
t=0:0.001:1;
set(0,‘defaultlinelinewidth’,2);
A=5;%Amplitude of signal
fm=input(‘Message frequency=’);%Accepting input value
fc=input(‘Carrier frequency=’);%Accepting input value (f2>f1)
mi=input(‘Modulation Index=’);%Modulation Index
Sm=A*sin(2*pi*fm*t);%Message Signal
subplot(3,1,1);%Plotting frame divided in to 3 rows and this fig appear at 1st
plot(t,Sm);
xlabel(‘Time’);
ylabel(‘Amplitude’);
title(‘Message Signal’);
grid on;
Sc=A*sin(2*pi*fc*t);%Carrier Signal
subplot(3,1,2);
plot(t,Sc);
xlabel(‘Time’);
ylabel(‘Amplitude’);
title(‘Carrier Signal’);
grid on;
Sfm=(A+mi*Sm).*sin(2*pi*fc*t);%AM Signal, Amplitude of Carrier changes to (A+Message)
subplot(3,1,3);
plot(t,Sfm);
xlabel(‘Time’);
ylabel(‘Amplitude’);
title(‘AM Signal’);
grid on;[/cc]
FM
clc;
clear all;
close all;
fm=input(‘Message Frequency=’);
fc=input(‘Carrier Frequency=’);
mi=input(‘Modulation Index=’);
t=0:0.0001:0.1;
m=sin(2*pi*fm*t);
subplot(3,1,1);
plot(t,m);
xlabel(‘Time’);
ylabel(‘Amplitude’);
title(‘Message Signal’);
grid on;
c=sin(2*pi*fc*t);
subplot(3,1,2);
plot(t,c);
xlabel(‘Time’);
ylabel(‘Amplitude’);
title(‘Carrier Signal’);
grid on;
y=sin(2*pi*fc*t+(mi.*sin(2*pi*fm*t)));%Frequency changing w.r.t Message
subplot(3,1,3);
plot(t,y);
xlabel(‘Time’);
ylabel(‘Amplitude’);
title(‘FM Signal’);
grid on;

Message Frequency=25
Carrier Frequency=400
Modulation Index=10
PM

clc;
clear all;
t = 0:0.001:1;
vm = input('Enter the amplitude of message signal = ');
vc = input('Enter the amplitude of carrier signal = ');
fm = input('Enter the message frequency = ');
fc = input('Enter the carrier frequency = ');
m = input('Enter modulation index = ');
sm = vm*sin(2*pi*fm*t);
subplot(3,1,1);
plot(t,sm);
xlabel('Time ---->');
ylabel('Amplitude ---->');
title('Message Signal');
grid on;
sc = vc*sin(2*pi*fc*t);
subplot(3,1,2);
plot(t,sc);
xlabel('Time ---->');
ylabel('Amplitude ---->');
title('Carrier Signal');
grid on;
y = vc*sin(2*pi*fc*t+m.*sin(2*pi*fm*t));
subplot(3,1,3);
plot(t,y);
xlabel('Time ---->');
ylabel('Amplitude ---->');
title('PM Wave');
grid on;

Output:
Enter the amplitude of message signal = 5
Enter the amplitude of carrier signal = 5
Enter the message frequency = 10
Enter the carrier frequency = 100
Enter modulation index = 4
Question no 2.

//Set the sample rate and carrier frequency.


fc = 10e3;
fs = 80e3;
//Generate a sinusoidal signal having a 0.01 s duration.
t = [0:1/fs:0.01]';
s = sin(2*pi*300*t)+2*sin(2*pi*600*t);
//Create a lowpass filter.
[num,den] = butter(10,fc*2/fs);
//Amplitude modulate the signal, s.
y = ammod(s,fc,fs);
//Demodulate the received signal.
s1 = amdemod(y,fc,fs,0,0,num,den);
//Plot the original and demodulated signals.
plot(t,s,'c',t,s1,'b--')
legend('Original Signal','Demodulated Signal')
xlabel('Time (s)')
ylabel('Amplitude')

The demodulated signal is nearly identical to the original signal.


Question no 3.

ASK (binary)
%b is the input binary bit stream
% f is the frequency of the carrier
b= input('Enter the Bit stream\n');
fc=input ('Carrier Frequency=');
n = length (b); %determine the length of bit stream
t= 0: .01: n; %time axis
x=1: 1: (n+1)*100;
for i = 1:n
for j= i:.1:i+1
bw(x(i*100:(i+1)*100)) = b(i); %loop
end
end
bw = bw(100:end); % binary bit stream
carrier = cos (2*pi*fc*t); %carrier signal
modulated = bw.*carrier; %modulated signal
subplot (3, 1, 1)
plot(t,bw)
grid on;
ylabel('amplitude')
xlabel('time')
title('message signal')
subplot(3,1,2)
plot(t, carrier)
grid on;
ylabel('amplitude')
xlabel('time')
title('carrier signal')
subplot(3,1,3)
plot (t, modulated)
grid on;
ylabel('amplitude')
xlabel('time')
title('modulated signal')

Enter the Bit Stream


[1 -1 1 -1 1]
Carrier Frequency = 0.8
ASK
clear all;
clc;
close all;
F1=input(‘Enter the frequency of carrier=’);
F2=input(‘Enter the frequency of pulse=’);
A=3;%Amplitude
t=0:0.001:1;
x=A.*sin(2*pi*F1*t);%Carrier Sine wave
u=A/2.*square(2*pi*F2*t)+(A/2);%Square wave message
v=x.*u;
subplot(3,1,1);
plot(t,x);
xlabel(‘Time’);
ylabel(‘Amplitude’);
title(‘Carrier’);
grid on;
subplot(3,1,2);
plot(t,u);
xlabel(‘Time’);
ylabel(‘Amplitude’);
title(‘Square Pulses’);
grid on;subplot(3,1,3);
plot(t,v);
xlabel(‘Time’);
ylabel(‘Amplitude’);
title(‘ASK Signal’);
grid on;
FSK (binary)
clear;
clc;
%b is the input binary bit stream
% f is the frequency of the carrier
b= input('Enter the Bit stream\n');
fc=input ('Carrier Frequency=');
n = length (b); %determine the length of bit stream
t= 0: .01: n; %time axis
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); %loop
end
end
bw = bw(100:end); % binary bit stream
wo=3*(2*pi*fc*t);
W=2*(2*pi*fc*t);
sinHt=sin(wo+W)
sinLt=sin(wo-W)
modulated = sin(wo+(bw).*W) %modulated signal
subplot (4, 1, 1)

plot(t,bw)
grid on;
ylabel('amplitude')
xlabel('time')
title('input signal')
subplot(4,1,2)

plot(t, sinHt)
grid on;
ylabel('amplitude')
xlabel('time')
title('High Frequency carrier signal')
subplot(4,1,3)

plot (t, sinLt)


grid on;
ylabel('amplitude')
xlabel('time')
title('Low Frequency carrier signal')
subplot(4,1,4)

plot (t, modulated)


grid on;
ylabel('amplitude')
xlabel('time')
title('Modulated signal')

Enter the Bit Stream


[1 0 1 0 1]
Carrier Frequency = 0.8
FSK
PSK (binary)
PSK
clear all;
clc;
close all;
set(0,’defaultlinelinewidth’,2);
A=5;
t=0:.001:1;
f1=input(‘Carrier Sine wave frequency =’);
f2=input(‘Message frequency =’);
x=A.*sin(2*pi*f1*t);%Carrier Sine
subplot(3,1,1);
plot(t,x);
xlabel(‘time’);
ylabel(‘Amplitude’);
title(‘Carrier’);
grid on;
u=square(2*pi*f2*t);%Message signal
subplot(3,1,2);
plot(t,u);
xlabel(‘time’);
ylabel(‘Amplitude’);
title(‘Message Signal’);
grid on;
v=x.*u;%Sine wave multiplied with square wave
subplot(3,1,3);
plot(t,v);
axis([0 1 -6 6]);
xlabel(‘t’);
ylabel(‘y’);
title(‘PSK’);
grid on;

Carrier Sine wave frequency =10


Message frequency =2
Question no 4.
Question no 5.

clear all; clc; close all;


L=100000; %Sample length for the random signal
mu=0;
sigma=2;
X=sigma*randn(L,1)+mu;

figure();
subplot(2,1,1)
plot(X);
title(['White noise : \mu_x=',num2str(mu),' \sigma^2=',num2str(sigma^2)])
xlabel('Samples')
ylabel('Sample Values')
grid on;
Question no 6.
%program to generate sine wave

f= input('enter the frequency in hertz of the sine wave');


t=0:.0001:5;
y=sin(2*pi*f*t);
plot(t,y);
ylabel ('Amplitude');
xlabel ('Time Index');
TITLE ('Sine wave');

%program to generate cosine wave

f= input('enter the frequency in hertz of the sine wave');


t=0:.0001:5;
y=cos(2*pi*f*t);
plot(t,y);
ylabel ('Amplitude');
xlabel ('Time Index');
TITLE ('cosine wave');
Create a sawtooth wave.
t = (0:0.1:10)';
x = sawtooth(t);
Apply white Gaussian noise and plot the results.
y = awgn(x,10,'measured');
plot(t,[x y])
legend('Original Signal','Signal with AWGN')
Question no 7.

I. Impulse function

Impulse function given by


∂ (t) = { 1 for t=0 ; 0 otherwise }

n1=input('Lower limit')
n2=input('Upper limit')
x=n1:n2;
y=[x==0];
stem(x,y,);

Output :

Plot of Impulse Funtion


n1=-5 ; n2=5
II. Unit Step Signal

u(t)={ 1 for t >=0 ; 0 for t< 0 }

Discrete

n1=input('Enter the lower limit');


n2=input('Enter the upper limit');
n=n1:n2;
x=[n>=0];
stem(n,x);
title('Unit Step Signal - Discrete');

Plot of Unit Step Signal – Discrete


n1= -4 ; n2 = 4
Continuous

n=input('Enter the upper limit');


t=0:n;
x=[t>=0];
plot(t,x);
title('Continuous');

Unit step signal – Continuous


n=6
III. Unit Ramp Signal

r(t)={ t for t >= 0 ;0 for t <0 }


Recall that ramp signal r(t)=t*u(t) where u(t) is unit step signal

n1=input('Enter lower limit');


n2=input('Enter upper limit');
n=n1:n2;
x=n.*[n>=0];
subplot(2,1,1);
plot(n,x,'r');
title('Continuous');
subplot(2,1,2);
stem(n,x,'b');
title('Discrete');

Output:

Unit Ramp Function


n1=-10 ; n2=10
Shortcut method for Generating Ramp Signal

n=input('Enter the upper limit')


t=0:n;
subplot(2,1,1);
plot(t,t);
title('Continuous');
subplot(2,1,2);
stem(t,t);
title('Discrete');

Output :

Unit Ramp Function Shortcut Method with n=10


Square signal
Create a vector of 100 equally spaced numbers from to . Generate a square wave with a period
of .
/*Plot the square wave and overlay a sine. Normalize the x-axis by . The generated square wave
has a value of at even multiples of and a value of at odd multiples of . It is never . */

t = linspace(0,3*pi)';
x = square(t);

plot(t/pi,x,'.-',t/pi,sin(t))
xlabel('t / \pi')
grid on

/*Repeat the calculation, but now evaluate square(2*t) at 121 equally spaced numbers
between and . Change the amplitude to . Plot the wave and overlay a sine with the same
parameters. This new wave is negative at and positive at the endpoints.*/

t = linspace(-pi,2*pi,121);
x = 1.15*square(2*t);

plot(t/pi,x,'.-',t/pi,1.15*sin(2*t))
xlabel('t / \pi')
grid on
Unit step signal program

t = (-1:0.01:1)';

impulse = t==0;
unitstep = t>=0;
ramp = t.*unitstep;
quad = t.^2.*unitstep;

plot(t,[impulse unitstep ramp quad])


sqwave = 0.81*square(4*pi*t);
plot(t,sqwave)

IMPULSE SIGNAL
t = (-1:0.01:1)';

impulse = t==0;
unitstep = t>=0;
ramp = t.*unitstep;
quad = t.^2.*unitstep;
All of these sequences are column vectors that inherit their shapes from t. Plot the sequences.
plot(t,[impulse unitstep ramp quad])

Generate and plot a square wave with period 0.5 and amplitude 0.81.
sqwave = 0.81*square(4*pi*t);
plot(t,sqwave)
Calculate Power and to plot Power spectral density
T=10; %Total evaluation time
Ts=0.001; %Sampling time =&gt; 1000 samples per second
Fs=1/Ts; % Sampling period
t=[0:Ts:T]; %define simulation time

%sample function to calculate power


x=cos(2*pi*100*t)+cos(2*pi*200*t)+sin(2*pi*300*t);
power = (norm(x)^2)/length(x);

%Plot Power Spectral Density - For older Matlab Versions


psd1=spectrum(x,1024);
specplot(psd1,Fs);

%For Latest Matlab Versions use psd function instead of specplot


psd1=spectrum(x,1024);
hpsd = dspdata.psd(psd1,'Fs',Fs); % Create a PSD data object.
plot(hpsd); % Plot the PSD

power = 1.5002 (W)

For energy

x=(1/2)*(1+cos(2*pi*n));
n=-4:1:4;
syms pi
x=(1/2)*(1+cos(2*pi*n))

sum(x)
Question no 8.

PULSE AMPLITUDE MODULATION


clc;
clear all;
a = input('Enter the amplitude = ');
f = input('Enter the frequency = ');
n = input('Enter the N value = ');
t = 0:0.1:n;
x1 = stem(-t/3);
x2 = sin(2*pi*f*t);
y = x1.*x2;
subplot(3,1,1);
stem(x1);
title('Impulse Signal');
ylabel('Amplitude ---->');
xlabel('n ---->');
grid on;
subplot(3,1,2)
plot(t,x2);
title('Sine Wave');
xlabel('Time ----->');
ylabel('Amplitude ----->');
grid on;
subplot(3,1,3)
stem(t,y);
title('Pulse Modulated Wave');
xlabel('Time ----->');
ylabel('Amplitude ----->');
grid on;

Output:

Enter the amplitude = 5


Enter the frequency = 1
Enter the N value = 2

Waveform:
Pulse Width Modulation
[cc lang=”Matlab”]clc;
clear all;
close all;
F2=input(‘Message frequency=’);
F1=input(‘Carrier Sawtooth frequency=’);
A=5;
t=0:0.001:1;
c=A.*sawtooth(2*pi*F1*t);%Carrier sawtooth
subplot(3,1,1);
plot(t,c);
xlabel(‘time’);
ylabel(‘Amplitude’);
title(‘Carrier sawtooth wave’);
grid on;
m=0.75*A.*sin(2*pi*F2*t);%Message amplitude must be less than Sawtooth
subplot(3,1,2);
plot(t,m);
xlabel(‘Time’);
ylabel(‘Amplitude’);
title(‘Message Signal’);
grid on;
n=length(c);%Length of carrier sawtooth is stored to ‘n’
for i=1:n%Comparing Message and Sawtooth amplitudes
if (m(i)>=c(i))
pwm(i)=1;
else
pwm(i)=0;
end
end
subplot(3,1,3);
plot(t,pwm);
xlabel(‘Time’);
ylabel(‘Amplitude’);
title(‘plot of PWM’);
axis([0 1 0 2]);%X-Axis varies from 0 to 1 & Y-Axis from 0 to 2
grid on;[/cc]
Generated PWM Signal
Inputs and Observation:
Message frequency=1
Carrier Saw tooth frequency=10
Pulse position modulation
clc;
clear all;
close all;
fc=1000;
fs=10000;
fm=200;
t=0:1/fs:(2/fm-1/fs);
mt=0.4*sin(2*pi*fm*t)+0.5;
st=modulate(mt,fc,fs,'PPM');
dt=demod(st,fc,fs,'PPM');
figure
subplot(3,1,1);
plot(mt);
title('message signal');
xlabel('timeperiod');
ylabel('amplitude');
axis([0 50 0 1])
subplot(3,1,2);
plot(st);
title('modulated signal');
xlabel('timeperiod');
ylabel('amplitude');
axis([0 500 -0.2 1.2])
subplot(3,1,3);
plot(dt);
title('demodulated signal');
xlabel('timeperiod');
ylabel('amplitude');
axis([0 50 0 1])
Question no 9.
MATLAB Implementation (Simulation) Of Pulse Code Modulation (PCM)
clc
close all
clear all
t = 0:0.0001:20; %sampling at niquist rate
c=input('Enter Bit Depth Of PCM Coding:');
part = -1:0.1:1; %A quantization partition defines several contiguous, nonoverlapping ranges
%of values within the set of real numbers.
codebook = -1:0.1:1.1;% A codebook tells the quantizer which common value to assign to inputs that
%fall into each range of the partition.
msg = cos(t);
[~,quants] = quantiz(msg,part,codebook);%returns a vector that tells which interval each input is in
subplot(3,1,1);
plot(t,msg);
title('Message Signal');
subplot(3,1,2);
plot(t,quants);
title('Quantized Signal');
y = uencode(quants,c);
ybin=dec2bin(y,c); %converting it to final binary form to make it transmit ready
subplot(3,1,3);
plot(t,y);
title('PCM PLOT');

Enter Bit Depth Of PCM Coding:2

PCM (Pulse Code Modulation) in MATLAB 2 bit Coding


PCM Demodulation
clc;
close all;
clear all;
n=input('Enter n value for n-bit PCM system : ');
n1=input('Enter number of samples in a period : ');
L=2^n;

% % Signal Generation
% x=0:1/100:4*pi;
% y=8*sin(x); % Amplitude Of signal is 8v
% subplot(2,2,1);
% plot(x,y);grid on;
% Sampling Operation
x=0:2*pi/n1:4*pi; % n1 nuber of samples have tobe selected
s=8*sin(x);
subplot(3,1,1);
plot(s);
title('Analog Signal');
ylabel('Amplitude--->');
xlabel('Time--->');
subplot(3,1,2);
stem(s);grid on; title('Sampled Sinal'); ylabel('Amplitude--->'); xlabel('Time--->');

% Quantization Process
vmax=8;
vmin=-vmax;
del=(vmax-vmin)/L;
part=vmin:del:vmax; % level are between vmin and vmax with difference of del
code=vmin-(del/2):del:vmax+(del/2); % Contaion Quantized valuses
[ind,q]=quantiz(s,part,code); % Quantization process
% ind contain index number and q contain quantized values
l1=length(ind);
l2=length(q);

for i=1:l1
if(ind(i)~=0) % To make index as binary decimal so started from 0 to N
ind(i)=ind(i)-1;
end
i=i+1;
end
for i=1:l2
if(q(i)==vmin-(del/2)) % To make quantize value inbetween the levels
q(i)=vmin+(del/2);
end
end
subplot(3,1,3);
stem(q);grid on; % Display the Quantize values
title('Quantized Signal');
ylabel('Amplitude--->');
xlabel('Time--->');

% Encoding Process
figure
code=de2bi(ind,'left-msb'); % Cnvert the decimal to binary
k=1;
for i=1:l1
for j=1:n
coded(k)=code(i,j); % convert code matrix to a coded row vector
j=j+1;
k=k+1;
end
i=i+1;
end
subplot(2,1,1); grid on;
stairs(coded); % Display the encoded signal
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'); % Getback the index in decimal form
q=del*index+vmin+(del/2); % getback Quantized values
subplot(2,1,2); grid on;
plot(q); % Plot Demodulated signal
title('Demodulated Signal');
ylabel('Amplitude--->');
xlabel('Time--->');
Question no 10.
Eb/No (SNR) Vs BER Curve Plotting for BPSK in AWGN Channel:
clc
clear all
close all
bit_count = 100000; %no. of random bits to be generated for a single shot of BER calculation
SNR = 0: 1: 10; %Range of SNR over which to simulate
for k = 1: 1: length(SNR)
tote = 0; %total error bits
totb = 0; %total bits
while tote < 100 %until you get 100 errors
rbits = round(rand(1,bit_count)); %generate random bits
tx = -2*(rbits-0.5); % BPSK Modulation: Directly to Bipolar NRZ
N0 = 1/10^(SNR(k)/10); %noise level
rx = tx + sqrt(N0/2)*(randn(1,length(tx))+i*randn(1,length(tx)));
rx2 = rx < 0; % BPSK demodulator logic at the Receiver
diff = rbits - rx2; % Calculate Bit Errors
tote = tote + sum(abs(diff)); %total errors
totb = totb + length(rbits); %total bits generated
end
BER(k) = tote / totb; % Calculate Bit Error Rate
end
semilogy(SNR,BER,'*r');
hold on;
xlabel('Eb/No (dB)');
ylabel('BER');
title('Eb/No(SNR) Vs BER plot for BPSK Modualtion in AWGN Channel');
thber = 0.5*erfc(sqrt(10.^(SNR/10))); % Theoretical BER
semilogy(SNR,thber);
grid on;
legend('Simulated Curve', 'Theoretical Curve');

Result of above Code:

You might also like