0% found this document useful (0 votes)
36 views

All All: For End If Else End

The document contains MATLAB code that performs various signal processing tasks including: 1) Computing the impulse and step responses of a discrete-time system using the impz and filter functions. 2) Analyzing the stability of the system. 3) Computing the frequency response of systems using freqz. 4) Convolving signals and analyzing the frequency response properties of convolution. 5) Demonstrating the time-shifting property using frequency response multiplication. 6) Implementing circular convolution, the discrete Fourier transform, and the inverse discrete Fourier transform from scratch.

Uploaded by

Irtaza Afzal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

All All: For End If Else End

The document contains MATLAB code that performs various signal processing tasks including: 1) Computing the impulse and step responses of a discrete-time system using the impz and filter functions. 2) Analyzing the stability of the system. 3) Computing the frequency response of systems using freqz. 4) Convolving signals and analyzing the frequency response properties of convolution. 5) Demonstrating the time-shifting property using frequency response multiplication. 6) Implementing circular convolution, the discrete Fourier transform, and the inverse discrete Fourier transform from scratch.

Uploaded by

Irtaza Afzal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

%lab 5 task_1

%input : given
%Sarwat Sarfraz SP15-BCE-013
%----------------------------------------------------------------------------
-----
clc
clear all
close all
%program starts here
%my roll_n0=R
R=13;
%denominator co-efficients
den=[1,.7,-.45,-.6]
%numerator co-efficients
num=[.8,-.44,.36,.02]
%impulse response
[h,t]=impz(num,den,2*R)
%plotting
figure
stem(t,h)
title('impulse response using impz function')
%------------------------------------------------------------
n=ones(1,2*R)
range=0:2*R-1;
%step response
[h]=filter(num,den,n)
%plotting
figure
stem(range,h)
title('step response')

%---------------------------------------------------------------
n1=[1,zeros(1,2*R-1)]
range1=0:2*R-1;
h=filter(num,den,n1)
figure
stem(range1,h)
title('impulse response using filter')
sum=0
for i=1:length(h)
sum=sum+abs(h(i))
end
if sum==inf
disp('UNSTABLE SYSTEM')
else
disp('STABLE SYSTEM')
end
%-------------------------------------------------------------------------

clc
clear all
close all
%my roll_no
R=13;
%denominator co-efficients
den=[1,-0.6]
%numerator co-efficients
num=[2,1]
%number of samples
N=2*R
%frequency response with k
[H w]=freqz(num,den,512)
figure
plot(w/pi,abs(H))
title('magnitude of H(w)')
figure
plot(w/pi,angle(H))
title('angle of H(w)')
figure
plot(w/pi,real(H))
title('real part of H(w)')
figure
plot(w/pi,imag(H))
title('imaginary part of H(w)')
%----------------------------------------------------------------------
%frequency response with whole
[H w]=freqz(num,den,512,'whole')
figure
plot(w/pi,abs(H))
title('magnitude of H(w) by defining the whole')
figure
plot(w/pi,angle(H))
title('angle of H(w) by defining the whole')
figure
plot(w/pi,real(H))
title('real part of H(w) by defining the whole')
figure
plot(w/pi,imag(H))
title('imaginary part of H(w) by defining the whole')
%---------------------------------------------------------------------
%sampling frequency
fs=2*10.^3
[H f]=freqz(num,den,512,fs);
figure
plot(f,abs(H))
title('magnitude of H(w)')
figure
plot(f,angle(H))
title('angle of H(w)')
figure
plot(f,real(H))
title('real part of H(w)')
figure
plot(f,imag(H))
title('imaginary part of H(w)')
%-----------------------------------------------------------------------

[H f]=freqz(num,den,512,'whole',fs)
figure
plot(f,abs(H))
title('magnitude of H(w) by defining whole in freq-domain')
figure
plot(f,angle(H))
title('angle of H(w) by defining whole in freq-domain')
figure
plot(f,real(H))
title('real part of H(w) by defining whole in freq-domain')
figure
plot(f,imag(H))
title('imaginary part of H(w) by defining whole in freq-domain')

