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

Acom SW Lab Xpts

The document describes the generation and processing of various digital and analog signals. It begins by generating simple signals like unit impulse, unit step, ramp, and exponential signals. It then generates continuous-time signals like sine waves and square waves. The document demonstrates amplitude modulation (AM) and phase modulation (PM) of carrier signals. It also covers mean square error estimation when quantizing a signal and time delay estimation using correlation. Finally, it demonstrates binary amplitude-shift keying (BASK) and binary frequency-shift keying (BFSK) for digital modulation and demodulation.

Uploaded by

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

Acom SW Lab Xpts

The document describes the generation and processing of various digital and analog signals. It begins by generating simple signals like unit impulse, unit step, ramp, and exponential signals. It then generates continuous-time signals like sine waves and square waves. The document demonstrates amplitude modulation (AM) and phase modulation (PM) of carrier signals. It also covers mean square error estimation when quantizing a signal and time delay estimation using correlation. Finally, it demonstrates binary amplitude-shift keying (BASK) and binary frequency-shift keying (BFSK) for digital modulation and demodulation.

Uploaded by

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

1.

GENERATION OF SIMPLE SIGNALS


%unit impulse
clc; clear all;
close all;
t=-2:2;
y=[zeros(1,2),1,zeros(1,2)];
subplot(221);
stem(t,y);
ylabel('Amplitude');
xlabel('Time');
title('Unit Impulse');
%unit step
n=input('Enter the length of step sequence n=');
t=-n:n;
y=[zeros(1,n),ones(1,n+1)];
subplot(222);
stem(t,y);
ylabel('Amplitude');
xlabel('Time');
title('Unit Step');
%ramp sequence
n=input('Enter the length of ramp sequence n=');
t=0:n;
subplot(223);
stem(t,t);
ylabel('Amplitude');
xlabel('Time');
title('Unit Ramp');
%exponential
n=input('Enter the length of exponential sequence n=');
t=0:n;
a=input('Enter the value of a=');
y=exp(a*t);
subplot(224);
stem(t,y);
ylabel('Amplitude');
xlabel('Time');
title('Exponential');

Enter the length of step sequence n=5


Enter the length of ramp sequence n=4
Enter the length of exponential sequence n=5
Enter the value of a=0.5

>>
Unit Step
1

0.8

0.8

0.6

0.6

Amplitude

Amplitude

Unit Impulse
1

0.4

0.2

0.4

0.2

0
-2

-1.5

-1

-0.5

0
Time

0.5

1.5

0
-5

-4

-3

-2

-1

Unit Ramp
14

3.5

12

3.5

4.5

10

2.5

Amplitude

Amplitude

Exponential

2
1.5

8
6
4

0.5
0

0
Time

0.5

1.5

2
Time

2.5

3.5

0.5

1.5

2.5
Time

2.GENERATION OF CONTINUOUS TIME SIGNALS


%sine wave
clc;
clear all;
close all;
t=0:0.001:1;
f=2;
y=sin(2*pi*f*t);
subplot(211);
plot(t,y);
grid on;
axis([0 1 -2 2]);
ylabel('Amplitude');
xlabel('Time');
title('Sine Wave');
%square wave
f=3;
t=0:1000;
duty=0.35; %required d
y=square(2*pi*f*t/1000,(duty*100));
subplot(212);
plot(t,y);
grid on;
axis([0 1000 -2 2]);
ylabel('Amplitude');
xlabel('Time');
title('square wave');

Sine Wave

Amplitude

2
1
0
-1
-2

0.1

0.2

0.3

0.4

0.5
0.6
Time
square wave

100

200

300

400

0.7

0.8

0.9

700

800

900

1000

Amplitude

2
1
0
-1
-2

500
Time

600

