Download as pdf or txt
Download as pdf or txt
You are on page 1of 55

Week 1:

1). Generate different signals in continuous time, discrete time. Plot and stem
respectively. Change time scale and write your observation.
CODE:
1. For Continuous
clc;
clear all;
close all;
t=0:.001:1;
f=input('Enter the value of frequency');
a=input('Enter the value of amplitude');
subplot(3,3,1); y=a*sin(2*pi*f*t);
plot(t,y,'r');
xlabel('time');
ylabel('amplitude');
title('sine wave') grid on;
subplot(3,3,2);
z=a*cos(2*pi*f*t);
plot(t,z); xlabel('time');
ylabel('amplitude');
title('cosine wave');
grid on;
subplot(3,3,3);
s=a*square(2*pi*f*t);
plot(t,s);
xlabel('time');
ylabel('amplitude');
title('square wave');
grid on;
subplot(3,3,4);
plot(t,t);
xlabel('time');

1|P ag e
ylabel('amplitude');
title('ramp wave');
grid on;
subplot(3,3,5);
plot(t,a,'r');
xlabel('time');
ylabel('amplitude');
title('unit step wave');
grid on;

INPUT:
Enter the value of frequency: 2
Enter the value of amplitude: 1

OUTPUT:

2|P ag e
2.For Discrete

CODE:

clc;
clear all;
close all;
n=0:1:50;
f=input('enter the value of frequency');
a=input('enter the value of amplitude');
N=input('enter the length of unit step');
subplot(3,3,1);
y=a*sin(2*pi*f*n);
stem(n,y,'r');
xlabel('time');
ylabel('amplitude');
title('sine wave');
grid on;
subplot(3,3,2);
z=a*cos(2*pi*f*n);
stem(n,z);
xlabel('time');
ylabel('amplitude');
title('cosine wave');
grid on;
subplot(3,3,3);
s=a*square(2*pi*f*n);
stem(n,s);
xlabel('time');
ylabel('amplitude');
title('square wave');
grid on;
subplot(3,3,4);
stem(n,n);
xlabel('time');

3|P ag e
ylabel('amplitude');
title('ramp wave');
grid on;
subplot(3,3,5);
x=0:N-1;
d=ones(1,N);
stem(x,d,'r');
xlabel('time');
ylabel('amplitude');
title('unit step wave');
grid on;

INPUT:
Enter the value of frequency: 0.03
Enter the value of amplitude: 1
Enter the value of unit step: 9

OUTPUT:

4|P ag e
Week 2:

1).Generate and plot the following signal

X(n)=2δ(n+2) – δ(n-4) for -5≤ n ≤ 5

CODE:

clc;
close all;
clear all;
n = -5:5;
% Define the range of n
X = 2 * (n == -2) - (n == 4);
% Generating X(n)
stem(n, X);
% Plotting the stem plot
xlabel('n');
ylabel('X(n)');
title('Signal X(n) = 2δ(n+2) - δ(n-4)');

OUTPUT:

5|P ag e
2). What are the results of these sets of commands?
A = zeros(1,5);
for n = 1:4
for m = 1:3
A = A + n*m;
end
end
disp(A);
CODE:
clc;
close all;
clear all;
A = zeros(1, 5);
for n = 1:4
for m = 1:3
A(n) = A(n) + n*m;
end
end
disp(A);

OUTPUT:
6 12 18 24 0

3). Perform STEM of the following signals. Choose the range of n should be.
1. f(n) = u(n) − u(n − 4)
2. g(n) = n · u(n) − 2 (n − 4) · u(n − 4) + (n − 8) · u(n − 8)
3. x(n) = δ(n) − 2 δ(n − 4)
4. y(n) = (0.9)n (u(n) − u(n − 20))
5. v(n) = cos(0.12 πn) u(n)

CODE:
clc;

6|P ag e
close all;
clear all;
n = 0:5; % Define the range of n
u_n = heaviside(n); % Generate unit step function for n >= 0
f = u_n - [zeros(1,4), u_n(1:end-4)]; % Generating f(n)
stem(n, f); % Plotting the stem plot
xlabel('n');
ylabel('f(n)');
title('Stem Plot of f(n) = u(n) - u(n-4)');
n = 0:15; % Define the range of n
u_n = heaviside(n); % Generate unit step function for n >= 0
g = n .* u_n - 2 * (n-4) .* (u_n - [zeros(1,4), u_n(1:end-4)]) + (n-8) .* (u_n - [zeros(1,8),
u_n(1:end-8)]); %
Generating g(n)
stem(n, g); % Plotting the stem plot
xlabel('n');
ylabel('g(n)');
title('Stem Plot of g(n) = n*u(n) - 2*(n-4)*u(n-4) + (n-8)*u(n-8)');
n = 0:10; % Define the range of n
x = (n == 0) - 2 * (n == 4); % Generating x(n)
stem(n, x); % Plotting the stem plot
xlabel('n');
ylabel('x(n)');
title('Stem Plot of x(n) = δ(n) - 2δ(n-4)');
n = 0:40; % Define the range of n
u_n = heaviside(n); % Generate unit step function for n >= 0
y = (0.9).^n .* (u_n - [zeros(1,20), u_n(1:end-20)]); % Generating y(n)
stem(n, y); % Plotting the stem plot
xlabel('n');
ylabel('y(n)');
title('Stem Plot of y(n) = (0.9)^n * (u(n) - u(n-20))');
n = 0:100; % Define the range of n

7|P ag e
u_n = heaviside(n); % Generate unit step function for n >= 0
v = cos(0.12 * pi * n) .* u_n; % Generating v(n)
stem(n, v); % Plotting the stem plot
xlabel('n');
ylabel('v(n)');
title('Stem Plot of v(n) = cos(0.12 \pi n) * u(n)');

OUTPUT:

