Page no.
Practical File
of
Introduction to Matlab Programming Lab
Sub Code: MECS25-113
[Link] (Computer Science and Engineering)
Batch: 2025
Session: Aug - Dec 2025
Submitted To:
Er. Rupinder Kaur
Assistant Professor, ECE Department.
Submitted By:
Gursharan Bhardwaj Uni. Roll no.: 2502017
Course and Branch: [Link] (CSE) Semester: 1st
University School of Computing
Rayat Bahra Professional University, Hoshiarpur
Page no. 2
Rayat Bahra Professional University, Hoshiarpur
University School of Computing
Sub: Introduction to Matlab Programming Lab Sub Code: MECS25-113
Table of Content
Sr. no. Name of the Practical Page no. Date Remarks
Write the Matlab program to
perform some basic operations on
1
matrices such as addition,
subtraction and multiplication.
Write the Matlab program to
generate various signals and
2 sequence of unit impulse, unit step,
unit ramp, sinusoidal and square
signals.
Write the Matlab program to
compute auto correlation and cross
3
correlation between signals and
sequences.
Write the Matlab program to prove
that convolution in time domain is
4 equal to multiplication in frequency
domain, also rotate the image by 45
degrees.
Write the Matlab program for:
a) Changing the intensity level of
the image.
5 b) Inverting the image.
c) Cropping the image.
d) Sub sampling the image.
e) Draw the plot.
Write the Matlab program for gray
level slicing the image:
6 a) Without background.
b) With background.
Write the Matlab program for
7
image enhancement.
Write the Matlab program to swap
8
the phase of two images.
Page no. 1
Practical no. 01
Aim: Write the Matlab program to perform some basic operations on matrices
such as addition, subtraction and multiplication.
Requirements: An Integrated Development Environment (IDE) like MATLAB.
Definition: A matrix is a rectangular arrangement of elements in rows and columns. Matrix
operations are basic mathematical operations performed on matrices.
Theory:
Matrix Addition: Possible only when matrices have the same order. Elements are added
position-wise.
Matrix Subtraction: Similar to addition, but elements are subtracted.
Matrix Multiplication: Possible when the number of columns of the first matrix equals the
number of rows of the second matrix.
Code:
clc;
clear;
close all;
% Matrix Operations Program
A = input('Enter first matrix A: ');
B = input('Enter second matrix B: ');
% Addition
if all(size(A) == size(B))
C_add = A + B;
disp('Addition of matrices:');
disp(C_add);
else
disp('Addition not possible');
end
% Subtraction
if all(size(A) == size(B))
C_sub = A - B;
disp('Subtraction of matrices:');
[Link] (Computer Science and Engineering) 2502017
Page no. 2
disp(C_sub);
else
disp('Subtraction not possible');
end
% Multiplication
if size(A,2) == size(B,1)
C_mul = A * B;
disp('Multiplication of matrices:');
disp(C_mul);
else
disp('Multiplication not possible');
end
Output:
[Link] (Computer Science and Engineering) 2502017
Page no. 3
Practical no. 02
Aim: Write the Matlab program to generate various signals and sequence of unit
impulse, unit step, unit ramp, sinusoidal and square signals.
Requirements: An Integrated Development Environment (IDE) like MATLAB.
Definition: A signal is a function that conveys information about the behaviour of a system.
Theory:
Unit Impulse: Value is 1 at n = 0, zero elsewhere.
Unit Step: Value is 1 for n ≥ 0.
Unit Ramp: Linearly increasing signal for n ≥ 0.
Sinusoidal Signal: Periodic signal defined using sine function.
Square Signal: Periodic signal alternating between high and low values.
Code:
clc;
clear;
close all;
n = -10:10;
% Unit Impulse
imp = (n == 0);
% Unit Step
step = (n >= 0);
% Unit Ramp
ramp = n .* (n >= 0);
% Sinusoidal Signal
t = 0:0.01:1;
sinusoid = sin(2*pi*5*t);
% Square Signal
square_sig = square(2*pi*5*t);
[Link] (Computer Science and Engineering) 2502017
Page no. 4
% Plotting
figure;
subplot(3,2,1); stem(n,imp); title('Unit Impulse');
subplot(3,2,2); stem(n,step); title('Unit Step');
subplot(3,2,3); stem(n,ramp); title('Unit Ramp');
subplot(3,2,4); plot(t,sinusoid); title('Sinusoidal Signal');
subplot(3,2,5); plot(t,square_sig); title('Square Signal');
Output:
[Link] (Computer Science and Engineering) 2502017
Page no. 5
Practical no. 03
Aim: Write the Matlab program to compute auto correlation and cross
correlation between signals and sequences.
Requirements: An Integrated Development Environment (IDE) like MATLAB.
Definition:
Auto-correlation: Measures similarity of a signal with itself.
Cross-correlation: Measures similarity between two different signals.
Theory:
Auto-correlation is used for signal detection and noise reduction.
Cross-correlation is used for pattern matching and time delay estimation.
MATLAB provides xcorr() function for correlation computation.
Code:
clc;
clear;
close all;
x = [1 2 3 4];
y = [2 1 3 2];
% Auto-correlation
auto_corr = xcorr(x);
disp('Auto-correlation:');
disp(auto_corr);
% Cross-correlation
cross_corr = xcorr(x, y);
disp('Cross-correlation:');
disp(cross_corr);
% Plot
figure;
subplot(2,1,1); stem(auto_corr); title('Auto-correlation');
subplot(2,1,2); stem(cross_corr); title('Cross-correlation');
[Link] (Computer Science and Engineering) 2502017
Page no. 6
Output:
[Link] (Computer Science and Engineering) 2502017
Page no. 7
Practical no. 04
Aim: Write the Matlab program to prove that convolution in time domain is
equal to multiplication in frequency domain, also rotate the image by 45 degrees.
Requirements: An Integrated Development Environment (IDE) like MATLAB.
Definition: Convolution is a mathematical operation that combines two signals to produce a third
signal.
Theory:
Convolution Theorem: Time Domain Convolution ⇔ Frequency Domain Multiplication.
FFT converts a signal into frequency domain.
Image rotation is a geometric transformation used in image processing.
Code:
clc;
clear;
close all;
x = [1 2 3];
h = [1 1 1];
% Time domain convolution
y_time = conv(x, h);
% Frequency domain multiplication
X = fft(x, length(y_time));
H = fft(h, length(y_time));
y_freq = ifft(X .* H);
disp('Time domain convolution:');
disp(y_time);
disp('Frequency domain result:');
disp(real(y_freq));
% Image Rotation
img = imread('[Link]');
rot_img = imrotate(img, 45);
[Link] (Computer Science and Engineering) 2502017
Page no. 8
figure;
imshow(rot_img);
title('Image Rotated by 45 Degrees');
Output:
[Link] (Computer Science and Engineering) 2502017
Page no. 9
Practical no. 05
Aim: Write the Matlab program for:
a) Changing the intensity level of the image.
b) Inverting the image.
c) Cropping the image.
d) Sub sampling the image.
e) Draw the plot.
Requirements: An Integrated Development Environment (IDE) like MATLAB.
Definition: Image processing involves manipulation of images to enhance or extract information.
Theory:
Intensity change: Adjusts brightness.
Inversion: Produces photographic negative.
Cropping: Extracts a region of interest.
Sub-sampling: Reduces image resolution.
These operations are preprocessing steps in computer vision.
Code:
clc;
clear;
close all;
img = imread('[Link]');
% [Link] is already grayscale
gray = img;
% (a) Change Intensity Level
bright = gray + 50;
% (b) Invert Image
invert = 255 - gray;
% (c) Crop Image
[Link] (Computer Science and Engineering) 2502017
Page no. 10
crop_img = imcrop(gray, [50 50 100 100]);
% (d) Sub-sampling
sub_sample = gray(1:2:end, 1:2:end);
% (e) Plot
figure;
subplot(2,3,1); imshow(gray); title('Original');
subplot(2,3,2); imshow(bright); title('Intensity Changed');
subplot(2,3,3); imshow(invert); title('Inverted');
subplot(2,3,4); imshow(crop_img); title('Cropped');
subplot(2,3,5); imshow(sub_sample); title('Sub-sampled');
Output:
[Link] (Computer Science and Engineering) 2502017
Page no. 11
Practical no. 06
Aim: Write the Matlab program for gray level slicing the image:
a) Without background.
b) With background.
Requirements: An Integrated Development Environment (IDE) like MATLAB.
Definition: Gray level slicing highlights specific intensity ranges in an image.
Theory:
Without background: Only selected gray levels are shown.
With background: Original image is retained except highlighted region.
Used in medical imaging and remote sensing.
Code:
clc;
clear;
close all;
img = imread('[Link]');
% [Link] is already grayscale
gray = img;
% Gray level slicing WITHOUT background
sliced = (gray > 100 & gray < 150) * 255;
figure;
imshow(sliced, []);
title('Gray Level Slicing Without Background');
% Gray level slicing WITH background
sliced_bg = gray;
mask = (gray > 100 & gray < 150);
sliced_bg(mask) = 255;
figure;
imshow(sliced_bg, []);
[Link] (Computer Science and Engineering) 2502017
Page no. 12
title('Gray Level Slicing With Background');
Output:
[Link] (Computer Science and Engineering) 2502017
Page no. 13
Practical no. 07
Aim: Write the Matlab program for image enhancement.
Requirements: An Integrated Development Environment (IDE) like MATLAB.
Definition: Image enhancement improves the visual quality of an image.
Theory:
Histogram Equalization redistributes pixel intensities.
Enhances contrast in low-contrast images.
Used in satellite imaging, medical diagnosis, and surveillance.
Code:
clc;
clear;
close all;
img = imread('[Link]');
% [Link] is already grayscale
gray = img;
% Contrast Enhancement using Histogram Equalization
enhanced = histeq(gray);
figure;
subplot(1,2,1); imshow(gray); title('Original Image');
subplot(1,2,2); imshow(enhanced); title('Enhanced Image');
[Link] (Computer Science and Engineering) 2502017
Page no. 14
Output:
[Link] (Computer Science and Engineering) 2502017
Page no. 15
Practical no. 08
Aim: Write the Matlab program to swap the phase of two images.
Requirements: An Integrated Development Environment (IDE) like MATLAB.
Definition: The Fourier Transform represents an image in terms of magnitude and phase.
Theory:
Magnitude contains intensity information.
Phase contains structural and edge information.
Swapping phases proves that phase plays a dominant role in image perception.
Demonstrates importance of frequency-domain representation.
Code:
clc;
clear;
close all;
% Read two images
img1 = imread('[Link]');
img2 = imread('[Link]');
% Convert to grayscale ONLY if image is RGB
if ndims(img1) == 3
img1 = rgb2gray(img1);
end
if ndims(img2) == 3
img2 = rgb2gray(img2);
end
% Resize images to same size
img2 = imresize(img2, size(img1));
% Convert images to double
img1 = im2double(img1);
img2 = im2double(img2);
[Link] (Computer Science and Engineering) 2502017
Page no. 16
% Compute FFT of images
F1 = fft2(img1);
F2 = fft2(img2);
% Extract magnitude and phase
mag1 = abs(F1);
phase1 = angle(F1);
mag2 = abs(F2);
phase2 = angle(F2);
% Swap phases
new_img1 = mag1 .* exp(1i * phase2);
new_img2 = mag2 .* exp(1i * phase1);
% Inverse FFT
out1 = real(ifft2(new_img1));
out2 = real(ifft2(new_img2));
% Display results
figure;
subplot(2,3,1); imshow(img1, []); title('Original Image 1');
subplot(2,3,2); imshow(img2, []); title('Original Image 2');
subplot(2,3,3); imshow(out1, []);
title('Mag(Image 1) + Phase(Image 2)');
subplot(2,3,4); imshow(out2, []);
title('Mag(Image 2) + Phase(Image 1)');
subplot(2,3,5); imshow(log(1 + mag1), []); title('Magnitude Spectrum 1');
subplot(2,3,6); imshow(log(1 + mag2), []); title('Magnitude Spectrum 2');
Output:
[Link] (Computer Science and Engineering) 2502017
Page no. 17
[Link] (Computer Science and Engineering) 2502017