3.GENERATION OF AM
%AM wave
clc;
clear all;
close all;
fc=input('Enter the frequency of the carrier signal fc=');
fm=input('Enter the frequency of the modulating signal fm=');
m=input('Modulation index m=');
t=0:0.001:1;
c=sin(2*pi*fc*t);
subplot(221);
plot(t,c);
ylabel('Amplitude');
xlabel('Time');
title('Carrier signal');

M=sin(2*pi*fm*t);
subplot(222);
plot(t,M);
ylabel('Amplitude');
xlabel('Time');
title('Modulating signal');
y=(1+m*M).*c;
subplot(223);
plot(t,y);
grid on;
ylabel('Amplitude');
xlabel('Time');
title('Modulated signal');
%AM with suppressed carrier
t=0:0.001:1;
c=sin(2*pi*fc*t);
M=sin(2*pi*fm*t);
y=M.*c;
subplot(224);
plot(t,y);
grid on;
axis([0 1 -2 2]);
ylabel('Amplitude');
xlabel('Time');
title('Suppressed carrier');

Enter the frequency of the carrier signal fc=50


Enter the frequency of the modulating signal fm=5
Modulation index m=0.75

>>
Modulating signal
1

0.5

0.5
Amplitude

Amplitude

Carrier signal
1

-0.5

-1

-0.5

0.1

0.2

0.3

0.4

0.5
Time

0.6

0.7

0.8

0.9

-1

0.1

0.2

0.3

1.5

1.5

0.5

0.5

0
-0.5

-1.5

-1.5

0.1

0.2

0.3

0.4

0.5
Time

0.6

0.7

0.8

0.9

0.7

0.8

0.9

-0.5
-1

0.6

-1

-2

0.5
Time

Suppressed carrier

Amplitude

Amplitude

Modulated signal

0.4

0.7

0.8

0.9

-2

0.1

0.2

0.3

0.4

0.5
Time

0.6

4.GENERATION OF PM
%PM generation
clc;
clear all;
close all;
fm=input('Message frequency=');
fc=input('Carrier frquency=');
m=input('Modulation index=');
t=0:0.0001:0.1;
c=sin(2*pi*fc*t);
M=sin(2*pi*fm*t);
subplot(311);
plot(t,c);
grid on;
ylabel('Amplitude');
xlabel('Time');
title('Carrier signal');
subplot(312);
plot(t,M);
grid on;
ylabel('Amplitude');
xlabel('Time');
title('Modulating signal');
y=cos((2*pi*fc*t)+(m.*sin(2*pi*fm*t)));
subplot(313);
plot(t,y);
grid on;
ylabel('Amplitude');
xlabel('Time');
title('PM signal');

Message frequency=20
Carrier frquency=400
Modulation index=10
>>

Carrier signal
1

Amplitude

0.5
0
-0.5
-1

0.01

0.02

0.03

0.04

0.05
Time

0.06

0.07

0.08

0.09

0.1

0.06

0.07

0.08

0.09

0.1

0.06

0.07

0.08

0.09

0.1

Modulating signal
1

Amplitude

0.5
0
-0.5
-1

0.01

0.02

0.03

0.04

0.05
Time
PM signal

Amplitude

0.5
0
-0.5
-1

0.01

0.02

5.MEAN SQR ERROR ESTIMATN

clc;
clear all;
close all;
fs=1000;
t=0:1/fs:1;
f=1;
A=1;
x=A*sin(2*pi*f*t);
subplot(2,1,1);
plot(t,x);
ylabel('x(t)');
xlabel('t(sec)');
title('Original signal');
axis([min(t) max(t) -A A]);
grid on;
M=8;
xq=quantise(x,M);
subplot(2,1,2);
plot(t,xq);
ylabel('xq(t)');

0.03

0.04

0.05
Time