8|P ag e
4. Let f(n) = u(n) − u(n − 4)
g(n) = n · u(n) − 2 (n − 4) · u(n − 4) + (n − 8) · u(n − 8).
Use the CONV command to compute the convolutions.
(a) f(n) ∗ f(n)
(b) f(n) ∗ g(n)
(c) g(n) ∗ δ(n)
(d) g(n) ∗ g(n) Comment on your observations: Do you see any relationship between f(n)
∗ f(n) and g(n) ? Compare f(n) with f(n) ∗ f(n) and with f(n) ∗ f(n) ∗ f(n). What happens
as you repeatedly convolve this signal with itself?
CODE:
clc;
close all;
clear all;
% Define the functions f(n) and g(n)
u = @(n) double(n >= 0); % Define the unit step function
f = @(n) u(n) - u(n - 4);
g = @(n) n .* u(n) - 2 * (n - 4) .* u(n - 4) + (n - 8) .* u(n - 8);
% (a) f(n) ∗ f(n)
n = 0:20; % Define the range of values for n
conv_ff = conv(f(n), f(n));
% (b) f(n) ∗ g(n)
conv_fg = conv(f(n), g(n));
% (c) g(n) ∗ δ(n) (where δ(n) is the impulse function)
delta = @(n) double(n == 0); % Define the impulse function
conv_g_delta = conv(g(n), delta(n));
% (d) g(n) ∗ g(n)
conv_gg = conv(g(n), g(n));
% Display results
disp('(a) f(n) ∗ f(n):');
disp(conv_ff);
disp('(b) f(n) ∗ g(n):');
disp(conv_fg);
disp('(c) g(n) ∗ δ(n):');
disp(conv_g_delta);
disp('(d) g(n) ∗ g(n):');
disp(conv_gg);
con_fff=conv(f(n),conv_ff);
disp(‘(e) f(n)*conv_ff:’);
disp(con_fff);
figure;
subplot(5, 1, 1);

9|P ag e
stem(conv_ff);
title('f(n) ∗ f(n)');
subplot(5, 1, 2);
stem(conv_fg);
title('f(n) ∗ g(n)');
subplot(5, 1, 3);
stem(conv_g_delta);
title('g(n) ∗ δ(n)');
subplot(5, 1, 4);
stem(conv_gg);
title('g(n) ∗ g(n)');
subplot(5,1,5);
stem(con_fff);
title(‘f(n)*f(n)*f(n)’);

OUTPUT:

10 | P a g e
5). Create a simple impulse response for an LTI system h(n)=ones(1,11).
STEM this response. Find output y(n) using CONV command where
y(n)=x(n)*h(n), Take x(n) to be unique
of your choice(may be random, any electrical signals ). Plot y(n),x(n)
and h(n)
What is your observation from this operation?. Change h(n) and
comment.
CODE:
clc;
close all;
clear all;
% Generating a random input signal x(n)
x = randn(1, 20);
% Defining the impulse response h(n)
h = ones(1, 11);
% Calculating the output using the convolution operation
y = conv(x, h);
% Plotting the signals
subplot(3,1,1);
stem(0:length(x)-1, x);
title('Input Signal x(n)');
xlabel('n');
ylabel('Amplitude');
subplot(3,1,2);
stem(0:length(h)-1, h);
title('Impulse Response h(n)');
xlabel('n');
ylabel('Amplitude');
subplot(3,1,3);
stem(0:length(y)-1, y);
title('Output Signal y(n) = x(n) * h(n)');
xlabel('n');
ylabel('Amplitude');

11 | P a g e
OUTPUT:

6).If y(n) = x(n) + 2 x(n − 1) − 0.95 y(n − 1) is difference equation of the system. Write
your own Matlab function, “diffeq” to implement this difference equation using a for
loop. Assume the input signal isN-samples long (0 ≤ n ≤ N − 1), your program should
find the first N sample of the output y(n) (0 ≤ n ≤ N − 1). (Hint Matlab indexing starts
from 1).

a) Use your Matlab function to compute y1 = diffeq(x1): x1=1/2 *u(n) − u(n − 10).
CODE:

clc;
close all;
clear all;
clc;
close all;
clear all;
x=[2 1 3 5 4];
y=diffeq(x);
disp(y);

12 | P a g e
function y=diffeq(x)
N=length(x);
y=zeros(1,N);
for n=1:N
if n==1
y(n)=x(n);
else
y(n)=x(n)+2*x(n-1)-0.95*y(n-1);
end
end
end

OUTPUT:
2.0000 3.1000 2.0550 9.0478 5.4046

7). Sketch the graphs (STEM) produced by the following code.


n = 0:10;
x = (n >= 1) - (n >=5);
stem(n,x)
h = (n == 5);
y = conv(h,x);
stem(0:20,y)
z = conv(x,x);
stem(0:20,z)

13 | P a g e
8). Write a Matlab code that will plot a sinusoid of frequency 50 Hz for 5 cycles. Change
the frequency of the sine wave from the previous section to 440 Hz and plot the signal
for the interval [−1, 1]. IF you Plot the samples that lie in the interval [0, 0.01] instead
what is your observation?
CODE:

clc;
close all;
clear all;
% Plot a sinusoid of frequency 50 Hz for 5 cycles
fs = 1000; % Sampling frequency (in Hz)
t1 = 0:1/fs:5*(1/50); % Time vector for 5 cycles of 50 Hz
x1 = sin(2*pi*50*t1);
figure;
subplot(3,1,1);
plot(t1, x1);
title('Sinusoid of 50 Hz for 5 cycles');
xlabel('Time (s)');
ylabel('Amplitude');
% Change frequency to 440 Hz and plot for the interval [-1, 1]
t2 = -1:1/fs:1; % Time vector for interval [-1, 1]
x2 = sin(2*pi*440*t2);
subplot(3,1,2);
plot(t2, x2);
title('Sinusoid of 440 Hz for interval [-1, 1]');
xlabel('Time (s)');
ylabel('Amplitude');
xlim([-1,1]);
% Plot samples within the interval [0, 0.01]
t3 = 0:1/fs:0.01; % Time vector for interval [0, 0.01]
x3 = sin(2*pi*440*t3);
subplot(3,1,3);
stem(t3, x3);
title('Samples within interval [0, 0.01]');
14 | P a g e
xlabel('Time (s)');
ylabel('Amplitude');
sgtitle('Sinusoid Plots');

OUTPUT:

9). Form an array of length 28, with zeros everywhere except in the 14 th position,
which has value 1. This array gives a discrete-time impulse, which is a signal that is zero
everywhere except at one sample point.. Plot the impulse signal, using both plot and
stem.
CODE:

clc;
close all;
clear all;
% Form an array with zeros and a single 1 at the 14th position
impulse_array = zeros(1, 28);
impulse_array(14) = 1;

15 | P a g e
% Plot the impulse signal using the plot function
figure;
subplot(2, 1, 1);
plot(impulse_array, 'o-');
title('Impulse Signal - Using plot');
xlabel('Sample Index');
ylabel('Amplitude');
% Plot the impulse signal using the stem function
subplot(2, 1, 2);
stem(impulse_array);
title('Impulse Signal - Using stem');
xlabel('Sample Index');
ylabel('Amplitude');
sgtitle('Discrete-Time Impulse Signal');

OUTPUT:

16 | P a g e
10).Write a MATLAB program to perform following operation on signals( choose
suitable
signals of your choice) for atleast length of not less than 10
a) add x1(n) and x2(n),
b) y(n)=prod(x1,x2), where prod is function to multiply.
c) y(n)=x1(n)*x1(-n)
d) y(n)=x1(2n)
CODE:
clc;
close all;
clear all;
% Define signals x1 and x2
n = 0:9;
x1 = sin(0.2*pi*n); % Example signal 1
x2 = cos(0.4*pi*n); % Example signal 2
% a) Addition of x1(n) and x2(n)
sum_signal = x1 + x2;
% b) Element-wise multiplication of x1(n) and x2(n)
product_signal = x1 .* x2;
% c) Multiplication of x1(n) and x1(-n)
mirror_signal = fliplr(x1); % Flip x1 to get x1(-n)
multiply_mirror_signal = x1 .* mirror_signal;
% d) x1(2n)
downsample_signal = x1(1:2:end);
% Display the results
disp('a) Addition of x1(n) and x2(n):')
disp(sum_signal)
disp('b) Element-wise multiplication of x1(n) and x2(n):')
disp(product_signal)
disp('c) Multiplication of x1(n) and x1(-n):')
disp(multiply_mirror_signal)
disp('d) x1(2n):')
disp(downsample_signal)

17 | P a g e
OUTPUT:

a) Addition of x1(n) and x2(n):


1.0000 0.8968 0.1420 0.1420 0.8968 1.0000 -0.2788 -1.7601 -1.7601 -
0.2788

b) Element-wise multiplication of x1(n) and x2(n):


0 0.1816 -0.7694 -0.7694 0.1816 0.0000 -0.1816 0.7694 0.7694 -0.1816

c) Multiplication of x1(n) and x1(-n):


0 -0.5590 -0.9045 -0.5590 0.0000 0.0000 -0.5590 -0.9045 -0.5590 0

d) x1(2n):
0 0.9511 0.5878 -0.5878 -0.9511

Week 3:

1).What is y after running the following MATLAB/octave commands 1)Y = X.*G


ii)y=x*g. Take x(n),g(n) of your choice (hint: Do not use DFT to compute ; instead use
the properties of the DFT. )
CODE:
clc;
close all;
clear all;
% Define two signals x(n) and g(n)
x = [1, 2, 3, 4]; % Define your signal x(n)
g = [0.5, 0.5]; % Define your signal g(n)
% Zero-pad the sequences to the appropriate length for circular convolution
len = length(x) + length(g) - 1;
x_padded = [x, zeros(1, len - length(x))];
g_padded = [g, zeros(1, len - length(g))];
% Compute the DFTs of zero-padded x and g
X = fft(x_padded);
G = fft(g_padded);
% Multiply the DFTs element-wise
Y = X .* G;

18 | P a g e
% Take the inverse DFT of the multiplied result
y = ifft(Y);
% Plot the input signals
subplot(3, 1, 1);
stem(x_padded, 'r', 'LineWidth', 2);
title('Input Signal x(n)');
xlabel('n');
ylabel('Amplitude');
subplot(3, 1, 2);
stem(g_padded, 'b', 'LineWidth', 2);
title('Input Signal g(n)');
xlabel('n');
ylabel('Amplitude');
% Plot the output signal
subplot(3, 1, 3);
stem(real(y), 'g', 'LineWidth', 2);
title('Output Signal y(n)');
xlabel('n');
ylabel('Amplitude');
% Display the result
disp('Result of Convolution using DFT Properties:');

OUTPUT:

19 | P a g e
2).Write Matlab/octave commands to listen to a one-second sinusoidal waveform given
by t ∈ [-1,1], f(t) = 2* sin(2π × 440t). sample at the rate of 8,000
samples/second.Construct a sound signal with a sequence of frequencies and Listen to
the sound, write the MATLAB commands used. Change the time interval, sample
frequency and write your observation.
CODE:
clc;
close all;
clear all;
% Define the time values from -1 to 1 with a step size corresponding to 1/8000 seconds
t = -1:1/8000:1;
% Generate the sinusoidal waveform
f_t = 2 * sin(2 * pi * 440 * t);
% Play the sound
sound(f_t, 8000);

OUTPUT:
The sound is generated at 440hz and at sample 8k/s.
If we increase frequency, the sound will be sharper with high pitch. Quality of the sound will
increase.

20 | P a g e
3. Consider a causal discrete-time LTI system implemented using the difference
equation,
y(n) = 0.1 x(n) − 0.1176 x(n − 1) + 0.1 x(n − 2) + 1.7119 y(n − 1) − 0.81 y(n − 2)
frequency response of the system, H(e j ω), Plot the magnitude and phase of the
frequency
response of the system using the suitable Matlab/Octave commands.
Plot the poles and zeros of the transfer function.
CODE:
clc;
close all;
clear all;
b = [0.1, -0.1176, 0.1];
a = [1, -1.7119, 0.81];
[H, w] = freqz(b, a);
subplot(2, 1, 1);
plot(w, abs(H));
title('Magnitude Response');
xlabel('Frequency (\omega)');
ylabel('|H(e^{j\omega})|');
subplot(2, 1, 2);
plot(w, angle(H));
title('Phase Response');
xlabel('Frequency (\omega)');
ylabel('Phase(H(e^{j\omega}))');
zplane(b, a);
title('Poles and Zeros');

OUTPUT:

