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

Practical File-MATHS

Uploaded by

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

Practical File-MATHS

Uploaded by

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

MATHEMATICAL FOUNDATION OF

COMPUTING
PRACTICAL FILE

SUBMITTED BY –ASHISH KUMAR

SUBMITTED TO – Dr. MRINAL DAS

ROLL NUMBER – 22285756024


INDEX
1. Program to read, display and write a (i) grayscale image, (ii) indexed image.
2. Program to find the details and compression ratio of an image file format
3. Program to convert a true-color image to an indexed image
4. Program to convert an RGB image to grayscale image.
5. Study of different image enhancement techniques.
6. Study of different filters in image processing.
7. Study of processing image histogram.
8. Study of image restoration technique.
Statistics:
1. Diagrammatic representation of data: Line diagram; bar diagram; multiple bar diagram; pie
diagram.
2. Graphic presentation of data- histogram, frequency polygon, frequency curve.
3. Measurements of central tendency- arithmetic mean, median, mode, geometric mean,
harmonic mean
4. Measurements of variation (or) Dispersion in data.
5. Correlation and covariance; Regression
Communication
(a) Analog
1. Amplitude modulation and demodulation
2. Frequency and phase modulation
3. Pulse amplitude modulation
4. Pulse width modulation
5. Sampling theorem
(b) Digital
1. Generation of uniform random numbers
2. Gaussian distributed random numbers
3. Binary digital modulation techniques (BASK, BPSK, BFSK, QPSK)
4. Bit error rate calculation.

1
IMAGE PROCESSING

2
PROGRAM - 1
Program to read, display and write a (i) grayscale image, (ii) indexed
image.

1. Grayscale

I = imread('test2.jpeg');
imshow(I)
imwrite(I,'output.jpg')

3
Indexed image

[corn_indexed,map] = imread('corn.tif',1);
imshow(corn_indexed,map)

4
PROGRAM - 2
Program to find the details and compression ratio of an image file format
f=imread('indexed.jpg');
imshow(f)
imwrite(f,'indexed.jpg');
k=imfinfo('indexed.jpg');
ib=k.Width*k.Height*k.BitDepth/8;
cb=k.FileSize;
cr=ib/cb
disp(cr)

5
PROGRAM - 3
Program to convert a true-color image to an indexed image
RGB = imread('IMG_20191215_145021.jpg');
[IND,map] = rgb2ind(RGB,40);
figure
imagesc(IND)
colormap(map)
axis image
zoom(4)

6
PROGRAM - 4
Program to convert an RGB image to grayscale image.
[X,map] = imread('corn.tif');
imshow(X,map)
newmap = rgb2gray(map);
imshow(X,newmap)

7
PROGRAM - 5
Study of different image enhancement techniques.

- A Using contrast stretching method for image enhancement


% contrast stretching method for image enhancement
pout = imread('pout.tif');
pout_imadjust = imadjust(pout);
pout_histeq = histeq(pout);
pout_adapthisteq = adapthisteq(pout);
% Display the original image and the three contrast adjusted images as a
montage.
montage({pout,pout_imadjust,pout_histeq,pout_adapthisteq},'Size',[1 4])
title("Original Image and Enhanced Images using imadjust, histeq, and
adapthisteq")
figure
subplot(1,2,1)
figure,imhist(pout)
title('Histogram of pout.tif')
figure,imhist(pout_adapthisteq)
title('Histogram of pout_adapthisteq.tif')

8
- Weiner filter enhancement
%weiner filter enhencement
Ioriginal = imread('cameraman.tif');
figure,imshow(Ioriginal)
title('Original Image')
PSF = fspecial('motion',21,11);
Idouble = im2double(Ioriginal);
blurred = imfilter(Idouble,PSF,'conv','circular');
figure, imshow(blurred)
title('Blurred Image')
wnr1 = deconvwnr(blurred,PSF);
figure, imshow(wnr1)
title('Restored Blurred Image')

9
10
- Fast Fourier transform method for image enhancement
%(c) fft--> fast fourier transform
i = imread('cameraman.tif');
i = im2double(i); % convert image to double precision
subplot(221),
imshow(i);
title('original image')
F = fft2(i);
subplot(222),
imshow(abs(F));
title('Magnitude Spectrum');
G = mat2gray(log10(1+abs(F)));% Convert matrix to intensity image
subplot(223),
imshow(G);
title('Log scaled magnitude spectrum');
I_recon = real(ifft2(F));
subplot(224),
imshow(I_recon);
title('Reconstructed image');

11
- Cosine transform and wavelet transform
% (f) cosine transform and wavelet transform
I = imread('cameraman.tif');
I = im2double(I); % convert image to double precision
ct = dct2(I);
[Ia,Ih,Iv,Id] = dwt2(I,'haar'); % Haar wavelet function--haar
out = [Ia,Ih; Iv,Id];
subplot(2,2,1),
imshow(ct);
title('cosine Transformed image')
I_recons = idct2(ct);
subplot(2,2,2),
imshow(I_recons);
title('Reconstructed image for cosine')
subplot(2,2,3)
imshow(out);
title('wavelet Transformed image');
I_recons =idwt2(Ia,Ih,Iv,Id,'haar');
subplot(2,2,4),
imshow(I_recons);
title('Reconstructed image for wavelet');

title('Reconstructed image for wavelet');

12
- Binary image, addition, subtraction and complement
%(d) binary image , addition , subtraction & complement
i= imread('cameraman.tif');
i1=i>120
figure()
subplot(2,2,1)
imshow(i1)
title('binary')
id=double(i);
id1=imadd(i,100)
subplot(2,2,2)
imshow(id1);
title('adding')
id1=imsubtract(i,50)
subplot(2,2,3)
imshow(id1)
title('subtracting')
id1=imcomplement(i);
subplot(2,2,4)
imshow(id1)
title('negative image')

13
PRACTICAL – 6
Study of different filters in image processing.
- Flash/ No- Flash Denoising with guided filter
%Perform Flash/No-flash Denoising with Guided Filter
A = imread('toysnoflash.png');
figure;
imshow(A);
title('Input Image - Camera Flash Off')
G = imread('toysflash.png');
figure;
imshow(G);
title('Guidance Image - Camera Flash On')
nhoodSize = 3;
smoothValue = 0.001*diff(getrangefromclass(G)).^2;
B = imguidedfilter(A, G, 'NeighborhoodSize',nhoodSize,
'DegreeOfSmoothing',smoothValue);
figure, imshow(B), title('Filtered Image')
figure;
h1 = subplot(1,2,1);
imshow(A), title('Region in Original Image'), axis on
h2 = subplot(1,2,2);
imshow(B), title('Region in Filtered Image'), axis on
linkaxes([h1 h2])
xlim([520 660])
ylim([150 250])

14
- Low pass filtering
% Matlab Program to demonstrate the "Low pass Filtering of an image using
% 2D-DFT"
a = imread('cameraman.tif');
b = im2double(a);
[m,n] = size(b);
c = zeros(2*m,2*n);
[p,q] = size(c);

for i = 1:p
for j = 1:q
if i <= m && j<= n
c(i,j) = b(i,j);
else
c(i,j) = 0;
end
end
end
imshow(b);title('original image');
figure;
imshow(c);title('padded image');

d = zeros(p,q);

for i = 1:p
for j = 1:q
d(i,j) = c(i,j).*(-1).^(i + j);
end
end
figure;
imshow(d);title('pre processed image for calculating DFT');

e = fft2(d);
figure;imshow(e);title('2D DFT of the pre processed image');

[x,y] = freqspace(p,'meshgrid');
z = zeros(p,q);
for i = 1:p
for j = 1:q
z(i,j) = sqrt(x(i,j).^2 + y(i,j).^2);
end
end
% Choosing the Cut off Frequency and hence defining the low pass filter
% mask
H = zeros(p,q);
for i = 1:p
for j = 1:q
if z(i,j) <= 0.4 % here 0.4 is the cut-off frequency of the LPF
H(i,j) = 1;
else
H(i,j) = 0;
end
end
end

15
figure;imshow(H);title('Low Pass Filter Mask');
h1 = e.*H;
figure;
imshow(h1);title('Low passed output');
h2 = ifft2(h1);
figure;
imshow(h2);title('output image after inverse 2D DFT');
h3 = zeros(p,q);
for i = 1:p
for j = 1:q
h3(i,j) = h2(i,j).*((-1).^(i+j));
end
end
figure;
imshow(h3);title('Post Processed image');
out = zeros(m,n);
for i = 1:m
for j = 1:n
out(i,j) = h3(i,j);
end
end
figure;
imshow([b out]);title('input image output image');

16
- Integral image domain filter
%Apply Multiple Filters to Integral Image(integral image domain filter)
originalImage = imread('cameraman.tif');
figure
imshow(originalImage)
title('Original Image')
filterSizes = [7 7;11 11;15 15];
maxFilterSize = max(filterSizes);
padSize = (maxFilterSize - 1)/2;
paddedImage = padarray(originalImage,padSize,'replicate','both');
intImage = integralImage(paddedImage);

figure
imshow(intImage,[])
title('Integral Image Representation')
filteredImage1 = integralBoxFilter(intImage, filterSizes(1,:));
filteredImage2 = integralBoxFilter(intImage, filterSizes(2,:));
filteredImage3 = integralBoxFilter(intImage, filterSizes(3,:));
whos filteredImage*
extraPadding1 = (maxFilterSize - filterSizes(1,:))/2;
filteredImage1 = filteredImage1(1+extraPadding1(1):end-extraPadding1(1),...
1+extraPadding1(2):end-extraPadding1(2) );

extraPadding2 = (maxFilterSize - filterSizes(2,:))/2;


filteredImage2 = filteredImage2(1+extraPadding2(1):end-extraPadding2(1),...
1+extraPadding2(2):end-extraPadding2(2) );

figure
imshow(filteredImage1,[])
title('Image filtered with [7 7] box filter')
figure
imshow(filteredImage2,[])
title('Image filtered with [11 11] box filter')

17
18
- Noise removal using filters
%nosie removal
a=imread('eight.tif');
I=imnoise(a,'salt & pepper',0.3);
figure;
imshow(I);
title('Image with Slat & Pepper Noise');
I_filtered=medfilt3(I,[5,5,3]);
figure;
imshow(I_filtered);
title('Filtered Image');

19
- Median filtering vs N-D filter
%(b) Median filtering vs N-D filter

I = imread('eight.tif');
subplot(2,2,1),
imshow(I);
title('original image');
h = ones(5,5)/30;
I_noise = imnoise(I,'salt & pepper',0.05); % Adding noise
subplot(2,2,2),
imshow(I_noise);
title('Noisy image');
J = imfilter(I_noise,h); % N-D Filtering image
subplot(2,2,3)
imshow(J)
title('N-D filter')
J = medfilt2(I_noise,[3,3]); % Filtering image
subplot(2,2,4),
imshow(J);
title('median Filtered image');

20
PROGRAM – 7
Study of processing image histogram.
% program 7:-- (a) histogram plotting in different functions
I = imread('cameraman.tif');
figure(),
imshow(I)
title('Input image');
h = imhist(I,256); % plotting using imhist function
figure()
subplot(2,2,1)
imhist(I);
title('Histogram using imhist function');
ylim('auto');
h1 = h(1:10:256);
horz = 1:10:256;
subplot(2,2,2)
bar(horz,h1); % plotting using bar function
title('Histogram using bar function');
subplot(2,2,3)
stem(horz,h1,'filled'); %plotting using stem function in fill mode
title('Histogram using stem function');
subplot(2,2,4)
plot(h); % plotting using plot function
title('Histogram using plot function');
1.

21
PROGRAM – 8
Study of image restoration technique.
% image resoration
I = imread('cameraman.tif');
% (a) Program to produce degradation in the image
n = imnoise(zeros(size(I)),'gaussian',0.1,0.01);
[R,psf] = degradation_model(I,n);
% (b)Program to restore the image using inverse filter
I_restore=deconvwnr(R,psf);
figure()
imshow(I_restore/255);
title('Restored image');
function [R,psf] = degradation_model(I,n)
figure(),
imshow(I);
title('Original image');
psf = fspecial('motion',7,45);
I_degrade = imfilter(I,psf,'circular');
i=double(I_degrade)
R= i + n;
figure(),
imshow(R/255);
title('Degraded image');
end

22
23
STATISTICS

24
PROGRAM – 1
Diagrammatic representation of data: Line diagram; bar diagram; multiple bar diagram; pie Diagram
a)
%diagrammatic representation of data from an, arbitrarily created data file named MATLAB
first we import our data file
clc
load MATLAB.dat
%the data file is added to a matrix depending on the dimensions
[n,p] = size(MATLAB);
%Create a time vector, t, containing integers from 1 to n.
t = 1:n;
%ploting the matrix and t
plot(t,MATLAB),
%the legend function sets the postion(i.e where on the output graph),
% and name in legend used
legend('frequency','Location','NorthWest')
%labels for the axis
xlabel('integer'), ylabel('frequency')
%we are giving a appropriate title to our plot
title('A sample output for line diagram')

