Вы находитесь на странице: 1из 9

%impulse function

clc
clear all
n=-5:1:5;
y=[zeros(1,5) 1 zeros(1,5)];
stem(n,y)
%unit step function
clc
clear all
n=-5:1:5;
y=[zeros(1,5) ones(1,6)];
stem(n,y)
%unit ramp function
clc
clear all
n=0:1:10;
y=n;
stem(n,y)
%exponential decay function'
clc
clear all
n=0:1:5;
y=0.6.^n;
stem(n,y)
%exponential increasing function
clc
clear all
n=0:1:5;
y=1.5.^n;
stem(n,y)
%complex exponential function
clf;
c=-(-1/12)+(pi/6)*i;
k=2;
n=0:40;
x=k*exp(c*n);
subplot(2,1,1);
stem(n,real(x));
xlabel('Time index n');
ylabel('Amplitude');
title('Real part');
subplot(2,1,2);
stem(n,imag(x));
xlabel('Time index n');
ylabel('Amplitude');
title('imaginary part');
%sine wave generation
clf;
clear all
t=0:0.01:1;
x=sin(2*pi*2*t);
stem(t,x)
xlabel('time index n');
ylabel('amplitude');

%sine wave addition


clf;
clear all
t=0:0.01:1;
x=sin(2*pi*2*t)+sin(2*pi*10*t);
stem(t,x)
xlabel('time index n');
ylabel('amplitude');
%% linear convolution
clc
clear all
x=input('enter the value of first sequence');
h=input('enter the value of second sequence');
m=length(x)
n=length(h)
for i=1:m
for j=i:n
y(i,i+j-1)=x(i)*h(j);
end
end
z=sum(y);
display(z)
stem(z);
%linear built-in convolution
clc
clear all
x=input('enter the value of first sequence');
h=input('enter the value of second sequence');
z=conv(x,h);
display(z);
stem(z);
%circular convolution
clc
clear all
x=input('enter the value of first sequence');
h=input('enter the value of second sequence');
n1=length(x)
n2=length(h)
p=max(n1,n2);
w=[x,zeros(1,p-n1)];
z=[h,zeros(1,p-n2)];
for i=0:p-1
y(i+1)=0;
for j=0:p-1
k=mod(i-j,p);
y(i+1)=y(i+1)+(x(k+1)*h(j+1));
end
end
display(y);
stem(y);

%covolution with sound


clc
clear all

load chirp;
x=y;
load gong;
z=conv(x,y);
player=audioplayer(x,Fs)
play(player)
player=audioplayer(z,Fs)
play(player)
display(z)
plot(z)
%% correlation
clc
clear all
x=input('enter the value of first sequence');
h=input('enter the value of second sequence');
h=fliplr(h);
m=length(x)
n=length(h)
for i=1:m
for j=i:n
y(i,i+j-1)=x(i)*h(j);
end
end
z=sum(y);
display(z)
stem(z);
clc
clear all
x=input('enter the value of first sequence');
h=input('enter the value of second sequence');
y=xcorr(x,h)
display(y)
stem(y)
%using real time signal
clc
clear all
load chirp
x=y;
load gong
z=xcorr(y,y)
player=audioplayer(x,Fs)
play(player)
player=audioplayer(y,Fs);
play(player)
player=audioplayer(z,Fs)
play(player)
display(z)
%cross correlation using real time signals
clc
clear all
load chirp
x=awgn(y,0.5)
p=x+y;
z=xcorr(y,x)
player=audioplayer(p,Fs)
play(player)

player=audioplayer(y,Fs);
play(player)
player=audioplayer(z,Fs)
play(player)
display(z)p
%% z transform
clc
clear all
syms z n
eq=2^n;
A=ztrans(eq)
AA=ztrans(A);
display(A);
display(AA);
%poles and zeros
clc
clear all
num=input('enter
den=input('enter
zplane(num,den)
%poles and zeros
clc
clear all
num=input('enter
den=input('enter
zer=roots(num)
pol=roots(den)
zplane(zer,pol)

the numerator coeff');


the denominator coeff');
method 2
the numerator coeff');
the denominator coeff');

% one more form


num=input('enter the numerator coeff');
den=input('enter the denominator coeff');
[z,p,k]=tf2zp(num,den)
zplane(z,p)
sos=zp2sos(z,p,k);
%Partial Form Of Z Transform
num=input('enter the numerator coeff');
den=input('enter the denominator coeff');
[r s t]=residue(num,den)
%stability
clc
clear all
num=input('enter the numerator coeff');
den=input('enter the denominator coeff');
zer=roots(num)
pol=roots(den)
a=abs(zer)
b=abs(pol)
if(b>1)
disp('unstable');
elseif(b==1)
disp('marginally stable')
else
disp('stable')
end

%% DFT equation for input1


clc
clear all
k=0:31;
x=[1 1 1 1 1 zeros(1,27)];
[h,w]=freqz(x,1,'whole');
plot(w/(2*pi),abs(h))
X=fft(x);hold on
stem(k/32,abs(X),'ro');grid;
%DFT for sinusoidal input
clc
clear all
k=0:31;
x=cos(2*pi*k*3/16);
[h,w]=freqz(x,1,'whole');
plot(w/(2*pi),abs(h))
X=fft(x);hold on
stem(k/32,abs(X),'ro');grid;
%DFT for addition of 2 sinusoid as input
clc
clear all
k=0:31;
x=cos(2*pi*k*3/16)+cos(2*pi*k*0.3);
[h,w]=freqz(x,1,'whole');
plot(w/(2*pi),abs(h))
X=fft(x);hold on
stem(k/32,abs(X),'ro');grid;

%DFT for addition of 3 sinusoid as input


clc
clear all
k=0:31;
x=cos(2*pi*k*0.1)+cos(2*pi*k*0.3)+cos(2*pi*k*0.5);
[h,w]=freqz(x,1,'whole');
plot(w/(2*pi),abs(h))
X=fft(x);hold on
stem(k/32,abs(X),'ro');grid;
%DFT without built-in function
clc
clear all
close all
n=0:10;
x=0.9*exp(j*pi/3).^n;
l=-200:200;
w=-2*pi:pi/100:2*pi
X=x*exp(-j*2*pi/100).^(n'*l)
subplot(2,1,1);plot(w/pi,abs(X));
subplot(2,1,2);plot(w/pi,angle(X));
%DFT without built-in function for another input
clc
clear all

x=[1 1 1 1 1 zeros(1,27)];
N=length(x);
n=[0:1:N-1];
k=[0:1:N-1];
WN=exp(-j*2*pi/N);
nk=n'*k;
WNnk=WN.^nk;
Xk=x*WNnk;
MagX=abs(Xk);
PhaseX=angle(Xk)*180/pi;
figure(1);
subplot(2,1,1); stem(k/32,MagX)
subplot(2,1,2); plot(k/32,PhaseX)
%% properties of dft
%increasing the length N
clc
clear all
f1=10;
fs=100;
n=[0:29];
x=sin(2*pi*(f1/fs)*n);
N1=256; N2=128; N3=64;
Y1=abs(fft(x,N1)); Y2=abs(fft(x,N2)); Y3=abs(fft(x,N3));
F1=0:fs/N1:(fs-fs/N1);
F2=0:fs/N2:(fs-fs/N2);
F3=0:fs/N3:(fs-fs/N3);
subplot(3,1,1);plot(F1,Y1,'-x');
subplot(3,1,2);plot(F2,Y2,'-x');
subplot(3,1,3);plot(F3,Y3,'-x');
%increasing the period
clc
clear all
f1=10;
fs=100;
n=[0:29];
x1=sin(2*pi*(f1/fs)*n);
x2=[x1 x1];
x3=[x1 x1 x1];
subplot(3,1,1);plot(x1);
subplot(3,1,2);plot(x2);
subplot(3,1,3);plot(x3);
%
clc
clear all
f1=10;
fs=100;
n=[0:29];
x1=sin(2*pi*(f1/fs)*n);
x2=[x1 x1];
x3=[x1 x1 x1];
N=512;
Y1=abs(fft(x1,N));
Y2=abs(fft(x2,N));
Y3=abs(fft(x3,N));

F1=0:fs/N:(fs-fs/N);
F2=0:fs/N:(fs-fs/N);
F3=0:fs/N:(fs-fs/N);
subplot(3,1,1);plot(F1,Y1,'-x');
subplot(3,1,2);plot(F2,Y2,'-x');
subplot(3,1,3);plot(F3,Y3,'-x');
%spectral shift
clc
clear all
n=[0:149];
x=cos(2*pi*n/101);
N=2048;
y=abs(fft(x,N));
Y1=fftshift(y);
F=[-N/2:N/2-1]/N;
plot(F,Y1)
%spectral shift for different frequencies
clc
clear all
f1=10;
f2=20;
fs=100;
n=[0:29];
x=sin(2*pi*(f1/fs)*n)+sin(2*pi*(f2/fs)*n);
N=512;
Y=abs(fft(x,N));
Y1=fftshift(Y);
F=[-N/2:N/2-1]/N;
%F=0:fs/N:(fs-fs/N);
plot(F,Y1,'-x');
%% Filter
%LPF
Fp=4000;
Fs=8000;
Wp=2*pi*Fp;
Ws=2*pi*Fs;
[n,wn]=buttord(Wp,Ws,0.5,40,'s');
[b1,a1]=butter(n,wn,'s');
wa=0:(3*Fs)/511:3*Fs;
A=freqs(b1,a1,wa);
subplot(2,2,1); plot(wa/(2*pi),20*log(abs (A))); grid;
title('Gain response- LPF Butterworth');
%HPF
Fp=4000;
Fs=8000;
Wp=2*pi*Fp;
Ws=2*pi*Fs;
[n,wn]=buttord(Wp,Ws,0.5,40,'s');
[b2,a2]=butter(n,wn,'high','s');
wa=0:(3*Fs)/511:3*Fs;
A=freqs(b2,a2,wa);
subplot(2,2,2); plot(wa/(2*pi),20*log(abs (A))); grid;
title('Gain response- HPF Butterworth');

%BPF
Fp=3000;
Fs=6000;
Wp=2*pi*Fp;
Ws=2*pi*Fs;
[n]=buttord(Wp,Ws,0.5,40,'s');
wn=[Wp,Ws];
[b3,a3]=butter(n,wn,'s');
wa=0:(3*Ws)/511:3*Ws;
A=freqs(b3,a3,wa);
subplot(2,2,3); plot(wa/(2*pi),20*log(abs(A))); grid;
title('Gain response - Butterworth-Band Pass');
axis([0 3*Fs -100 5]);
%BSF
Fp=3000;
Fs=6000;
Wp=2*pi*Fp;
Ws=2*pi*Fs;
[n]=buttord(Wp,Ws,0.5,40,'s');
wn=[Wp,Ws];
[b4,a4]=butter(n,wn,'stop','s');
wa=0:(3*Ws)/511:3*Ws;
A=freqs(b4,a4,wa);
subplot(2,2,4); plot(wa/(2*pi),20*log(abs(A))); grid;
title('Gain response - Butterworth-Band Stop');
axis([0 3*Fs -100 5]);
%% DSP ASSN
%DFT
clc
clear all
k=0:8;
x=[0 1 2 3 4 5 6 7];
[h,w]=freqz(x,1,'whole');
plot(w/(2*pi),abs(h))
X=fft(x);hold on
display(X);
stem(k/8,abs(X),'ro');grid;
%% frequency spectrum of filtered signal
clc;
clear all;
close all;
fs=100;f1=5;f2=15;f3=30;Rp=0.4;Rs=50;;Wp=10;Ws=20;
t=(1:100)/fs;
s1=sin(2*pi*f1*t);
s2=sin(2*pi*f2*t);
s3=sin(2*pi*f3*t);
s=s1+s2+s3;
figure(1);
plot(t,s);
title('s=s1+s2+s3');
xlabel('time');
ylabel('Amplitude');
[b,a]=ellip(4,0.4,50,[10 20]*2/fs);
[h,w]=freqz(b,a,512);

figure(2);
plot(w*fs/2*pi,abs(h));
title('response of filter');
xlabel('frequency');
ylabel('gain in dB');
sf=filter(b,a,s);
figure(3);
plot(sf);
title('filtered signal');
xlabel('time');
ylabel('amplitude');
s=fft(s,512);
sf=fft(sf,512);
p=(0:255)/256*(fs/2);
figure(4);
plot(p,abs([s(1:256)' sf(1:256)']));
title('frequency spectrum of filtered signal');
xlabel('frequency in Hz');
ylabel('PSD');

Вам также может понравиться