21 | P a g e
4). Verify the circular convolution property of the DFT in Matlab/octave.
Write two Matlab functions to compute the circular convolution of two
sequences of equal length. One function should use built in FFT command ,
the other function should compute the circular convolution directly and not
use the FFT. Verify if both functions in Matlab/octave give the same
results.
CODE:
clc;
close all;
clear all;
% Verification
x = [1, 2, 3, 4];
y = [5, 6, 7, 8];
result_fft = circular_convolution_fft(x, y);
result_direct = circular_convolution_direct(x, y);

22 | P a g e
% Check if the results are approximately equal
tolerance = 1e-10;
if max(abs(result_fft - result_direct)) < tolerance
disp('Both methods produce approximately equal results.');
else
disp('Results differ. There may be a numerical precision issue.');
end
% Plot the input sequences
figure;
subplot(3, 1, 1);
stem(x);
title('Input Sequence x');
subplot(3, 1, 2);
stem(y);
title('Input Sequence y');
% Plot the circular convolution results
subplot(3, 1, 3);
stem(result_fft);
hold on;
stem(result_direct);
legend('FFT Circular Convolution', 'Direct Circular Convolution');
title('Circular Convolution Results');
% Function to compute circular convolution using FFT
function result_fft = circular_convolution_fft(x, y)
N = length(x);
if N ~= length(y)
error('Input sequences must have equal length');
end
X = fft(x);
Y = fft(y);
result_fft = ifft(X .* Y);
end
% Function to compute circular convolution directly
function result_direct = circular_convolution_direct(x, y)

23 | P a g e
N = length(x);
if N ~= length(y)
error('Input sequences must have equal length');
end
result_direct = zeros(1, N);
for n = 1:N
for k = 1:N
result_direct(n) = result_direct(n) + x(k) * y(mod(n - k, N) + 1);
end
end
end

OUTPUT:
Both the methods produce approximate results.

5). The following MATLAB commands define three ten-point signals.


x1 = sin([0:9]/10*pi);
x2 = sin([0:9]/9*2*pi);
x3 = sin([0:9]/10*2*pi); write MATLAB /octave commands to find their DFT using
FFT.

24 | P a g e
Draw their magnitude spectrum and find pole location. Comment on results.
CODE:
clc;
close all;
clear all;
% Define the signals
x1 = sin([0:9]/10*pi);
x2 = sin([0:9]/9*2*pi);
x3 = sin([0:9]/10*2*pi);
% Compute the DFT using FFT
X1 = fft(x1);
X2 = fft(x2);
X3 = fft(x3);
% Calculate the magnitude spectrum
mag_spectrum_X1 = abs(X1);
mag_spectrum_X2 = abs(X2);
mag_spectrum_X3 = abs(X3);
% Plot the magnitude spectrum
subplot(3,1,1);
stem(mag_spectrum_X1);
title('Magnitude Spectrum of x1');
subplot(3,1,2);
stem(mag_spectrum_X2);
title('Magnitude Spectrum of x2');
subplot(3,1,3);
stem(mag_spectrum_X3);
title('Magnitude Spectrum of x3');
% Find pole location (roots of the polynomial)
pole_X1 = roots(x1);
pole_X2 = roots(x2);
pole_X3 = roots(x3);

OUTPUT:

25 | P a g e
6). If x(n)=[1,2,1,2]. Write a program to compute a. N=4 point DFT, b. N=8 point DFT
c)N=16 point DFT. .Plot magnitude spectrum .write your observations.
CODE:
clc;
close all;
clear all;
% Given signal x(n)
x = [2, 1, 2, 1];
% Compute DFT for different point sizes
N_values = [4, 8, 16];
for i = 1:length(N_values)
N = N_values(i);
X = fft(x, N);
disp(['N=' num2str(N) ' Point DFT:']);
disp(X);
% Plot magnitude spectrum
magnitude = abs(X);
stem(magnitude);
xlabel('Frequency');

26 | P a g e
ylabel('Magnitude');
title(['Magnitude Spectrum (N=' num2str(N) ')']);
grid on;
end

OUTPUT:
N=4 Point DFT:
6 0 2 0

N=8 Point DFT:


6.0000 + 0.0000i 2.0000 - 3.4142i 0.0000 + 0.0000i 2.0000 + 0.5858i
2.0000 + 0.0000i 2.0000 - 0.5858i 0.0000 + 0.0000i 2.0000 + 3.4142i

N=16 Point DFT:


Columns 1 through 8

6.0000 + 0.0000i 4.7208 - 2.7208i 2.0000 - 3.4142i 0.0446 - 1.9554i


0.0000 + 0.0000i 1.1270 + 0.8730i 2.0000 + 0.5858i 2.1077 + 0.1077i

Columns 9 through 16

2.0000 + 0.0000i 2.1077 - 0.1077i 2.0000 - 0.5858i 1.1270 - 0.8730i


0.0000 + 0.0000i 0.0446 + 1.9554i 2.0000 + 3.4142i 4.7208 + 2.7208i

27 | P a g e
7). Using Matlab/octave program get the response y(n) of the LTI system with impulse
response h(n)=[1, 2, 3, 2, 1], and input to the system is [1,2,3,4]. [hint:use FFT]. Find
number of complex multiplications and additions.
CODE:
clc;
close all;
clear all;
h = [1, 2, 3, 2, 1]; % Impulse response
x = [1, 2, 3, 4]; % Input signal
% Length of the output signal
output_length = length(h) + length(x) - 1;
% Perform FFT
fft_size = 2^nextpow2(output_length);
H = fft(h, fft_size);
X = fft(x, fft_size);
% Frequency domain multiplication
Y = H .* X;
% Inverse FFT to get time domain output
y = ifft(Y);
% Extract the relevant part of the output
y = real(y(1:output_length));
% Number of complex multiplications and additions
num_multiplications = 2 * fft_size - 1; % Multiplications in FFT
num_additions = 2 * fft_size - 2; % Additions in FFT
fprintf('Output y(n): %s\n', mat2str(y));
fprintf('Number of complex multiplications: %d\n', num_multiplications);
fprintf('Number of complex additions: %d\n', num_additions);

OUTPUT:
Output y(n): [1 4 10 18 22 20 11 4]
Number of complex multiplications: 15
Number of complex additions: 14