xlabel('t(sec)');
title('Quantised signal');
axis([min(t) max(t) -A A]);
grid on;
sig_energy=0;
sqr_error=0;
for i=1:length(x)
sqr_error=sqr_error+((x(i)-xq(i))^2);
sig_energy=sig_energy+(x(i)^2);
end
mean=sqr_error/length(x)
SNRdB=10*log10(sig_energy/sqr_error)
%function quantize
function y=quantise(x,M)
minx=min(x);
maxx=max(x);
delta=(maxx-minx)/M;
y=zeros(size(x));
for i=1:length(x)
for j=1:M
if(x(i)<=(minx+(j*delta)))
y(i)=minx+((2*j-1)/2)*delta;
break;
end
end
end
end

mean =
0.0062
SNRdB =
19.0821

Original signal
1

x(t)

0.5
0
-0.5
-1

0.1

0.2

0.3

0.4

0.5
0.6
t(sec)
Quantised signal

0.7

0.8

0.9

0.1

0.2

0.3

0.4

0.7

0.8

0.9

xq(t)

0.5
0
-0.5
-1

0.5
t(sec)

0.6

6.TIME DELAY ESTIMATION USING CORRELATION FN


clc;
clear all;
close all;
fs=1000;
T=2;
t=0:1/fs:1;
f=1;
A=1;
x=A*sin(2*pi*f*t);
subplot(3,1,1);
plot(t,x);
ylabel('x(t)');
xlabel('t(sec)');
title('Original signal');
axis([min(t) max(t)+T -A A]);
grid on;
t1=min(t):1/fs:max(t)+T;
y=zeros(1,T*fs);
y=[y,x];
y=awgn(y,-10,'measured');
subplot(3,1,2);
plot(t1,y);
ylabel('y(t)');
xlabel('t(sec)');

title('Delayed signal');
axis([min(t) max(t)+T -A A]);
grid on;
corr=zeros(1,length(t1));
for i=1:length(y)
for j=1:length(x)
if i+j-1<=length(y)
corr(i)=corr(i)+y(i+j-1)*x(j);
else
break
end
end
end
subplot(3,1,3);
plot(t1,corr);
ylabel('corr');
xlabel('t(sec)');
title('Correlation function');
grid on;
delay=(find(corr==max(corr))-1)/fs

delay =

1.9920

Original signal

x(t)

1
0
-1

0.5

1.5
t(sec)
Delayed signal

2.5

0.5

1.5
t(sec)
Correlation function

2.5

0.5

1.5
t(sec)

2.5

y(t)

1
0
-1

corr

500
0
-500

