0% found this document useful (0 votes)
8 views68 pages

matlab programs

matlab programs related to it

Uploaded by

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

matlab programs

matlab programs related to it

Uploaded by

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

ABSTRACT

The lab manual with the matlab


codes illustrating some of signals
and Systems topics

DoS in Electronics,
Hemagangothri,
Hassan.

Matlab codes
Lab manual
1a. Unit Sample Sequence
A unit sample sequence (also called a delta function) is defined as:
δ(n)={1,n=00,n≠0\delta(n) = \begin{cases} 1, & n = 0 \\ 0, & n \neq 0 \
end{cases}δ(n)={1,0,n=0n=0
MATLAB Code:
clc; clear; close all;
n = -10:10; % Define the range
x = (n == 0); % Unit sample sequence

stem(n, x, 'filled'); % Plot the sequence


xlabel('n'); ylabel('Amplitude');
title('Unit Sample Sequence');
grid on;

Unit Sample Sequence


1

0.9

0.8

0.7
Amplitude

0.6

0.5

0.4

0.3

0.2

0.1

0
-10 -8 -6 -4 -2 0 2 4 6 8 10
n
1b. Unit Step sequence
clc; clear; close all;

n = -10:10; % Define the range

x = (n >= 0); % Unit step sequence

stem(n, x, 'filled');

xlabel('n'); ylabel('Amplitude');

title('Unit Step Sequence');

grid on;

Unit Step Sequence


1

0.9

0.8

0.7

0.6
Amplitude

0.5

0.4

0.3

0.2

0.1

0
-10 -8 -6 -4 -2 0 2 4 6 8 10
n
Ramp Sequence
A ramp sequence is defined as:

r(n)={n,n≥00,n<0r(n) = \begin{cases} n, & n \geq 0 \\ 0, & n < 0 \end{cases}r(n)={n,0,n≥0n<0

MATLAB Code:

clc; clear; close all;

n = -10:10; % Define the range

x = (n >= 0) .* n; % Ramp function

stem(n, x, 'filled');

xlabel('n'); ylabel('Amplitude');

title('Ramp Sequence');

grid on;

Ramp Sequence
10

6
Amplitude

0
-10 -8 -6 -4 -2 0 2 4 6 8 10
n
Real & Complex Exponential Sequence
A real exponential sequence is given by:

x(n)=Aeαnx(n) = A e^{\alpha n}x(n)=Aeαn

A complex exponential sequence is given by:

x(n)=Aejωn=A(cos⁡(ωn)+jsin⁡(ωn))x(n) = A e^{j\omega n} = A (\cos(\omega n) + j\sin(\omega


n))x(n)=Aejωn=A(cos(ωn)+jsin(ωn))

MATLAB Code:

clc; clear; close all;

n = -10:10; % Define range

A = 1; % Amplitude

alpha = 0.2; % Growth/Decay factor for real exponential

omega = pi/4; % Frequency for complex exponential

% Real Exponential

x_real = A * exp(alpha * n);

% Complex Exponential

x_complex = A * exp(1j * omega * n);

% Plot Real Exponential

subplot(2,1,1);

stem(n, x_real, 'filled');

xlabel('n'); ylabel('Amplitude');

title('Real Exponential Sequence');

grid on;

% Plot Complex Exponential (Real and Imaginary Parts)

subplot(2,1,2);
stem(n, real(x_complex), 'r', 'filled'); hold on;

stem(n, imag(x_complex), 'b', 'filled');

xlabel('n'); ylabel('Amplitude');

title('Complex Exponential Sequence (Red: Real, Blue: Imaginary)');

grid on; legend('Real Part', 'Imaginary Part');

Real Exponential Sequence


8

6
Amplitude

0
-10 -8 -6 -4 -2
0 2 4 6 8 10
n
Complex Exponential Sequence (Red: Real, Blue: Imaginary)
1
Real Part
0.5
Amplitude

Imaginary Part

-0.5

-1
-10 -8 -6 -4 -2 0 2 4 6 8 10
n
Sinusoidal Sequence
A sinusoidal sequence is given by:

x(n)=Asin⁡(ωn+ϕ)x(n) = A \sin(\omega n + \phi)x(n)=Asin(ωn+ϕ)

MATLAB Code:

clc; clear; close all;

n = 0:20; % Define range

A = 2; % Amplitude

omega = pi/8; % Frequency

phi = 0; % Phase shift

x = A * sin(omega * n + phi);

stem(n, x, 'filled');

xlabel('n'); ylabel('Amplitude');

title('Sinusoidal Sequence');

grid on;
Sinusoidal Sequence
2

1.5

0.5
Amplitude

-0.5

-1

-1.5

-2
0 2 4 6 8 10 12 14 16 18 20
n
Cosine Sequence
A cosine sequence is given by:

x(n)=Acos⁡(ωn+ϕ)x(n) = A \cos(\omega n + \phi)x(n)=Acos(ωn+ϕ)

MATLAB Code:

clc; clear; close all;

n = 0:20; % Define range

A = 2; % Amplitude

omega = pi/8; % Frequency

phi = 0; % Phase shift


x = A * cos(omega * n + phi);

stem(n, x, 'filled');

xlabel('n'); ylabel('Amplitude');

title('Cosine Sequence');

grid on;

Cosine Sequence
2

1.5

1
Amplitude

0.5

-0.5

-1

-1.5

-2
0 2 4 6 8 10 12 14 16 18 20
n
Periodic Sequence
A periodic sequence repeats itself after a fixed interval T:

x(n+T)=x(n)x(n + T) = x(n)x(n+T)=x(n)

MATLAB Code:

clc; clear; close all;

N = 20; % Total samples

T = 5; % Period

x = mod(0:N-1, T); % Periodic sequence

stem(0:N-1, x, 'filled');

xlabel('n'); ylabel('Amplitude');

title(['Periodic Sequence with Period T = ', num2str(T)]);

grid on;

Periodic Sequence with Period T = 5


4

3.5

2.5
Amplitude

1.5

0.5

0
0 2 4 6 8 10 12 14 16 18 20
n
Even & Odd Components of a Given Sequence
A discrete-time signal x(n)x(n)x(n) can be decomposed into even and odd components using:

xeven(n)=x(n)+x(−n)2x_{even}(n) = \frac{x(n) + x(-n)}{2}xeven(n)=2x(n)+x(−n) xodd(n)=x(n)


−x(−n)2x_{odd}(n) = \frac{x(n) - x(-n)}{2}xodd(n)=2x(n)−x(−n)

MATLAB Code:

clc; clear; close all;

n = -5:5; % Define range

x = [0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0]; % Example sequence

% Compute even and odd components

x_even = (x + fliplr(x)) / 2;

x_odd = (x - fliplr(x)) / 2;

% Plot original sequence

subplot(3,1,1);

stem(n, x, 'filled');

xlabel('n'); ylabel('Amplitude');

title('Original Sequence x(n)');

grid on;

% Plot Even Component

subplot(3,1,2);

stem(n, x_even, 'filled');

xlabel('n'); ylabel('Amplitude');

title('Even Component x_{even}(n)');

grid on;
% Plot Odd Component

subplot(3,1,3);

stem(n, x_odd, 'filled');

xlabel('n'); ylabel('Amplitude');

title('Odd Component x_{odd}(n)');

grid on;

Original Sequence x(n)


Amplitude

-5
-5 -4 -3 -2 -1 0 1 2 3 4 5
n
Even Component x even(n)
Amplitude

-5
-5 -4 -3 -2 -1 0 1 2 3 4 5
n
Odd Component x odd(n)
Amplitude

-1
-5 -4 -3 -2 -1 0 1 2 3 4 5
n
Amplitude Scaling
Amplitude scaling involves multiplying a sequence x(n)x(n)x(n) by a scalar AAA:

y(n)=A⋅x(n)y(n) = A \cdot x(n)y(n)=A⋅x(n)

MATLAB Code:

clc; clear; close all;

n = -5:5;

x = [0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0]; % Example sequence

A = 2; % Scaling factor Original Sequence x(n)


6

4
Amplitude

y = A * x; % Amplitude scaling
2

% Plot original and scaled sequences 0


-5 -4 -3 -2 -1 0 1 2 3 4 5
n
subplot(2,1,1); Amplitude Scaled Sequence (A = 2)
10
stem(n, x, 'filled');

xlabel('n'); ylabel('Amplitude');
Amplitude

5
title('Original Sequence x(n)');

grid on; 0
-5 -4 -3 -2 -1 0 1 2 3 4 5
n

subplot(2,1,2);

stem(n, y, 'filled');

xlabel('n'); ylabel('Amplitude');

title(['Amplitude Scaled Sequence (A = ', num2str(A), ')']);

grid on;

Time Scaling
Time scaling is not straightforward in discrete signals since we can’t interpolate values
arbitrarily. Instead, we perform time compression (down-sampling) and time expansion
(up-sampling).

MATLAB Code:
clc; clear; close all;

n = -5:5;

x = [0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0]; % Example sequence

% Time Compression (Down-Sampling)

compression_factor = 2; % Take every 2nd sample

n_compressed = n(1:compression_factor:end);

x_compressed = x(1:compression_factor:end);

% Time Expansion (Up-Sampling)

expansion_factor = 2;

n_expanded = n(1):1/expansion_factor:n(end);

x_expanded = interp1(n, x, n_expanded, 'linear'); % Linear interpolation

% Plot original sequence

subplot(3,1,1);

stem(n, x, 'filled');

xlabel('n'); ylabel('Amplitude');

title('Original Sequence x(n)');

grid on;

% Plot compressed sequence

subplot(3,1,2);

stem(n_compressed, x_compressed, 'filled');

xlabel('n'); ylabel('Amplitude');

title('Compressed (Down-Sampled) Sequence');

grid on;
% Plot expanded sequence

subplot(3,1,3);

stem(n_expanded, x_expanded, 'filled');

xlabel('n'); ylabel('Amplitude');

title('Expanded (Up-Sampled) Sequence');

grid on;

Original Sequence x(n)


Amplitude

0
-5 -4 -3 -2 -1 0 1 2 3 4 5
n
Compressed (Down-Sampled) Sequence
Amplitude

0
-5 -4 -3 -2 -1 0 1 2 3 4 5
n
Expanded (Up-Sampled) Sequence
Amplitude

0
-5 -4 -3 -2 -1 0 1 2 3 4 5
n

Up Sampling
Up sampling inserts zeros between samples to increase the rate.

MATLAB Code:

clc; clear; close all;

n = 0:5;

x = [1, 2, 3, 4, 5, 6]; % Example sequence

L = 3; % Up-sampling factor
y = zeros(1, L*length(x)); % Initialize output

y(1:L:end) = x; % Insert values

n_new = 0:length(y)-1; % Adjusted time indices

stem(n_new, y, 'filled');

xlabel('n'); ylabel('Amplitude');

title(['Up-Sampled Sequence (L = ', num2str(L), ')']);

grid on;

Up-Sampled Sequence (L = 3)
6

2
Amplitude

-2

-4

-6
0 2 4 6 8 10 12 14 16 18
n

Down Sampling
Down sampling reduces the number of samples by keeping every M-th sample.

MATLAB Code:

clc; clear; close all;


n = 0:20;

x = sin(0.2 * pi * n); % Example sequence

M = 3; % Down-sampling factor

y = x(1:M:end); % Pick every M-th sample

n_new = n(1:M:end); % Adjusted time indices

stem(n_new, y, 'filled');

xlabel('n'); ylabel('Amplitude');

title(['Down-Sampled Sequence (M = ', num2str(M), ')']);

grid on;

Down-Sampled Sequence (M = 3)
1

0.8

0.6

0.4

0.2
Amplitude

-0.2

-0.4

-0.6

-0.8

-1
0 2 4 6 8 10 12 14 16 18
n
Time Shifting
Shifting a sequence by kkk is given by:

y(n)=x(n−k)y(n) = x(n - k)y(n)=x(n−k)

MATLAB Code:

clc; clear; close all;

n = -5:5;

x = [0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0]; % Example sequence

k = 3; % Shift amount

n_shifted = n + k; % Shift the time indices

stem(n_shifted, x, 'filled');

xlabel('n'); ylabel('Amplitude');

title(['Time-Shifted Sequence (k = ', num2str(k), ')']);

grid on;

Time-Shifted Sequence (k = 3)
5

4.5

3.5

3
Amplitude

2.5

1.5

0.5

0
-2 -1 0 1 2 3 4 5 6 7 8
n
Folding (Time Reversal)
Folding (time reversal) flips the sequence about n=0n = 0n=0:

y(n)=x(−n)y(n) = x(-n)y(n)=x(−n)

MATLAB Code:

clc; clear; close all;

n = -5:5;

x = [0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0]; % Example sequence

n_folded = -fliplr(n); % Reverse time indices

x_folded = fliplr(x); % Reverse sequence values

stem(n_folded, x_folded, 'filled');

xlabel('n'); ylabel('Amplitude');

title('Time-Reversed (Folded) Sequence');

grid on;

Time-Reversed (Folded) Sequence


9

6
Amplitude

0
-5 -4 -3 -2 -1 0 1 2 3 4 5
n
Addition of Two Sequences
Addition of two signals:

y(n)=x1(n)+x2(n)y(n) = x_1(n) + x_2(n)y(n)=x1(n)+x2(n)

MATLAB Code:

clc; clear; close all;

n = 0:5;

x1 = [4, 3, 3, 5, -5, -6];

x2 = [6, 5, 4, 3, 2, 1];

y = x1 + x2; % Element-wise addition

stem(n, y, 'filled');

xlabel('n'); ylabel('Amplitude');

title('Addition of Two Sequences');

grid on;

Addition of Two Sequences


10

5
Amplitude

-5
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
n
Subtraction of Two Sequences
Subtraction of signals:

y(n)=x1(n)−x2(n)y(n) = x_1(n) - x_2(n)y(n)=x1(n)−x2(n)

MATLAB Code:

clc; clear; close all;

n = 0:5;

x1 = [1, 2, 3, 4, 5, 6];

x2 = [6, 5, 4, 3, 2, 1];

y = x1 - x2; % Element-wise subtraction

stem(n, y, 'filled');

xlabel('n'); ylabel('Amplitude');

title('Subtraction of Two Sequences');grid on;

Subtraction of Two Sequences


5

1
Amplitude

-1

-2

-3

-4

-5
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
n
Multiplication of Two Sequences
Element-wise multiplication:

y(n)=x1(n)⋅x2(n)y(n) = x_1(n) \cdot x_2(n)y(n)=x1(n)⋅x2(n)

MATLAB Code:

clc; clear; close all;

n = 0:5;

x1 = [1, 2, 3, 4, 5, 6];

x2 = [6, 5, 4, 3, 2, 1];

y = x1 .* x2; % Element-wise multiplication

stem(n, y, 'filled');

xlabel('n'); ylabel('Amplitude');

title('Multiplication of Two Sequences');

grid on;

Multiplication of Two Sequences


12

10

8
Amplitude

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
n
Multiplication of Periodic & Aperiodic Signals
A periodic signal xp(n)x_p(n)xp(n) and an aperiodic signal xa(n)x_a(n)xa(n) are multiplied:

y(n)=xp(n)⋅xa(n)y(n) = x_p(n) \cdot x_a(n)y(n)=xp(n)⋅xa(n)

MATLAB Code:

clc; clear; close all;

n = 0:10;

x_p = sin(0.4 * pi * n); % Periodic signal (sine wave)

x_a = [1, 2, 3, 4, 5, 4, 3, 2, 1, 0, 0]; % Aperiodic sequence

y = x_p .* x_a; % Multiplication

subplot(3,1,1);

stem(n, x_p, 'filled');

xlabel('n'); ylabel('Amplitude');

title('Periodic Signal x_p(n)');

grid on;

subplot(3,1,2);

stem(n, x_a, 'filled');

xlabel('n'); ylabel('Amplitude');

title('Aperiodic Signal x_a(n)');

grid on;

subplot(3,1,3);

stem(n, y, 'filled');

xlabel('n'); ylabel('Amplitude');

title('Multiplication of Periodic & Aperiodic Signals');

grid on;
Periodic Signal x p(n)
Amplitude

-1
0 1 2 3 4 5 6 7 8 9 10
n
Aperiodic Signal x a(n)
Amplitude

0
0 1 2 3 4 5 6 7 8 9 10
n
Multiplication of Periodic & Aperiodic Signals
Amplitude

-5
0 1 2 3 4 5 6 7 8 9 10
n

Verify the Sampling


Theorem in MATLAB
Theory: Sampling Theorem

The Nyquist Sampling Theorem states that a continuous-time signal x(t)x(t)x(t) can be
perfectly reconstructed from its samples if it is band-limited and sampled at a rate fs≥2fmf_s
\geq 2f_mfs≥2fm, where fmf_mfm is the highest frequency component of the signal.

 If fs<2fmf_s < 2f_mfs<2fm (Under-sampling), aliasing occurs.

 If fs=2fmf_s = 2f_mfs=2fm, it is the Nyquist rate.

 If fs>2fmf_s > 2f_mfs>2fm, it is over-sampling, and reconstruction is easy.

MATLAB Code

clc; clear; close all;

% Define continuous-time signal

fm = 5; % Maximum frequency component (Hz)

t = 0:0.001:1; % Continuous time


x_t = cos(2*pi*fm*t); % Continuous signal

% Define sampling rates

fs1 = 2*fm; % Nyquist rate

fs2 = 1.5*fm; % Under-sampling (Aliasing)

fs3 = 4*fm; % Over-sampling

% Generate discrete samples

n1 = 0:1/fs1:1; x1 = cos(2*pi*fm*n1); % Nyquist sampling

n2 = 0:1/fs2:1; x2 = cos(2*pi*fm*n2); % Under-sampling

n3 = 0:1/fs3:1; x3 = cos(2*pi*fm*n3); % Over-sampling

% Plot the original continuous-time signal

subplot(3,1,1);

plot(t, x_t, 'k', 'LineWidth', 1.5); hold on;

stem(n1, x1, 'r', 'filled'); % Nyquist sampled

xlabel('Time (s)'); ylabel('Amplitude');

title('Sampling at Nyquist Rate (fs = 2fm)');

grid on;

subplot(3,1,2);

plot(t, x_t, 'k', 'LineWidth', 1.5); hold on;

stem(n2, x2, 'b', 'filled'); % Under-sampled

xlabel('Time (s)'); ylabel('Amplitude');

title('Under-Sampling (Aliasing, fs < 2fm)');

grid on;

subplot(3,1,3);
plot(t, x_t, 'k', 'LineWidth', 1.5); hold on;

stem(n3, x3, 'g', 'filled'); % Over-sampled

xlabel('Time (s)'); ylabel('Amplitude');

title('Over-Sampling (fs > 2fm)');

grid on;

Sampling at Nyquist Rate (fs = 2fm)


1

0.5
Amplitude

-0.5

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Time (s)
Under-Sampling (Aliasing, fs < 2fm)
1

0.5
Amplitude

-0.5

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Time (s)
Over-Sampling (fs > 2fm)
1

0.5
Amplitude

-0.5

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Time (s)

Find and Plot the Linear Convolution of Two Finite


Duration Sequences
Theory: Linear Convolution

Linear convolution of two sequences x(n)x(n)x(n) and h(n)h(n)h(n) is given by:

y(n)=x(n)∗h(n)=∑k=−∞∞x(k)h(n−k)y(n) = x(n) * h(n) = \sum_{k=-\infty}^{\infty} x(k) h(n-


k)y(n)=x(n)∗h(n)=k=−∞∑∞x(k)h(n−k)

In MATLAB, we can compute this using the conv() function.

MATLAB Code
clc; clear; close all;

% Define two finite duration sequences

x = [1, 2, 3, 4]; % Input sequence

h = [1, -1, 2, 1]; % Impulse response

% Perform linear convolution

y = conv(x, h);

% Define time indices for sequences

nx = 0:length(x)-1;

nh = 0:length(h)-1;

ny = 0:length(y)-1;

% Plot the input sequences

subplot(3,1,1);

stem(nx, x, 'filled', 'r');

xlabel('n'); ylabel('Amplitude');

title('Input Sequence x(n)');

grid on;

subplot(3,1,2);

stem(nh, h, 'filled', 'b');

xlabel('n'); ylabel('Amplitude');

title('Impulse Response h(n)');

grid on;

% Plot the convolution result

subplot(3,1,3);
stem(ny, y, 'filled', 'g');

xlabel('n'); ylabel('Amplitude');

title('Linear Convolution Output y(n)');

grid on;
Input Sequence x(n)
4
Amplitude

0
0 0.5 1 1.5 2 2.5 3
n
Impulse Response h(n)
2
Amplitude

-2
0 0.2 0.4 0.6 0.81 1.2 1.4 1.6 1.8 2
n
Linear Convolution Output y(n)
10
Amplitude

0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
n
Evaluate and Plot Cross-Correlation of Two Finite
Duration Sequences
Theory: Cross-Correlation

Cross-correlation measures the similarity between two sequences as a function of time shift.
It is defined as:

rxy(m)=∑nx(n)⋅y(n+m)r_{xy}(m) = \sum_{n} x(n) \cdot y(n+m)rxy(m)=n∑x(n)⋅y(n+m)

This helps analyze how one signal aligns with another.

MATLAB Code

clc; clear; close all;

% Define two sequences

x = [1, 2, 3, 4]; % First sequence

y = [4, 3, 2, 1]; % Second sequence

% Compute cross-correlation

r_xy = xcorr(x, y);

% Define lag indices

lags = -(length(x)-1):(length(x)-1);

% Plot the input sequences

subplot(3,1,1);

stem(0:length(x)-1, x, 'filled', 'r');

xlabel('n'); ylabel('Amplitude');

title('Sequence x(n)');

grid on;
subplot(3,1,2);

stem(0:length(y)-1, y, 'filled', 'b');

xlabel('n'); ylabel('Amplitude');

title('Sequence y(n)');

grid on;

% Plot the cross-correlation result

subplot(3,1,3);

stem(lags, r_xy, 'filled', 'g');

xlabel('Lag (m)'); ylabel('Amplitude');

title('Cross-Correlation r_{xy}(m)');

grid on;

Sequence x(n)
4
Amplitude

0
0 0.5 1 1.5 2 2.5 3
n
Sequence y(n)
4
Amplitude

0
0 0.5 1 1.5 2 2.5 3
n
Cross-Correlation rxy (m)
40
Amplitude

20

0
-3 -2 -1 0 1 2 3
Lag (m)
Evaluate and Plot Auto-Correlation of a Sequence
Theory: Auto-Correlation

Auto-correlation measures how a sequence correlates with itself at different time shifts. It is
defined as:

rxx(m)=∑nx(n)⋅x(n+m)r_{xx}(m) = \sum_{n} x(n) \cdot x(n+m)rxx(m)=n∑x(n)⋅x(n+m)

where:

 rxx(m)r_{xx}(m)rxx(m) is the auto-correlation function,

 x(n)x(n)x(n) is the sequence,

 mmm represents the lag (shift).

Auto-correlation helps in analyzing signal periodicity, energy distribution, and detecting


repeating patterns.

MATLAB Code

clc; clear; close all;

% Define the sequence

x = [1, 2, 3, 4, 3, 2, 1]; % Example finite sequence

% Compute auto-correlation

r_xx = xcorr(x, x);

% Define lag indices

lags = -(length(x)-1):(length(x)-1);

% Plot the original sequence

subplot(2,1,1);

stem(0:length(x)-1, x, 'filled', 'r');

xlabel('n'); ylabel('Amplitude');
title('Sequence x(n)');

grid on;

% Plot the auto-correlation result

subplot(2,1,2);

stem(lags, r_xx, 'filled', 'g');

xlabel('Lag (m)'); ylabel('Amplitude');

title('Auto-Correlation r_{xx}(m)');

grid on;

Sequence x(n)
4

3
Amplitude

0
0 1 2 3 4 5 6
n
Auto-Correlation rxx (m)
60
Amplitude

40

20

0
-6 -4 -2 0 2 4 6
Lag (m)

Convolution of a Periodic and an Aperiodic Signal


Theory: Convolution of Periodic and Aperiodic Signals
Convolution is used to determine the output of a system when an input is applied. When a
periodic signal xp(n)x_p(n)xp(n) and an aperiodic signal xa(n)x_a(n)xa(n) are convolved, the
result follows:

y(n)=xp(n)∗xa(n)=∑k=−∞∞xp(k)xa(n−k)y(n) = x_p(n) * x_a(n) = \sum_{k=-\infty}^{\infty}


x_p(k) x_a(n-k)y(n)=xp(n)∗xa(n)=k=−∞∑∞xp(k)xa(n−k)

 The periodic signal is typically a repeating pattern (e.g., sine wave, square wave).

 The aperiodic signal is a finite-duration sequence (e.g., impulse response).

MATLAB Code

clc; clear; close all;

% Define periodic signal (sine wave)

n = 0:10;

x_p = sin(0.4 * pi * n); % Periodic sequence

% Define aperiodic signal (finite duration sequence)

x_a = [1, 2, 3, 4, 3, 2, 1];

% Perform convolution

y = conv(x_p, x_a);

% Define time indices for output

ny = 0:length(y)-1;

% Plot the periodic signal

subplot(3,1,1);

stem(n, x_p, 'filled', 'r');

xlabel('n'); ylabel('Amplitude');

title('Periodic Signal x_p(n)');


grid on;

% Plot the aperiodic signal

subplot(3,1,2);

stem(0:length(x_a)-1, x_a, 'filled', 'b');

xlabel('n'); ylabel('Amplitude');

title('Aperiodic Signal x_a(n)');

grid on;

% Plot the convolution result

subplot(3,1,3);

stem(ny, y, 'filled', 'g');

xlabel('n'); ylabel('Amplitude');

title('Convolution of Periodic & Aperiodic Signals y(n)');

grid on;

Periodic Signal x p(n)


Amplitude

-1
0 1 2 3 4 5 6 7 8 9 10
n
Aperiodic Signal x a(n)
Amplitude

0
0 1 2 3 4 5 6
n
Convolution of Periodic & Aperiodic Signals y(n)
Amplitude

-5
0 2 4 6 8 10 12 14 16
n
Compute the DTFS of a Sequence and Plot Magnitude
& Phase Response
The Discrete-Time Fourier Series (DTFS) represents a periodic discrete-time signal as a sum
of complex exponentials. The DTFS coefficients are given by:

X[k]=∑n=0N−1x[n]e−j(2πkn/N)X[k] = \sum_{n=0}^{N-1} x[n] e^{-j(2\pi k n/N)}X[k]=n=0∑N−1


x[n]e−j(2πkn/N)

where:

 x[n]x[n]x[n] is the periodic sequence of length NNN,

 X[k]X[k]X[k] are the DTFS coefficients.

MATLAB Code

clc; clear; close all;

% Define the periodic sequence (Modify as needed)

N = 8; % Period of the sequence

n = 0:N-1; % Time indices

x = [1 2 3 4 3 2 1 0]; % Example periodic sequence

% Compute DTFS coefficients using DFT

X_k = fft(x) / N; % DTFS coefficients

% Frequency index

k = 0:N-1;

% Compute magnitude and phase response


magnitude = abs(X_k);

phase = angle(X_k);

% Display Results

disp('DTFS Coefficients:');

disp(X_k.');

% Plot Magnitude Spectrum

subplot(2,1,1);

stem(k, magnitude, 'filled');

title('Magnitude Spectrum |X[k]|');

xlabel('k'); ylabel('|X[k]|');

grid on;

% Plot Phase Spectrum

subplot(2,1,2);

stem(k, phase, 'filled');

title('Phase Spectrum ∠X[k]');

xlabel('k'); ylabel('Phase (radians)');

grid on;
Magnitude Spectrum |X[k]|
2

1.5
|X[k]|

0.5

0
0 1 2 3 4 5 6 7
k
Phase Spectrum X[k]
4
Phase (radians)

-2

-4
0 1 2 3 4 5 6 7
k

Compute the IDTFS of a Sequence and Plot Magnitude


& Phase Response
The Inverse Discrete-Time Fourier Series (IDTFS) reconstructs a periodic discrete-time signal
from its DTFS coefficients. It is given by:

x[n]=∑k=0N−1X[k]ej(2πkn/N)x[n] = \sum_{k=0}^{N-1} X[k] e^{j(2\pi k n/N)}x[n]=k=0∑N−1


X[k]ej(2πkn/N)

where:

 X[k]X[k]X[k] are the DTFS coefficients.

 x[n]x[n]x[n] is the reconstructed periodic sequence.

 NNN is the period of the sequence.


MATLAB Code

clc; clear; close all;

% Define the DTFS coefficients (Modify as needed)

N = 8; % Period of the sequence

k = 0:N-1; % Frequency indices

X_k = [3.5, -0.5 - 0.5j, 0, -0.5 + 0.5j, 0, -0.5 - 0.5j, 0, -0.5 + 0.5j]; % Example DTFS coefficients

% Compute IDTFS using IFFT (Inverse Fourier Transform)

x_n = ifft(X_k) * N; % Multiply by N to get original x[n]

% Compute magnitude and phase of reconstructed signal

magnitude = abs(x_n);

phase = angle(x_n);

% Display Results

disp('Reconstructed Sequence x[n]:');

disp(real(x_n).'); % Only real part since x[n] is usually real

% Plot Reconstructed Signal

subplot(3,1,1);

stem(0:N-1, real(x_n), 'filled');

title('Reconstructed Signal x[n]');

xlabel('n'); ylabel('x[n]');

grid on;

% Plot Magnitude Spectrum

subplot(3,1,2);
stem(k, magnitude, 'filled');

title('Magnitude Spectrum |x[n]|');

xlabel('n'); ylabel('|x[n]|');

grid on;

% Plot Phase Spectrum

subplot(3,1,3);

stem(k, phase, 'filled');

title('Phase Spectrum ∠x[n]');

xlabel('n'); ylabel('Phase (radians)');

grid on;
Reconstructed Signal x[n]
10
x[n]

0
0 1 2 3 4 5 6 7
n
Magnitude Spectrum |x[n]|
10
|x[n]|

0
0 1 2 3 4 5 6 7
n
Phase Spectrum ?x[n]
Phase (radians)

-1
0 1 2 3 4 5 6 7
n
Discrete-Time Fourier Transform (DTFT) Computation
and Manual Solution
The Discrete-Time Fourier Transform (DTFT) of a sequence x[n]x[n]x[n] is defined as:

X(ejω)=∑n=−∞∞x[n]e−jωnX(e^{j\omega}) = \sum_{n=-\infty}^{\infty} x[n] e^{-j\omega


n}X(ejω)=n=−∞∑∞x[n]e−jωn

where:

 x[n]x[n]x[n] is the discrete-time sequence.

 X(ejω)X(e^{j\omega})X(ejω) is a continuous function of ω\omegaω over [−π,π][-\pi, \


pi][−π,π].

 The DTFT gives the magnitude spectrum and phase spectrum of x[n]x[n]x[n].

1. MATLAB Code for DTFT Computation

clc; clear; close all;

% Define the discrete-time sequence

n = -5:5; % Time indices

x = [0 0 1 2 3 4 3 2 1 0 0]; % Example sequence

% Define the frequency range for DTFT

omega = linspace(-pi, pi, 1000); % Frequency range from -π to π

Xw = zeros(size(omega)); % Initialize DTFT

% Compute DTFT using summation formula

for k = 1:length(omega)

Xw(k) = sum(x .* exp(-1j * omega(k) * n));

End

% Compute magnitude and phase response

magnitude = abs(Xw);
phase = angle(Xw);

% Plot Magnitude Spectrum

subplot(2,1,1);

plot(omega, magnitude, 'b', 'LineWidth', 1.5);

title('Magnitude Spectrum |X(e^{j\omega})|');

xlabel('\omega (radians/sample)'); ylabel('|X(e^{j\omega})|');

grid on;

% Plot Phase Spectrum

subplot(2,1,2);

plot(omega, phase, 'r', 'LineWidth', 1.5);

title('Phase Spectrum ∠X(e^{j\omega})');

xlabel('\omega (radians/sample)'); ylabel('Phase (radians)');

grid on;

Magnitude Spectrum |X(ej )|
20

15
|X(ej )|

10

0
-4 -3 -2 -1 0 1 2 3 4
 (radians/sample)

-11
x 10 Phase Spectrum ?X(ej )
1
Phase (radians)

0.5

-0.5

-1
-4 -3 -2 -1 0 1 2 3 4
 (radians/sample)
Inverse Discrete-Time Fourier Transform (IDTFT)
Computation in MATLAB
The Inverse Discrete-Time Fourier Transform (IDTFT) reconstructs a discrete-time sequence
x[n]x[n]x[n] from its frequency-domain representation X(ejω)X(e^{j\omega})X(ejω). The
IDTFT formula is:

x[n]=12π∫−ππX(ejω)ejωndωx[n] = \frac{1}{2\pi} \int_{-\pi}^{\pi} X(e^{j\omega}) e^{j\omega


n} d\omegax[n]=2π1∫−ππX(ejω)ejωndω

where:

 X(ejω)X(e^{j\omega})X(ejω) is the DTFT spectrum of x[n]x[n]x[n].

 x[n]x[n]x[n] is the recovered discrete-time signal.

 Integration is performed over the frequency range −π≤ω≤π-\pi \leq \omega \leq \
pi−π≤ω≤π.

MATLAB Code to Compute IDTFT and Plot the Magnitude & Phase Response

clc; clear; close all;

% Define the frequency range for IDTFT

omega = linspace(-pi, pi, 1000); % Frequency range from -π to π

% Define a sample DTFT function X(e^jω) (Modify as needed)

Xw = 2*cos(omega) + 1j*sin(omega); % Example DTFT function

% Define the time indices for reconstruction

n = -10:10; % Range of n values

% Compute IDTFT using numerical integration (Trapezoidal rule)

x_n = zeros(size(n)); % Initialize output signal

for i = 1:length(n)
x_n(i) = trapz(omega, (1/(2*pi)) * Xw .* exp(1j * omega * n(i)));

end

% Compute magnitude and phase of reconstructed sequence

magnitude = abs(x_n);

phase = angle(x_n);

% Display results

disp('Reconstructed Sequence x[n]:');

disp(real(x_n).'); % Display only the real part

% Plot Reconstructed Signal

subplot(3,1,1);

stem(n, real(x_n), 'filled');

title('Reconstructed Signal x[n]');

xlabel('n'); ylabel('x[n]');

grid on;

% Plot Magnitude Spectrum

subplot(3,1,2);

stem(n, magnitude, 'filled');

title('Magnitude Spectrum |x[n]|');

xlabel('n'); ylabel('|x[n]|');

grid on;

% Plot Phase Spectrum

subplot(3,1,3);

stem(n, phase, 'filled');


title('Phase Spectrum ∠x[n]');

xlabel('n'); ylabel('Phase (radians)');

grid on;

Reconstructed Signal x[n]


2
x[n]

0
-10 -8 -6 -4 -2 0 2 4 6 8 10
n
Magnitude Spectrum |x[n]|
2
|x[n]|

0
-10 -8 -6 -4 -2 0 2 4 6 8 10
n
Phase (radians)

Phase Spectrum ?x[n]


5

-5
-10 -8 -6 -4 -2 0 2 4 6 8 10
n
Computing the N-Point DFT and Plotting the
Magnitude & Phase Response in MATLAB
The Discrete Fourier Transform (DFT) of a discrete-time sequence x[n]x[n]x[n] is given by:

X[k]=∑n=0N−1x[n]e−j(2πkn/N)X[k] = \sum_{n=0}^{N-1} x[n] e^{-j(2\pi kn/N)}X[k]=n=0∑N−1


x[n]e−j(2πkn/N)

where:

 X[k]X[k]X[k] is the frequency-domain representation of x[n]x[n]x[n].

 NNN is the number of points in the DFT.

 kkk represents the frequency index.

MATLAB Code to Compute the N-Point DFT

clc; clear; close all;

% Define the input sequence (Modify as needed)

x = [1 2 3 4 3 2 1 0]; % Example sequence

N = length(x); % Number of DFT points

% Compute the N-point DFT using the formula

X_k = zeros(1, N); % Initialize output frequency spectrum

for k = 0:N-1

for n = 0:N-1

X_k(k+1) = X_k(k+1) + x(n+1) * exp(-1j * 2 * pi * k * n / N);

end

end

% Compute magnitude and phase response

magnitude = abs(X_k);
phase = angle(X_k);

% Define frequency axis

k = 0:N-1;

% Plot Magnitude Spectrum

subplot(2,1,1);

stem(k, magnitude, 'filled');

title('Magnitude Spectrum |X[k]|');

xlabel('Frequency index k'); ylabel('|X[k]|');

grid on;

% Plot Phase Spectrum

subplot(2,1,2);

stem(k, phase, 'filled');

title('Phase Spectrum ∠X[k]');

xlabel('Frequency index k'); ylabel('Phase (radians)');

grid on;

% Display Results

disp('DFT Coefficients X[k]:');


disp(X_k);

Magnitude Spectrum |X[k]|


20

15
|X[k]|

10

0
0 1 2 3 4 5 6 7
Frequency index k
Phase Spectrum ?X[k]
4
Phase (radians)

-2

-4
0 1 2 3 4 5 6 7
Frequency index k
Computing the N-Point Inverse Discrete Fourier
Transform (IDFT) in MATLAB
The Inverse Discrete Fourier Transform (IDFT) is used to reconstruct a discrete-time
sequence x[n]x[n]x[n] from its frequency-domain representation X[k]X[k]X[k]. The IDFT
formula is:

x[n]=1N∑k=0N−1X[k]ej(2πkn/N)x[n] = \frac{1}{N} \sum_{k=0}^{N-1} X[k] e^{j(2\pi


kn/N)}x[n]=N1k=0∑N−1X[k]ej(2πkn/N)

where:

 x[n]x[n]x[n] is the time-domain sequence.

 X[k]X[k]X[k] is the frequency-domain representation.

 NNN is the number of IDFT points.

 kkk represents the frequency index.

MATLAB Code to Compute the N-Point IDFT

clc; clear; close all;

% Define the frequency-domain sequence (Modify as needed)

X_k = [10 -2+2j -2 -2-2j 0 -2-2j -2 -2+2j]; % Example DFT sequence

N = length(X_k); % Number of IDFT points

% Compute the N-point IDFT using the formula

x_n = zeros(1, N); % Initialize time-domain sequence

for n = 0:N-1

for k = 0:N-1

x_n(n+1) = x_n(n+1) + (1/N) * X_k(k+1) * exp(1j * 2 * pi * k * n / N);

end

end
% Compute magnitude and phase of reconstructed sequence

magnitude = abs(x_n);

phase = angle(x_n);

% Define time index

n = 0:N-1;

% Plot Reconstructed Signal

subplot(3,1,1);

stem(n, real(x_n), 'filled');

title('Reconstructed Signal x[n]');

xlabel('n'); ylabel('x[n]');

grid on;

% Plot Magnitude Spectrum

subplot(3,1,2);

stem(n, magnitude, 'filled');

title('Magnitude Spectrum |x[n]|');

xlabel('n'); ylabel('|x[n]|');

grid on;

% Plot Phase Spectrum

subplot(3,1,3);

stem(n, phase, 'filled');

title('Phase Spectrum ∠x[n]');

xlabel('n'); ylabel('Phase (radians)');

grid on;
% Display Results

disp('Reconstructed Sequence x[n]:');

disp(real(x_n).'); % Display only the real part

Reconstructed Signal x[n]


2
x[n]

-2
0 1 2 3 4 5 6 7
n
Magnitude Spectrum |x[n]|
2
|x[n]|

0
0 1 2 3 4 5 6 7
n
Phase Spectrum ?x[n]
Phase (radians)

-5
0 1 2 3 4 5 6 7
n
Linearity Property
ax1[n]+bx2[n]→DTFTaX1(ejω)+bX2(ejω)a x_1[n] + b x_2[n] \xrightarrow{\text{DTFT}} a
X_1(e^{j\omega}) + b X_2(e^{j\omega})ax1[n]+bx2[n]DTFTaX1(ejω)+bX2(ejω)

MATLAB Code for Verification:

clc; clear; close all;

% Define two input sequences

n = -5:5;

x1 = [0 1 2 3 4 3 2 1 0 0 0];

x2 = [0 0 0 1 2 3 2 1 0 0 0];

a = 2; b = 3; % Scalars

% Compute DTFT

omega = linspace(-pi, pi, 1000);

X1 = arrayfun(@(w) sum(x1 .* exp(-1j * w * n)), omega);

X2 = arrayfun(@(w) sum(x2 .* exp(-1j * w * n)), omega);

% Compute linear combination in time and frequency domains

lhs = arrayfun(@(w) sum((a*x1 + b*x2) .* exp(-1j * w * n)), omega);

rhs = a*X1 + b*X2;

% Plot verification

figure;

subplot(2,1,1);

plot(omega, abs(lhs), 'b', omega, abs(rhs), 'r--');

title('Verification of Linearity Property');

legend('LHS', 'RHS');
xlabel('\omega'); ylabel('Magnitude');

grid on;

Verification of Linearity Property


60
LHS
RHS
50

40
Magnitude

30

20

10

0
-4 -3 -2 -1 0 1 2 3 4

Time Shifting Property
x[n−n0]→DTFTe−jωn0X(ejω)x[n - n_0] \xrightarrow{\text{DTFT}} e^{-j\omega n_0} X(e^{j\
omega})x[n−n0]DTFTe−jωn0X(ejω)

MATLAB Code for Verification:

n0 = 3; % Time shift

% Compute DTFT of time-shifted sequence

X_shifted = arrayfun(@(w) sum(x1 .* exp(-1j * w * (n - n0))), omega);

% Compute expected shift in frequency

X_expected = X1 .* exp(-1j * omega * n0);

% Plot verification

figure;

plot(omega, abs(X_shifted), 'b', omega, abs(X_expected), 'r--');

title('Verification of Time Shifting Property');

legend('Shifted DTFT', 'Expected');

xlabel('\omega'); ylabel('Magnitude');

grid Verification of Time Shifting Property on;


16
Shifted DTFT
14 Expected

12

10
Magnitude

0
-4 -3 -2 -1 0 1 2 3 4

Frequency Shifting Property
ejω0nx[n]→DTFTX(ej(ω−ω0))e^{j\omega_0 n} x[n] \xrightarrow{\text{DTFT}} X(e^{j(\omega -
\omega_0)})ejω0nx[n]DTFTX(ej(ω−ω0))

MATLAB Code for Verification:

omega0 = pi/4; % Frequency shift

% Compute DTFT of frequency-shifted sequence

x_freq_shifted = x1 .* exp(1j * omega0 * n);

X_freq_shifted = arrayfun(@(w) sum(x_freq_shifted .* exp(-1j * w * n)), omega);

% Compute expected shift in frequency

X_expected = interp1(omega, X1, omega - omega0, 'linear', 0);

% Plot verification

figure;

plot(omega, abs(X_freq_shifted), 'b', omega, abs(X_expected), 'r--');

title('Verification of Frequency Shifting Property');

legend('Shifted DTFT', 'Expected');

xlabel('\omega'); ylabel('Magnitude');

Verification of Frequency Shifting Property grid on;


16
Shifted DTFT
14 Expected

12

10
Magnitude

0
-4 -3 -2 -1 0 1 2 3 4
Convolution Property
x1[n]∗x2[n]→DTFTX1(ejω)X2(ejω)x_1[n] * x_2[n] \xrightarrow{\text{DTFT}} X_1(e^{j\
omega}) X_2(e^{j\omega})x1[n]∗x2[n]DTFTX1(ejω)X2(ejω)

MATLAB Code for Verification:

x_conv = conv(x1, x2);

n_conv = min(n) + min(n) : max(n) + max(n);

X_conv = arrayfun(@(w) sum(x_conv .* exp(-1j * w * n_conv)), omega);

X_mult = X1 .* X2;

% Plot verification

figure;

plot(omega, abs(X_conv), 'b', omega, abs(X_mult), 'r--');

title('Verification of Convolution Property');

legend('DTFT of Convolution', 'Multiplication in Frequency');

xlabel('\omega'); ylabel('Magnitude');

grid on;
Verification of Convolution Property
150
DTFT of Convolution
Multiplication in Frequency

100
Magnitude

50

0
-4 -3 -2 -1 0 1 2 3 4

Multiplication Property
x1[n]x2[n]→DTFT12πX1(ejω)∗X2(ejω)x_1[n] x_2[n] \xrightarrow{\text{DTFT}} \frac{1}{2\pi}
X_1(e^{j\omega}) * X_2(e^{j\omega})x1[n]x2[n]DTFT2π1X1(ejω)∗X2(ejω)

MATLAB Code for Verification:

% Compute the DTFT of the product x1[n] * x2[n]

x_mult = x1 .* x2;

X_mult = arrayfun(@(w) sum(x_mult .* exp(-1j * w * n)), omega);

% Compute expected convolution in the frequency domain

X_conv_freq = conv(X1, X2) / (2 * pi); % Full convolution without 'same'

% Adjust length to match omega

L = length(omega);

X_conv_freq = X_conv_freq(1:L); % Trim excess values

% Plot verification

figure;

plot(omega, abs(X_mult), 'b', omega, abs(X_conv_freq), 'r--');

title('Verification of Multiplication Property');

legend('DTFT of Product', 'Convolution in Frequency');

xlabel('\omega'); ylabel('Magnitude');

grid on;
Verification of Multiplication Property
4000
DTFT of Product
3500 Convolution in Frequency

3000

2500
Magnitude

2000

1500

1000

500

0
-4 -3 -2 -1 0 1 2 3 4

Differentiation in Frequency Property
nx[n]→DTFTjddωX(ejω)n x[n] \xrightarrow{\text{DTFT}} j \frac{d}{d\omega} X(e^{j\
omega})nx[n]DTFTjdωdX(ejω)

MATLAB Code for Verification:

x_n_mult = n .* x1;

X_n_mult = arrayfun(@(w) sum(x_n_mult .* exp(-1j * w * n)), omega);

% Compute derivative of X(w)

dX_dw = gradient(X1, omega) * 1j;

% Plot verification

figure;

plot(omega, abs(X_n_mult), 'b', omega, abs(dX_dw), 'r--');

title('Verification of Differentiation in Frequency');

legend('DTFT of n*x[n]', 'j dX/d\omega');

xlabel('\omega'); ylabel('Magnitude');

grid on;
Verification of Differentiation in Frequency
20
DTFT of n*x[n]
18 j dX/d

16

14

12
Magnitude

10

0
-4 -3 -2 -1 0 1 2 3 4

Scaling Property
x[an]→DTFT1∣a∣X(ejω/a),a>0x[a n] \xrightarrow{\text{DTFT}} \frac{1}{|a|} X\left(e^{j\
omega/a}\right), \quad a > 0x[an]DTFT∣a∣1X(ejω/a),a>0

MATLAB Code for Verification:

a = 2; % Scaling factor

n_scaled = round(n / a); % Downsampling indices

x_scaled = zeros(size(x1));

valid_indices = ismember(n, n_scaled);

x_scaled(valid_indices) = x1(valid_indices);

X_scaled = arrayfun(@(w) sum(x_scaled .* exp(-1j * w * n)), omega);

X_expected = (1/abs(a)) * interp1(omega, X1, omega / a, 'linear', 0);

% Plot verification

figure;

plot(omega, abs(X_scaled), 'b', omega, abs(X_expected), 'r--');

title('Verification of Scaling Property');

legend('DTFT of Scaled x[n]', 'Expected');

xlabel('\omega'); ylabel('Magnitude');

grid on;

Verification of Scaling Property


15
DTFT of Scaled x[n]
Expected

10
Magnitude

0
-4 -3 -2 -1 0 1 2 3 4

MATLAB Code to Plot Poles and Zeros of a Given Z-
Transform
The Z-transform of a discrete-time signal x[n]x[n]x[n] is given by:

X(z)=∑n=−∞∞x[n]z−nX(z) = \sum_{n=-\infty}^{\infty} x[n] z^{-n}X(z)=n=−∞∑∞x[n]z−n

where X(z)X(z)X(z) is typically represented as a ratio of polynomials:

X(z)=N(z)D(z)X(z) = \frac{N(z)}{D(z)}X(z)=D(z)N(z)

where:

 N(z)N(z)N(z) (numerator) determines the zeros.

 D(z)D(z)D(z) (denominator) determines the poles.

MATLAB Code for Pole-Zero Plot

clc; clear; close all;

% Define the numerator and denominator coefficients of X(z)

numerator = [1 -0.5]; % Example: (z - 0.5)

denominator = [1 -1.2 0.36]; % Example: (z - 0.6)(z - 0.6)

% Compute poles and zeros

zeros_ = roots(numerator);

poles_ = roots(denominator);

% Plot pole-zero diagram

figure;

zplane(numerator, denominator);

title('Pole-Zero Plot of the Given Z-Transform');

xlabel('Real Part');

ylabel('Imaginary Part');
grid on;

% Display results

disp('Zeros of the system:');

disp(zeros_);

disp('Poles of the system:');

disp(poles_);

Pole-Zero Plot of the Given Z-Transform

0.8

0.6

0.4
Imaginary Part

0.2
2
0

-0.2

-0.4

-0.6

-0.8

-1

-1 -0.5 0 0.5 1
Real Part
Inverse Z-Transform using Partial Fraction Expansion
clc; clear; close all;

% Define the numerator and denominator coefficients of X(z)

numerator = [1 -0.5]; % Example: (z - 0.5)

denominator = [1 -1.2 0.36]; % Example: (z - 0.6)(z - 0.6)

% Compute the partial fraction expansion

[residues, poles, direct_terms] = residuez(numerator, denominator);

% Display the results

disp('Residues:');

disp(residues);

disp('Poles:');

disp(poles);

disp('Direct Terms:');

disp(direct_terms);

% Define range for n

n = 0:10;

% Compute inverse Z-transform manually using the formula x[n] = sum(R_k * P_k^n)

x_n = zeros(size(n)); % Initialize output sequence

for k = 1:length(residues)

x_n = x_n + residues(k) * (poles(k).^n);

end

% Add direct terms if present


if ~isempty(direct_terms)

x_n = x_n + direct_terms;

end

% Plot the inverse Z-transform

figure;

stem(n, x_n, 'filled');

title('Inverse Z-Transform x[n]');

xlabel('n'); ylabel('x[n]');

grid on;

Inverse Z-Transform x[n]


1.4

1.2

0.8
x[n]

0.6

0.4

0.2

0
0 1 2 3 4 5 6 7 8 9 10
n
Solve a Difference Equation
A difference equation represents a discrete-time system and can be solved to find y[n]
given an input x[n] and initial conditions.

The standard linear constant-coefficient difference equation is:

y[n]=−∑k=1Naky[n−k]+∑k=0Mbkx[n−k]y[n] = -\sum_{k=1}^{N} a_k y[n-k] + \sum_{k=0}^{M}


b_k x[n-k]y[n]=−k=1∑Naky[n−k]+k=0∑Mbkx[n−k]

where:

 aka_kak are the output coefficients.

 bkb_kbk are the input coefficients.

 x[n]x[n]x[n] is the input signal.

 y[n]y[n]y[n] is the output response.

MATLAB Code to Solve a Difference Equation

clc; clear; close all;

% Define the coefficients of the difference equation

b = [1 -0.5]; % Coefficients of x[n] (Numerator)

a = [1 -1.2 0.36]; % Coefficients of y[n] (Denominator)

% Define the input signal x[n] (Example: Unit step signal)

n = 0:20; % Define the time range

x = ones(size(n)); % Unit step input

% Define initial conditions (zero for causal system)

y_init = [];

% Solve the difference equation using filter function

y = filter(b, a, x);
% Plot input and output signals

figure;

subplot(2,1,1);

stem(n, x, 'filled', 'b');

title('Input Signal x[n]');

xlabel('n'); ylabel('x[n]');

grid on;

subplot(2,1,2);

stem(n, y, 'filled', 'r');

title('Output Signal y[n] (Solution of Difference Equation)');

xlabel('n'); ylabel('y[n]');

grid on;

Input Signal x[n]


1
x[n]

0.5

0
0 2 4 6 8 10 12 14 16 18 20
n
Output Signal y[n] (Solution of Difference Equation)
4

3
y[n]

0
0 2 4 6 8 10 12 14 16 18 20
n
Plot the Magnitude and Phase Response of a Given
Difference Equation
A difference equation describes a discrete-time system and can be represented as:

y[n]=−∑k=1Naky[n−k]+∑k=0Mbkx[n−k]y[n] = -\sum_{k=1}^{N} a_k y[n-k] + \sum_{k=0}^{M}


b_k x[n-k]y[n]=−k=1∑Naky[n−k]+k=0∑Mbkx[n−k]

where:

 aka_kak and bkb_kbk are the coefficients of the equation.

 The system's frequency response is given by:

H(ejω)=B(z)A(z)∣z=ejωH(e^{j\omega}) = \frac{B(z)}{A(z)} \Big|_{z=e^{j\


omega}}H(ejω)=A(z)B(z)z=ejω

where B(z)B(z)B(z) and A(z)A(z)A(z) are the numerator and denominator polynomials in the
Z-transform domain.

MATLAB Code

clc; clear; close all;

% Define the coefficients of the difference equation

b = [1 -0.5]; % Coefficients of x[n] (Numerator)

a = [1 -1.2 0.36]; % Coefficients of y[n] (Denominator)

% Frequency response computation

[H, w] = freqz(b, a, 1024, 'whole'); % Compute H(e^jω) over 1024 points

% Compute magnitude and phase

magnitude = abs(H);

phase = angle(H);
% Plot magnitude response

figure;

subplot(2,1,1);

plot(w/pi, magnitude, 'b', 'LineWidth', 1.5);

title('Magnitude Response');

xlabel('Normalized Frequency (\times \pi rad/sample)');

ylabel('|H(e^{j\omega})|');

grid on;

% Plot phase response

subplot(2,1,2);

plot(w/pi, unwrap(phase), 'r', 'LineWidth', 1.5);

title('Phase Response');

xlabel('Normalized Frequency (\times \pi rad/sample)');

ylabel('Phase (radians)');

grid on;

Magnitude Response
4

3
|H(e )|
j

0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
Normalized Frequency (   rad/sample)
Phase Response
1
Phase (radians)

0.5

-0.5

-1
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
Normalized Frequency (  rad/sample)

You might also like