Question No1:
Part a Code:
% Define time vector from 0 to 2 seconds
t = linspace(0, 2, 1000); % Assuming 1000 samples for smooth plot
% Initialize signal vector
x_a = zeros(size(t));
% Evaluate the expression using a for loop
for i = 1:length(t)
x_a(i) = 2 * cos(10*pi*t(i)) * sin(16*pi*t(i));
end
% Take the absolute value of the resulting signal
x_a_abs = abs(x_a);
% Plotting both signals in the same figure using subplot
figure;
subplot(2, 1, 1); % First subplot for the original signal
plot(t, x_a);
title('Original Signal x(t)');
xlabel('Time (seconds)');
ylabel('Amplitude');
subplot(2, 1, 2); % Second subplot for the absolute value signal
plot(t, x_a_abs);
title('Absolute Value of x(t)');
xlabel('Time (seconds)');
ylabel('Amplitude');
% Adjust subplot spacing manually
subplot(2, 1, 1);
pos = get(gca, 'Position'); % Get position of the first subplot
pos(4) = pos(4) - 0.05; % Reduce height of the first subplot
set(gca, 'Position', pos); % Set the new position for the first subplot
subplot(2, 1, 2);
pos = get(gca, 'Position'); % Get position of the second subplot
pos(2) = pos(2) + 0.05; % Increase vertical position of the second subplot
pos(4) = pos(4) - 0.05; % Reduce height of the second subplot
set(gca, 'Position', pos); % Set the new position for the second subplot
Part b Code:
% Define time vector from 0 to 2 seconds (assuming the same time vector as before)
% No need to redefine t if it's already defined in the previous part
% Initialize signal vector
x_b = zeros(size(t));
% Evaluate the expression using a for loop
for i = 1:length(t)
x_b(i) = -2 * exp(0.5*t(i)) * sin(2*pi*t(i));
end
% Take the absolute value of the resulting signal
x_b_abs = abs(x_b);
% Plotting both signals in the same figure using subplot
figure;
subplot(2, 1, 1); % First subplot for the original signal
plot(t, x_b);
title('Original Signal x(t)');
xlabel('Time (seconds)');
ylabel('Amplitude');
subplot(2, 1, 2); % Second subplot for the absolute value signal
plot(t, x_b_abs);
title('Absolute Value of x(t)');
xlabel('Time (seconds)');
ylabel('Amplitude');
% Adjust subplot spacing manually
subplot(2, 1, 1);
pos = get(gca, 'Position'); % Get position of the first subplot
pos(4) = pos(4) - 0.05; % Reduce height of the first subplot
set(gca, 'Position', pos); % Set the new position for the first subplot
subplot(2, 1, 2);
pos = get(gca, 'Position'); % Get position of the second subplot
pos(2) = pos(2) + 0.05; % Increase vertical position of the second subplot
pos(4) = pos(4) - 0.05; % Reduce height of the second subplot
set(gca, 'Position', pos); % Set the new position for the second subplot
Part C Code:
% Define a sample data matrix x (replace with your actual data)
x = [1, 2, 3; 4, 5, 6; 7, 8, 9];
% Given data matrix x with size NxM
N = size(x, 1);
M = size(x, 2);
% Initialize summed_signal matrix
summed_signal = zeros(1, M);
% Calculate summed_signal using a for loop
for k = 1:M
for i = 1:N
summed_signal(1, k) = summed_signal(1, k) + x(i, k);
end
end
% Display the result
disp('Summed Signal:');
disp(summed_signal);
Question No 02
Here's a concise explanation of the purpose of each built-in MATLAB function, including their
format, acceptable inputs, and an example:
1. **input()**
- **Format:** `variable = input(prompt)`
- **Purpose:** Reads user input from the command window and assigns it to a variable.
- **Acceptable Inputs:** Any valid MATLAB expression or data.
- **Example:**
name = input('Enter your name: ', 's');
disp(['Hello, ' name '!']);
2. **pause()**
- **Format:** `pause(duration)`
- **Purpose:** Pauses the execution of a script or function for a specified duration (in
seconds).
- **Acceptable Inputs:** Positive numeric value representing the duration in seconds.
- **Example:**
disp('This message will appear immediately.');
pause(3); % Pauses execution for 3 seconds
disp('This message will appear after 3 seconds.');
3. **sum()**
- **Format:** `total = sum(vector)`
- **Purpose:** Calculates the sum of elements in a vector or array.
- **Acceptable Inputs:** Numeric vector or array.
- **Example:**
data = [1, 2, 3, 4, 5];
total = sum(data); % total will be 15
4. **num2str()**
- **Format:** `str = num2str(number)`
- **Purpose:** Converts a numeric value to a string.
- **Acceptable Inputs:** Numeric value or array.
- **Example:**
value = 123;
str = num2str(value); % str will be '123'
5. **str2num()**
- **Format:** `number = str2num(string)`
- **Purpose:** Converts a string to a numeric value.
- **Acceptable Inputs:** String representing a numeric value.
- **Example:**
str = '456';
value = str2num(str); % value will be 456
6. **subplot()**
- **Format:** `subplot(m, n, p)`
- **Purpose:** Creates a subplot grid and selects a specific subplot for plotting.
- **Acceptable Inputs:** Positive integers `m`, `n`, and `p` specifying the grid and subplot
position.
- **Example:**
subplot(2, 2, 3); % Creates a 2x2 grid and selects the third subplot
7. **disp()**
- **Format:** `disp(expression)`
- **Purpose:** Displays the value of an expression or variable in the command window.
- **Acceptable Inputs:** Any MATLAB expression or variable.
- **Example:**
disp('Hello, MATLAB!'); % Displays 'Hello, MATLAB!'
8. **min()**
- **Format:** `min(array)`
- **Purpose:** Finds the minimum value in an array.
- **Acceptable Inputs:** Numeric array.
- **Example:**
values = [10, 5, 8, 3];
minValue = min(values); % minValue will be 3
9. **length()**
- **Format:** `len = length(vector)`
- **Purpose:** Calculates the length of a vector or array.
- **Acceptable Inputs:** Vector or array.
- **Example:**
data = [1, 2, 3, 4, 5];
dataLength = length(data); % dataLength will be 5
10. **any()**
- **Format:** `tf = any(vector)`
- **Purpose:** Checks if any element in a logical vector is true.
- **Acceptable Inputs:** Logical vector.
- **Example:**
logicData = [true, false, true];
hasTrue = any(logicData); % hasTrue will be true
11. **all()**
- **Format:** `tf = all(vector)`
- **Purpose:** Checks if all elements in a logical vector are true.
- **Acceptable Inputs:** Logical vector.
- **Example:**
logicData = [true, false, true];
allTrue = all(logicData); % allTrue will be false
12. **xor()**
- **Format:** `result = xor(a, b)`
- **Purpose:** Computes the exclusive OR (XOR) operation between two logical values or
arrays.
- **Acceptable Inputs:** Logical values or arrays.
- **Example:**
a = true;
b = false;
result = xor(a, b); % result will be true
13. find()
- Format: `indices = find(vector)`
- Purpose:Finds the indices of nonzero elements in a vector.
- Acceptable Inputs: Numeric vector.
- Example:
data = [0, 3, 0, 7, 0];
nonzeroIndices = find(data); % nonzeroIndices will be [2, 4]
Question No 03
3. Construct a sound signal that consists of a sequence of half second sinusoids with
exponentially decaying envelopes, as in the previous lab, but with a sequence of frequencies:
494, 440, 392, 440, 494, 494, and 494. Mathematically these tones are represented as,
x(t)=sin(2πft) Vt E [0,0.5]
Do 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.
Solution: