Sunteți pe pagina 1din 12

Exp1:

%to plot sine waveform


clc;
A=5;
f=5;
t=0:0.005:0.5;
y=A*sin(2*3.14*f*t);
subplot(2,6,1);
plot(y);
title('sine wave');
xlabel('---->t');
ylabel('---->A');
subplot(2,6,2);
stem(y);
title('sine discrete');
xlabel('---->t');
ylabel('---->A');
%to plot cosine waveform
clc;
A=5;
f=5;
t=0:0.005:0.5;
y=A*cos(2*3.14*f*t);
subplot(2,6,3);
plot(y);
title('cosine wave');
xlabel('---->t');
ylabel('---->A');
subplot(2,6,4);
stem(y);
title('cosine discrete');
xlabel('---->t');
ylabel('---->A');
%to plot unit impule
s1=[zeros(1,20),1,zeros(1,20)];
n1=-20:20;
subplot(2,6,5);
stem(n1,s1);
title('impluse discrete');
xlabel('---->n1');
ylabel('---->s1');
%to plot unit step
s2=[zeros(1,20),1,ones(1,20)];
n2=-20:20;
subplot(2,6,6);
stem(n2,s2);
title('step discrete');
xlabel('---->n2');

ylabel('---->s2');
%to plot unit ramp
n3=0:5;
subplot(2,6,7);
stem(n3,n3);
title('ramp discrete');
xlabel('---->n3');
ylabel('---->s3');
%to plot rising exponential
n4=0:5;
s2=exp(n4);
subplot(2,6,8);
stem(n4,s2);
title('rising exponential');
xlabel('---->n4');
ylabel('---->s2');
%to plot decaying exponential
n5=0:5;
s2=exp(-n5);
subplot(2,6,9);
stem(n5,s2);
title('decaying exponential');
xlabel('---->n5');
ylabel('---->s2');

exp2:
%prgm fr sampling & aliasing
t=0:0.001:1;
f=input('enter the freq in cycles per sec');
x=cos(2*3.14*f*t);
subplot(3,1,1);
plot(t,x);
title('continuous time signal');
xlabel('---->time index t');
ylabel('---->signal x(t)');
%sampling in time domain at nyquist rate
fs=input('enter the nyquist rate fs');
ts=1/fs;
t=0:ts:1;
xn=cos(2*3.14*f*t);
subplot(3,1,2);
stem(t,xn);
title('analog signal at nyquist rate');
xlabel('---->time index');
ylabel('---->discrete signal x(n)');
%sampling in freq domain less than nyquist rate
fsln=input('enter the sampling freq less than nyquist rate');

tsln=1/fsln;
tln=0:tsln:1;
xln=cos(2*3.14*f*tln);
subplot(3,1,3);
stem(tln,xln);
title('analog signal samples less than nyquist rate');
xlabel('---->time index t');
ylabel('---->amplitude');
figure;
%alising in freq domain at nyquist rate
xnf=fft(xn);
subplot(2,1,1);
plot(abs(xnf));
title('freq domain rep of signal aliasing at nyquist');
xlabel('---->index n');
ylabel('---->amplitude');
%alising in freq domain less than nyquist rate
xlnf=fft(xn);
subplot(2,1,2);
plot(abs(xlnf));
title('freq domain rep of signal aliasing aless than nyquist');
xlabel('---->index n');
ylabel('---->amplitude');

exp3:
%computation of fft
clc;
xn=input('enter the sequence for dft:');
n=length(xn);
y=fft(xn,n);
subplot(2,2,1);
stem(1:n,real(y));
title('real part of x(k)');
xlabel('index k');
ylabel('real of x(k)');
subplot(2,2,2);
stem(1:n,imag(y));
title('imaginary part of x(k)');
xlabel('index k');
ylabel('imaginary of x(k)');
m=abs(y);
subplot(2,2,3);
stem(m);
title('magnitude of x(k)');
xlabel('index k');
ylabel('real of x(k)');
disp(real(y));

disp('imag of x(k)');
disp(imag(y));
disp('magnitude of x(k)');
disp(m);
p=angle(y);
subplot(2,2,4);
stem(p);
title('phase of x(k)');
xlabel('index k');
ylabel('phase of x(k)');
disp('phase of x(k)');
disp(p);
%computation of ifft
figure;
xk=y;
n=length(xk);
z=ifft(xk,n);
subplot(2,2,1);
stem(1:n,real(z));
title('real part of x(n)');
xlabel('index n');
ylabel('real of x(n)');
subplot(2,2,2);
stem(1:n,imag(z));
title('imaginary part of x(n)');
xlabel('index n');
ylabel('imaginary of x(n)');
m=abs(z);
subplot(2,2,3);
stem(m);
title('magnitude of x(n)');
xlabel('index n');
ylabel('real of x(n)');
disp(real(z));
disp('imag of x(n)');
disp(imag(z));
disp('magnitude of x(n)');
disp(m);
p=angle(z);
subplot(2,2,4);
stem(p);
title('phase of x(n)');
xlabel('index n');
ylabel('phase of x(n)');
disp('phase of x(n)');
disp(p);

exp4:
%step response
b=1;
a=[1,sqrt(2),1];
t=(0:.01:30);
x=ones(size(t));
subplot(2,1,1);
plot(t,x);
xlabel('time in seconds');
ylabel('amplitude');
title('step response');
figure;
%tme response
y=lsim(b,a,x,t);
subplot(2,1,1);
plot(t,y);
grid on;
axis([0 30 0 1.5]);
title('time response');
xlabel('time in seconds');
ylabel('amplitude');
figure;
%frequency response
clc;
a=[1,sqrt(2),2];
b=1;
f=0:.01:50;
w=2*3.14*f;
h=freqs(b,a,w);
title('frequency response');
mag=abs(h);
plot(f,mag);
grid on;
xlabel('normalized freq');
ylabel('amplitude');
axis([0 5 0 1.3]);
title('amplitude response');

exp5:
%linear convolution
clc;
x=input('enter the sequence x(n):');
len1=length(x);
subplot(3,1,1);
stem(x);
xlabel('time index');
ylabel('amplitude');

title('input sequence');
h=input('enter the sequence h(n):');
len2=length(h);
subplot(3,1,2);
stem(h);
xlabel('time index');
ylabel('amplitude');
title('impulse response of h(n)');
len=len1+len2-1;
x1=fft(x,len);
h1=fft(h,len);
y1=x1.*h1;
y=ifft(y1,len);
n=0:1:len-1;
subplot(3,1,3);
disp('convoluted sequence');
disp(y);
stem(n,y);
xlabel('time index');
ylabel('amplitude');
title('convolution of y(n)');
%circular convolution
figure;
xn=input('enter the sequence x(n):');
len1=length(xn);
subplot(4,1,1);
stem(xn);
xlabel('time index');
ylabel('amplitude');
title('input sequence');
hn=input('enter the sequence h(n):');
len2=length(hn);
subplot(4,1,2);
stem(hn);
xlabel('time index');
ylabel('amplitude');
title('impulse response of h(n)');
len=max(len1,2);
N=len;
x=[xn zeros(1,N-len2)];
n2=length(hn);
x1=fft(x,len);
h1=fft(hn,len);
y1=x1.*h1;
y=ifft(y1,len);
n=0:1:len-1;
subplot(4,1,3);
disp('convoluted sequence');

disp(y);
stem(n,y);
xlabel('time index');
ylabel('amplitude');
title('convolution of y(n)');

exp6:
%rectangular window
clear all;
wc=0.5*pi;
N=9;
alpha=(N-1)/2;
eps=0.0001;
n=0:1:N-1;
hd=sin(wc*(n-alpha+eps))./(pi*(n-alpha+eps));
wr=boxcar(N);
hn=hd.*wr';
w=0:0.1:pi;
h=freqz(hn,1,w);
z=10*log(h);
plot(w/pi,z,'-^r');
hold on
%hamming window
wh=hamming(N);
hn=hd.*wh';
w=0:0.1:pi;
h=freqz(hn,1,w);
c=10*log(h);
plot(w/pi,c,'-*g');
%hanning window
N=9;
alpha=(N-1)/2;
wc=0.3*pi;
eps=0.001;
n=0:1:N-1;
hd=sin(wc*(n-alpha+eps))./(pi*(n-alpha+eps));
wr=boxcar(N);
hn=hd.*wr';
w=0:0.1:pi;
h=freqz(hn,1,w);
z=10*log(h);
wh=hanning(N);
hn=hd.*wh';
w=0:0.1:pi;
h=freqz(hn,1,w);
a=10*log(h);
plot(w/pi,a,'-+b');

%blackman window
wc=0.7*pi;
N=9;
alpha=(N-1)/2;
eps=0.001;
n=0:1:N-1;
hd=sin(wc*(n-alpha+eps))./(pi*(n-alpha+eps));
wr=boxcar(N);
hn=hd.*wr';
w=0:0.1:pi;
h=freqz(hn,1,w);
z=10*log(h);
wh=blackman(N);
hn=hd.*wh';
w=0:0.1:pi;
h=freqz(hn,1,w);
u=10*log(h);
plot(w/pi,u,'-*black');
grid;
xlabel('normalized freq');
ylabel('magnitude');
title('comparison');
hold off
exp7:
%butterworth low pass
clc;
alphap=0.4;
alphas=20;
fp=400;
fs=800;
F=2000;
omp=2*fp/F;
oms=2*fs/F;
[n,wn]=buttord(omp,oms,alphap,alphas);
[b,a]=butter(n,wn);
w=0:0.01:pi;
[h,ph]=freqz(b,a,w,'whole');
m=abs(h);
an=angle(h);
subplot(4,1,1);
plot(ph/pi,20*log(m));
ylabel('gain');
xlabel('normalized freq');
subplot(4,1,2);
plot(ph/pi,an);
grid;
ylabel('phase');

