1) Consider two sinusoidal signals of frequencies 10Hz and 110Hz.
Write MATLAB code
to plot these two signals on the same axes. These two signals are then sampled at
the sampling rate of 100Hz. Plot the two sampled signals in another figure. What is
your inference by looking at these two sampled signals? Why is this inference? Can
you say how these two frequencies 10Hz and 110Hz are related? Can you suggest any
other combination of frequencies that will lead to the same inference?
Ans-----
% Parameters
fs = 100; % Sampling rate (Hz)
t_cont = 0:0.001:1; % Continuous time (1 ms resolution)
t_sampled = 0:1/fs:1; % Sampled time points (10 ms resolution)
% Frequencies
f1 = 10; % Frequency of first signal (Hz)
f2 = 110; % Frequency of second signal (Hz)
% Continuous signals
signal1 = sin(2*pi*f1*t_cont);
signal2 = sin(2*pi*f2*t_cont);
% Sampled signals
sampled_signal1 = sin(2*pi*f1*t_sampled);
sampled_signal2 = sin(2*pi*f2*t_sampled);
% Plot continuous signals
figure;
plot(t_cont, signal1, 'b', 'DisplayName', '10Hz Signal'); hold on;
plot(t_cont, signal2, 'r', 'DisplayName', '110Hz Signal');
xlabel('Time (s)');
ylabel('Amplitude');
title('Continuous Signals (10Hz and 110Hz)');
legend;
grid on;
% Plot sampled signals
figure;
stem(t_sampled, sampled_signal1, 'b', 'DisplayName', 'Sampled 10Hz Signal'); hold on;
stem(t_sampled, sampled_signal2, 'r', 'DisplayName', 'Sampled 110Hz Signal');
xlabel('Time (s)');
ylabel('Amplitude');
title('Sampled Signals (10Hz and 110Hz)');
legend;
grid on;
2.With the help of MATLAB code, plot elementary discrete-time signals (a) Unit impulse, (b)
Unit ramp, (c) unit step, and (d) exponential signal for different values of constant 'a'. Any
significance of unit impulse signal? What is the important thing that we must keep in mind
while sampling a continuous-time signal to obtain a discrete-time signal? Can you say
whether these signals are energy signals or power signals?
Ans-----
% Define time vector
n = -10:10; % Discrete-time indices
% (a) Unit Impulse Signal
impulse = (n == 0);
% (b) Unit Step Signal
step = (n >= 0);
% (c) Unit Ramp Signal
ramp = n .* (n >= 0);
% (d) Exponential Signal for different 'a'
a_values = [0.5, 1, 2]; % Different values of 'a'
exponential_signals = arrayfun(@(a) a.^n .* (n >= 0), a_values, 'UniformOutput', false);
% Plotting the signals
figure;
% (a) Unit Impulse Signal
subplot(2, 2, 1);
stem(n, impulse, 'filled');
title('Unit Impulse Signal');
xlabel('n'); ylabel('Amplitude');
grid on;
% (b) Unit Step Signal
subplot(2, 2, 2);
stem(n, step, 'filled');
title('Unit Step Signal');
xlabel('n'); ylabel('Amplitude');
grid on;
% (c) Unit Ramp Signal
subplot(2, 2, 3);
stem(n, ramp, 'filled');
title('Unit Ramp Signal');
xlabel('n'); ylabel('Amplitude');
grid on;
% (d) Exponential Signals for different 'a'
subplot(2, 2, 4);
hold on;
colors = ['r', 'g', 'b'];
for i = 1:length(a_values)
stem(n, exponential_signals{i}, colors(i));
end
title('Exponential Signals for Different a');
xlabel('n'); ylabel('Amplitude');
legend(arrayfun(@(a) ['a = ' num2str(a)], a_values, 'UniformOutput', false));
grid on;
hold off;
3Write a MATLAB program to perform x(n-3), x(n + 3) x(- n) x(- n + 3) x(- n - 3) and x(n / 3)
and x(2n) of the given sequence x(n) = \{1, 1, 2, 3, 4, 6\} . Can you say whether x(2n) leads to
time compression or time expansion frequency analysis of the signal x(2n) then in the
frequency domain can you say whether the signal x(2n) leads to expansion or compression
in the frequency domain? What about x(n / 2) ? Can time-shifting change a signal from
causal to noncausal?
Ans-----
% Original sequence x(n)
x = [1, 1, 2, 3, 4, 6];
n = 0:length(x)-1; % Indices for x(n)
% Shift operations
x_n_minus_3 = circshift(x, 3); % x(n-3)
x_n_plus_3 = circshift(x, -3); % x(n+3)
x_minus_n = fliplr(x); % x(-n)
x_minus_n_plus_3 = circshift(x_minus_n, -3); % x(-n+3)
x_minus_n_minus_3 = circshift(x_minus_n, 3); % x(-n-3)
% Scaling operations
n_scaled_down = 0:3; % Downsampled indices for x(n/3)
x_n_div_3 = x(1:3:end); % x(n/3), using downsampling
n_scaled_up = 0:2*(length(x)-1); % Expanded indices for x(2n)
x_2n = zeros(1, length(n_scaled_up));
x_2n(1:2:end) = x; % x(2n), upsampling with zeros
% Plot the original sequence and transformed sequences
figure;
% Plot x(n)
subplot(3, 2, 1);
stem(n, x, 'b', 'filled', 'DisplayName', 'x(n)');
title('Original Sequence x(n)');
xlabel('n'); ylabel('Amplitude');
grid on;
% Plot x(n-3)
subplot(3, 2, 2);
stem(n, x_n_minus_3, 'r', 'filled', 'DisplayName', 'x(n-3)');
title('x(n-3)');
xlabel('n'); ylabel('Amplitude');
grid on;
% Plot x(n+3)
subplot(3, 2, 3);
stem(n, x_n_plus_3, 'g', 'filled', 'DisplayName', 'x(n+3)');
title('x(n+3)');
xlabel('n'); ylabel('Amplitude');
grid on;
% Plot x(-n)
subplot(3, 2, 4);
stem(-fliplr(n), x_minus_n, 'k', 'filled', 'DisplayName', 'x(-n)');
title('x(-n)');
xlabel('n'); ylabel('Amplitude');
grid on;
% Plot x(-n+3)
subplot(3, 2, 5);
stem(-fliplr(n), x_minus_n_plus_3, 'm', 'filled', 'DisplayName', 'x(-n+3)');
title('x(-n+3)');
xlabel('n'); ylabel('Amplitude');
grid on;
% Plot x(-n-3)
subplot(3, 2, 6);
stem(-fliplr(n), x_minus_n_minus_3, 'c', 'filled', 'DisplayName', 'x(-n-3)');
title('x(-n-3)');
xlabel('n'); ylabel('Amplitude');
grid on;
% Plot scaling transformations in another figure
figure;
% Plot x(n/3)
subplot(2, 1, 1);
stem(n_scaled_down, x_n_div_3, 'r', 'filled', 'DisplayName', 'x(n/3)');
title('x(n/3) - Time Expansion');
xlabel('n'); ylabel('Amplitude');
grid on;
% Plot x(2n)
subplot(2, 1, 2);
stem(n_scaled_up, x_2n, 'b', 'filled', 'DisplayName', 'x(2n)');
title('x(2n) - Time Compression');
xlabel('n'); ylabel('Amplitude');
grid on;
4) Write a MATLAB program to perform the linear convolution of the following sequences
x(n)= (1, 2, 3, 1) and h(n)= (1, 2, 1, 1) to obtain y(n) where y(n) = x(n) * h(n) Here indicates
linear ↑
convolution. Plot x(n), h(n) and y(n). What is the rule for deciding the time index at which
the output y(n) starts? How is the total length y(n) related to that of x(n) and h(n)?
Ans-----
% Define the input sequences
x = [1 2 3 1];
h = [1 2 1 1];
% Perform linear convolution
y = conv(x, h);
% Generate time indices for plotting
n_x = 0:length(x)-1;
n_h = 0:length(h)-1;
n_y = 0:length(y)-1;
% Plot the sequences
figure;
subplot(3,1,1);
stem(n_x, x);
xlabel('n');
ylabel('x(n)');
title('Input Sequence x(n)');
subplot(3,1,2);
stem(n_h, h);
xlabel('n');
ylabel('h(n)');
title('Impulse Response h(n)');
subplot(3,1,3);
stem(n_y, y);
xlabel('n');
ylabel('y(n)');
title('Output Sequence y(n)');
% Display the output sequence
disp('Output sequence y(n):');
disp(y);
% Rule for deciding the time index at which the output y(n) starts
disp('Rule for deciding the time index at which the output y(n) starts:');
disp('The output sequence y(n) starts at n = 0.');
% Relationship between the lengths of x(n), h(n), and y(n)
disp('Relationship between the lengths of x(n), h(n), and y(n):');
disp(['Length of y(n) = Length of x(n) + Length of h(n) - 1']);
disp(['Length of y(n) = ', num2str(length(x)), ' + ', num2str(length(h)), ' - 1 = ',
num2str(length(y))]);
5) Write a MATLAB program to verify the commutative, associative and distributive property
of the convolution of the following sequences. Display proper results.
x(n) = (1, 2, 3, 1) and h(n) = (1, 2, 1, - 1) . What is the rule for deciding the time index at
which the
How y(n) related to that of x(n) and h(n)?
Ans-----
% Define the input sequences
x = [1 2 3 1];
h1 = [1 2 1 -1];
h2 = [1 -1];
h3 = [1 1];
% Commutative Property
y1 = conv(x, h1);
y2 = conv(h1, x);
disp('Commutative Property:')
disp(['y1 = conv(x, h1): ', mat2str(y1)])
disp(['y2 = conv(h1, x): ', mat2str(y2)])
disp(['Verification: ', num2str(isequal(y1, y2))])
% Associative Property
y3 = conv(conv(x, h2), h3);
y4 = conv(x, conv(h2, h3));
disp('Associative Property:')
disp(['y3 = conv(conv(x, h2), h3): ', mat2str(y3)])
disp(['y4 = conv(x, conv(h2, h3)): ', mat2str(y4)])
disp(['Verification: ', num2str(isequal(y3, y4))])
% Distributive Property
y5 = conv(x, (h2 + h3));
y6 = conv(x, h2) + conv(x, h3);
disp('Distributive Property:')
disp(['y5 = conv(x, (h2 + h3)): ', mat2str(y5)])
disp(['y6 = conv(x, h2) + conv(x, h3): ', mat2str(y6)])
disp(['Verification: ', num2str(isequal(y5, y6))])
% Rule for deciding the time index at which the output y(n) starts
disp('Rule for deciding the time index at which the output y(n) starts:')
disp('The output sequence y(n) typically starts at n = 0, assuming both input sequences are
causal (i.e., they are zero for n < 0).')
% Relationship between the lengths of x(n), h(n), and y(n)
disp('Relationship between the lengths of x(n), h(n), and y(n):')
disp(['Length of y(n) = Length of x(n) + Length of h(n) - 1'])
6) ) Write a MATLAB program to perform the circular convolution of the following sequences
x(n)= (1,2,3,1) and h(n) (1,1,1) to obtain y(n) where y(n) =x(n) odot h(n). Here indicates
circularconvolution. Plot x(n), and y(n). What is the rule for deciding the time index at which
the h(n) output y(n) starts? How total length y(n) related to that of x(n) and h(n)?
Ans-----
% Define the input sequences
x = [1 2 3 1];
h = [1 1 1];
% Calculate circular convolution
y = cconv(x, h, length(x));
% Generate time indices for plotting
n_x = 0:length(x)-1;
n_y = 0:length(y)-1;
% Plot the sequences
figure;
subplot(2,1,1);
stem(n_x, x);
xlabel('n');
ylabel('x(n)');
title('Input Sequence x(n)');
subplot(2,1,2);
stem(n_y, y);
xlabel('n');
ylabel('y(n)');
title('Circular Convolution Output y(n)');
% Display the output sequence
disp('Output sequence y(n):');
disp(y);
% Rule for deciding the time index at which the output y(n) starts
disp('Rule for deciding the time index at which the output y(n) starts:')
disp('In circular convolution, the output sequence y(n) typically starts at the same index as
the input sequence x(n).');
% Relationship between the lengths of x(n), h(n), and y(n)
disp('Relationship between the lengths of x(n), h(n), and y(n):')
disp(['For circular convolution, the length of y(n) is equal to the length of x(n).']);
7) Using MATLAB code to obtain an expression of the Z-transform of a ^ prime prime and n
using properties of Z-transform. Can you say what will be the ROC of these signals? Why is
ROC necessary?
Ans-----
syms n z A(z)
% Define the Z-transform of a(n) as A(z)
eqn1 = 'A(z) == ztrans(a(n), n, z)';
% Apply the time-shifting property: Z{a(n-k)} = z^(-k) * A(z)
eqn2 = 'ztrans(a(n-1), n, z) == z^(-1) * A(z)';
eqn3 = 'ztrans(a(n-2), n, z) == z^(-2) * A(z)';
% Define the second-order difference equation (assuming a(n) is causal)
eqn4 = 'a(n) == a(n-1) + a(n-2)';
% Apply the Z-transform to the difference equation
eqn5 = 'ztrans(eqn4, n, z)';
% Substitute the Z-transforms of a(n-1) and a(n-2)
eqn6 = subs(eqn5, {ztrans(a(n-1), n, z), ztrans(a(n-2), n, z)}, {z^(-1) * A(z), z^(-2) * A(z)});
% Solve for A(z)
eqn7 = solve(eqn6, A(z));
% Simplify the expression
eqn8 = simplify(eqn7);
% Display the result
disp(['Z-transform of a(n): ', char(eqn8)]);
% Find the Z-transform of n*a(n) using the differentiation property
eqn9 = 'ztrans(n*a(n), n, z) == -z * diff(A(z), z)';
% Calculate the Z-transform of n*a(n)
eqn10 = subs(eqn9, A(z), eqn8);
% Simplify the expression
eqn11 = simplify(eqn10);
% Display the result
disp(['Z-transform of n*a(n): ', char(eqn11)]);
8) Write a MATLAB code to find the inverse Z-transform of n * a ^ n Also verify the linearity
property of Z-transform considering two signals 2 ^ - and 3 ^ n Can you find out the ROC of
the signals 2 ^ prime prime and 3 ^ *(x ^ 2 + 0.9x + 0.5)/(x ^ 2 + 0.5)
Ans-----
% Define symbolic variables
syms z n a
% Z-transform of a^n
X1_z = z / (z - a);
% Z-transform of n*a^n using the differentiation property
X2_z = -z * diff(X1_z, z);
% Simplify the expression
X2_z = simplify(X2_z);
% Inverse Z-transform of n*a^n
x2_n = iztrans(X2_z, z, n);
% Display the result
disp(['Inverse Z-transform of n*a^n: ', char(x2_n)]);
% ------------------------------------------------------------------------
% Verify linearity property
% Define signals
x1_n = 2^n;
x2_n = 3^n;
% Z-transforms of the signals
X1_z = ztrans(x1_n, n, z);
X2_z = ztrans(x2_n, n, z);
% Z-transform of a*x1_n + b*x2_n (a and b are constants)
a = 2;
b = 3;
X3_z = ztrans(a*x1_n + b*x2_n, n, z);
% Verify linearity
if isequal(X3_z, a*X1_z + b*X2_z)
disp('Linearity property verified.');
else
disp('Linearity property not verified.');
end
% ------------------------------------------------------------------------
% Find ROC of 2^n and 3^n
% ROC of 2^n
disp(['ROC of 2^n: |z| > 2']);
% ROC of 3^n
disp(['ROC of 3^n: |z| > 3']);
9) Write a MATLAB code to plot poles and zeros of Also plot these poles and zeros in the Z-
plane. How do you define poles and zeros of the system? Should ROC contain
poles and/or zeros?
Ans-----
% Define the numerator and denominator polynomials
num = [1, 0.9, 0.5];
den = [1, 0.5];
% Find poles and zeros
z, p, k = tf2zpk(num, den);
% Plot poles and zeros
figure;
zplane(num, den);
title('Pole-Zero Plot');
% Display poles and zeros
disp('Zeros:');
disp(z);
disp('Poles:');
disp(p);
% Explanation of poles and zeros
disp('Explanation:');
disp('Poles are the values of ''z'' that make the denominator of the transfer function zero.');
disp('Zeros are the values of ''z'' that make the numerator of the transfer function zero.');
disp('The Region of Convergence (ROC) does not contain any poles.');
10) Write a MATLAB program to find the 5-point DFT of the given sequence
x(n)= [1,2,3,4,5]
Plot magnitude response and phase response. Also calculate the IFFT of the DFT obtained to
get back the signal x(n). Plot all the signals.
How does discrete-Fourier transform differ from discrete-time Fourier transform? If the
sequence is id length L and if you are to calculate N-point DFT of this sequence, then how N
and L should be related? Justify your answer.
Ans-----
% Define the input sequence
x = [1, 2, 3, 4, 5];
% Calculate the 5-point DFT
X = fft(x, length(x));
% Calculate magnitude and phase response
magX = abs(X);
phaseX = angle(X);
% Calculate the IFFT to recover the original signal
x_recovered = ifft(X);
% Plot the signals
figure;
subplot(3,1,1);
stem(0:length(x)-1, x);
xlabel('n');
ylabel('x(n)');
title('Original Signal');
subplot(3,1,2);
stem(0:length(x)-1, magX);
xlabel('k');
ylabel('Magnitude');
title('Magnitude Spectrum');
subplot(3,1,3);
stem(0:length(x)-1, phaseX);
xlabel('k');
ylabel('Phase');
title('Phase Spectrum');
figure;
stem(0:length(x)-1, real(x_recovered));
xlabel('n');
ylabel('x(n)');
title('Recovered Signal');
% Difference between DFT and DTFT
disp('Difference between DFT and DTFT:');
disp('- DFT is a discrete-time, discrete-frequency transform.');
disp('- DTFT is a discrete-time, continuous-frequency transform.');
disp('- DFT is a sampled version of the DTFT.');
disp('- DFT is computed for a finite-length sequence, while DTFT is computed for infinite-
length sequences.');
11) Write a MATLAB program to verify the properties of twiddle factor. Can you write down
the DFT and IDFT formulas using twiddle factor? What is the value of Wa and We?
Ans-----
% Define the twiddle factor function
twiddle = @(N, k) exp(-1i*2*pi*k/N);
% Define the number of points for verification
N = 8;
% Verify properties of twiddle factor
% 1. Periodicity: Wn^(k+N) = Wn^k
for k = 0:N-1
assert(abs(twiddle(N, k) - twiddle(N, k+N)) < 1e-10, 'Periodicity property not verified');
end
% 2. Symmetry: Wn^(N-k) = conj(Wn^k)
for k = 0:N-1
assert(abs(twiddle(N, N-k) - conj(twiddle(N, k))) < 1e-10, 'Symmetry property not
verified');
end
% 3. Inverse: Wn^(-k) = conj(Wn^k)
for k = 0:N-1
assert(abs(twiddle(N, -k) - conj(twiddle(N, k))) < 1e-10, 'Inverse property not verified');
end
% Display verification message
disp('Twiddle factor properties verified.');
% DFT and IDFT formulas using twiddle factor
disp('DFT formula:');
disp('X(k) = sum_{n=0}^{N-1} x(n) * Wn^(n*k)');
disp('IDFT formula:');
disp('x(n) = (1/N) * sum_{k=0}^{N-1} X(k) * Wn^(-n*k)');
% Values of Wa and We
disp('Wa = 1');
disp('We = exp(-1i*2*pi/N)');
12) Write a MATLAB program to design and plot magnitude response and phase response of
digital Butterworth lowpass filter with the following specifications.
passband attenuationc 4dB.
stopband attenuation: 30dB
passband digital frequency:0.2*pi rad/sample
stopband digital frequency: 0.4*pi rad/sample
Can you draw the sketch and mark different parameters and define those parameters? How
does order affect the magnitude response?
Ans-----
% Define filter specifications
Rp = 4; % Passband ripple (dB)
Rs = 30; % Stopband attenuation (dB)
fp = 0.2*pi; % Passband edge frequency (rad/sample)
fs = 0.4*pi; % Stopband edge frequency (rad/sample)
% Calculate filter order and cutoff frequency
[N, Wn] = buttord(fp, fs, Rp, Rs);
% Design Butterworth filter
[b, a] = butter(N, Wn);
% Generate frequency response
[h, w] = freqz(b, a, 1024);
% Convert frequency to normalized frequency (radians)
w = w*pi;
% Plot magnitude response in dB
figure;
plot(w/pi, 20*log10(abs(h)));
xlabel('Normalized Frequency (x pi rad/sample)');
ylabel('Magnitude (dB)');
title('Butterworth Lowpass Filter Magnitude Response');
grid on;
% Plot phase response in degrees
figure;
plot(w/pi, angle(h)*180/pi);
xlabel('Normalized Frequency (x pi rad/sample)');
ylabel('Phase (degrees)');
title('Butterworth Lowpass Filter Phase Response');
grid on;
% Sketch and parameter definitions
figure;
plot(w/pi, 20*log10(abs(h)));
xlabel('Normalized Frequency (x pi rad/sample)');
ylabel('Magnitude (dB)');
title('Butterworth Lowpass Filter Magnitude Response');
grid on;
hold on;
% Mark passband edge
xline(fp/pi, '--r', 'LineWidth', 1.5);
text(fp/pi, -5, 'fp', 'HorizontalAlignment', 'right');
% Mark stopband edge
xline(fs/pi, '--r', 'LineWidth', 1.5);
text(fs/pi, -5, 'fs', 'HorizontalAlignment', 'left');
% Mark passband ripple
yline(-Rp, '--g', 'LineWidth', 1.5);
text(0.1, -Rp, 'Rp', 'HorizontalAlignment', 'right');
% Mark stopband attenuation
yline(-Rs, '--g', 'LineWidth', 1.5);
text(0.5, -Rs, 'Rs', 'HorizontalAlignment', 'left');
hold off;
% Effect of order on magnitude response
disp('Effect of filter order on magnitude response:');
disp('- Higher order filters provide sharper transitions between passband and stopband.');
disp('- Higher order filters generally have a more gradual roll-off in the transition band.');
disp('- Higher order filters can be more complex to implement and may exhibit more
pronounced phase distortion.');
13Write a MATLAB program to design and plot magnitude response and phase response of
digital Butterworth highpass filter with the following specifications.
passband attenuation:.4dB
stopband attenuation: 30dB
passband digital frequency: 0.4*pi rad/sample
stopband digital frequency: 0.2*pi rad/sample
Can you draw the sketch and mark different parameters and define those parameters? How
does order affect the magnitude response?
Ans-----
% Specifications
Ap = 0.4; % Passband attenuation in dB
As = 30; % Stopband attenuation in dB
fp = 0.4 * pi; % Passband frequency (rad/sample)
fs = 0.2 * pi; % Stopband frequency (rad/sample)
% Normalize frequencies (0 to 1 range, for digital filters)
fs_norm = 2; % Sampling frequency assumed normalized to 1
Wp = fp / pi; % Normalized passband frequency
Ws = fs / pi; % Normalized stopband frequency
% Design filter order and cutoff frequency
[N, Wn] = buttord(Wp, Ws, Ap, As); % Calculate order and cutoff frequency
[b, a] = butter(N, Wn, 'high'); % Design Butterworth highpass filter
% Frequency response
[H, f] = freqz(b, a, 1024, fs_norm); % Compute frequency response
% Plot magnitude and phase response
figure;
subplot(2, 1, 1);
plot(f, 20*log10(abs(H))); % Magnitude response in dB
title('Magnitude Response of Butterworth Highpass Filter');
xlabel('Normalized Frequency (\times \pi rad/sample)');
ylabel('Magnitude (dB)');
grid on;
subplot(2, 1, 2);
plot(f, angle(H)); % Phase response in radians
title('Phase Response of Butterworth Highpass Filter');
xlabel('Normalized Frequency (\times \pi rad/sample)');
ylabel('Phase (radians)');
grid on;
14Write a MATLAB program to design and plot magnitude response and phase response of
digital Butterworth bandpass filter with the following specifications.
passband attenuation (in dB) for bandpass filter: 4
stopband attenuation(in dB) for bandpass filter: 30
passband digital frequency range in rad/sample for bandpass filter: [0.2*pi,0.4*pi]
stopband digital frequency range in rad/sample for bandpass filter: (0.1*pi,0.5*pi]
Can you draw the sketch and mark different parameters and define those parameters? How
does order affect the magnitude response?
Ans----
% Specifications
Ap = 4; % Passband attenuation in dB
As = 30; % Stopband attenuation in dB
passband_range = [0.2*pi, 0.4*pi]; % Passband frequency range (rad/sample)
stopband_range = [0.1*pi, 0.5*pi]; % Stopband frequency range (rad/sample)
% Normalize frequencies (0 to 1 range, for digital filters)
fs_norm = 2; % Sampling frequency assumed normalized to 1
Wp = passband_range / pi; % Normalized passband frequencies
Ws = stopband_range / pi; % Normalized stopband frequencies
% Design filter order and cutoff frequencies
[N, Wn] = buttord(Wp, Ws, Ap, As); % Calculate order and cutoff frequencies
[b, a] = butter(N, Wn, 'bandpass'); % Design Butterworth bandpass filter
% Frequency response
[H, f] = freqz(b, a, 1024, fs_norm); % Compute frequency response
% Plot magnitude and phase response
figure;
subplot(2, 1, 1);
plot(f, 20*log10(abs(H))); % Magnitude response in dB
title('Magnitude Response of Butterworth Bandpass Filter');
xlabel('Normalized Frequency (\times \pi rad/sample)');
ylabel('Magnitude (dB)');
grid on;
subplot(2, 1, 2);
plot(f, angle(H)); % Phase response in radians
title('Phase Response of Butterworth Bandpass Filter');
xlabel('Normalized Frequency (\times \pi rad/sample)');
ylabel('Phase (radians)');
grid on;
15Write a MATLAB program to design and plot magnitude response and phase response of
digital Butterworth bandstop filter with the following specifications.
passband attenuation (in dB) for bandstop filter..4
stopband attenuation (in dB) for bandstop filter:30
passband digital frequency range in rad/sample for bandstop filter: (0.1*pi,0.5*pi]
stopband digital frequency range in rad/sample for bandstop filter: [0.2*pi,0.4*pi]
Can you draw the sketch and mark different parameters and define those parameters? How
does order affect the magnitude response?
Ans—
% Specifications
Ap = 0.4; % Passband attenuation in dB
As = 30; % Stopband attenuation in dB
passband_freq = [0.1*pi, 0.5*pi]; % Passband frequency range (rad/sample)
stopband_freq = [0.2*pi, 0.4*pi]; % Stopband frequency range (rad/sample)
% Normalize frequencies (from rad/sample to normalized 0-1 range)
fs = 2; % Sampling frequency (assumed normalized to 1 for digital filters)
Wp = passband_freq / pi; % Passband edges normalized
Ws = stopband_freq / pi; % Stopband edges normalized
% Design filter order and cutoff frequencies
[N, Wn] = buttord(Ws, Wp, As, Ap, 's'); % Calculate order and cutoff
[b, a] = butter(N, Wn, 'stop'); % Design Butterworth bandstop filter
% Frequency response
[H, f] = freqz(b, a, 1024, fs); % Compute frequency response
% Magnitude and Phase response
figure;
subplot(2, 1, 1);
plot(f, 20*log10(abs(H)));
title('Magnitude Response of Butterworth Bandstop Filter');
xlabel('Normalized Frequency (\times \pi rad/sample)');
ylabel('Magnitude (dB)');
grid on;
subplot(2, 1, 2);
plot(f, angle(H));
title('Phase Response of Butterworth Bandstop Filter');
xlabel('Normalized Frequency (\times \pi rad/sample)');
ylabel('Phase (radians)');
grid on;
16 Write a MATLAB program to design and plot magnitude response of digital FIR filter using
rectangular window with the following specifications
rp (passband ripple) =.05 rs (stopband ripple) =.04
fp (passband discrete-time frequency)=1500 rad/sample
fs (passband discrete-time frequency)=2000 rad/sample
sampling frequency-9000 rad/sample.
Why is windowing technique used?
Can you draw the sketch of rectangular window?
Mention some properties of FIR filter.
Ans---
% Specifications
rp = 0.05; % Passband ripple
rs = 0.04; % Stopband ripple
fp = 1500; % Passband frequency (rad/sample)
fs = 2000; % Stopband frequency (rad/sample)
sampling_freq = 9000; % Sampling frequency (rad/sample)
% Normalize frequencies
Wp = fp / (sampling_freq / 2); % Passband edge (normalized)
Ws = fs / (sampling_freq / 2); % Stopband edge (normalized)
% Calculate filter order using formula
delta_p = rp; % Tolerance in passband
delta_s = rs; % Tolerance in stopband
delta = min(delta_p, delta_s);
A = -20*log10(delta); % Attenuation
if A > 50
beta = 0.1102*(A - 8.7);
elseif A >= 21
beta = 0.5842*(A - 21)^0.4 + 0.07886*(A - 21);
else
beta = 0;
end
% Calculate filter order
N = ceil((A - 7.95) / (2.285 * (Ws - Wp) * pi));
% Ensure odd order for linear phase
if mod(N, 2) == 0
N = N + 1;
end
% Design Rectangular window
rectangular_window = rectwin(N);
% Design FIR filter using Rectangular window
b = fir1(N-1, Wp, rectangular_window);
% Frequency response
[H, f] = freqz(b, 1, 1024, sampling_freq);
% Plot magnitude response
figure;
plot(f, abs(H));
title('Magnitude Response of FIR Filter (Rectangular Window)');
xlabel('Frequency (rad/sample)');
ylabel('Magnitude');
grid on;
% Plot Rectangular window
figure;
stem(rectangular_window, 'filled');
title('Rectangular Window');
xlabel('Sample');
ylabel('Amplitude');
grid on;
17 Write a MATLAB program to design and plot magnitude response of digital FIR filter using
hanning window with the following specifications
rp (passband ripple) =.05 rs (stopband ripple) =.04
fp (passband discrete-time frequency)=1500 rad/sample fs (passband discrete-time
frequency)=2000 rad/sample
sampling frequency=9000 rad/sample
Why is windowing technique used?
Can you draw the sketch of Hanning window?
Mention some properties of FIR filter.
Ans---
% Specifications
rp = 0.05; % Passband ripple
rs = 0.04; % Stopband ripple
fp = 1500; % Passband frequency (rad/sample)
fs = 2000; % Stopband frequency (rad/sample)
sampling_freq = 9000; % Sampling frequency (rad/sample)
% Normalize frequencies
Wp = fp / (sampling_freq / 2); % Passband edge (normalized)
Ws = fs / (sampling_freq / 2); % Stopband edge (normalized)
% Calculate filter order using Kaiser formula
delta_p = rp; % Tolerance in passband
delta_s = rs; % Tolerance in stopband
delta = min(delta_p, delta_s);
A = -20*log10(delta); % Attenuation
if A > 50
beta = 0.1102*(A - 8.7);
elseif A >= 21
beta = 0.5842*(A - 21)^0.4 + 0.07886*(A - 21);
else
beta = 0;
end
% Calculate filter order
N = ceil((A - 7.95) / (2.285 * (Ws - Wp) * pi));
% Ensure odd order for linear phase
if mod(N, 2) == 0
N = N + 1;
end
% Design Hanning window
hanning_window = hann(N);
% Design FIR filter using Hanning window
b = fir1(N-1, Wp, hanning_window);
% Frequency response
[H, f] = freqz(b, 1, 1024, sampling_freq);
% Plot magnitude response
figure;
plot(f, abs(H));
title('Magnitude Response of FIR Filter (Hanning Window)');
xlabel('Frequency (rad/sample)');
ylabel('Magnitude');
grid on;
% Plot Hanning window
figure;
stem(hanning_window, 'filled');
title('Hanning Window');
xlabel('Sample');
ylabel('Amplitude');
grid on;
18)Write a MATLAB program to design and plot magnitude response of digital FIR filter
using Blackman window with the following specifications
rp (passband ripple) =.05
rs (stopband ripple) =.04
fp (passband discrete-time frequency)=1500 rad/sample fs (passband discrete-time
frequency)=2000 rad/sample
sampling frequency-9000 rad/sample. Why is windowing technique used?
Can you draw the sketch of Blackman window? Mention some properties of FIR filter.
Ans---
% Specifications
rp = 0.05; % Passband ripple
rs = 0.04; % Stopband ripple
fp = 1500; % Passband frequency (rad/sample)
fs = 2000; % Stopband frequency (rad/sample)
sampling_freq = 9000; % Sampling frequency (rad/sample)
% Normalize frequencies
Wp = fp / (sampling_freq / 2); % Passband edge (normalized)
Ws = fs / (sampling_freq / 2); % Stopband edge (normalized)
% Calculate filter order using Kaiser formula
delta_p = rp; % Tolerance in passband
delta_s = rs; % Tolerance in stopband
delta = min(delta_p, delta_s);
A = -20*log10(delta); % Attenuation
if A > 50
beta = 0.1102*(A - 8.7);
elseif A >= 21
beta = 0.5842*(A - 21)^0.4 + 0.07886*(A - 21);
else
beta = 0;
end
% Calculate filter order
N = ceil((A - 7.95) / (2.285 * (Ws - Wp) * pi));
% Ensure odd order for linear phase
if mod(N, 2) == 0
N = N + 1;
end
% Design Blackman window
blackman_window = blackman(N);
% Design FIR filter using Blackman window
b = fir1(N-1, Wp, blackman_window);
% Frequency response
[H, f] = freqz(b, 1, 1024, sampling_freq);
% Plot magnitude response
figure;
plot(f, abs(H));
title('Magnitude Response of FIR Filter');
xlabel('Frequency (rad/sample)');
ylabel('Magnitude');
grid on;
% Plot Blackman window
figure;
stem(blackman_window, 'filled');
title('Blackman Window');
xlabel('Sample');
ylabel('Amplitude');
grid on;