Sie sind auf Seite 1von 14

programs

%SAMPLING THEOREM
clc;
close all;
clear all;
f=input('Enter the input signal frequency, f=');
t=0:0.001:0.1;
x=cos(2*pi*f*t);
fs=input('Enter the sampling frequency fs>>f,fs=');
ts=1/fs;
tn=0:ts:0.1;
x1=cos(2*pi*f*tn);
subplot(3,1,1);
plot(t,x);
xlabel('Time');
ylabel('Amplitude');
title('Input signal');
subplot(3,1,2);
stem(tn,x1);
xlabel('Time');
ylabel('Amplitude');
title('Sampled output');
subplot(3,1,3);
plot(tn,x1);
xlabel('Time');
ylabel('Amplitude');
title('From sampled data-Reconstructed wave form');

% impulse response by using impz() function


clc;
close all;
clear all;
disp('Enter the input and output co-efficients as b=[1]& a=[1 -1 0.9]');
b=input('Enter the x(n) i.e numerator coefficients b=');
a=input('Enter the y(n) i.e denominator coefficients a=');
N=input('Enter the desired no of samples in impulse response:N=');
[h,t]=impz(b,a,N);
r=(0:length(h)-1);
stem(r,h);
xlabel('Samples number,n');
ylabel('Amplitude,h(n)');
title('Impulse response');
disp('Impulse response coefficients of the given system is:');
disp(h);

Page 1
programs

% impulse response by using filter() function


clc;
close all;
clear all;
disp('Enter the co-efficients eg b=[1]& a=[1 -0.75 0.125]');
b=input('Enter the numerator coefficients b=');
a=input('Enter the denominator coefficients a=');
N=input('Enter the no of samples N=');
imp=[1;zeros((N-1),1)];
y=filter(b,a,imp);
disp('The impulse response of the system')
display(y);
subplot(2,1,1);
n=0:length(imp)-1;
stem(n,imp);
grid on;
xlabel('Samples number,n');
ylabel('Amplitude');
title('Impulse input');
subplot(2,1,2);
n=0:length(imp)-1;
stem(n,y);
grid on;
xlabel('Samples number,n');
ylabel('Amplitude of h(n)');
title('Impulse response');

Page 2
programs

% step response by using filter() function


clc;
close all;
clear all;
disp('Enter the co-efficients eg a=[1]& b=[1 -0.75 0.125]');
b=input('Enter the numerator coefficients a=');
a=input('Enter the denominator coefficients b=');
N=input('Enter the no of samples N=');
step=ones(1,N);
y=filter(a,b,step);
disp('The step response of the system')
display(y);
subplot(2,1,1);
n=0:length(step)-1;
stem(n,step);
grid on;
xlabel('Samples number,n');
ylabel('Amplitude');
title('Unit step pulses');
subplot(2,1,2);
n=0:length(y)-1;
stem(n,y);
grid on;
xlabel('Samples number,n');
ylabel('Amplitude');
title('Unit step response')

Page 3
programs

% LINEAR CONV() FUNCTION


clc;
close all;
clear all;
x1=input('Enter the first sequence to be convoluted,x1=[]:');
x2=input('Enter the second sequence to be convoluted,x2=[]:');
subplot(3,1,1);
stem(x1);
xlabel('Time');
ylabel('Amplitude');
title('First sequence,x1(n)');
subplot(3,1,2);
stem(x2);
xlabel('Time');
ylabel('Amplitude');
title('Second sequence,x2(n)');
Y=conv(x1,x2);
disp('The convoluted sequence is Y(n)=x1(n)*x2(n)');
disp(Y);
subplot(3,1,3);
stem(Y);
xlabel('Time');
ylabel('Amplitude');
title('The convoluted sequence,Y(n)=x1(n)*x2(n)');

Page 4
programs

E11

clc;
close all;
clear all;
x1=input('Enter the first sequence x1(n) to be convoluted =:');
x2=input('Enter the second sequence x2(n) to be convoluted =:');
subplot(3,1,1);
stem(x1);
xlabel('Time');
ylabel('Amplitude');
title('First sequence,x1(n)');
subplot(3,1,2);
stem(x2);
xlabel('Time');
ylabel('Amplitude');
title('Second sequence,x2(n)');
L1=length(x1);
L2=length(x2);
if L1>L2
L=L1-L2;
x1=[x1,zeros(1,L)];
elseif L2>L1
L=L2-L1;
x1=[x1,zeros(1,L)];
end
N=max(L1,L2);
for k=0:N-1
sum=0;
for j=0:N-1
sum=sum+x1(j+1)*x2(mod(k-j,N)+1);
end
Y1(k+1)=sum;
end
disp('Circular convolution by expression');
disp(Y1);
subplot(3,1,3);
stem(Y1);
xlabel('Time');
ylabel('Amplitude');
title('The Circular convoluted sequence,Y(n)=x1(n)*x2(n)');
Y2=cconv(x1,x2,N);
disp('The Circular convolution sequence by using cconv(),is');
disp(Y2);

Page 5
programs

E12

clc;
close all;
clear all;
a=input('Enter the first input sequence,a=');
b=input('Enter the second input sequence,b=');
N=max(length (a), length (b));
A=fft(a,N);
B=fft(b,N);
C=A.*B;
x=ifft(C,N);
disp('The circular convolved output,x is');
disp(x);
r=0: length (x)-1;
subplot(2,1,1);
figure (1)
stem(r,x);
xlabel ('Number of samples');
ylabel('Amplitude');
title('Circular convolution by DFT & IDFT method');
subplot(2,1,2);
L1=length(a);
L2=length(b);
N1=L1+L2-1;
A=fft(a,N1);
B=fft(b,N1);
C=A.*B;
y=ifft(C,N1);
disp('The linear convolution is');
disp(y);
r=0: length (y)-1;
stem(r,y);
xlabel ('Number of samples');
ylabel('Amplitude');
title('Linear convolution by DFT & IDFT method');

Page 6
programs

linear convolution of 2 sequences using DFT & IDFT

figure(2)
L1=length(a);
L2=length(b);
N1=L1+L2-1;
A=fft(a,N1);
B=fft(b,N1);
C=A.*B;
y=ifft(C,N1);
disp('The linear convolution is');
disp(y);
r=0: length (y)-1;
stem(r,y);
xlabel ('Number of samples');
ylabel('Amplitude');
title('Linear convolution by DFT & IDFT method');

Page 7
programs

E14

clc;
close all;
clear all;
disp('Enter the input sequence x(n): as x=[1 2 3 4....]');
xn=input('enter the input sequence x(n):');
N=length(xn);
Xk=zeros(1,N);
for k=0:N-1
for n=0:N-1
Xk(k+1)=Xk(k+1)+(xn(n+1))*exp((-i)*2*pi*k*n/N);
end
end
t=0:N-1;
subplot(3,1,1);
stem(t,xn);
ylabel('Amplitude , |x|');
xlabel('Time index , n');
title('Input sequence,x(n)');
magnitude=abs(Xk);
disp('The magnitude response of X(k):');
disp(magnitude);
t=0:N-1;
subplot(3,1,2);
stem(t,magnitude);
ylabel('Amplitude , |X(k)|');
xlabel('Sample no: ,k');
title(' Magnitude sequence of X(k)');
phase=angle(Xk);
disp('The phase response of X(k):');
disp(phase);
t=0:N-1;
subplot(3,1,3);
stem(t,phase);
ylabel('Phase');
xlabel('Sample no: ,k');
title(' Phase response of X(k)');

Page 8
programs

E15

clc;
close all;
clear all;
disp('Enter the input sequence x(n): as x=[1 2 3 4....]');
xn=input('enter the input sequence x(n):');
N=length(xn);
Xk=zeros(1,N);
n=0:N-1;
disp(' ');
disp('--------The DFT,X(k)={......} is ---------');
Xk=fft(xn);
disp(Xk);
disp('The IDFT of the sequence,X(k) is ,y(n)={.....}');
y=ifft(Xk);
disp(y);
subplot(3,2,1);
stem(n,xn);
title('Input Sequence,x(n)');
% subplot(3,2,2);
% stem(n,Xk);
% title('Input Sequence in, X(k)');
subplot(3,2,3);
stem(n,abs(Xk));
title('Magnitude spectrum of DFT, |X(k)|');
subplot(3,2,4);
stem(n,angle(Xk));
title('Phase spectrum of DFT, |X(k)|,arg(k)');
subplot(3,2,5);
stem(n,y);
title('y(n)= IDFT of X(k)');

Page 9
programs
E16

%BUTTERWORTH LOW PASS ANALOG FILTER


clc;
close all;
clear all;
k1=input('Enter the pass band gain, k1 in db: ');
k2=input('Enter the pass band gain, k2 in db: ');
w1=input('Enter the pass band edge frequency in rad/sec:');
w2=input('Enter the stop band edge frequency in rad/sec:');
[N,Wc]=buttord(w1,w2,k1,k2,'s');
disp('The order ,N is:');
disp(N);
disp('The cutoff frequency,wc is:');
disp(Wc);
[b,a]=butter(N,Wc,'low','s');
title('Phase response of Butterworth LPF');
disp('Transfer Function H(s) is given by');
printsys(b,a);
freqs(b,a);
title('Magnitude response of Butterworth LPF');

Page 10
programs
E17

%DESIGN OF IIR FILTER BUTTERWORTH LOW PASS ANALOG FILTER


