Codes

Experiment no 1: Generation Of 

DT signal

clc;

clear all;

close all; 

N=30;

n=0:N-1;

u=ones (1,N) 

subplot(3,1,1)

stem(n,u,'k')

ylabel('u(n)');

xlabel('n'); 

title('unit step response') 

x=sin (0.2*pi*n) 

subplot(3,2,3) 

stem(n,x,'r')

ylabel('sin'); 

xlabel('n'); 

title('sine step response') 

y=cos (0.2*pi*n) 

subplot(3,2,4) 

stem(n,y,'r')

ylabel('cosine');

xlabel('n'); 

title('sine step response') 

p=exp (n) 

subplot(3,2,5) 

stem(n,p,'r')

ylabel('exp'); 

xlabel('n');

title(' expo step response')

r=exp (-n);

subplot(3,2,6) 

stem(n,r,'b')

ylabel('negative exp'); 

xlabel('n');

title('negative expo step 

response')

EXP 2:DFT

clc;

clear all; 

close all; 

N=input('Enter DFT point');

x=input('Enter i/p seq to 

obtain DFT '); 

L=length(x); 

if N<L

disp('error'); 

else

x=[x,zeros(1,N-L)]; 

x=transpose(x); 

for n=0:1:N-1 

for k=0:1:N-1 

p=exp(-

(1j*2*pi*n*k)/N); 

w(n+1,k+1)=p

end; 

end;

disp(w); 

X=w*x n=0:1:N-1; 

subplot(3,1,1) 

stem(n,transpose(x)); 

xlabel('n'),

ylabel('x(n)') 

title('i/p seq'); 

subplot(3,1,2) 

stem(n,abs(X)) 

xlabel('k'),

ylabel('X(k)') 

title('magnitude'); 

subplot(3,1,3) 

stem(n,angle(X)) 

xlabel('n'),

ylabel('angle') 

title('phase');

end; 

y=fft(x,N)

EXP 3:IDFT

clc; 

clear all; 

close all; 

N=input('Enter IDFT point'); 

X=input('Enter i/p seq to 

obtain IDFT '); 

L=length(X); 

if N<L

disp('error'); 

else 

X=[X,zeros(1,N-L)]; 

X=transpose(X); 

for n=0:1:N-1 

for k=0:1:N-1 

p=exp((1j*2*pi*n*

k)/N); 

w(n+1,k+1)=p; 

end; 

end; 

disp(w);

x=(w*X)/N 

n=0:1:N-1; 

subplot(3,1,1)

stem(n,transpose(x)); 

xlabel('k'),ylabel('X(k)') 

title('i/p seq'); 

subplot(3,1,2) 

stem(n,abs(X)) 

xlabel('n'),ylabel('x(n)')

title('magnitude'); 

subplot(3,1,3) 

stem(n,angle(X)) 

xlabel('n'),ylabel('angle'

title('phase'); 

end; 

y=ifft(X,N)

EXP 4:LINEAR CONVOLUTION

clc; 

clear all; 

close all; 

X=input('Enter the 1st 

seqeuence:'); 

H=input('Enter the 2nd 

sequence:'); 

L=length(X); 

M=length(H); 

n=0:1:L-1; 

subplot(5,1,1); 

stem(n,X); 

xlabel('n');

ylabel('X'); 

title('X[n]'); 

n=0:1:M-1; 

subplot(5,1,2); 

stem(n,H); 

xlabel('n');

ylabel('H'); 

title('H[n]'); 

%Linear convolution 

Y=conv(X,H) 

N=length(Y); 

n=0:1:N-1; 

subplot(5,1,3); 

stem(n,Y); 

xlabel('n');

ylabel('Y'); 

title('Linear convolution'); 

%Auto corelation 

Y1=xcorr(X) 

n=0:1:(length(Y1))-1; 

subplot(5,1,4); 

stem(n,Y1); 

xlabel('n');

ylabel('Y1'); 

title('Auto correlation'); 

%Cross corelation 

Y2=xcorr(X,H) 

n=0:1:(length(Y2))-1; 

subplot(5,1,5); 

stem(n,Y2); 

xlabel('n');

ylabel('Y2'); 

title('Cross correlation');

EXPT 5:CIRCULAR CONVOLUTION

clc; 

clear all; 

close all; 

x= input('Enter 1st sequence: 

'); 

h= input('Enter 2nd sequence: 

'); 

L=length(x); 

M=length(h); 

N=max(L,M); 

x=[x,zeros(1,(N-L))];

h=[h,zeros(1,(N-M))]; 

w=x; x=x'; 

c(:,1)=x; 

for i=2:

N x=circshift(x,1);

c(:,i)=x; 

end; 

disp(c); 

y=c*h'; 

disp(y); 

n=0:1:N-1; 

subplot(3,1,1) 

stem(n,w) 

xlabel('n'); 

ylabel('x(n)'); 

title('First sequence'); 

subplot(3,1,2)

stem(n,h) 

xlabel('n'); 

ylabel('h(n)'); 

title('Second sequence'); 

subplot(3,1,3) 

stem(n,y) 

xlabel('n'); 

ylabel('y(n)'); 

title('Circular Convolution'); 

y1 = cconv(w,h,N) 

disp(y1)

Using FFT and IFFT: Program: 

clc; 

clear all; 

close all; 

x= input('Enter 1st sequence: 

'); 

h= input('Enter 2nd sequence: 

'); 

L=length(x); 

M=length(h); 

N=max(L,M); 

x=[x,zeros(1,(N-L))]; 

h=[h,zeros(1,(N-M))]; 

X=fft(x); 

H=fft(h); 

Y=X.*H; 

y=ifft(Y); 

y1=cconv(x,h,N); 

disp(y) 

disp(y1)

EXPT 6:RECTANGULAR WINDOW

clc; 

clear all; 

close all; 

n=20; 

fp=200; 

fs=600; 

f=1000; 

wp=2*(fp/f); 

ws=2*(fs/f); 

%wn=[wp,ws]; 

window=boxcar(n+1); 

%window=bartlett(n+1); 

%window=hamming(n+1); 

%window=hanning(n+1); 

%window=kaiser(n+1); 

wn=2*(fp/f); 

b=fir1(n,wn,window); 

%b=fir1(n,wn,'high',window); 

[H,w]=freqz(b,1); 

subplot(211) 

plot(w/pi,20*log(abs(H))); 

xlabel('nf'); 

ylabel('mag in dB'); 

title('Magnitude response'); 

subplot(212) 

plot(w/pi,angle(H)); 

xlabel('nf'); 

ylabel('angle h'); 

title('Phase response');

EXPT 7:HANNING WINDOW

clc; 

clear all; 

close all; 

n=20; 

fp=200; 

fs=600; 

f=1000; 

wp=2*(fp/f); 

ws=2*(fs/f); 

%wn=[wp,ws]; 

%window=boxcar(n+1); 

%window=bartlett(n+1); 

%window=hamming(n+1); 

window=hanning(n+1); 

%window=kaiser(n+1); 

wn=2*(fp/f); 

b=fir1(n,wn,window); 

%b=fir1(n,wn,'high',window); 

[H,w]=freqz(b,1); 

subplot(211) 

plot(w/pi,20*log(abs(H))); 

xlabel('nf'); 

ylabel('mag in dB'); 

title('Magnitude response'); 

subplot(212) 

plot(w/pi,angle(H)); 

xlabel('nf'); 

ylabel('angle h'); 

title('Phase response');

EXPT 8:HANNING WINDOW

clc; 

clear all; 

close all; 

n=20; 

fp=200; 

fs=600; 

f=1000; 

wp=2*(fp/f); 

ws=2*(fs/f); 

%wn=[wp,ws]; 

%window=boxcar(n+1); 

%window=bartlett(n+1); 

%window=hamming(n+1); 

window=hanning(n+1); 

%window=kaiser(n+1); 

wn=2*(fp/f); 

b=fir1(n,wn,window); 

%b=fir1(n,wn,'high',window); 

[H,w]=freqz(b,1); 

subplot(211) 

plot(w/pi,20*log(abs(H))); 

xlabel('nf'); 

ylabel('mag in dB'); 

title('Magnitude response'); 

subplot(212) 

plot(w/pi,angle(H)); 

xlabel('nf'); 

ylabel('angle h'); 

title('Phase response');

EXPT 9:IIM METHOD

clc; 

clear all; 

close all; 

N = 2; 

fc = 120; 

fs = 1280 ; 

wc = 2*pi*fc; 

[b,a] = butter(N,wc,'low','s'); 

[bz,az] = impinvar(b,a,fs); 

[h,f] = freqz(bz,az,1024,fs); 

subplot(3,1,1)

plot(f,20*log10(h)) 

xlabel('f') 

ylabel('h(w)') 

title('frequency response') 

subplot(3,1,2) 

plot(f,angle(h)) 

xlabel('f') 

ylabel('angle') 

title('phase response') 

subplot(3,1,3) 

zplane(bz,az) 

z = roots(bz) 

p = roots(az)

EXPT 11:BLT/IIM METHOD

clc; 

clear all; 

close all; 

N = 2; 

fc = 125; 

fs = 1285 ; 

wc = 2*pi*fc; 

[b,a] = butter(N,wc,'low','s'); 

[bz,az] = impinvar(b,a,fs);

[bzl, azl] = bilinear(b, a,

fs);

subplot(3,1,1);

zplane(b,a);

z = roots(b);

p = roots(a);

title('S PLANE');

subplot(3,1,2)

zplane(bz,az);

z = roots(bz);

p = roots(az);

title('IIM METHOD') 

subplot(3,1,3);

zplane(bzl,azl);

roots(bzl);

roots(azl);

title('BLT METHOD') 

Post a Comment

0 Comments