3
clc
clear all
close all
x1=[1 1 2 3]
x2=[1 2 3 4]
den=1
%conv x1 and x2
y=conv(x1,x2)
%freq response of x1
[h1 w1]=freqz(x1,1,512,'whole')
%freq response of x2
[h2 w2]=freqz(x2,1,512,'whole')
%multiplying the freq response of x1 and x2
h3=h1.*h2
%freq response of y
[h4 w4]=freqz(y,1,512,'whole')
figure
subplot(2,2,1)
plot(w1/pi,abs(h3))
title('|H3(w)|')

subplot(2,2,2)
plot(w1/pi,angle(h3))
title('<H3(w)')

hold on
subplot(2,2,3)
plot(w1/pi,abs(h4))
title('|H4(w)|')

subplot(2,2,4)
plot(w1/pi,angle(h4))
title('<H4(w)')
hold off

4
clc
clear all
close all
%my roll_no
R=13;
n0=round(R/2);
%array of 1's and 0's
x=[ones(1,R) zeros(1,n0)]
%shifting signal
x1=[zeros(1,n0) ones(1,R)]
%freq response of x
[h1 w1]=freqz(x,1,512,'whole');
%freq response of x1
[h2 w2]=freqz(x1,1,512,'whole');
%multiplying freq response of x with e(jwn0) for time shifting property
X=h1.*exp(-j*w1*n0);

figure
subplot(2,2,1)
plot(w1/pi,abs(X))
title('|X(w)|')

subplot(2,2,2)
plot(w1/pi,angle(X))
title('<X(w)')
hold on

subplot(2,2,3)
plot(w2/pi,abs(h2))
title('|h2(w)|')

subplot(2,2,4)
plot(w2/pi,angle(h2))
title('<h2(w)')
hold off

SHIFT
function [ x_o n_o] = circular_sh( xin,range_in,shift )
a=mod(range_in+shift,length(range_in))+1;
x_o(a)=xin(range_in+1);
n_0=range_in

end

CC
function [ y_o range_out ] = c_conv(xin,hin,N )
xin=[xin zeros(1,N-length(xin))];
hin=[hin zeros(1,N-length(hin))];
hin(2:end)=hin(end:-1:2)
n1=0:length(xin)-1;
n2=0:length(hin)-1;
for i = 1:N
h_shift=circular_sh(hin,n2,i-1)
y_o(i)=sum(xin.*h_shift);
end
range_out=0:length(y_o)-1;

TEST
clc
clear all
close all

x=[1 2 2]
h=[1 2 3 4]
N=4
[y n]=c_conv(x,h,N)

DFT

% Output:DFT
% -------------------------------------------------------------------------
% Program Starts Here

function [dft_out out_range] = custom_dft(disc_input_sig,max)

n = 0:max-1;
k=n';

w = exp(-j*2*pi/max)
a=(k*n)
wnk = w.^a
dft_out = disc_input_sig*wnk;

out_range = k;
end

IDFT
% Name= Muhammad Shahab uddin
% Reg#=SP15-BCE-015
% Task 2
% Input: X(k) and K
% Output:DFT
% -------------------------------------------------------------------------
% Program Starts Here

function [disc_input_sig out_range] = custom_i_dft(dft_out,K)

n = 0:K-1;
k=n';

w = exp(-j*2*pi/K)
a=(k*n)
wnk = w.^-a
disc_input_sig =(dft_out*wnk)/K
out_range = k
end

TEST

% -------------------------------------------------------------------------
% Program Starts Here

clc
clear all
close all

disc_input_sig = [1 1 1 1];
x1 = [1 2 1 2 0 0 0 0]
max = length(disc_input_sig);
max2 = length(x1);

[X k] = custom_dft(disc_input_sig,max);

figure
stem(k,abs(X))
title('|H(w)|')
xlabel('w in rad/s')
ylabel('Amp')

figure
stem(k,angle(X))
title('<H(w)')
xlabel('w in rad/s')
ylabel('Angle')

% [H w] = freqz(x1,1,512,'whole');
[X1 k] = custom_dft(x1,max2);
figure
stem(k,abs(X1))
title('|H(w)|')
xlabel('w in rad/s')
ylabel('Amp')

figure
stem(k,angle(X1))
title('<H(w)')
xlabel('w in rad/s')
ylabel('Angle')

[x n] = custom_i_dft(X1,max2)
figure
stem(n,x)

You might also like