0% found this document useful (0 votes)
27 views12 pages

Lab3 PDF

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

Lab3 PDF

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

Department of Electrical Engineering

Faculty Member:Munadi Sial Dated__23rd September 2024

Section:_____C________________________ Semester:___5th
____________

EE-232 : Signals and Systems


Lab 3 : Signal Transformations

PLO5 –CLO3 PLO5-CLO3 PLO8-CLO4 PLO9-CLO5


Name Reg. No Viva / Quiz / Analysis Modern Tool Ethics and Individual
Lab of data in Usage Safety and Team
Performance Lab Work
Report

5 Marks 5 Marks 5 Marks 5 Marks 5 Marks


Ali Masood Khan 410507

Muhammad Ibrahim 403522

Abdullah khan 411082

EE-232 Signals and Systems Page 1


Lab3: Signal Transformations

Objectives
This Lab experiment has been designed to introduce students to signal transformations using
MATLAB

✓ How to perform time shifting


✓ How to perform time compression/expansion
✓ Even and Odd Signals

Lab Instructions
✓ The students should perform and demonstrate each lab task separately for step-wise
evaluation(please ensure that course instructor/lab engineer hasverified each step after ascertaining
its functional verification)
✓ Only those tasks that completed during the allocated lab time will be credited to the students.
Students are however encouraged to practice on their own in spare time for enhancing their skills.

Lab Report Instructions


All questions should be answered precisely to get maximum credit. Lab report must ensure following
items:
✓ MATLAB codes
✓ Results (graphs/tables) duly commented and discussed
✓ Discussion on answers

EE-232 Signals and Systems Page 2


3.0 Pre-Lab Tasks
Use of Matlab to Flip, Compress and Expand an audio sequence in time:
i) Load the audio file titled “song.wav” using MATLAB. Play the audio file.
ii) Flip the audio sequence and play it back again. (Hint: The operation is time reversal).
iii) Next perform time compression on the flipped signal by a factor of 4. What does the audio
sequence sound like?
iv) Perform time expansion by a factor of 2. What does the flipped audio sequence sound like?
Hints: You may use the commands ‘fliplr’ ‘flipud’ for flipping the signal. Also the same can be achieved
using a for loop.

You will need to provide plots of the original, flipped, compressed and expanded audio signals.

### ANSWER STARTS HERE ###


ii)

iii)

EE-232 Signals and Systems Page 3


iv)

[y, Fs] = audioread("lab3_test_song.wav");


t = (0:(size(y)-1))*2/Fs;
lr = fliplr(y);
ud = flipud(y);

EE-232 Signals and Systems Page 4


subplot(4,1,1); plot(y)
title("original")
xlabel("time")
ylabel("amplitude")
subplot(4,1,2);plot(lr)
title("left right invert")
xlabel("time")
ylabel("amplitude")
subplot(4,1,3);plot(ud)
title("up down invert")
xlabel("time")
ylabel("amplitude")
subplot(4,1,4);plot(t,ud)
title("compressed")
xlabel("time")
ylabel("amplitude")

sound(ud,Fs/2);

### ANSWER ENDS HERE ###

3.1 Basic Transformation of Signals


a) Transformations of the Time Index for Discrete time signals:
i) Define a Matlab vector n such that -3 ≤n < 7, representing time/sample indices of a Discrete
Time Signal x[n] such that:

Enter the Matlab command(s) through which you defined this vector

ii) Make a generalized function that can shift the signal x[n] by delaying or advancing it by
a specified amount.
(Hint: You can make use of ‘find’ command in Matlab).
The inputs to the function should be the data and shift factor while output should be the
shifted signal. Please make sure that the indices accompanying the shifted signal are
correct.

### ANSWER STARTS HERE ###

EE-232 Signals and Systems Page 5


function[y1,shift_val] = shifter(y1, shift_val)
count = shift_val;
if shift_val > 0
while count
y1(2:length(y1)) = y1(1:length(y1)-1);
count = count -1;
end

elseif shift_val <0


while count
y1(1:length(y1)-1) = y1(2:length(y1));
count = count +1;
end
end
end

EE-232 Signals and Systems Page 6


### ANSWER ENDS HERE ###

b) Use of Matlab command zeros and ones:


iii) Implement the following function in Matlab. Your function should take k and n as input
from user.

𝑢[𝑘 − 𝑛] = {10 ,𝑘≥𝑛


,𝑘<𝑛

Attach a screenshot of the figure generated by your function for the input values of n = 4. Also copy
the function code to your report.

### ANSWER STARTS HERE ###

CODE:

EE-232 Signals and Systems Page 7


function[k,n] = unitstep(k,n)
arr = zeros(k,1);
for i = 1:size(arr)
if i >= n
arr(i) = 1;
end
end
plot(arr);
end

### ANSWER ENDS HERE ###

c) Even and Odd part of a signal:


iv) Every signal x[n] is sum of its even part and odd part. Given the signal x[n] find its even and
odd part.

1
𝑥𝑒 [𝑛] = [𝑥[𝑛] + 𝑥[−𝑛]]
2
1
𝑥𝑜 [𝑛] = [𝑥[𝑛] − 𝑥[−𝑛]]
2

Write a function that will produce three figures. The first figure shall show the signal x[n] given
above. The second figure shall show the signal xe[n]. The third figure shall show the signal xo[n].

You should copy the contents of the function code and the figures into the report.

### ANSWER STARTS HERE ###

EE-232 Signals and Systems Page 8


CODE:
function[x] = plot_eo(x)
odd = (1/2)*(x + flip(x))
even = (1/2)*(x - flip(x))

subplot(311);stem(x)
title("original discrete signal")
subplot(312);stem(odd)
title("odd sequence")
subplot(313);stem(even)
title("even sequence")
end

### ANSWER ENDS HERE ###

d) Image Transformation:
v) Load the file ‘image.jpg’ in Matlab. Display the image. Assuming that each row is a
separate signal perform time reversal of each row. Now display the image again.
Use the following commands:
▪ For loading the image: img=imread(‘filename.extension’);
▪ For displaying the image: imagesc(img);
▪ For finding the dimensions of the image: [height width channels]=size(img);
▪ To access each channel separately, try this: output(:,:,1)=input(:,:,1). This means
that we are copying rows and columns of the blue channel from input to output.

### ANSWER STARTS HERE ###

EE-232 Signals and Systems Page 9


IMAGE USED

This picture is of channel 1 extracted


CODE:
img = imread('ynwa.jpg');
imagesc(img);
[height,width,channel] = size(img);
output(:,:,1) = img(:,:,1);
imagesc(output);

EE-232 Signals and Systems Page 10


Time Reversed Image:

CODE;
img = imread('ynwa.jpg');
%imagesc(img);
[height,width,channels] = size(img)
output(:,:,:) = img(:,:,:);
for c = 1:channels
for row = 1:height
output(row,:,c) = fliplr(img(row,:,c));
end
end

figure
imagesc(output);
title('Time reversed image');

EE-232 Signals and Systems Page 11


### ANSWER ENDS HERE ###

3.2 Additional Tasks for Fun: Audio Signal based tasks:


i) Given an audio file and the functions developed above perform a mathematical operation
that will introduce echo in the song.
CODE:

[audio, Fs] = audioread("lab3_test_song.wav");


flipped_stereo = flipud(audio);
%converting to mono
flipped = mean(flipped_stereo, 2); %upon research the given signal was in stereo
form, which has separated channels for the audio
delay = 1.3;
attenuation = 0.4;
delay_samples = round(delay*Fs)
echo_signal = [zeros(delay_samples,1);flipped]; %produces a delayed version of the
song basically
padding = [flipped; zeros(delay_samples,1)]; %pads the original audio as the echoed
version is longer
output = padding + echo_signal;%adds the echo to og signal
output = output/max(abs(output));
sound(output, Fs);

ii) Assume that you want to develop a system that loads a song and gives you control to
forward or rewind the song. For this you should load the song. The total time of the song
can be calculated by dividing the Total No of Samples / Sampling Frequency. Next for
each second, load the duration of song, play it, check if the user wants to forward or
rewind the song by pressing ‘r’ or ‘f’ (getkeywait(seconds)) and modify the
samples being used to play the song.

CONCLUSION:

We learned how to plot and code scripts for even and odd functions. We learned how to handle audio
and image files and do operations like time shift and channel selection on them.

We learned how to transform images. We also made functions implementing time shift on discrete
signals

EE-232 Signals and Systems Page 12

You might also like