n=[-3,-2,-1,0,1,2,3,4]
x=[2,1,-1,0,1,4,3,7]
n=
Columns 1 through 6
-3 -2 -1 0 1 2
Columns 7 through 8
3 4
x=
Columns 1 through 6
2 1 -1 0 1 4
Columns 7 through 8
3 7
1. Unit Sample Sequence
Function:
% Function to generate a unit sample sequence δ(n - n0)
function [x, n] = impseq(n1, n2, n0)
n = n1:n2; % Define the range of n
x = (n == n0); % Create impulse at n0 (1 at n0, 0 elsewhere)
end
% Display grid for better visualization
Code:
% Example usage
[x, n] = impseq(-5, 5, 2); % Generate impulse at n = 2 over range [-5,5]
stem(n, x, 'filled'); % Plot impulse sequence
xlabel('n'); % Label x-axis
ylabel('Amplitude'); % Label y-axis
title('Unit Sample Sequence'); % Title of the plot
grid on;
2. Unit Step Sequence
Function:
% Function to generate a unit step sequence u(n - n0)
function [x, n] = stepseq(n1, n2, n0)
n = n1:n2; % Define the range of n
x = (n >= n0); % Create step function (1 for n >= n0, 0 elsewhere)
end
Code:
% Example usage
[x, n] = stepseq(-5, 5, -1); % Generate step sequence starting at n = -1
stem(n, x, 'filled'); % Plot step sequence
xlabel('n'); % Label x-axis
ylabel('Amplitude'); % Label y-axis
title('Unit Step Sequence'); % Title of the plot
grid on; % Display grid for better visualization
3.Real-Valued Exponential Sequence
% Generate and plot a real-valued exponential sequence x(n) = (0.9)^n
n = 0:10; % Define the range of n
x = (0.9).^n; % Compute the exponential values
stem(n, x, 'filled'); % Plot the exponential sequence
xlabel('n'); % Label x-axis
ylabel('Amplitude'); % Label y-axis
title('Real-Valued Exponential Sequence'); % Title of the plot
grid on; % Display grid for better visualization
4. Complex-Valued Exponential Sequence
% Generate and plot a complex-valued exponential sequence x(n) = exp((2+j3)n)
n = 0:10; % Define the range of n
x = exp((2+3j)*n); % Compute the complex exponential values
% Plot real and imaginary parts separately
subplot(2,1,1);
stem(n, real(x), 'filled'); % Plot real part
xlabel('n');
ylabel('Amplitude');
title('Real Part of Complex Exponential');
grid on;
subplot(2,1,2);
stem(n, imag(x), 'filled'); % Plot imaginary part
xlabel('n');
ylabel('Amplitude');
title('Imaginary Part of Complex Exponential');
grid on;
5. Sinusoidal Sequence
Code:
% Generate and plot a sinusoidal sequence
n = 0:10; % Define the range of n
x = 3*cos(0.1*pi*n + pi/3) + 2*sin(0.5*pi*n); % Compute the sinusoidal sequence
stem(n, x, 'filled'); % Plot the sinusoidal sequence
xlabel('n'); % Label x-axis
ylabel('Amplitude'); % Label y-axis
title('Sinusoidal Sequence'); % Title of the plot
grid on; % Display grid for better visualization
6. Periodic Sequence
Code:
function xtilde = periodic_seq(x, P)
xtilde = repmat(x, 1, P); % Repeats x for P periods
end
x = [1, 2, 3];
xtilde = periodic_seq(x, 3);
stem(0:length(xtilde)-1, xtilde);
Operations on Sequences
7. Signal Addition
Function:
% Function to add two signals x1(n1) and x2(n2)
function [x, n] = sigadd(x1, n1, x2, n2)
n = min(n1(1), n2(1)):max(n1(end), n2(end)); % Define common index range
x1_new = zeros(size(n)); % Zero-padding
x2_new = zeros(size(n));
x1_new(ismember(n, n1)) = x1; % Align x1
x2_new(ismember(n, n2)) = x2; % Align x2
x = x1_new + x2_new; % Perform sample-wise addition
end
Code:
% Example Usage:
[x1, n1] = impseq(-5, 5, 4);
[x2, n2] = impseq(-5, 5, -2);
[x, n] = sigadd(x1, n1, x2, n2);
stem(n, x);
title('Signal Addition');
xlabel('n'); ylabel('x(n)');
grid on;
8. Signal Multiplication
Function:
% Function to multiply two signals x1(n1) and x2(n2)
function [x, n] = sigmult(x1, n1, x2, n2)
n = min(n1(1), n2(1)):max(n1(end), n2(end)); % Common index range
x1_new = zeros(size(n)); % Zero-padding
x2_new = zeros(size(n));
x1_new(ismember(n, n1)) = x1; % Align x1
x2_new(ismember(n, n2)) = x2; % Align x2
x = x1_new .* x2_new; % Perform sample-wise multiplication
end
Code:
% Example Usage:
[x1, n1] = impseq(-5, 5, 4);
[x2, n2] = impseq(-5, 5, -2);
[x, n] = sigmult(x1, n1, x2, n2);
stem(n, x);
title('Signal Multiplication');
xlabel('n'); ylabel('x(n)');
grid on;
9. Signal Shifting
Function:
% Function to shift signal x(n) by k steps
function [y, n] = sigshift(x, n, k)
n = n + k; % Shift indices by k
y = x; % Maintain same signal values
end
Code:
% Example Usage:
[x, n] = impseq(-5, 5, 0);
[y, n] = sigshift(x, n, 2);
stem(n, y);
title('Shifted Signal');
xlabel('n'); ylabel('x(n)');
grid on;
10. Signal Folding (Time Reversal)
Function:
% Function to fold (reverse) a signal around n = 0
function [yf, nf] = sig_reverse(x, n)
% Ensure x is a row vector before flipping
x = x(:)'; % Convert to row vector if not already
% Flip the sequence values
yf = fliplr(x);
% Flip the time indices
nf = -fliplr(n);
end
Code:
% Example Usage:
[x, n] = impseq(-5, 5, 2); % Generate impulse δ(n-2)
[yf, nf] = sig_reverse(x, n); % Fold the signal around n=0
% Plot Original Signal
subplot(2,1,1);
stem(n, x, 'filled');
title('Original Signal x(n)');
xlabel('n'); ylabel('x(n)');
grid on;
% Plot Folded Signal
subplot(2,1,2);
stem(nf, yf, 'filled');
title('Folded Signal y(n) = x(-n)');
xlabel('n'); ylabel('y(n)');
grid on;
11. Signal Energy Calculation
Function:
% Function to compute the energy of a discrete-time signal
function E = sigenergy(x)
E = sum(abs(x).^2); % Compute signal energy
End
% Example Usage:
x = [1, 2, 3, 4, 5]; % Example signal
E = sigenergy(x);
disp(['Signal Energy: ', num2str(E)]);
Result:
Signal Energy: 55
clc; clear; close all;
% Define the range of n
n = 0:10;
% Generate impulse sequences
x = 3*(n == 3) - (n == 6);
% Plot the sequence
stem(n, x, 'filled');
xlabel('n'); ylabel('Amplitude');
title('x[n] = 3\delta[n-3] - \delta[n-6]');
grid on;
MAIN LAB
2A: Generate and Plot the Following Sequences
a) x [ n ] =3 δ [ n−3 ] −δ [ n−6 ], 0 ≤ n ≤1
Code:
clc; clear; close all;
% Define the range of n
n = 0:10;
% Generate impulse sequences
x = 3*(n == 3) - (n == 6);
% Plot the sequence
stem(n, x, 'filled');
xlabel('n'); ylabel('Amplitude');
title('x[n] = 3\delta[n-3] - \delta[n-6]');
grid on;
b) x [ n ] =n { u [ n ] −u [ n−10 ] }+ 10 e−0.3(n−10) {u [ n−10 ] −u[n−20 ]} , 0 ≤ n ≤20
Code:
clc; clear; close all;
% Define the range of n
n = 0:20;
% Generate unit step functions
u1 = (n >= 0) - (n >= 10); % u[n] - u[n-10]
u2 = (n >= 10) - (n >= 20); % u[n-10] - u[n-20]
% Define the sequence
x = n .* u1 + 10 * exp(-0.3 * (n - 10)) .* u2;
% Plot the sequence
stem(n, x, 'filled');
xlabel('n'); ylabel('Amplitude');
title('x[n] = n(u[n] - u[n-10]) + 10e^{-0.3(n-10)}(u[n-10] - u[n-20])');
grid on;
c) ~ x [ n ] ={…5 , 4 , 3 ,2 , 1 ,5 , 4 ,3 , 2 , 1, 5 , 4 ,3 , 2 ,1 , … . } −10 ≤ n ≤9
Code:
% Define the repeating pattern
x_period = [5 4 3 2 1];
% Generate the periodic sequence from -10 to 9
n = -10:9;
x_tilde = repmat(x_period, 1, ceil(length(n)/length(x_period)));
x_tilde = x_tilde(1:length(n)); % Trim to fit the range
% Plot the sequence
stem(n, x_tilde, 'filled');
xlabel('n'); ylabel('Amplitude');
title('Periodic Sequence x̃ [n]');
grid on;
2B: Let x(n) = {−1 , 2 ,3 , 4 ,5 , 6 , 7 , 6 , 5 , 4 , 3 ,−2 , 1} Determine and plot the following
sequences
CODE:
clc; clear; close all; % Clear workspace and close all figures
% ---- Define the original sequence x(n) ----
n = -6:6; % Given time indices
x = [-1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, -2, 1]; % Given signal x(n)
% ---- Compute x1[n] = 2x[n-5] - 3x[n+4] ----
[x_shift1, n_shift1] = sigshift(x, n, 5); % x[n-5]
[x_shift2, n_shift2] = sigshift(x, n, -4); % x[n+4]
x1 = 2 * x_shift1 - 3 * x_shift2; % Compute x1[n]
n1 = n_shift1; % Since both signals share the same index range
% ---- Compute x2[n] = 4x[5-n] - 3x[n+3] ----
[x_fold, n_fold] = sig_reverse(x, n); % x[-n]
[x_shift3, n_shift3] = sigshift(x, n, -3); % x[n+3]
x2 = 4 * sigshift(x_fold, n_fold, 5) - 3 * x_shift3; % Compute x2[n]
n2 = n_shift3; % Index range of x2
% ---- Plot all sequences ----
figure;
% Original Signal
subplot(3,1,1);
stem(n, x, 'filled');
title('Original Sequence x(n)');
xlabel('n'); ylabel('x(n)');
grid on;
% Plot x1[n]
subplot(3,1,2);
stem(n1, x1, 'filled');
title('Sequence x_1[n] = 2x[n-5] - 3x[n+4]');
xlabel('n'); ylabel('x_1(n)');
grid on;
% Plot x2[n]
subplot(3,1,3);
stem(n2, x2, 'filled');
title('Sequence x_2[n] = 4x[5-n] - 3x[n+3]');
xlabel('n'); ylabel('x_2(n)');
grid on;
a) x 1 [ n ]=2 x [ n−5 ]−3 x [n+ 4]
b) x 2 [ n ] =4 x [ 5−n ]−3 x [n+last digit of your student ID]
Let last digit of your student ID=3
2C: Generate the complex-valued signal
(0.1+ 0.3 j)n
x [ n ] =e −10 ≤ n ≤10
and plot its magnitude, phase, the real part, and the imaginary part in four separate subplots.
Code:
clc; clear; close all; % Clear workspace and close all figures
% ---- Define n and the complex signal ----
n = -10:10; % Given range for n
x = exp((0.1 + 0.3j) * n); % Compute the complex-valued signal
% ---- Compute components ----
mag_x = abs(x); % Magnitude of x[n]
phase_x = angle(x); % Phase of x[n]
real_x = real(x); % Real part of x[n]
imag_x = imag(x); % Imaginary part of x[n]
% ---- Plot all components ----
figure;
% Plot Magnitude
subplot(2,2,1);
stem(n, mag_x, 'filled');
title('Magnitude of x[n]');
xlabel('n'); ylabel('|x[n]|');
grid on;
% Plot Phase
subplot(2,2,2);
stem(n, phase_x, 'filled');
title('Phase of x[n]');
xlabel('n'); ylabel('∠x[n]');
grid on;
% Plot Real Part
subplot(2,2,3);
stem(n, real_x, 'filled');
title('Real Part of x[n]');
xlabel('n'); ylabel('Re{x[n]}');
grid on;
% Plot Imaginary Part
subplot(2,2,4);
stem(n, imag_x, 'filled');
title('Imaginary Part of x[n]');
xlabel('n'); ylabel('Im{x[n]}');
grid on;
Magnitude plot:
Phase plot:
Real part:
Imaginary part: