0% found this document useful (0 votes)
48 views13 pages

Digital Signal Processing Sessional: Rajshahi University of Engineering & Technology

The document contains the code for 8 programs written in MATLAB. Program 1 represents basic signals like unit sample, unit step, and unit ramp. Program 2 reads and writes audio files and allows audio recording. Program 3 produces single and multiple level echoes for an audio signal. Program 4 performs time compression and expansion of audio. Program 5 performs convolution of two discrete signals. Program 6 adds two discrete signals. Program 7 reads and converts an image file to grayscale. Program 8 subtracts two images and displays the absolute difference.

Uploaded by

Mehedi Hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views13 pages

Digital Signal Processing Sessional: Rajshahi University of Engineering & Technology

The document contains the code for 8 programs written in MATLAB. Program 1 represents basic signals like unit sample, unit step, and unit ramp. Program 2 reads and writes audio files and allows audio recording. Program 3 produces single and multiple level echoes for an audio signal. Program 4 performs time compression and expansion of audio. Program 5 performs convolution of two discrete signals. Program 6 adds two discrete signals. Program 7 reads and converts an image file to grayscale. Program 8 subtracts two images and displays the absolute difference.

Uploaded by

Mehedi Hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Rajshahi University of Engineering & Technology

Course No: EEE 3218


Course Title:
Digital Signal Processing Sessional

Experiment No:01 & 02


Experiment Name: Audio File & Image Processing in MATLAB, Convolution and Addition
of Two Discreet Signal in MATLAB Platform

Submitted by:
Md. Mehedi Hasan
Roll:1601118
Section: B
Program 1:Reprentation of basic signals in Matlab
clc

clear all

close all

%% Taking the time limit

n_lower = input('Enter the lower limit: ');

n_upper = input('Enter the upper limit: ');

n = n_lower : n_upper;

%% Unit Sample Sequence %%

x = [n==0];

subplot 221, stem(n,x)

xlabel('Time in Seconds')

ylabel('Unit sample value')

title('The Unit Sample Sequence')

%% Unit Step Sequence %%

x = [n>=0];

subplot 222, stem(n,x)

xlabel('Time in Seconds')

ylabel('Unit step value')

title('The Unit Step Sequence')

%% Unit Ramp Sequence %%

x = n.*[n>=0];

subplot 223, stem(n,x)

xlabel('Time in Seconds')

ylabel('Unit ramp value')

title('The Unit Ramp Sequence')


OUTPUT:

program 2:Matlab code to read,write and recording of


audio signal
%% *** Reading The Audio Signal *** %%
[n, Fs] = audioread('C:\Users\Acer\Downloads -dragon.mp3');
% Where n = Number of data in the audio signal % And Fs = no of Samples
per second in the audio signal
sound(n)
% This Function Plays the audio

[filename, pathname] = uigetfile('*', 'select the file');


[n,Fs] = audioread(num2str(filename));
P = audioplayer(n,Fs);
play(P)
% This function plays the audio

%% Recording the audio using audiorecorder function


Fs = 37500; % The rate of sampling
nBits = 16; % the number of bits
nCnl=1; %channel is defined
recObj = audiorecorder(Fs,nBits,nCnl);
disp('Start Recording')
record(recObj);
pause(5); % Defines the duration of record
disp('End of record')
y = getaudiodata(recObj); %storage of recording
sound(y,Fs);% Listening the record
audiowrite('My_Sound.wav',y,37500)% record is shaved

Program 3:Matlab code to produce single level and


multiple level echo

%% *** Program to make Echo of a given audio signal *** %%


clc
clear all
close all
% Defining the audio input function
[filename, pathname] = uigetfile('*', 'select the file');
[x,fs] = audioread(num2str(filename));
l = length(x); % length of audio signal
a=0.4; % amplitude of echo is defined
m = 0.2*fs; % The time after echo is generated
%loop adjustment
for i=1:m
x=[x; 0];
end
% Loop for producing echo
for i = 1:l+m
if i<=m
y(i) = x(i);
else
y(i) = x(i) + a*x(i-m);
end
end
p1 = audioplayer(x,fs) % The main audio
p0 = audioplayer(y,fs) % The audio added with echo
play(p0)

%% *** A program to produce multiple Echo levels *** %%


clc
clear all
close all
% Defining the audio input function from the user
[filename, pathname] = uigetfile('*', 'select the file');
[x,fs] = audioread(num2str(filename));
l = length(x); % Defining the length of audio signal
a=0.4; % amplitude
m = 0.2*fs; % The delay time after echo is generated
% Loop for adjusting the echo duration with main audio signal
for i=1:m
x=[x; 0];
end
% Loop
for i = 1:l+m
if i<=m
y(i) = x(i);
else
y(i) = x(i) + a*y(i-m); % audio is added repeatedly
end
end
p1 = audioplayer(x,fs) % The main file
p0 = audioplayer(y,fs) % The audio added with echo
play(p0)