28 | P a g e
8). Using Matlab plot magnitude response , pole zero plot and identify the
type of system
i) H(z) =0.1/1−0.8z^-1
ii)h(n) =1/2[δ(n) − δ(n − 1)]
iii) h(n) =1/2[δ(n) − δ(n − 2)]
iv) h(n) =1/2[δ(n) + δ(n − 2)]
CODE:
clc;
close all;
clear all;
% Define the transfer function
num = 0.1;
den = [1, -0.8];
% Create a digital filter
sys = tf(num, den, 1); % 1 indicates a discrete-time system
% Plot the magnitude response
figure;
subplot(2, 1, 1);
freqz(num, den);
title('Magnitude Response of H(z)');
% Plot the pole-zero plot
subplot(2, 1, 2);
pzmap(sys);
title('Pole-Zero Plot of H(z)');
% Define the impulse response
h = [1, -1]/2;
% Create a digital filter
sys_ii = dfilt.df2(h);
% Plot the magnitude response
figure;
subplot(2, 1, 1);
freqz(sys_ii);
title('Magnitude Response of h(n)');

29 | P a g e
% Plot the pole-zero plot
subplot(2, 1, 2);
zplane(sys_ii);
title('Pole-Zero Plot of h(n)');
% Define the impulse response for system iii
h_iii = [1, zeros(1, 1), -1]/2; % Impulse response coefficients
% Create a digital filter
sys_iii = dfilt.df2(h_iii);
% Plot the magnitude response
figure;
subplot(2, 1, 1);
freqz(sys_iii);
title('Magnitude Response of h(n) for System iii');
% Plot the pole-zero plot
subplot(2, 1, 2);
zplane(sys_iii);
title('Pole-Zero Plot of h(n) for System iii');
% Define the impulse response for system iv
h_iv = [1, zeros(1, 1), 1]/2; % Impulse response coefficients
% Create a digital filter
sys_iv = dfilt.df2(h_iv);
% Plot the magnitude response
figure;
subplot(2, 1, 1);
freqz(sys_iv);
title('Magnitude Response of h(n) for System iv');
% Plot the pole-zero plot
subplot(2, 1, 2);
zplane(sys_iv);
title('Pole-Zero Plot of h(n) for System iv');

OUTPUT:

30 | P a g e
31 | P a g e
32 | P a g e
9).Write the program to Demonstrate circular convolution, circular
correlation, conjugate symmetry frequency and time shifting properties of
DFT . b) demonstrate through program, how linear convolution can be
performed using circular convolution using DFT.[note: take sequences of
your own ] Comment on results obtained.
CODE:
clc;
close all;
clear all;
% Define two sequences of your own
x = [1, 2, 3, 4]; % Sequence 1
h = [1, 1, 1]; % Sequence 2
% Circular Convolution using DFT
N = length(x) + length(h) - 1;
X = fft(x, N);
H = fft(h, N);
convolution_result = ifft(X .* H);
% Circular Correlation using DFT
correlation_result = ifft(X .* conj(H));
% Conjugate Symmetry
conj_symmetry = isequal(fft(conj(x)), conj(fft(x)));
% Frequency Shifting (circular shift in frequency domain)
k_shift = 2; % Shift amount
X_shifted = circshift(X, [0, k_shift]);
x_shifted = ifft(X_shifted);
% Time Shifting (circular shift in time domain)
n_shift = 1; % Shift amount
x_time_shifted = circshift(x, [0, n_shift]);
% Linear Convolution using Circular Convolution (via DFT)
linear_convolution_result = ifft(fft(x, N) .* fft(h, N));
% Plotting the sequences
figure;
subplot(3, 2, 1);

33 | P a g e
stem(0:length(x)-1, x, 'r', 'LineWidth', 2);
title('Sequence 1 (x)');
xlabel('n');
ylabel('x[n]');
grid on;
subplot(3, 2, 2);
stem(0:length(h)-1, h, 'b', 'LineWidth', 2);
title('Sequence 2 (h)');
xlabel('n');
ylabel('h[n]');
grid on;
% Plotting Circular Convolution result
subplot(3, 2, 3);
stem(0:N-1, convolution_result, 'g', 'LineWidth', 2);
title('Circular Convolution Result');
xlabel('n');
ylabel('y_{circular}[n]');
grid on;
% Plotting Circular Correlation result
subplot(3, 2, 4);
stem(0:N-1, correlation_result, 'm', 'LineWidth', 2);
title('Circular Correlation Result');
xlabel('n');
ylabel('r[n]');
grid on;
% Plotting Frequency Shifted Sequence
subplot(3, 2, 5);
stem(0:N-1, real(x_shifted), 'k', 'LineWidth', 2);
title('Frequency Shifted Sequence');
xlabel('n');
ylabel('x_{shifted}[n]');
grid on;
% Plotting Time Shifted Sequence
subplot(3, 2, 6);

34 | P a g e
stem(0:length(x_time_shifted)-1, x_time_shifted, 'c', 'LineWidth', 2);
title('Time Shifted Sequence');
xlabel('n');
ylabel('x_{time-shifted}[n]');
grid on;
% Adjusting the layout
sgtitle('Circular Convolution and Correlation Results');
% Plotting Linear Convolution result
figure;
stem(0:length(linear_convolution_result)-1, linear_convolution_result, 'b', 'LineWidth', 2);
title('Linear Convolution using Circular Convolution Result');
xlabel('n');
ylabel('y_{linear}[n]');
grid on;

OUTPUT:

35 | P a g e
10). Write program to illustrate overlap add/save method to perform block convolution
of long input with system impulse response of size 3. Indicate DFTs required to compute
this block convolution. [note each student has to choose their own long input and decide
on size of each block convolution i.e N]
CODE:
clc;
close all;
clear all;
% Define your long input signal
long_input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; % Replace this with your own input
% Define the impulse response
impulse_response = [1, 0.5, 0.25];
% Choose the block size (N)
N = 4; % You can change this to your desired block size
% Perform block convolution using overlap-add method
M = length(impulse_response);
L = N - M + 1;
% Zero-pad the impulse response to the length of N
impulse_response_padded = [impulse_response, zeros(1, N - M)];
% Initialize the output array
output = zeros(1, length(long_input) + length(impulse_response_padded) - 1);

