Matlab Task 2
Matlab Task 2
Part a Code:
% Define time vector from 0 to 2 seconds
t = linspace(0, 2, 1000); % Assuming 1000 samples for smooth plot
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));
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:
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:**
3. **sum()**
- **Format:** `total = sum(vector)`
- **Purpose:** Calculates the sum of elements in a vector or array.
- **Acceptable Inputs:** Numeric vector 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:**
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:**
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: