Zia Signals Lab Report
Zia Signals Lab Report
Zia Ur Rehman
Submitted to:
Ma’am Zainab
Registration No:
FA22-EEE-045
Date:
April 23,2024
Lab report #1
Question No. 1
Evaluate the following expressions in MATLAB without using the element by element operator.
(using for Loop). Take absolute value of the resulting signal (abs()), and plot both these signals in
same figure (use subplot()).
a. x(t) = 2cos(10πt).sin(16π t)
Matlab
t = 0:0.01:2;
x = 2*cos(10*pi*t).*sin(16*pi*t);
figure;
subplot(2,1,1)
plot(t,x)
title('x(t) = 2cos(10πt).sin(16π t)')
xlabel('t')
ylabel('x(t)')
subplot(2,1,2)
plot(t,abs(x))
title('abs(x(t))')
xlabel('t')
ylabel('abs(x(t))')
b. x(t) = -2e^(0.5t)sin(2πt)
Matlab
t = 0:0.01:2;
x = -2*exp(0.5*t).*sin(2*pi*t);
figure;
subplot(2,1,1)
plot(t,x)
title('x(t) = -2e^(0.5t)sin(2πt)')
xlabel('t')
ylabel('x(t)')
subplot(2,1,2)
plot(t,abs(x))
title('abs(t(x))')
xlabel('t')
ylabel('abs(x(t))')
Matlab
summed_signal = zeros(1,length(x));
for k = 1:length(x)
summed_signal(k) = x(k);
end
Question No 2
What is the purpose of the following built-in MATLAB functions; give concise answers include
format of the functions, acceptableinputs and an example as well. (Use mathworks website or
MATLAB help) input(), pause(), sum(), num2str(), str2num(), subplot(), disp(), min(), length(), any(),
all(), xor(), find()
Answer
input()
Format: input(prompt)
Example:
pause()
Acceptable Inputs: A numeric value specifying the number of seconds to pause execution.
Example:
disp('Starting...');
pause(3);
sum()
Format: sum(A)
Example:
A = [1, 2, 3, 4, 5];
total = sum(A);
disp(['Sum of elements in A: ' num2str(total)]);
num2str()
Format: num2str(number)
Example:
num = 42;
str = num2str(num);
disp(['The number as a string: ' str]);
str2num()
Format: str2num(str)
Example:
str = '42';
num = str2num(str);
disp(['The string as a number: ' num2str(num)]);
subplot()
Purpose: To create and manage plots within a grid of subplots.
Acceptable Inputs: Integers m, n, and p specifying the number of rows, columns, and position of the
subplot, or a single integer mnp where m specifies the number of rows, n specifies the number of
columns, and p specifies the subplot position.
Example:
x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);
subplot(2, 1, 1);
plot(x, y1);
title('Sin(x)');
subplot(2, 1, 2);
plot(x, y2);
title('Cos(x)');
disp()
Format: disp(expr)
Example:
name = 'John';
disp(['Hello, ' name '!']);
min()
Example:
A = [3, 1, 4, 1, 5, 9];
minValue = min(A);
disp(['Minimum value in A: ' num2str(minValue)]);
length()
Example:
A = [1, 2, 3, 4, 5];
len = length(A);
disp(['Length of A: ' num2str(len)]);
any()
Example:
A = [0, 0, 1, 0, 0];
hasNonZero = any(A);
disp(['Does A contain any nonzero element? ' num2str(hasNonZero)]);
all()
Example:
A = [1, 1, 1, 1, 1];
allNonZero = all(A);
disp(['Are all elements of A nonzero? ' num2str(allNonZero)]);
xor()
Format: xor(A, B)
Example:
A = [true, false, true];
B = [true, true, false];
result = xor(A, B);
disp(['Result of XOR operation: ' num2str(result)]);
find()
Example
A = [0, 3, 0, 7, 0];
indices = find(A);
disp(['Indices of nonzero elements: ' num2str(indices)]);
Question No 3
Construct a sound signal that consists of a sequence of half second sinusoids with exponentially
494, 494, and 494. Mathematically these tones are represented as, x(t) = 𝑠𝑖𝑛(2𝜋ft) ∀ t ∈ [0, 0.5] Do
decaying envelopes, as in the previous lab, but with a sequence of frequencies: 494, 440, 392, 440,
you recognize the sound? In your lab report, give the MATLAB commands that produce the sound.
a. Do the above by concatenating the vectors (x494, x440 … , x494).
b. Now modify your code such that, the frequencies and the time duration for these tones are entered
by the user. Use a sample rate (Fs) of 8,000 samples/second. The maximum number of input
frequencies should not exceed 8. Using functions, loops and conditional statements is necessary. The
output of this function should be:
i. A plot containing only one cycle of each signal, plotted separately in a figure (using subplot).
Hint: use signal frequency and time vector to compute length such that it plots only one cycle
ii. An audio played after execution of the code.
iii. An audio file saved in the directory (*.wav or *.mp3 file format) Note: Help from sources [1]
and [2] was used besides other online resources for this Lab.
i. close all;clear all;clc
Fs = 8000; % Sample rate
duration = 0.5; % Duration of each tone
t = 0:1/Fs:duration; % Time vector
frequencies = [494, 440, 392, 440, 494, 494, 494]; % Sequence of frequencies
x = []; % Initialize signal
for f = frequencies
tone = sin(2*pi*f*t); % Generate sinusoidal tone
envelope = exp(-(0:1:length(tone)-1)/(Fs*0.1)); % Exponentially decaying envelope
tone = tone .* envelope; % Apply envelope to tone
x = [x, tone]; % Concatenate tones
end
for i = 1:length(frequencies)
subplot(length(frequencies), 1, i);
plot(t, x((i-1)*length(t)+1:i*length(t)));
xlabel('Time (s)');
ylabel('Amplitude');
title(['Frequency: ' num2str(frequencies(i)) ' Hz']);
end
% Play audio
sound(x, Fs);
% Save audio file
filename = 'sound_signal.wav';
audiowrite(filename, x, Fs);
% Sample rate
Fs = 8000;
% Time vector
t = 0:1/Fs:duration;
% Play audio
sound(signal, Fs);