36 | P a g e
% Perform block convolution
for i = 1:L:length(long_input)
block = long_input(i:min(i + N - 1, length(long_input)));
block_padded = [block, zeros(1, N - length(block))];
block_fft = fft(block_padded);
block_convolved = ifft(block_fft .* fft(impulse_response_padded));
output(i:i + length(block_convolved) - 1) = output(i:i + length(block_convolved) - 1) +
block_convolved;
end
% Display the resulting convolved signal
disp('Convolved signal:');
disp(output);

OUTPUT:
Convolved signal:
3.7500 3.5000 11.5000 13.0000 18.5000 20.0000 25.5000 27.0000
32.5000 34.0000 29.2500 37.5000 8.7500 3.0000 0

Week 4:

FILTER DESIGN simulation

1). Design digital LP Butterworth filter with given specifications: Wp=0.2pi; ws=0.3pi,
Ap=1dB, As=15dB. T=1sec use i)IIM and ii)BLT
Plot magnitude, phase response and pole zero plot.

CODE:
clc;
close all;
clear all;
% Butterworth Filter Design using IIM and BLT
% Given specifications
Wp = 0.2 * pi; % Passband frequency in rad/sample
Ws = 0.3 * pi; % Stopband frequency in rad/sample
Ap = 1; % Passband ripple in dB
As = 15; % Stopband attenuation in dB
T = 1; % Sampling period in seconds
% Design Butterworth filter using IIM
[num_iim, den_iim] = butterworth_iim(Wp, Ws, Ap, As, T);

37 | P a g e
% Design Butterworth filter using BLT
[num_blt, den_blt] = butterworth_blt(Wp, Ws, Ap, As, T);
% Frequency response analysis
analyze_filter(num_iim, den_iim, T, 'IIM');
analyze_filter(num_blt, den_blt, T, 'BLT');
function [num, den] = butterworth_iim(Wp, Ws, Ap, As, T)
% Butterworth filter design using IIM
[B, A] = butter(1, Wp/(2*pi), 's');
[num, den] = impinvar(B, A, 1/T);
end
function [num, den] = butterworth_blt(Wp, Ws, Ap, As, T)
% Butterworth filter design using BLT
[B, A] = butter(1, Wp/(2*pi), 's');
[num, den] = bilinear(B, A, 1/T);
end
function analyze_filter(num, den, T, method)
% Frequency response analysis and plotting
figure;

% Magnitude response
subplot(3, 1, 1);
freqz(num, den, 'half', 2/T);
title(['Magnitude Response - ' method]);

% Phase response
subplot(3, 1, 2);
[H, Freq] = freqz(num, den, 2/T);
plot(Freq, rad2deg(angle(H)));
xlabel('Frequency (Hz)');
ylabel('Phase (degrees)');
title(['Phase Response - ' method]);

% Pole-Zero plot
subplot(3, 1, 3);
zplane(num, den);
title(['Pole-Zero Plot - ' method]);
end

OUTPUT:

38 | P a g e
2). Convert the above design to HP Butterworth filter and plot , magnitude, phase
response and pole zero plot.

CODE:
clc;
close all;
clear all;
% High-pass Butterworth Filter Design using IIM and BLT
% Given specifications
Wp = 0.3 * pi; % Passband frequency in rad/sample (changed for high-pass)
Ws = 0.2 * pi; % Stopband frequency in rad/sample (changed for high-pass)
Ap = 1; % Passband ripple in dB

39 | P a g e
As = 15; % Stopband attenuation in dB
T = 1; % Sampling period in seconds
% Design high-pass Butterworth filter using IIM
[num_iim_hp, den_iim_hp] = butterworth_iim(Wp, Ws, Ap, As, T);
% Design high-pass Butterworth filter using BLT
[num_blt_hp, den_blt_hp] = butterworth_blt(Wp, Ws, Ap, As, T);
% Frequency response analysis
analyze_filter(num_iim_hp, den_iim_hp, T, 'IIM - High-pass');
analyze_filter(num_blt_hp, den_blt_hp, T, 'BLT - High-pass');
function [num, den] = butterworth_iim(Wp, Ws, Ap, As, T)
% Butterworth filter design using IIM
[B, A] = butter(1, Wp/(2*pi), 'high', 's');
[num, den] = impinvar(B, A, 1/T);
end
function [num, den] = butterworth_blt(Wp, Ws, Ap, As, T)
% Butterworth filter design using BLT
[B, A] = butter(1, Wp/(2*pi), 'high', 's');
[num, den] = bilinear(B, A, 1/T);
end
function analyze_filter(num, den, T, method)
% Frequency response analysis and plotting
figure;

% Magnitude response
subplot(3, 1, 1);
freqz(num, den, 'half', 2/T);
title(['Magnitude Response - ' method]);

% Phase response
subplot(3, 1, 2);
[H, Freq] = freqz(num, den, 2/T);
plot(Freq, rad2deg(angle(H)));
xlabel('Frequency (Hz)');
ylabel('Phase (degrees)');
title(['Phase Response - ' method]);

% Pole-Zero plot
subplot(3, 1, 3);
zplane(num, den);
title(['Pole-Zero Plot - ' method]);
end

OUTPUT:

40 | P a g e
3). Design digital LP Chebyshev type I filter with given specifications: Wp=0.2pi;
ws=0.3pi, Ap=1dB, As=15dB. T=1sec use i)IIM and ii)BLT
Plot magnitude, phase response and pole zero plot.

CODE:
clc;
close all;
clear all;
% Chebyshev Type I Filter Design using IIM and BLT
% Given specifications
Wp = 0.2 * pi; % Passband frequency in rad/sample
Ws = 0.3 * pi; % Stopband frequency in rad/sample

41 | P a g e
Ap = 1; % Passband ripple in dB
As = 15; % Stopband attenuation in dB
T = 1; % Sampling period in seconds
% Design Chebyshev Type I filter using IIM
[num_iim, den_iim] = chebyshev_iim(Wp, Ws, Ap, As, T);
% Design Chebyshev Type I filter using BLT
[num_blt, den_blt] = chebyshev_blt(Wp, Ws, Ap, As, T);
% Frequency response analysis
analyze_filter(num_iim, den_iim, T, 'IIM - Chebyshev Type I');
analyze_filter(num_blt, den_blt, T, 'BLT - Chebyshev Type I');
function [num, den] = chebyshev_iim(Wp, Ws, Ap, As, T)
% Chebyshev Type I filter design using IIM
[B, A] = cheby1(4, Ap, Wp/(2*pi), 's');
[num, den] = impinvar(B, A, 1/T);
end
function [num, den] = chebyshev_blt(Wp, Ws, Ap, As, T)
% Chebyshev Type I filter design using BLT
[B, A] = cheby1(4, Ap, Wp/(2*pi), 's');
[num, den] = bilinear(B, A, 1/T);
end
function analyze_filter(num, den, T, method)
% Frequency response analysis and plotting
figure;

% Magnitude response
subplot(3, 1, 1);
freqz(num, den, 'half', 2/T);
title(['Magnitude Response - ' method]);

% Phase response
subplot(3, 1, 2);
[H, Freq] = freqz(num, den, 2/T);
plot(Freq, rad2deg(angle(H)));
xlabel('Frequency (Hz)');
ylabel('Phase (degrees)');
title(['Phase Response - ' method]);

% Pole-Zero plot
subplot(3, 1, 3);
zplane(num, den);
title(['Pole-Zero Plot - ' method]);

OUTPUT:

42 | P a g e
4). Convert the above design to HP Butterworth filter and plot , magnitude, phase
response and pole zero plot.

CODE:
clc;
close all;
clear all;
% High-pass Butterworth Filter Design from Chebyshev Type I using IIM and BLT
% Given specifications
Wp = 0.2 * pi; % Passband frequency in rad/sample
Ws = 0.3 * pi; % Stopband frequency in rad/sample
Ap = 1; % Passband ripple in dB
As = 15; % Stopband attenuation in dB
T = 1; % Sampling period in seconds
% Convert to high-pass Butterworth filter specifications
Wp_hp = pi - Wp; % High-pass passband frequency
43 | P a g e
Ws_hp = pi - Ws; % High-pass stopband frequency
% Design high-pass Butterworth filter using IIM
[num_iim_hp, den_iim_hp] = butterworth_iim(Wp_hp, Ws_hp, Ap, As, T);
% Design high-pass Butterworth filter using BLT
[num_blt_hp, den_blt_hp] = butterworth_blt(Wp_hp, Ws_hp, Ap, As, T);
% Frequency response analysis
analyze_filter(num_iim_hp, den_iim_hp, T, 'IIM - High-pass Butterworth');
analyze_filter(num_blt_hp, den_blt_hp, T, 'BLT - High-pass Butterworth');
function [num, den] = butterworth_iim(Wp, Ws, Ap, As, T)
% Butterworth filter design using IIM
[B, A] = butter(1, Wp/(2*pi), 'high', 's');
[num, den] = impinvar(B, A, 1/T);
end
function [num, den] = butterworth_blt(Wp, Ws, Ap, As, T)
% Butterworth filter design using BLT
[B, A] = butter(1, Wp/(2*pi), 'high', 's');
[num, den] = bilinear(B, A, 1/T);
end
function analyze_filter(num, den, T, method)
% Frequency response analysis and plotting
figure;

% Magnitude response
subplot(3, 1, 1);
freqz(num, den, 'half', 2/T);
title(['Magnitude Response - ' method]);

% Phase response
subplot(3, 1, 2);
[H, Freq] = freqz(num, den, 2/T);
plot(Freq, rad2deg(angle(H)));
xlabel('Frequency (Hz)');
ylabel('Phase (degrees)');
title(['Phase Response - ' method]);

% Pole-Zero plot
subplot(3, 1, 3);
zplane(num, den);
title(['Pole-Zero Plot - ' method]);
end

OUTPUT:

44 | P a g e
5). Design digital LP Chebyshev type II filter with given specifications: Wp=0.2pi;
ws=0.3pi, Ap=1dB, As=15dB. T=1sec use i)IIM and ii)BLT Plot magnitude, phase
response and pole zero plot.

CODE:
clc;
close all;
clear all;
% Digital Chebyshev Type II Filter Design and Plotting
% Analog filter specifications
Wp = 0.2*pi;
Ws = 0.3*pi;
Ap_dB = 1;
As_dB = 15;
% Convert ripple and attenuation to linear scale
Ap = 10^(Ap_dB/20);
As = 10^(As_dB/20);

45 | P a g e
% Calculate analog filter order
N = cheb2ord(Wp, Ws, Ap_dB, As_dB, 's');
% Design analog Chebyshev Type II filter
[b, a] = cheby2(N, As_dB, Ws, 's');
% Plot analog filter response
figure;
freqs(b, a, 1000);
title('Analog Chebyshev Type II Filter');
% Digital filter design using IIM
T = 1; % Sampling period in seconds
[num_iim, den_iim] = impinvar(b, a, 1/T);
% Digital filter design using BLT
[num_blt, den_blt] = bilinear(b, a, 1/T);
% Plot magnitude and phase responses for both methods
figure;
freqz(num_iim, den_iim, 1024, 1/T, 1);
hold on;
freqz(num_blt, den_blt, 1024, 1/T, 1);
title('Digital Chebyshev Type II Filter Design');
legend('IIM', 'BLT');
% Plot pole-zero diagram for both methods
figure;
zplane(num_iim, den_iim);
hold on;
zplane(num_blt, den_blt);
title('Pole-Zero Plot');
legend('IIM', 'BLT');

OUTPUT:

46 | P a g e
6). Convert the above filter to BP with 200Hz BW and centre frequency of 500HZ

CODE:
clc;
close all;
clear all;

