matlab programs
matlab programs
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
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;
stem(n, x, 'filled');
xlabel('n'); ylabel('Amplitude');
grid on;
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:
MATLAB Code:
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:
MATLAB Code:
A = 1; % Amplitude
% Real Exponential
% Complex Exponential
subplot(2,1,1);
xlabel('n'); ylabel('Amplitude');
grid on;
subplot(2,1,2);
stem(n, real(x_complex), 'r', 'filled'); hold on;
xlabel('n'); ylabel('Amplitude');
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:
MATLAB Code:
A = 2; % Amplitude
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:
MATLAB Code:
A = 2; % Amplitude
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:
T = 5; % Period
stem(0:N-1, x, 'filled');
xlabel('n'); ylabel('Amplitude');
grid on;
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:
MATLAB Code:
x_even = (x + fliplr(x)) / 2;
x_odd = (x - fliplr(x)) / 2;
subplot(3,1,1);
stem(n, x, 'filled');
xlabel('n'); ylabel('Amplitude');
grid on;
subplot(3,1,2);
xlabel('n'); ylabel('Amplitude');
grid on;
% Plot Odd Component
subplot(3,1,3);
xlabel('n'); ylabel('Amplitude');
grid on;
-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:
MATLAB Code:
n = -5:5;
4
Amplitude
y = A * x; % Amplitude scaling
2
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');
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;
n_compressed = n(1:compression_factor:end);
x_compressed = x(1:compression_factor:end);
expansion_factor = 2;
n_expanded = n(1):1/expansion_factor:n(end);
subplot(3,1,1);
stem(n, x, 'filled');
xlabel('n'); ylabel('Amplitude');
grid on;
subplot(3,1,2);
xlabel('n'); ylabel('Amplitude');
grid on;
% Plot expanded sequence
subplot(3,1,3);
xlabel('n'); ylabel('Amplitude');
grid on;
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:
n = 0:5;
L = 3; % Up-sampling factor
y = zeros(1, L*length(x)); % Initialize output
stem(n_new, y, 'filled');
xlabel('n'); ylabel('Amplitude');
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:
M = 3; % Down-sampling factor
stem(n_new, y, 'filled');
xlabel('n'); ylabel('Amplitude');
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:
MATLAB Code:
n = -5:5;
k = 3; % Shift amount
stem(n_shifted, x, 'filled');
xlabel('n'); ylabel('Amplitude');
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:
n = -5:5;
xlabel('n'); ylabel('Amplitude');
grid on;
6
Amplitude
0
-5 -4 -3 -2 -1 0 1 2 3 4 5
n
Addition of Two Sequences
Addition of two signals:
MATLAB Code:
n = 0:5;
x2 = [6, 5, 4, 3, 2, 1];
stem(n, y, 'filled');
xlabel('n'); ylabel('Amplitude');
grid on;
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:
MATLAB Code:
n = 0:5;
x1 = [1, 2, 3, 4, 5, 6];
x2 = [6, 5, 4, 3, 2, 1];
stem(n, y, 'filled');
xlabel('n'); ylabel('Amplitude');
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:
MATLAB Code:
n = 0:5;
x1 = [1, 2, 3, 4, 5, 6];
x2 = [6, 5, 4, 3, 2, 1];
stem(n, y, 'filled');
xlabel('n'); ylabel('Amplitude');
grid on;
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:
MATLAB Code:
n = 0:10;
subplot(3,1,1);
xlabel('n'); ylabel('Amplitude');
grid on;
subplot(3,1,2);
xlabel('n'); ylabel('Amplitude');
grid on;
subplot(3,1,3);
stem(n, y, 'filled');
xlabel('n'); ylabel('Amplitude');
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
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.
MATLAB Code
subplot(3,1,1);
grid on;
subplot(3,1,2);
grid on;
subplot(3,1,3);
plot(t, x_t, 'k', 'LineWidth', 1.5); hold on;
grid on;
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)
MATLAB Code
clc; clear; close all;
y = conv(x, h);
nx = 0:length(x)-1;
nh = 0:length(h)-1;
ny = 0:length(y)-1;
subplot(3,1,1);
xlabel('n'); ylabel('Amplitude');
grid on;
subplot(3,1,2);
xlabel('n'); ylabel('Amplitude');
grid on;
subplot(3,1,3);
stem(ny, y, 'filled', 'g');
xlabel('n'); ylabel('Amplitude');
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:
MATLAB Code
% Compute cross-correlation
lags = -(length(x)-1):(length(x)-1);
subplot(3,1,1);
xlabel('n'); ylabel('Amplitude');
title('Sequence x(n)');
grid on;
subplot(3,1,2);
xlabel('n'); ylabel('Amplitude');
title('Sequence y(n)');
grid on;
subplot(3,1,3);
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:
where:
MATLAB Code
% Compute auto-correlation
lags = -(length(x)-1):(length(x)-1);
subplot(2,1,1);
xlabel('n'); ylabel('Amplitude');
title('Sequence x(n)');
grid on;
subplot(2,1,2);
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)
The periodic signal is typically a repeating pattern (e.g., sine wave, square wave).
MATLAB Code
n = 0:10;
% Perform convolution
y = conv(x_p, x_a);
ny = 0:length(y)-1;
subplot(3,1,1);
xlabel('n'); ylabel('Amplitude');
subplot(3,1,2);
xlabel('n'); ylabel('Amplitude');
grid on;
subplot(3,1,3);
xlabel('n'); ylabel('Amplitude');
grid on;
-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:
where:
MATLAB Code
% Frequency index
k = 0:N-1;
phase = angle(X_k);
% Display Results
disp('DTFS Coefficients:');
disp(X_k.');
subplot(2,1,1);
xlabel('k'); ylabel('|X[k]|');
grid on;
subplot(2,1,2);
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
where:
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
magnitude = abs(x_n);
phase = angle(x_n);
% Display Results
subplot(3,1,1);
xlabel('n'); ylabel('x[n]');
grid on;
subplot(3,1,2);
stem(k, magnitude, 'filled');
xlabel('n'); ylabel('|x[n]|');
grid on;
subplot(3,1,3);
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:
where:
The DTFT gives the magnitude spectrum and phase spectrum of x[n]x[n]x[n].
for k = 1:length(omega)
End
magnitude = abs(Xw);
phase = angle(Xw);
subplot(2,1,1);
grid on;
subplot(2,1,2);
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:
where:
Integration is performed over the frequency range −π≤ω≤π-\pi \leq \omega \leq \
pi−π≤ω≤π.
MATLAB Code to Compute IDTFT and Plot the Magnitude & Phase Response
for i = 1:length(n)
x_n(i) = trapz(omega, (1/(2*pi)) * Xw .* exp(1j * omega * n(i)));
end
magnitude = abs(x_n);
phase = angle(x_n);
% Display results
subplot(3,1,1);
xlabel('n'); ylabel('x[n]');
grid on;
subplot(3,1,2);
xlabel('n'); ylabel('|x[n]|');
grid on;
subplot(3,1,3);
grid on;
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)
-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:
where:
for k = 0:N-1
for n = 0:N-1
end
end
magnitude = abs(X_k);
phase = angle(X_k);
k = 0:N-1;
subplot(2,1,1);
grid on;
subplot(2,1,2);
grid on;
% Display Results
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:
where:
for n = 0:N-1
for k = 0:N-1
end
end
% Compute magnitude and phase of reconstructed sequence
magnitude = abs(x_n);
phase = angle(x_n);
n = 0:N-1;
subplot(3,1,1);
xlabel('n'); ylabel('x[n]');
grid on;
subplot(3,1,2);
xlabel('n'); ylabel('|x[n]|');
grid on;
subplot(3,1,3);
grid on;
% Display Results
-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ω)
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
% Plot verification
figure;
subplot(2,1,1);
legend('LHS', 'RHS');
xlabel('\omega'); ylabel('Magnitude');
grid on;
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ω)
n0 = 3; % Time shift
% Plot verification
figure;
xlabel('\omega'); ylabel('Magnitude');
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))
% Plot verification
figure;
xlabel('\omega'); ylabel('Magnitude');
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ω)
X_mult = X1 .* X2;
% Plot verification
figure;
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ω)
x_mult = x1 .* x2;
L = length(omega);
% Plot verification
figure;
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ω)
x_n_mult = n .* x1;
% Plot verification
figure;
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
a = 2; % Scaling factor
x_scaled = zeros(size(x1));
x_scaled(valid_indices) = x1(valid_indices);
% Plot verification
figure;
xlabel('\omega'); ylabel('Magnitude');
grid on;
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(z)D(z)X(z) = \frac{N(z)}{D(z)}X(z)=D(z)N(z)
where:
zeros_ = roots(numerator);
poles_ = roots(denominator);
figure;
zplane(numerator, denominator);
xlabel('Real Part');
ylabel('Imaginary Part');
grid on;
% Display results
disp(zeros_);
disp(poles_);
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;
disp('Residues:');
disp(residues);
disp('Poles:');
disp(poles);
disp('Direct Terms:');
disp(direct_terms);
n = 0:10;
% Compute inverse Z-transform manually using the formula x[n] = sum(R_k * P_k^n)
for k = 1:length(residues)
end
end
figure;
xlabel('n'); ylabel('x[n]');
grid on;
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.
where:
y_init = [];
y = filter(b, a, x);
% Plot input and output signals
figure;
subplot(2,1,1);
xlabel('n'); ylabel('x[n]');
grid on;
subplot(2,1,2);
xlabel('n'); ylabel('y[n]');
grid on;
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:
where:
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
magnitude = abs(H);
phase = angle(H);
% Plot magnitude response
figure;
subplot(2,1,1);
title('Magnitude Response');
ylabel('|H(e^{j\omega})|');
grid on;
subplot(2,1,2);
title('Phase Response');
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)