0% found this document useful (0 votes)
22 views6 pages

SP Codes 1-5

Uploaded by

darshansrathod12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views6 pages

SP Codes 1-5

Uploaded by

darshansrathod12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

%1.

1 discrete
%Unit Imp seq
function[y,n]=impseq(a,b,c)
n=a:b;
y=(n-c)==0;
stem(n,y)
%Unit Step seq
function[y,n]=stepseq(a,b,c)
n=a:b;
y=(n-c)>=0;
stem(n,y)
%Unit Ramp seq
function[y,n]=rampseq(a,b,c)
n=a:b;
y=((n-c)>=0).*(n-c);
stem(n,y)
%Real exp a^n
function[y,n]=exp(a,b,c)
n=b:c
y=exp(a^n);
stem(n,y)
%Complex exp e^jwn
n=-10:10;
y=exp(j*0.3*n);
subplot(211); stem(n,real(y))
subplot(212); stem(n,imag(y))
figure;
subplot(211); stem(n,abs(y))
subplot(212); stem(n,phase(y)*180/pi)
%Sin
n=-10:10;
y=sin(n)
stem(n,y)
%Exp seq
B=1;
r=0.85;
n=-10:10;
x=B*r.^n;
subplot(311); stem(n,x,'filled'); xlabel('Time(n)'); ylabel('x[n]');
x=B*r.^(-n);
subplot(312); stem(n,x,'filled'); xlabel('Time(n)'); ylabel('x[n]');
x=B*r.^abs(n);
subplot(313); stem(n,x,'filled'); xlabel('Time(n)'); ylabel('x[n]');

%1.2 continous
%Step sig
function[y,t]=stepsig(a,b,c)
t=a:0.01:b;
y=(n-c)>=0;
plot(t,y)
%Ramp sig
function[y,t]=ranpsig(a,b,c)
t=a:0.01:b;
y=((n-c)>=0).*(n-c);
plot(t,y)
%Real exp sig
function[y,t]=expsig(a,b,c)
t=b:0.01:c;
y=exp(a*t);
plot(t,y)
%Comp exp sig
t=-10:0.01:10;
y=exp(j*w*t);
subplot(211); plot(t,abs(y))
subplot(212); plot(t,phase(y)*180/pi)
%sin
t=-10:0.01:10;
y=sin(t);
plot(y,t)

%1.3 gen of sq and tri waves


clc; clear all; close all;
t=-10:0.01:10;
f1=input('Enter the sq wave 1, f1= ');
sq_wave1=square(2*pi*f1*t)
figure; plot(t,sq_wave1);
f2=input('Enter the sq wave 2, f2= ');
sq_wave2=square(2*pi*f2*t,75)
figure; plot(t,sq_wave2);
f3=input('Enter the tri wave 1, f3= ');
tri_wave1=sawtooth(2*pi*f3*t,0.5)
figure; plot(t,tri_wave1);
f4=input('Enter the sawtooth wave 1, f4= ');
sawtooth1=sawtooth(2*pi*f4*t,0)
figure; plot(t,sawtooth1);
f5=input('Enter the sawtooth 2, f5= ');
sawtooth2=sawtooth(2*pi*f5*t,1)
figure; plot(t,sawtooth2);

%2.1 response of lti


%response
clc; clear all; close all;
b=1;
a=[1 -1 0.9];
n=0:50;
delta=(n==0);
h=filter(b,a,delta);
figure(1)
subplot(211); stem(n,delta); title('Impulse Input')
subplot(212); stem(n,h); title('Impulse Response')
step=(n>=5);
s=filter(b,a,step);
figure(2)
subplot(211); stem(n,step); title('step Input')
subplot(212); stem(n,s); title('step Response')
x=0.25.^n;
y=filter(b,a,x);
figure(3)
subplot(211); stem(n,x); title('real exp input')
subplot(212); stem(n,y); title('output response')

%2.2 convol sum


clc; clear all; close all;
x=input('Enter 1st seq');
nx=input('enter index 1st');
h=input('Enter 2st seq');
nh=input('enter index 2nd');
n=min(nx)+min(nh):max(nx)+max(nh);
y=conv(x,h);
disp(The conv seq);y
disp(the index of conv seq);n
subplot(311); stem(nx,x); title('1st seq')
subplot(312); stem(nh,h); title('2nd seq')
subplot(313); stem(n,y); title('final seq')

%3.1 find imp res


clc; clear all; close all;
x=[1,zeros(1,10)];
num=[10 5 0]
den=[1 -1 0.2]
y=filter(num,dem,x)
disp(y)
sys1=tf(num,den,-1)
pzmap(sys1);

%3.2 plot poles n zeros, find trans func, partial frac expansion, freq res
clc; clear all; close all;
num=[1 0 -1]
den=[1 0 -0.81]
zplane(num,den)
sys=tf(num,den,-1)
[R,P,K]=residuez(num,den)
[b,a]=residuez(R,P,K)
figure;
omega=0:pi/100:pi;
y=freq(num,den,omega);
subplot(211); stem(omega/pi,abs(y)); xlabel('freq in pi units');
ylabel('Magnitude'); title('mag resp');
subplot(212); stem(omega/pi,phase(y)/pi); xlabel('freq in pi units');
ylabel('Phase'); title('phase resp');

%4.1 find dft using dft matrix


Plot mag, phase res of transformed seq. Find time dom seq through idft
x=('Type in N')
N=length(x);
W=dftmtx(N);
X=W*x';
magX=abs(X);
phaX=angle(X);
n=0:N-1;
subplot(311); stem(n,x);
k=0:N-1;
subplot(312); stem(k,magX);
subplot(313); stem(n,phaX);
disp('Input seq'); disp(x)
disp('Dft of seq'); disp(X)
x1=W.*X/N;
disp('time dom seq through IDFT'); disp(x1)

%4.2 find dft through fft


x=('Type in N')
N=length(x);
X=fft(x,N);
magX=abs(X);
phaX=angle(X);
n=0:N-1;
subplot(311); stem(n,x);
k=0:N-1;
subplot(312); stem(k,magX);
subplot(313); stem(n,phaX);
disp('Input seq'); disp(x)
disp('Dft of seq'); disp(X)
x1=ifft(X,N);
disp('time dom seq through IDFT'); disp(x1)

%5.1 cir conv in time dom


x=input('enter')
h=input('enter')
N1=length(x); N2=length(h); N=max(N1,N2);
x=[x zeros(1,N-N1)];
h=[h zeros(1,N-N2)];
y=cconv(x,h,N);
n=1:N;
disp('output'); disp(y)
stem(n,y);

%5.1.1 own func using for loops


x=input('enter')
h=input('enter')
N1=length(x); N2=length(h); N=max(N1,N2);
x=[x zeros(1,N-N1)];
h=[h zeros(1,N-N2)];
y=x;
for i=1:N-1
y=[y;circshift(x,i)];
end
y=y';
z=y*h';
n=1:N-1;
disp('output'); disp(z)
stem(n,z);

%5.2 cir conv in freq dom


x=input('enter')
h=input('enter')
N1=length(x); N2=length(h); N=max(N1,N2);
x=[x zeros(1,N-N1)];
h=[h zeros(1,N-N2)];
y=ifft(fft(x,N).*fft(h,N));
n=1:N;
disp('output'); disp(y)
stem(n,y);

%5.3 lin conv by cir conv in time


x=input('1st'); nx=input('index 1st');
h=input('2nd'); nh=input('index 2nd');
N=length(x)+length(h)-1;
x=[x zeros(1,N-length(x))];
h=[h zeros(1,N-length(h))];
ny=min(nx)+min(nh):max(nx)+max(nh);
y=cconv(x,h,N);
disp('output'); disp(y); disp(ny);
stem(ny,y);
%5.3.1 circ conv by using lin conv
x=input('1st'); nx=input('index 1st');
h=input('2nd'); nh=input('index 2nd');
N=length(x)+length(h);
x=[x zeros(1,N-length(x))];
h=[h zeross(1,N-length(h))];
lin_conv=conv(x,h);
cir_conv=lin_conv(1:N)+[zeros(1,N-length(lin_conv(N+1:end))),lin_conv(N+1:end)];
n=0:N-1;
disp('output'); disp(cir_conv)
stem(n,cir_conv)

%5.4 lin conv by cir conv in freq dom


x=input('1st'); nx=input('index 1st');
h=input('2nd'); nh=input('index 2nd');
N=length(x)+length(h)-1;
x=[x zeros(1,N-length(x))];
h=[h zeross(1,N-length(h))];
y=ifft(fft(x,N).*fft(h,N));
disp('output'); disp(y) disp(ny)
stem(ny,y);

You might also like