Program 4:Matlab code for Time Compression &


Expansion of audio signal
%%For expansion of Time %%
clear all;
close all;
r = audioread('audio_file - dragon.mp3');
p1 = audioplayer(r,52000) % Sampling Frequency = 52000
play(p1);
% We can increase the sample number for expanding the signal

for i = 1 : (length(r));
y(2*i - 1) = r(i);
y(2*i) = 0;
end
% The upper loop will pad zero to the even positions of matrix y
p2 = audioplayer(r,12000); % Sampling Frequency = 12000
play(p2); %

%%program for Time Compression %%


% We can decrease the sample number for compressing the signal
for i = 1 : (length(r))/2;
y2(i) = r(2*i);
end
% The upper loop will be responsible for cancelling the odd elements in
signal x

%%for loop to recover Original Signal by Average Method


for i = 1 : (length(y2))
y2(2*1 -1) = y2(i);
if i < length(y2)
y(2*1) = (y2(i) + y2(i+1))/2;
else
y(2*i) = y2(i);
end
end
p3 = audioplayer(y2,32000);
play(p3);
Program 5: Matlab Program to Perform Convolution:
xn=input('enter the sequence 1 ');
l1=length(xn);
hn=input('enter the sequence 2 ');
l2=length(hn);
m=l1+l2-1;
z=zeros(1,m);
z=conv(xn,hn);
z1=zeros(1,m);
disp(z);
xnl1=[xn,zeros(1,l2-1)];
disp(xnl1);
hnl1=[hn,zeros(1,l1-1)];
disp(hnl1);
for i=1:m
for j=1:i
z1(i)=z1(i)+xnl1(j)*hnl1(i-j+1);
end;
end;
disp(z1)

Output is illustrated below:

>> convolution

enter the sequence 1 [1 2 3]

enter the sequence 2 [1 2 3]

1 4 10 12 9

1 2 3 0 0

1 2 3 0 0

1 4 10 12 9
Program 6:Code to add two discrete
signals
clc
clear all
close all
% Taking the signals as input from the user
x1 = input('Enter the first signal x1: ');
x2 = input('Enter the second signal x2: ');
n1 = input('No of Elements before zeroth value in x1: ');
n2 = input('No of Elements before zeroth value in x2: ');
% The adding process
Pos_x1 = 0 - n1;
for x = 2:length(x1)
Pos_x1(x) = Pos_x1(x-1) + 1;
end
Pos_x1;
Pos_x2 = 0 - n2;
for xx = 2:length(x2)
Pos_x2(xx) = Pos_x2(xx-1) + 1;
end
Pos_x2;
Pos_y = min(Pos_x2):max(Pos_x1);
if Pos_x1(1)~=Pos_x2(1)
x1_new = [zeros(1,(Pos_x1(1) - Pos_x2(1))) x1];
end
if Pos_x1(1)~=Pos_x2(1)
x2_new = [x2 zeros(1,(Pos_x1(1) - Pos_x2(1)))];
end
%% The output of the addition
disp('The sum of x1 and x2 is given by')
y = x1_new + x2_new

OUTPUT :

Enter the first signal x1: [1 2 3 4 5]

Enter the second signal x2: [6 7 8 -2 -5]

No of Elements before zeroth value in x1: 2

No of Elements before zeroth value in x2: 8

The sum of x1 and x2 is given by

y=

6 7 8 -2 -5 0 1 2 3 4 5
Program 7 :Matlab code to read image file

clc
clear all
close all
% Taking the image as input
% RGB Image
img = uigetfile('*', 'Select your image');
x = imread(img);
figure(1)
imshow(x)
% GrayScale Image
x1 = rgb2gray(x);
figure(2)
imshow(x1)

output :

Figure 1:RGB image


Figure 2:Grayscale image

Program 8:Matlab program to subtract two images


clc
clear all
[filename, pathname] = uigetfile('*', 'Select the 1st Image')
x= imread(num2str(filename));

[filename, pathname] = uigetfile('*', 'Select the 2nd Image')


y= imread(num2str(filename));
X1=rgb2gray(x);
Y1=rgb2gray(y);
figure;
imshow(X1)
figure;
imshow(Y1)
Z=abs(X1-Y1);
figure;
imshow(Z)

Input and Output are illustrated below:

You might also like