Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
12 views4 pages

DSP Lab

The document contains MATLAB code for various signal processing techniques including impulse response calculation, Non-Discrete Fourier Transform (NDFT) using both formula and matrix methods, linear convolution, circular convolution, and zero padding. It prompts the user for input signals and coefficients, computes the required transformations, and displays the results in graphical form. The code also includes error handling for input lengths and provides magnitude and phase spectrum visualizations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

DSP Lab

The document contains MATLAB code for various signal processing techniques including impulse response calculation, Non-Discrete Fourier Transform (NDFT) using both formula and matrix methods, linear convolution, circular convolution, and zero padding. It prompts the user for input signals and coefficients, computes the required transformations, and displays the results in graphical form. The code also includes error handling for input lengths and provides magnitude and phase spectrum visualizations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

%impulse

clc;
clear all;
close all;

a=input("enter a coefficients");
b=input("enter b coefficients");
N=input("enter no of samples");
[h,n]=impz(b,a,N);
h
stem(n,h)
title("impulse response")
xlabel("n--->")
ylabel("amplitude->")

%formula method NDFT


clc;
clear all;
close all;
x=input("enter discrete time signal");
N=input("enter the vaule of N");
if N<length(x)
disp("N should be atleast equal to length of x");
N=lengh(x);
end
X=zeros(1,N);
for k=0:N-1
total=0;
for n=0:length(x)-1
total=total+x(n+1)*exp(-j*2*pi*n*k/N);
end
X(k+1)=total;
end
disp('magnitude')
disp(abs(X));
disp('phase')
disp(angle(X)*180/pi)
k=0:N-1;
subplot(2,1,1)
stem(k,abs(X));
title('magnitude spectrum')
xlabel('k--->')
ylabel('amplitude-->')
subplot(2,1,2)
stem(k,angle(X).*180/pi);
title('phase spectrum')
xlabel('k--->')
ylabel('angle-->')

%matrix method NDFT

clc;
clear all;
close all;
x=input("enter discrete time signal");
N=input("enter the vaule of N");
if N<length(x)
disp("N should be atleast equal to length of x");
N=lengh(x);
end
n=0:length(x)-1;
k=0:N-1;
w=exp(-j*2*pi/N);
W=w.^(n'*k);
X=x*W
disp('magnitude')
disp(abs(X));
disp('phase')
disp(angle(X)*180/pi)

subplot(2,1,1)
stem(k,abs(X));
title('magnitude spectrum')
xlabel('k--->')
ylabel('amplitude-->')
subplot(2,1,2)
stem(k,angle(X).*180/pi);
title('phase spectrum')
xlabel('k--->')
ylabel('angle-->')

%linear convoltion

clc;
clear all;
close all;
x=input("enter x");
h=input("enter h");
M=length(h);
N=length(x);
y=zeros(1,M+N-1);
for n=1:M+N-1
total=0;
for k=1:M
index=n-k+1;
if (index>0 && index<=N)
total=total+(h(k)*x(n-k+1));
end
end
y(n)=total;
end
y
stem(y);
title("linear convolution")
xlabel('n--->')
ylabel('y--->')
%linear conv. circMat

clc;
clear all;
close all;
x=input("enter x");
h=input("enter h");
M=length(h);
N=length(x);
L=M+N-1;
x=[x zeros(1,L-N)]';
h=[h zeros(1,L-M)]';
circMat=zeros(L,L);
for i=1:L
circMat(:,i)=circshift(x,i-1);
end
y=circMat*h
stem(y)
title("linear convolution")
xlabel('n--->')
ylabel('y--->')

%circ conv, formula method

x=[1 2 3 4]
h=[1 2 3 4]
N=max(length(x),length(h));
for n=0:N-1
y(n+1)=0;
for k=0:N-1
i=mod((n-k),N);
if i<0
i=i+N;
end
y(n+1)=y(n+1)+h(k+1)*x(i+1);
end
end
disp("circular convolution");
disp(y)
n1=0:N-1;
stem(n1,y)
title("circular convolution")
xlabel('n-->')
ylabel('y-->')

%zero padding

x=[1 2 3 4]
h=[1 2 3 4]
N=length(x)+length(h)-1;
x=[x zeros(1,N-length(x))]';
h=[h zeros(1,N-length(h))]';
for n=0:N-1
y(n+1)=0;
for k=0:N-1
i=mod((n-k),N);
if i<0
i=i+N;
end
y(n+1)=y(n+1)+h(k+1)*x(i+1);
end
end
disp("circular convolution");
disp(y)
n1=0:N-1;
stem(n1,y)
title("circular convolution")
xlabel('n-->')
ylabel('y-->')

You might also like