b)

x = 1:10:10;
Y = [75 91 16 123.5 18 32 47 83 95 110];
bar(Y)
legend('eg. quantity','Location','NorthWest')
xlabel('e.g. quantity'), ylabel('e.g. frequency')
title('A sample outpt for a simple bar diagram')

25
Y = [5,1
8,3
9,6
5,5
3,2];
figure
bar(Y)
legend('data A','data B','Location','NorthWest')
%labels for the axis
xlabel('e.g. quantity'), ylabel('e.g. frequency')
%we are giving a appropriate title to our plot
title('A sample output for a 2-D Bar diagram')

Y = [5,1
8,3
9,6
5,5

26
3,2];
figure
bar3(Y)
legend('data A','data B','Location','NorthWest')
%labels for the axis
xlabel('e.g. quantity1'), ylabel('e.g. quantity2'), zlabel('e.g. frequency')
%we are giving a appropriate title to our plot
title('A sample output for a 3-D Bar Graph graph')

c)

A = [30 40 13 12]
cut =[1 1 1 0]
label={'partition1','partition2','partition3','partition4'};
subplot(121)
pie(A,cut,label)
title('Pie diagram 1')
A = [30 40 13 12]
subplot(122)
pie(A,label)
title('Pie diagram 2')

27
PROGRAM – 2
Graphic presentation of data – histogram, frequency polygon, frequency curve

(a)
x = -2.9:0.1:2.9;
y = randn(1000,1);
histogram(y,x)
xlabel('e.g. quantity1(units)'), ylabel('e.g. frequency')
title('This is an example of a histogram made from randomly generated numbers')

(b)
n2 = normrnd(100,15,[1000 1])
histfit(n2)
xlabel('e.g. quantity1(units)'), ylabel('e.g. frequency')
title('histogram with a frquency polygon')

28
c)

x = -3:0.1:3;
f = x./10;
g = cos(x);
plot(x,f,x,g)
title("two frequency curves plotted on a single canvas")

29
PROGRAM – 3
Measurement of central tendency -arithmetic mean, median, mode,
geometric mean, harmonic mean

% clear session and screen


clear all; clc
% create a new column vector with 10 elements
% with random numbers between 0 and 100
y=randi([1,100],1,100);
disp(y)
% basic statistics operations performed on ungrouped i.e discreete statistical data
disp('Average : ')
m=mean(y);
disp('Median : ')
median(y)
disp('Mode: ')
mode(y)
disp('geometric mean : ')
geomean(y)
disp('Median: ') harmmean(y)

30
PROGRAM – 4

Measurements of variation (or) Dispersion in data

clc
x = [300 430 170 470 600];
disp(x)
V=var(x);
disp('the standard deviation in data is :')
sd=sqrt(V)