7.BASK
clc; clear all; close all;
flag = 0;
%variable to check whether carrier frequencies are integer
multiples of bitrate
while ~flag
fc = input('Enter the Carrier Frequency: ');
bitRate = input('Enter the Bit Rate: ');
if mod(fc,bitRate)~=0
disp('Carrier frequency should be an integer multiple of
Rate!!!');
continue
end
flag = 1;

Bit

t = 0:1/(10*fc):1/bitRate-1/(10*fc);
flag1 = 0; %variable to check whether input data has any non-binary
value
sig0 = zeros(size(t));
sig1 = cos(2*pi*fc*t);
while ~flag1
x = input('Enter the binary Data: ');
y = [];
data = [];
taxis = [];
for i = 1:length(x)
if x(i)~=0 && x(i)~=1
disp('please enter only binary values');
flag1 = 0;
break;
else
taxis = [taxis (i-1)*max(t)+t];
flag1 = 1;
if x(i) == 0
y = [y sig0];
data = [data zeros(size(t))];
else
y = [y sig1];
data = [data ones(size(t))];
end
end
end
end
subplot(311)
plot(taxis,data);
title('Binary Data');
xlabel('t(sec)-->');
axis([0 max(taxis) -0.5 1.5]);
subplot(312)
plot(taxis,y);
title('BASK Modulated Signal');
xlabel('t(sec)-->');
axis([0 max(taxis) -1.5 1.5]);

end
xcap = [];
datacap = [];
for i = 1:length(x)
st_ind = (i-1)*length(t) + 1;
en_ind = i*length(t);
sig = y(st_ind:en_ind);
d = sig*sig1';
if d<length(t)/4
xcap = [xcap 0];
datacap = [datacap zeros(size(t))];
else
xcap = [xcap 1];
datacap = [datacap ones(size(t))];
end
end
subplot(313)
plot(taxis,datacap);
title('Demodulated Data');
xlabel('t(sec)-->');
axis([0 max(taxis) -0.5 1.5]);
disp('The demodulated data = ');
disp(xcap);

Enter the Carrier Frequency: 1000


Enter the Bit Rate: 100
Enter the binary Data: [1 0 0 1]
The demodulated data =
1

>>

Binary Data
1.5
1
0.5
0
-0.5

0.005

0.01

0.015

0.02
t(sec)-->
BASK Modulated Signal

0.025

0.03

0.035

0.005

0.01

0.015

0.02
t(sec)-->
Demodulated Data

0.025

0.03

0.035

0.005

0.01

0.015

0.02
t(sec)-->

0.025

0.03

0.035

-1

1.5
1
0.5
0
-0.5

8.BFSK

clc; clear all; close all;


flag = 0;
%variable to check whether carrier frequencies are integer
multiples of bitrate
while ~flag
fc1 = input('Enter the Carrier Frequency1: ');
fc2 = input('Enter the Carrier Frequency2: ');
bitRate = input('Enter the Bit Rate: ');
if (mod(fc1,bitRate)~=0 || mod(fc2,bitRate)~=0)
disp('Carrier frequencies should be an integer multiple of
Rate!!!');
continue
end
flag = 1;

Bit

t = 0:1/(100*fc1):1/bitRate-1/(100*fc1);
flag1 = 0; %variable to check whether input data has any non-binary
value
sig0 = cos(2*pi*fc1*t);

sig1 = cos(2*pi*fc2*t);
while ~flag1
x = input('Enter the binary Data: ');
y = [];
data = [];
taxis = [];
for i = 1:length(x)
if x(i)~=0 && x(i)~=1
disp('please enter only binary values');
flag1 = 0;
break;
else
taxis = [taxis (i-1)*max(t)+t];
flag1 = 1;
if x(i) == 0
y = [y sig0];
data = [data zeros(size(t))];
else
y = [y sig1];
data = [data ones(size(t))];
end
end
end
end
subplot(311)
plot(taxis,data);
title('Binary Data');
xlabel('t(sec)-->');
axis([0 max(taxis) -0.5 1.5]);
subplot(312)
plot(taxis,y);
title('BFSK Modulated Signal');
xlabel('t(sec)-->');
axis([0 max(taxis) -1.5 1.5]);
end
xcap = [];
datacap = [];
for i = 1:length(x)
st_ind = (i-1)*length(t) + 1;
en_ind = i*length(t);
sig = y(st_ind:en_ind);
d1 = sig*sig0';
d2 = sig*sig1';
if d1>d2
xcap = [xcap 0];
datacap = [datacap zeros(size(t))];
else
xcap = [xcap 1];
datacap = [datacap ones(size(t))];
end
end

subplot(313)
plot(taxis,datacap);
title('Demodulated Data');
xlabel('t(sec)-->');
axis([0 max(taxis) -0.5 1.5]);
disp('The demodulated data = ');
disp(xcap);

Enter the Carrier Frequency1: 1000


Enter the Carrier Frequency2: 2000
Enter the Bit Rate: 100
Enter the binary Data: [1 0 0 1]
The demodulated data =
1

>>

Binary Data
1.5
1
0.5
0
-0.5

0.005

0.01

0.015

0.02
t(sec)-->
BFSK Modulated Signal

0.025

0.03

0.035

0.005

0.01

0.015

0.02
t(sec)-->
Demodulated Data

0.025

0.03

0.035

0.005

0.01

0.015

0.02
t(sec)-->

0.025

0.03

0.035

-1

1.5
1
0.5
0
-0.5

9.BPSK
clc; clear all; close all;
flag = 0;
%variable to check whether carrier frequencies are integer
multiples of bitrate
while ~flag
fc = input('Enter the Carrier Frequency: ');
bitRate = input('Enter the Bit Rate: ');
if mod(fc,bitRate)~=0
disp('Carrier frequency should be an integer multiple of
Rate!!!');
continue
end
flag = 1;

Bit

t = 0:1/(10*fc):1/bitRate-1/(10*fc);
flag1 = 0; %variable to check whether input data has any non-binary
value
sig0 = -cos(2*pi*fc*t);
sig1 = cos(2*pi*fc*t);
while ~flag1
x = input('Enter the binary Data: ');
y = [];
data = [];
taxis = [];
for i = 1:length(x)
if x(i)~=0 && x(i)~=1
disp('please enter only binary values');
flag1 = 0;
break;
else
taxis = [taxis (i-1)*max(t)+t];
flag1 = 1;
if x(i) == 0
y = [y sig0];
data = [data zeros(size(t))];
else
y = [y sig1];
data = [data ones(size(t))];
end
end
end
end
subplot(311)
plot(taxis,data);
title('Binary Data');
xlabel('t(sec)-->');
axis([0 max(taxis) -0.5 1.5]);
subplot(312)
plot(taxis,y);
title('BPSK Modulated Signal');

xlabel('t(sec)-->');
axis([0 max(taxis) -1.5 1.5]);
end
xcap = [];
datacap = [];
for i = 1:length(x)
st_ind = (i-1)*length(t) + 1;
en_ind = i*length(t);
sig = y(st_ind:en_ind);
d = sig*sig0';
if d>0
xcap = [xcap 0];
datacap = [datacap zeros(size(t))];
else
xcap = [xcap 1];
datacap = [datacap ones(size(t))];
end
end
subplot(313)
plot(taxis,datacap);
title('Demodulated Data');
xlabel('t(sec)-->');
axis([0 max(taxis) -0.5 1.5]);
disp('The demodulated data = ');
disp(xcap);

Enter the Carrier Frequency: 1000


Enter the Bit Rate: 100
Enter the binary Data: [1 0 0 1]
The demodulated data =
1

>>

Binary Data
1.5
1
0.5
0
-0.5

0.005

0.01

0.015

0.02
t(sec)-->
BPSK Modulated Signal

0.025

0.03

0.035

0.005

0.01

0.015

0.02
t(sec)-->
Demodulated Data

0.025

0.03

0.035

0.005

0.01

0.015

0.02
t(sec)-->

0.025

0.03

0.035

-1

1.5
1
0.5
0
-0.5

10.HUFFMAN ENCODING
clc;
clear all;
close all;
symbols=[1:6];
p=[.5 .125 .125 .125 .0625 .0625];
[dict,avglen] = huffmandict(symbols,p);
temp = dict;
for i = 1:length(temp)
temp{i,2} = num2str(temp{i,2});
end

fprintf('===HUFFMAN DICTIONARY ===\n\n\n');


disp(temp);
sig=input('\n ENTER THE SIGNAL TO BE ENCODED (SYMBOLS B/W 1 & 6):');
coded_sig=huffmanenco(sig,dict);
fprintf('\n Huffman encoded signal:');
disp(coded_sig);
fprintf('===HUFFMAN DECODING ===\n\n\n');
fprintf('\n Decoded signal is:');
decoded_sig=huffmandeco(coded_sig,dict);
disp(decoded_sig);

===HUFFMAN DICTIONARY ===

[1] '0'
[2] '1 0 0'
[3] '1 1 1'
[4] '1 1 0'
[5] '1 0 1 1'
[6] '1 0 1 0'

ENTER THE SIGNAL TO BE ENCODED (SYMBOLS B/W 1 & 6):[1 2 5 4]

Huffman encoded signal:

===HUFFMAN DECODING ===

Decoded signal is:

>>

You might also like