47 | P a g e
% Analog Bandpass Filter Design
% New Bandpass filter specifications
center_frequency = 500; % Center frequency in Hz
bandwidth = 200; % Bandwidth in Hz
% Set the sampling period
T = 1; % Sampling period in seconds
% Calculate the passband and stopband frequencies in the analog domain
Wp_bp1 = (center_frequency - bandwidth/2) * 2 * pi;
Wp_bp2 = (center_frequency + bandwidth/2) * 2 * pi;
% Determine the filter order for the lowpass filter
Wp_lp = 0.2*pi;
Ws_lp = 0.3*pi;
Ap_dB = 1;
As_dB = 15;
N = cheb2ord(Wp_lp, Ws_lp, Ap_dB, As_dB, 's');
% Increase the filter order
N = N + 2;
% Design analog Lowpass filter
[b_lp, a_lp] = cheby2(N, As_dB, Ws_lp, 's');
% Transform the lowpass filter to a bandpass filter
[b_bp, a_bp] = lp2bp(b_lp, a_lp, center_frequency, bandwidth);
% Plot analog Bandpass filter response
figure;
freqs(b_bp, a_bp, 1000);
title('Analog Bandpass Filter');
% Convert analog filter to zpk representation
analog_filter_zpk = zpk(tf(b_bp, a_bp));
% Digital Bandpass Filter Design using Matched Z-Transform
digital_filter_matched = c2d(analog_filter_zpk, 1/T, 'matched');
% Extract numerator and denominator coefficients
[num_bp_matched, den_bp_matched] = tfdata(digital_filter_matched, 'v');
% Digital Bandpass Filter Design using Bilinear Transformation
[num_bp_blt, den_bp_blt] = c2d(analog_filter_zpk, 1/T, 'tustin');
% Plot magnitude and phase responses for both methods
figure;
freqz(num_bp_matched, den_bp_matched, 1024, 1/T, 1);
hold on;
freqz(num_bp_blt, den_bp_blt, 1024, 1/T, 1);
title('Digital Bandpass Filter Design');
legend('Matched Z-Transform', 'Bilinear Transformation');
% Plot pole-zero diagram for both methods
figure;
zplane(num_bp_matched, den_bp_matched);
hold on;

48 | P a g e
zplane(num_bp_blt, den_bp_blt);
title('Bandpass Pole-Zero Plot');
legend('Matched Z-Transform', 'Bilinear Transformation');

OUTPUT:

FIR FILTER
1).Design a bandstop filter using the Hamming window design technique. The
specifications are lower stopband edge: 0.4pi As = 40 dB ,upper stopband edge: 0.6pi,
lower passband edge: 0.3pi, Rp=0.5 dB upper passband edge: 0.7pi. Plot the impulse
response and the magnitude response (in dB) of the designed filter.

CODE:
clc;

49 | P a g e
close all;
clear all;
% Specifications
fs = 1; % Sampling frequency
f1 = 0.3*pi; % Lower passband edge
f2 = 0.4*pi; % Lower stopband edge
f3 = 0.6*pi; % Upper stopband edge
f4 = 0.7*pi; % Upper passband edge
Rp = 0.5; % Passband ripple in dB
As = 40; % Stopband attenuation in dB
% Normalize frequencies
omega1 = f1/pi;
omega2 = f2/pi;
omega3 = f3/pi;
omega4 = f4/pi;
% Calculate filter order using empirical formula (you may adjust it as needed)
N = ceil((As - 8) / (2.285 * (min(omega2-omega1, omega4-omega3))));
% Ensure N is odd
N = N + rem(N+1, 2);
% Design FIR filter coefficients using the window method with Hamming window
h = fir1(N, [omega2, omega4], hamming(N+1));
% Plot impulse response
figure;
stem(0:N, h);
title('Impulse Response');
xlabel('Sample Index');
ylabel('Amplitude');
% Plot magnitude response in dB
figure;
freqz(h, 1, 102, fs);
title('Magnitude Response');
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');

OUTPUT:

50 | P a g e
51 | P a g e
2). Design a highpass filter using the a)Hamming b)Trangular window design
techniques.
The specifications are stopband edge: 0.4pi, As = 60 dB passband edge: 0.6pi, &Ap = 0.5
Db.

CODE:
clc;
close all;
clear all;
% Specifications
stopband_edge = 0.4 * pi;
passband_edge = 0.6 * pi;
As = 60; % Stopband attenuation in dB
Ap = 0.5; % Passband ripple in dB
% Design using Hamming window
M = ceil(6.6 * pi / (stopband_edge - passband_edge)); % Filter order
if rem(M, 2) == 0
M = M + 1; % Ensure filter order is odd
end
% Use hamming function
h_hamming = fir1(M, passband_edge / pi, 'high', hamming(M + 1));
% Frequency response plot
freqz(h_hamming, 1, 1024, 2 * pi);
% Title and labels
title('Highpass Filter Design using Hamming Window');
xlabel('Frequency (rad/sample)');
ylabel('Magnitude (dB)');
% Specifications
stopband_edge = 0.4 * pi;
passband_edge = 0.6 * pi;
As = 60; % Stopband attenuation in dB
Ap = 0.5; % Passband ripple in dB
% Design using Triangular window
M = ceil(6.6 * pi / (stopband_edge - passband_edge)); % Filter order
if rem(M, 2) == 0
M = M + 1; % Ensure filter order is odd
end
% Triangular window design
h_triangular = fir1(M, passband_edge / pi, 'high', triang(M + 1));
% Frequency response plot
freqz(h_triangular, 1, 1024, 2 * pi);
% Title and labels
title('Highpass Filter Design using Triangular Window');
xlabel('Frequency (rad/sample)');

52 | P a g e
ylabel('Magnitude (dB)');

OUTPUT:

53 | P a g e
3). Consider an ideal lowpass filter with the cutoff frequency Wc= 0.3pi. design this
filter Using a frequency sampling design in which choose 40 samples by Type 1 design.

CODE:
clc;
close all;
clear all;
% FIR Lowpass Filter Design using Frequency Sampling Method (Type 1)
% Cutoff frequency Wc = 0.3pi
% Number of samples = 40
% Define filter specifications
Wc = 0.3 * pi; % Cutoff frequency
N = 40; % Number of filter taps
% Calculate the ideal frequency response
H_ideal = zeros(1, N);
for n = 1:N
H_ideal(n) = sin(Wc * (n - (N-1)/2)) / (pi * (n - (N-1)/2));
end
% Design FIR filter using frequency sampling method
h = ifft(H_ideal, 'symmetric');
% Plot the frequency response of the designed filter
fvtool(h, 1, 'Fs', 2*pi, 'Color', 'White')
% Display the filter coefficients
disp('Filter Coefficients:')
disp(h)

OUTPUT:

54 | P a g e
55 | P a g e

You might also like