31
PROGRAM – 5
Correlation and covariance; Regression
(a)
clc
clear all
close all
format bank
%static independent variable
x=[1 2 4 3 5 6 7 8 9 10];
a1=mean(x);
z=[];
for i=1:length(x)
z=[z (x(i)-a1)^2];
end
s1=sum(z);
s1=s1/(length(x)-1);
s1=sqrt(s1);
%static dependent variable
y=[1 3 3 2 5 7 7 2 9 4];
a2=mean(y);
z=[];
for i=1:length(y)
z=[z (y(i)-a2)^2];
end
s2=sum(z);
s2=s2/(length(x)-1);
s2=sqrt(s2);
x=x-a1;
y=y-a2;
ak=x*y';
cova=ak/(length(x)-1);
corr=cova/(s1*s2);
slope=corr*s2/s1;
yR=a2-slope*a1;
%ploting explanatory and response variables
plot(x,y,'+')
title("relation between x and y")

32
(b) Least square fitting.

x=[2 3 5 7 9 ];
y=[ 4 5 7 10 15 ];
stem(x,y);
a=[];
for i=1:length(x)
a=[a ; x(i) 1];
end
c =a\y';
yR = c(1)*x + c(2); % the fitted line
hold on;
plot(x,yR);

33
COMMUNICATION SYSTEM

34
ANALOG COMMUNICATION SYSTEM
PROGRAM – 1
Amplitude modulation and demodulation

%AMPLITUDE MODULATION AND DEMODULATION


clc
%Time Period Duration
t=0:0.001:0.5;
%Message Amplitude
a=1;
%Carrier Amplitude
ac=2;
%Message Frequency
fm=10;
%Carrier Frequency
fc=100;
%Modulation Index
%k<1 so under modulation as seen in figure
k=a/ac;
%Carrier Signal
ct=ac*sin(2*pi*fc*t);
%Message Signal
mt=a*sin(2*pi*fm*t);
%Modulated Signal
s=(1+k*mt).*ct;
subplot(4,1,1);
plot(t,mt);
title("Modulating Signal");
xlabel("time");
ylabel("m(t)");
subplot(4,1,2)
plot(t,ct);
title("Carrier Signal");
xlabel("time");
ylabel("c(t)");
subplot(4,1,3);
plot(t,s,t,ac+mt,t,-ac-mt);
title("Mdulated signal with Envelope");
xlabel("time");
ylabel("s(t)");
subplot(4,1,4);
%Demodulated Signal
s2=(1/pi)*(ac+mt);
plot(t,mt,t,s2);
title("Demodulated Signal");
xlabel("time");
ylabel("d(t)");

35
36
PROGRAM – 2
Frequency and phase modulation
%FREQUENCY AND PHASE MODULATION
%Time Period Duration
t=0:0.0001:0.5;
%Message Amplitude
a=1;
%Carrier Amplitude
ac=1;
%Message Frequency
fm=2;
%Carrier Frequency
fc=20;
%Message Signal
m=a*cos(2*pi*fm*t);
%Carrier Signal
ct=ac*cos(2*pi*fc*t);
%Frequency Sensitivity
kf=14;
%Modulation Index
beta=(kf*a)/fm;
%Fm Modulated Signal
sfm=ac*cos(2*pi*fc*t + beta*(sin(2*pi*fm*t)));
subplot(3,1,1);
plot(t,m);
title("Modulating Signal");
xlabel("time");
ylabel("m(t)");
subplot(3,1,2)
plot(t,ct);
title("Carrier Signal");
xlabel("time");
ylabel("c(t)");
subplot(3,1,3);
plot(t,sfm);
title("FM SIGNAL");
xlabel("time");
ylabel("y(t)");

37
b) Phase Modulation

clear all
close all
t = 0:0.01:1; % time variable
fc = 5; % carrier frequency

%
% create message signal m(t)
m = sin(2*pi*t);
%

kp = pi/2; % phase deviation constant

38
%
% modulating the carrier with the message signal
carrier = cos(2*pi*fc*t);
modulated = cos(2*pi*fc*t + kp*m);
%

%
% Plotting the signals
plot(t,m,'b',t,carrier,'r',t,modulated,'k--')
axis([0 1 -1.5 1.5]);
xlabel('Time(seconds)');
ylabel('Amplitude(volt)');
title('Phase modulation');
legend('Message','Carrier','Modulated');

39
PROGRAM – 3
Pulse amplitude modulation
%Pulse Amplitude Modulation and Demodulation
close all;
%Carrier Frequency
fc=100;
%Message Frequency
fm=fc/10;
%Sampling Frequency
fs=100*fc;
t=0:1/fs:4/fm;
%Message Signal
mt=cos(2*pi*fm*t);
%Square carrier wave
ct=0.5*square(2*pi*fc*t)+0.5;
%Pulse Modulated Signal
st=mt.*ct;

%Single Sided PAM signal


tt=[];
for i=1:length(st)
if st(i)==0;
tt=[tt,st(i)];
else
tt=[tt,st(i)+2];
end
end
subplot(5,1,1)
plot(t,mt);
title("Message Signal");
xlabel("time");
ylabel("m(t)");
subplot(5,1,2)
plot(t,ct);
title("Carrier Signal");
xlabel("time");
ylabel("c(t)");
subplot(5,1,3)
plot(t,st);
title("Bipolar PAM Signal");
xlabel("time");
ylabel("amplitude");
subplot(5,1,4)
plot(t,tt);
title("Unipolar PAM Signal");
xlabel("time");
ylabel("amplitude");
%Demodulation
dt=st.*ct;
filter=fir1(200,fm/fs,'low');
original_t_signal=conv(filter,dt);