clc;
close all;
clear all;
Rp=input('Enter the pass band Attenuation/Ripple Rp in db: ');
Rs=input('Enter the pass band Attenuation/Ripple Rs in db: ');
fp=input('Enter the pass band frequency in Hz:');
fs=input('Enter the stop band frequency in Hz:');
Fs=input('Enter the Sampling frequency in Hz:');
Wp=2*fp/Fs;
Ws=2*fs/Fs;
[N,Wc]=buttord(Wp,Ws,Rp,Rs);
[z,p]=butter(N,Wc);
disp('Order of Butterworth Filter,N: ');
disp(N);
disp('and cutoff Frequency Wc:');
disp(Wc);
[b,a]=butter(N,Wc,'low');
disp('Transfer Function H(s) is given by ');
printsys(b,a);
title('Low pass filter');
freqs(z,p);
title('Butterworth frequency response');

Page 11
programs

E18

% BILINEAR TRANSFORMATION
clc;
close all;
clear all;
T=1;
wp=0.5*pi;
ws=0.75*pi;
rp=0.707;
rs=0.2;
Rp=-20*log10(rp);
Rs=-20*log10(rs);
Wp=(2/T)*tan(wp/2);
Ws=(2/T)*tan(ws/2);
[N wn]=buttord(Wp,Ws,Rp,Rs,'s');
[b,a]=butter(N,wn,'s');
disp('The analog transfer function, H(s) is given by:')
printsys(b,a);
disp(' ');
[B,A]=bilinear(b,a,1/T);
disp('**************************************************************');
disp(' ');
disp('The digital transfer function, H(s) is given by:')
printsys(B,A,'z');
[H,w]=freqz(B,A);
figure;
subplot(2,1,1);
plot(w,20*log(abs(H)));
grid;
xlabel('Frequency');
ylabel('Magnitude');
title('Magnitude Response of filter using Bilinear Transformation method');
subplot(2,1,2);
plot(w,angle(H));
grid;
xlabel('Frequency');
ylabel('Phase');
title('Phase Response of filter using Bilinear Transformation method');

Page 12
programs

E19

%IMPULSE INVARIANT TRANSFORMATION


clc;
close all;
clear all;
T=1;
wp=0.5*pi;
ws=0.75*pi;
rp=0.707;
rs=0.2;
Rp=-20*log10(rp);
Rs=-20*log10(rs);
Wp=wp/T;
Ws=ws/T;
[N wn]=buttord(Wp,Ws,Rp,Rs,'s');
[b,a]=butter(N,wn,'s');
disp('The analog transfer function, H(s) is given by:')
printsys(b,a);
disp(' ');
disp('**************************************************************');
disp(' ');
[B,A]=bilinear(b,a,1/T);
disp('The digital transfer function, H(s) is given by:')
printsys(B,A,'z');
[H,w]=freqz(B,A);
figure;
subplot(2,1,1);
plot(w,20*log(abs(H)));
grid;
xlabel('Frequency');
ylabel('Magnitude');
title('Magnitude Response of filter using Impulse Transformation method');
subplot(2,1,2);
plot(w,angle(H));
grid;
xlabel('Frequency');
ylabel('Phase');
title('Phase Response of filter using Impulse Transformation method');

Page 13
programs

E20

% COMPLETE DESIGN OF DIGITAL FILTER


clc;
close all;
clear all;
T=1;
wp=0.5*pi;
ws=0.75*pi;
rp=0.707;
rs=0.2;
Rp=-20*log10(rp);
Rs=-20*log10(rs);
Wpi=wp/T;
Wsi=ws/T;
[Ni wn]=buttord(Wpi,Wsi,Rp,Rs,'s');
[bi,ai]=butter(Ni,wn,'s');
[Bi,Ai]=bilinear(bi,ai,1/T);
[Hi,w]=freqz(Bi,Ai);
figure;
subplot(2,1,1);
plot(w,20*log(abs(Hi)));
grid;
xlabel('Frequency');
ylabel('Magnitude');
title('Magnitude Response of filter using Impulse Transformation method');
subplot(2,1,2);
plot(w,angle(Hi));
grid;
xlabel('Frequency');
ylabel('Phase');
title('Phase Response of filter using Impulse Transformation method');
Wpb=(2/T)*tan(wp/2);
Wsb=(2/T)*tan(ws/2);
[Nb wn]=buttord(Wpb,Wsb,Rp,Rs,'s');
[bb,ab]=butter(Nb,wn,'s');
[Bb,Ab]=bilinear(bb,ab,1/T);
[Hb,w]=freqz(Bb,Ab);
figure(2);
subplot(2,1,1);
plot(w,20*log(abs(Hb)));
grid;
xlabel('Frequency');
ylabel('Magnitude');
title('Magnitude Response of filter using Bilinear Transformation method');
subplot(2,1,2);
plot(w,angle(Hb));
grid;
xlabel('Frequency');
ylabel('Phase');
title('Phase Response of filter using Bilinear Transformation method');
Page 14

Das könnte Ihnen auch gefallen