xlabel('normalized freq');
%butterworth high pass
clc;
alphap=0.4;
alphas=30;
fp=400;
fs=800;
F=2000;
omp=2*fp/F;
oms=2*fs/F;
[n,wn]=buttord(omp,oms,alphap,alphas);
[b,a]=butter(n,wn,'high');
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log(abs(h));
an=angle(h);
subplot(4,1,3);
plot(om/pi,m);
grid;
ylabel('gain');
xlabel('normalized freq');
subplot(4,1,4);
plot(om/pi,an);
grid;
ylabel('phase');
xlabel('normalized freq');
figure;
%butterworth band pass
clc;
alphap=2;
alphas=20;
wp=[0.2*pi,0.4*pi];
ws=[0.1*pi,0.5*pi];
[n,wn]=buttord(wp/pi,ws/pi,alphap,alphas);
[b,a]=butter(n,wn);
w=0:0.01:pi;
[h,ph]=freqz(b,a,w);
m=20*log(abs(h));
an=angle(h);
subplot(2,1,1);
plot(ph/pi,m);
grid;
ylabel('gain');
xlabel('normalized freq');
subplot(2,1,2);
plot(ph/pi,an);
grid;
ylabel('phase');

xlabel('normalized freq');
figure;
%butterworth reject
clc;
alphap=2;
alphas=20;
wp=[0.2*pi,0.4*pi];
ws=[0.1*pi,0.5*pi];
[n,wn]=buttord(wp/pi,ws/pi,alphap,alphas);
[b,a]=butter(n,wn);
w=0:0.01:pi;
[h,ph]=freqz(b,a,w);
m=-20*log(abs(h));
an=angle(h);
subplot(2,1,1);
plot(ph/pi,m);
grid;
ylabel('gain');
xlabel('normalized freq');
subplot(2,1,2);
plot(ph/pi,an);
grid;
ylabel('phase');
xlabel('normalized freq');

exp8:
clc;
alphap=1;
alphas=15;
wp=0.2*pi;
ws=0.3*pi;
[n,wn]=cheb1ord(wp/pi,ws/pi,alphap,alphas);
[b,a]=cheby1(n,alphap,wn);
w=0:0.01:pi;
[h,ph]=freqz(b,a,w);
m=20*log(abs(h));
an=angle(h);
subplot(4,2,1);
plot(ph/pi,m);
grid;
ylabel('gain');
xlabel('normalized freq');
subplot(4,2,2);
plot(ph/pi,an);
grid;
ylabel('phase');
xlabel('normalized freq');

%band pass
clc;
alphap=2;
alphas=20;
wp=[0.2*pi,0.4*pi];
ws=[0.1*pi,0.5*pi];
[n,wn]=cheb1ord(wp/pi,ws/pi,alphap,alphas);
[b,a]=cheby1(n,alphap,wn);
w=0:0.01:pi;
[h,ph]=freqz(b,a,w);
m=20*log(abs(h));
an=angle(h);
subplot(4,2,3);
plot(ph/pi,m);
grid;
ylabel('gain');
xlabel('normalized freq');
subplot(4,2,4);
plot(ph/pi,an);
grid;
ylabel('phase');
xlabel('normalized freq');
%high pass
clc;
alphap=1;
alphas=15;
wp=0.2*pi;
ws=0.3*pi;
[n,wn]=cheb1ord(wp/pi,ws/pi,alphap,alphas);
[b,a]=cheby1(n,alphap,wn);
w=0:0.01:pi;
[h,ph]=freqz(b,a,w);
m=20*log(abs(h));
an=angle(h);
subplot(4,2,5);
plot(ph/pi,m);
grid;
ylabel('gain');
xlabel('normalized freq');
subplot(4,2,6);
plot(ph/pi,an);
grid;
ylabel('phase');
xlabel('normalized freq');
%band reject
clc;
alphap=2;
alphas=20;

wp=[0.2*pi,0.4*pi];
ws=[0.1*pi,0.5*pi];
[n,wn]=cheb1ord(wp/pi,ws/pi,alphap,alphas);
[b,a]=cheby1(n,alphap,wn);
w=0:0.01:pi;
[h,ph]=freqz(b,a,w);
m=20*log(abs(h));
an=angle(h);
subplot(4,2,7);
plot(ph/pi,m);
grid;
ylabel('gain');
xlabel('normalized freq');
subplot(4,2,8);
plot(ph/pi,an);
grid;
ylabel('phase');
xlabel('normalized freq');

S-ar putea să vă placă și