40
t1=0:1/(length(original_t_signal)-1):1;
subplot(5,1,5)
plot(t1,original_t_signal);
title("Demodulated Signal");
xlabel("time");
ylabel("amplitude")

41
PROGRAM – 4
Pulse width modulation
%PWM Modulation and Demodulation
%Message Signal Frequency
clc;
fm=2;
%Carrier Signal Frequency
fc=20;
%Sampling Frequency
fs=1e4;
%Time Period Duration
t=0:1/fs:1;
%Message Wave
m=sin(2*pi*fm*t);
subplot(4,1,1);
plot(t,m);
title('MESSAGE SIGNAL');
xlabel('time');
ylabel('m(t)');
%Sawtooth Wave
c=1.5*sawtooth(2*pi*fc*t+pi);
subplot(4,1,2);
plot(t,c);
title('SAWTOOTH WAVE');
xlabel('time');
ylabel('c(t)');
n=length(c);
%PWM signal generation
p=[];
for i=1 : n
if(m(i)>=c(i))
p=[p 1];
else
p=[p 0];
end
end
subplot(4,1,3);
plot(t,p);
title('PWM SIGNAL');
xlabel('time');
ylim([-1.1 1.1])
d=demod(p,fc,fs,'pwm','centered');
t=0:0.05:1;
subplot(4,1,4);
plot(t,d); title('DEMODULATED
SIGNAL');
xlabel('time');
ylim([0 1]);

42
43
PROGRAM – 5
Sampling theorem
%Sampling Theorem
%Sampling occur Appropriately when sampling frequency is greater than
%message signal frequency
clc;
clear all;
%Duration of Time
t=-100:0.01:100;
%Modulating Frequency
fm=0.02;
x=cos(2*pi*t*fm)
subplot(4,1,1)
plot(t,x);
xlabel("time(sec)");
ylabel("x(t)");
title("Continuous time Signal");
%fs<2fm;
fs1=0.02;
n=-2:2;
x1=cos(2*pi*fm*n/fs1);
subplot(4,1,2)
stem(n,x1);
hold on
subplot(4,1,2);
plot(n,x1,':');
xlabel("n");
ylabel("x(n)");
title("Discrete Time Signal for fs<2fm");
%fs=2fm
fs2=0.04;
n1=-4:4;
x2=cos(2*pi*fm*n1/fs2);
subplot(4,1,3)
stem(n1,x2);
hold on
subplot(4,1,3);
plot(n1,x2,':');
xlabel("n1");
ylabel("x(n1)");
title("Discrete Time Signal for fs=2fm");
%fs>2fm
fs3=0.5;
n2=-100:100;
x3=cos(2*pi*fm*n2/fs3);
subplot(4,1,4)
stem(n2,x3);
hold on
subplot(4,1,4);
plot(n2,x3,':');

44
xlabel("n2");
ylabel("x(n2)");
title("Discrete Time Signal for fs>2fm");

45
DIGITAL COMMUNICATION SYSTEM
PROGRAM – 1
Generation of uniform random numbers

%%Uniform random number generation in range 0 to 1 using rand() function


%rand()
x=rand()

%%Uniform random number generation in range of 5 to 10 using rand()


%%function
%rand()*(upper_limit-lower_limit)+lower_limit
y=rand()*5+5

%%Matrix of 1x10 containing random numbers between 5 to 10 using rand()


%%function
%rand(numberofrows,numberofcolumns)*(upper_limit-lower_limit)+lower_limit
z=rand(1,10)*5+5

%%Generation of random integers from 1 10 10 and showing result as matrix


%%of 1x10
%randi(upper_limit,number_of_rows,number_of_columns)
a=randi(10,1,10)

%or
a=randi([1,10],1,10)

%%Generation of random integers from -10 to 1 and showing result as matrix


%%of 1x10
%randi(|upper_limit-lower_limit|,num_of_rows,num_of_cols)+lower_limit
%as range have 1 as lower limit
b=randi(12,1,10)-11

%or
b=randi([-10,1],1,10)

46
47
PROGRAM – 2
Gaussian distributed random numbers
%to clear the variables(clear), clear the command window (clc)and for closing all the
%figures(close all)
clc;clear;close all;
%%Generation of gaussian distributive random numbers and their plot
%%randn(): creates random number on the normal(gaussian) distribution ("with replacement")
with mean 0 and standard deviation 1.
a=randn()

%%Generation of random number and putting it as matrix of dimensions 1x10


b=randn(1,10)

%%Generation of PDF (probability distribution function) plot for gaussian random


%%variable
%N= number of samples
%x=row vectors of equally spaced numbers
N=1000000;
x=-5:0.01:5;

%Main function for PDF of gaussian random numbers


f=@(x) 1/sqrt(2*pi)*exp(-x.^2/2);

%Generate vector of gaussian distributed random numbers with mean 0 and


%variance 1
X=randn(1,N);

figure(1)
histogram(X,200,'Normalization','pdf');
hold on;
plot(x,f(x),'r-','Linewidth',3);
%legend('Simulated','Theoretical','fontsize',14,'location','northeast');
xlabel('x','fontsize',12);
ylabel('f(x)','fontsize',12);
title('PDF of standard gaussian random variable');

48
49
PROGRAM – 3
Binary digital modulation techniques (BASK, BPSK, BFSK,QPSK)

- BASK
- clc;
- clear all;
- close all;
- % ********************* Define transmitted signal *************************
- N=10; % Number of bits , size of transmitted signal x_inp=[x_1 x_2 ... x_N]
- x_inp= randi([0,1],N); % binary signal 0 or 1 % message to be transmitted
- Tb=0.0001; % bit period (second)
- % ********************* Represent input signal as digital signal ****
- x_bit=[];
- nb=100; % bbit/bit
- for n=1:1:N %
- if x_inp(n)==1; %
- x_bitt=ones(1,nb);
- else x_inp(n)==0;
- x_bitt=zeros(1,nb);
- end
- x_bit=[x_bit x_bitt];
- end
- t1=Tb/nb:Tb/nb:nb*N*(Tb/nb); % time of the signal
- %f1 = figure(1);
- %set(f1,'color',[1 1 1]);
- subplot(3,1,1);
- plot(t1,x_bit,'lineWidth',2);grid on;
- axis([ 0 Tb*N -0.5 1.5]);
- ylabel('Amplitude(volt)');
- xlabel(' Time(sec)');
- title('Input signal as digital signal');
- % ********************* Define BASK Modulation ****************************
- Ac1=15; % Amplitude of carrier signal for bit 1
- Ac2=5; % Amplitude of carrier signal for bit 0
- mc=10; % fc>>fs fc=mc*fs fs=1/Tb
- fc=mc*(1/Tb); % carrier frequency
- t2=Tb/nb:Tb/nb:Tb;
- t2L=length(t2);
- x_mod=[];
- for (i=1:1:N)
- if (x_inp(i)==1)
- x_mod0=Ac1*cos(2*pi*fc*t2);%modulation signal with carrier signal 1
- else
- x_mod0=Ac2*cos(2*pi*fc*t2);%modulation signal with carrier signal 2
- end
- x_mod=[x_mod x_mod0];
- end
- t3=Tb/nb:Tb/nb:Tb*N;
- subplot(3,1,2);

50
- plot(t3,x_mod);
- xlabel('Time(sec)');
- ylabel('Amplitude(volt)');
- title('Signal of BASK modulation ');
- % ********************* Transmitted signal x ******************************
- x=x_mod;
- % ********************* Channel model h and w *****************************
- h=1; % Fading
- w=0; % Noise
- % ********************* Received signal y *********************************
- y=h.*x+w;
- % ********************* Define BASK Demodulation **************************
- y_dem=[];
- for n=t2L:t2L:length(y)
- t=Tb/nb:Tb/nb:Tb;
- c=cos(2*pi*fc*t); % carrier siignal
- y_dem0=c.*y((n-(t2L-1)):n);
- t4=Tb/nb:Tb/nb:Tb;
- z=trapz(t4,y_dem0); % intregation
- A_dem=round((2*z/Tb));
- if(A_dem>((Ac1+Ac2)/2)) % logic level = (Ac1+Ac2)/2
- A=1;
- else
- A=0;
- end
- y_dem=[y_dem A];
- end
- x_out=y_dem; % output signal;
- % *************** Represent output signal as digital signal ***************
- xx_bit=[];
- for n=1:length(x_out);
- if x_out(n)==1;
- xx_bitt=ones(1,nb);
- else x_out(n)==0;
- xx_bitt=zeros(1,nb);
- end
- xx_bit=[xx_bit xx_bitt];
- end
- t4=Tb/nb:Tb/nb:nb*length(x_out)*(Tb/nb);
- subplot(3,1,3)
- plot(t4,xx_bit,'LineWidth',2);grid on;
- axis([ 0 Tb*length(x_out) -0.5 1.5]);
- ylabel('Amplitude(volt)');
- xlabel(' Time(sec)');
- title('Output signal as digital signal');
- % **************************** end of program *****************************

51
- BPSK
- % ********************* BPSK modulation and de-modulation ****************%
- clc;
- clear all;
- close all;
- % ********************* Define transmitted signal *************************
- N=10; % Number of bits , size of transmitted signal x_inp=[x_1 x_2 ... x_N]
- x_inp= randi([0,1],N); % binary signal 0 or 1 % message to be transmitted
- Tb=0.0001; % bit period (second)
- % ********************* Represent input signal as digital signal ****
- x_bit=[];
- nb=100; % bbit/bit
- for n=1:1:N %
- if x_inp(n)==1; %
- x_bitt=ones(1,nb);
- else x_inp(n)==0;
- x_bitt=zeros(1,nb);
- end
- x_bit=[x_bit x_bitt];
- end
- t1=Tb/nb:Tb/nb:nb*N*(Tb/nb); % time of the signal
- f1 = figure(1);
- set(f1,'color',[1 1 1]);
- subplot(3,1,1);

52
- plot(t1,x_bit,'lineWidth',2);grid on;
- axis([ 0 Tb*N -0.5 1.5]);
- ylabel('Amplitude(volt)');
- xlabel(' Time(sec)');
- title('Input signal as digital signal');
- % ********************* Define BFSK Modulation ****************************
- Ac=5; % Amplitude of carrier signal
- mc=4; % fc>>fs fc=mc*fs fs=1/Tb
- fc=mc*(1/Tb); % carrier frequency for bit 1
- fi1=0; % carrier phase for bit 1
- fi2=pi; % carrier phase for bit 0
- t2=Tb/nb:Tb/nb:Tb;
- t2L=length(t2);
- x_mod=[];
- for (i=1:1:N)
- if (x_inp(i)==1)
- x_mod0=Ac*cos(2*pi*fc*t2+fi1);%modulation signal with carrier signal 1
- else
- x_mod0=Ac*cos(2*pi*fc*t2+fi2);%modulation signal with carrier signal 2
- end
- x_mod=[x_mod x_mod0];
- end
- t3=Tb/nb:Tb/nb:Tb*N;
- subplot(3,1,2);
- plot(t3,x_mod);
- xlabel('Time(sec)');
- ylabel('Amplitude(volt)');
- title('Signal of BASK modulation ');
- % ********************* Transmitted signal x ******************************
- x=x_mod;
- % ********************* Channel model h and w *****************************
- h=1; % Fading
- w=0; % Noise
- % ********************* Received signal y *********************************
- y=h.*x+w;
- % ********************* Define BPSK Demodulation **************************
- y_dem=[];
- for n=t2L:t2L:length(y)
- t=Tb/nb:Tb/nb:Tb;
- c=cos(2*pi*fc*t); % carrier siignal
- y_dem0=c.*y((n-(t2L-1)):n);
- t4=Tb/nb:Tb/nb:Tb;
- z=trapz(t4,y_dem0); % intregation
- A_dem=round((2*z/Tb));
- if(A_dem>Ac/2) % logic level = Ac/2
- A=1;
- else
- A=0;
- end
- y_dem=[y_dem A];
- end
- x_out=y_dem; % output signal;

53
- % *************** Represent output signal as digital signal ***************
- xx_bit=[];
- for n=1:length(x_out);
- if x_out(n)==1;
- xx_bitt=ones(1,nb);
- else x_out(n)==0;
- xx_bitt=zeros(1,nb);
- end
- xx_bit=[xx_bit xx_bitt];
- end
- t4=Tb/nb:Tb/nb:nb*length(x_out)*(Tb/nb);
- subplot(3,1,3)
- plot(t4,xx_bit,'LineWidth',2);grid on;
- axis([ 0 Tb*length(x_out) -0.5 1.5]);
- ylabel('Amplitude(volt)');
- xlabel(' Time(sec)');
- title('Output signal as digital signal');
- % **************************** end of program *****************************

54
- BFSK
- % ********************* BFSK modulation and de-modulation ****************%
- clc;
- clear all;
- close all;
- % ********************* Define transmitted signal *************************
- N=10; % Number of bits , size of transmitted signal x_inp=[x_1 x_2 ... x_N]
- x_inp= randi([0,1],N); % binary signal 0 or 1 % message to be transmitted
- Tb=0.0001; % bit period (second)
- % ********************* Represent input signal as digital signal ****
- x_bit=[];
- nb=100; % bbit/bit
- for n=1:1:N %
- if x_inp(n)==1; %
- x_bitt=ones(1,nb);
- else x_inp(n)==0;
- x_bitt=zeros(1,nb);
- end
- x_bit=[x_bit x_bitt];
- end
- t1=Tb/nb:Tb/nb:nb*N*(Tb/nb); % time of the signal
- f1 = figure(1);
- set(f1,'color',[1 1 1]);
- subplot(3,1,1);
- plot(t1,x_bit,'lineWidth',2);grid on;
- axis([ 0 Tb*N -0.5 1.5]);
- ylabel('Tmplitude(volt)');
- xlabel(' Time(sec)');
- title('Input signal as digital signal');
- % ********************* Define BFSK Modulation ****************************
- Ac=5; % Amplitude of carrier signal
- mc1=16; % fc>>fs fc=mc*fs fs=1/Tb
- mc2=4;
- fc1=mc1*(1/Tb); % carrier frequency for bit 1
- fc2=mc2*(1/Tb); % carrier frequency for bit 0
- t2=Tb/nb:Tb/nb:Tb;
- t2L=length(t2);
- x_mod=[];
- for (i=1:1:N)
- if (x_inp(i)==1)
- x_mod0=Ac*cos(2*pi*fc1*t2);%modulation signal with carrier signal 1
- else
- x_mod0=Ac*cos(2*pi*fc2*t2);%modulation signal with carrier signal 2
- end
- x_mod=[x_mod x_mod0];
- end
- t3=Tb/nb:Tb/nb:Tb*N;
- subplot(3,1,2);
- plot(t3,x_mod);
- xlabel('Time(sec)');
- ylabel('Amplitude(volt)');
- title('Signal of BASK modulation ');

55
- % ********************* Transmitted signal x ******************************
- x=x_mod;
- % ********************* Channel model h and w *****************************
- h=1; % Fading
- w=0; % Noise
- % ********************* Received signal y *********************************
- y=h.*x+w;
- % ********************* Define BFSK Demodulation **************************
- y_dem=[];
- for n=t2L:t2L:length(y)
- t=Tb/nb:Tb/nb:Tb;
- c_dem1=cos(2*pi*fc1*t); % carrier siignal for information 1
- c_dem2=cos(2*pi*fc2*t); % carrier siignal for information 0
- y_dem1=c_dem1.*y((n-(t2L-1)):n);
- y_dem2=c_dem2.*y((n-(t2L-1)):n);
- t4=Tb/nb:Tb/nb:Tb;
- z1=trapz(t4,y_dem1); % intregation
- z2=trapz(t4,y_dem2); % intregation
- A_dem1=round(2*z1/Tb);
- A_dem2= round(2*z2/Tb);
- if(A_dem1>Ac/2) % % logic level = (Ac)/2
- a=1;
- else(A_dem2>Ac/2)
- a=0;
- end
- y_dem=[y_dem a];
- end
- x_out=y_dem; % output signal;
- % *************** Represent output signal as digital signal ***************
- xx_bit=[];
- for n=1:length(x_out);
- if x_out(n)==1;
- xx_bitt=ones(1,nb);
- else x_out(n)==0;
- xx_bitt=zeros(1,nb);
- end
- xx_bit=[xx_bit xx_bitt];
- end
- t4=Tb/nb:Tb/nb:nb*length(x_out)*(Tb/nb);
- subplot(3,1,3)
- plot(t4,xx_bit,'LineWidth',2);grid on;
- axis([ 0 Tb*length(x_out) -0.5 1.5]);
- ylabel('Amplitude(volt)');
- xlabel(' Time(sec)');
- title('Output signal as digital signal');
- % **************************** end of program *****************************

56
57
- QPSK
- %XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- %XXXX QPSK Modulation and Demodulation without consideration of noise XXXXX
- %XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- clc;
- clear all;
- close all;
- data=[0 1 0 1 1 1 0 0 1 1]; % information
- %Number_of_bit=1024;
- %data=randint(Number_of_bit,1);
- figure(1)
- stem(data, 'linewidth',3), grid on;
- title(' Information before Transmiting ');
- axis([ 0 11 0 1.5]);
- data_NZR=2*data-1; % Data Represented at NZR form for QPSK modulation
- s_p_data=reshape(data_NZR,2,length(data)/2); % S/P convertion of data
- br=10.^6; %Let us transmission bit rate 1000000
- f=br; % minimum carrier frequency
- T=1/br; % bit duration
- t=T/99:T/99:T; % Time vector for one bit information
- % XXXXXXXXXXXXXXXXXXXXXXX QPSK modulatio XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- y=[];
- y_in=[];
- y_qd=[];
- for(i=1:length(data)/2)
- y1=s_p_data(1,i)*cos(2*pi*f*t); % inphase component
- y2=s_p_data(2,i)*sin(2*pi*f*t) ;% Quadrature component
- y_in=[y_in y1]; % inphase signal vector
- y_qd=[y_qd y2]; %quadrature signal vector
- y=[y y1+y2]; % modulated signal vector
- end
- Tx_sig=y; % transmitting signal after modulation
- tt=T/99:T/99:(T*length(data))/2;
- figure(2)
- subplot(3,1,1);
- plot(tt,y_in,'linewidth',3), grid on;
- title(' wave form for inphase component in QPSK modulation ');
- xlabel('time(sec)');
- ylabel(' amplitude(volt0');
- subplot(3,1,2);
- plot(tt,y_qd,'linewidth',3), grid on;
- title(' wave form for Quadrature component in QPSK modulation ');
- xlabel('time(sec)');
- ylabel(' amplitude(volt0');
- subplot(3,1,3);
- plot(tt,Tx_sig,'r','linewidth',3), grid on;
- title('QPSK modulated signal (sum of inphase and Quadrature phase signal)');
- xlabel('time(sec)');
- ylabel(' amplitude(volt0');
- % XXXXXXXXXXXXXXXXXXXXXXXXXXXX QPSK demodulation XXXXXXXXXXXXXXXXXXXXXXXXXX
- Rx_data=[];
- Rx_sig=Tx_sig; % Received signal

58
- for(i=1:1:length(data)/2)
- %%XXXXXX inphase coherent dector XXXXXXX
- Z_in=Rx_sig((i-1)*length(t)+1:i*length(t)).*cos(2*pi*f*t);
- % above line indicat multiplication of received & inphase carred signal
-
- Z_in_intg=(trapz(t,Z_in))*(2/T);% integration using trapizodial rull
- if(Z_in_intg>0) % Decession Maker
- Rx_in_data=1;
- else
- Rx_in_data=0;
- end
-
- %%XXXXXX Quadrature coherent dector XXXXXX
- Z_qd=Rx_sig((i-1)*length(t)+1:i*length(t)).*sin(2*pi*f*t);
- %above line indicat multiplication ofreceived & Quadphase carred signal
-
- Z_qd_intg=(trapz(t,Z_qd))*(2/T);%integration using trapizodial rull
- if (Z_qd_intg>0)% Decession Maker
- Rx_qd_data=1;
- else
- Rx_qd_data=0;
- end
-
-
- Rx_data=[Rx_data Rx_in_data Rx_qd_data]; % Received Data vector
- end
- figure(3)
- stem(Rx_data,'linewidth',3)
- title('Information after Receiveing ');
- axis([ 0 11 0 1.5]), grid on;
- % XXXXXXXXXXXXXXXXXXXXXXXXX end of program XXXXXXXXXXXXXXXXXXXXXXXXXX
-

59
60
61
PROGRAM – 4
Bit error rate calculation.
%%% Consider Source Signal to be
x=[0 1 0 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 0 0 0 0 0 0 0]
%%% Consider received Signal to be
y=[0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 1 1 1 0 0 1 0 1 0 0 1 0 0 1 0]
%%% then bit error rate is calculated using inbuilt function
%% here num is num of error occurred and ratio is of number of error to the number of data
samples
[num,ratio]=biterr(x,y)

62

You might also like