DSP


 1.Bilinear


clc;

clear all;

close all;

b=[0.5266];

a=[1 1.03 0.5266];

fs=100;

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


2.Butter worth Band stop


clc;

clear all;

close all;

fp1=200;

fp2=400;

fs1=100;

fs2=500;

Fs=2000;

Wp1=2*fp1/Fs;

Wp2=2*fp2/Fs;

Ws1=2*fs1/Fs;

Ws2=2*fs2/Fs;

Rp=2;

Rs=20;

[N,Wc]=buttord([Wp1,Wp2],[Ws1,Ws2],Rp,Rs);

[b,a]=butter(N,Wc,'stop');

w=0:0.1:pi;

[h,omega]=freqz(b,a,w,'whole');

subplot(2,1,1);

plot(omega/pi,20*log10(abs(h)))

xlabel('omega/pi');

ylabel('Gain,db');

title(sprintf('N=%d Butterworth Lowpass Filter',N));

grid on;

an=angle(h);

subplot(2,1,2);

plot(omega/pi,unwrap(an));

grid on;

xlabel('normalized frequency');

ylabel('phase in radians');


3.Butter worth Band pass


clc;

clear all;

close all;

fp1=200;

fp2=400;

fs1=100;

fs2=500;

Fs=2000;

Wp1=2*fp1/Fs;

Wp2=2*fp2/Fs;

Ws1=2*fs1/Fs;

Ws2=2*fs2/Fs;

Rp=2;

Rs=20;

[N,Wc]=buttord([Wp1,Wp2],[Ws1,Ws2],Rp,Rs);

[b,a]=butter(N,Wc);

w=0:0.1:pi;

[h,omega]=freqz(b,a,w,'whole');

subplot(2,1,1);

plot(omega/pi,20*log10(abs(h)))

xlabel('omega/pi');

ylabel('Gain,db');

title(sprintf('N=%d Butterworth Lowpass Filter',N));

grid on;

an=angle(h);

subplot(2,1,2);

plot(omega/pi,unwrap(an));

grid on;

xlabel('normalized frequency');

ylabel('phase in radians');


4.butter worth high pass


clc;

clear all;

close all;

fp=1000;

fs=3000;

Fs=8000;

Wp=2*fp/Fs;

Ws=2*fs/Fs;

Rp=2;

Rs=20;

[N,Wc]=buttord(Wp,Ws,Rp,Rs);

[b,a]=butter(N,Wc,"high");

w=0:0.1:pi;

freqz(b,a,1000,Fs);

% [h,omega]=freqz(b,a,w,'whole');

% subplot(2,1,1);

% plot(omega/pi,20*log10(abs(h)))

% xlabel('omega/pi');

% ylabel('Gain,db');

% title(sprintf('N=%d Butterworth Lowpass Filter',N));

% grid on;

% an=angle(h);

% subplot(2,1,2);

% plot(omega/pi,an);

% grid on;

% xlabel('normalized frequency');

% ylabel('phase in radians');


5.Butter worth low pass


clc;

clear all;

close all;

fp=1000;

fs=3000;

Fs=8000;

Wp=2*fp/Fs;

Ws=2*fs/Fs;

Rp=2;

Rs=20;

[N,Wc]=buttord(Wp,Ws,Rp,Rs);

[b,a]=butter(N,Wc);

w=0:0.1:pi;

 freqz(b,a,1000,Fs);

% [h,omega]=freqz(b,a,w,'whole');

% subplot(2,1,1);

% plot(omega/pi,20*log10(abs(h)))

% xlabel('omega/pi');

% ylabel('Gain,db');

% title(sprintf('N=%d Butterworth Lowpass Filter',N));

% grid on;

% an=angle(h);

% subplot(2,1,2);

% plot(omega/pi,an);

% grid on;

% xlabel('normalized frequency');

% ylabel('phase in radians');


6.cheby worth Band stop


clc;

clear all;

close all;

fp1=200;

fp2=400;

fs1=100;

fs2=500;

Fs=2000;

Wp1=2*fp1/Fs;

Wp2=2*fp2/Fs;

Ws1=2*fs1/Fs;

Ws2=2*fs2/Fs;

Rp=2;

Rs=20;

[N,Wc]=cheb1ord([Wp1,Wp2],[Ws1,Ws2],Rp,Rs);

[b,a]=cheby1(N,Rp,Wc,'stop');

w=0:0.1:pi;

freqz(b,a,1000,Fs);

% [h,omega]=freqz(b,a,w,'whole');

% subplot(2,1,1);

% plot(omega/pi,abs(h))

% xlabel('omega/pi');

% ylabel('Gain,db');

% title(sprintf('N=%d Chebyshev Bandstop Filter',N));

% grid on;

% an=angle(h);

% subplot(2,1,2);

% plot(omega/pi,an);

% grid on;

% xlabel('normalized frequency');

% ylabel('phase in radians');


7.cheby worth Band pass


clc;

clear all;

close all;

fp1=200;

fp2=400;

fs1=100;

fs2=500;

Fs=2000;

Wp1=2*fp1/Fs;

Wp2=2*fp2/Fs;

Ws1=2*fs1/Fs;

Ws2=2*fs2/Fs;

Rp=2;

Rs=20;

[N,Wc]=cheb1ord([Wp1,Wp2],[Ws1,Ws2],Rp,Rs);

[b,a]=cheby1(N,Rp,Wc);

w=0:0.1:pi;

freqz(b,a,1000,Fs);

% [h,omega]=freqz(b,a,w,'whole');

% subplot(2,1,1);

% plot(omega/pi,20*log10(abs(h)))

% xlabel('omega/pi');

% ylabel('Gain,db');

% title(sprintf('N=%d Chebyshev Lowpass Filter',N));

% grid on;

% an=angle(h);

% subplot(2,1,2);

% plot(omega/pi,an);

% grid on;

% xlabel('normalized frequency');

% ylabel('phase in radians');


8.cheby worth high pass


clc;

clear all;

close all;

fp=1000;

fs=3000;

Fs=8000;

Wp=2*fp/Fs;

Ws=2*fs/Fs;

Rp=2;

Rs=20;

[N,Wc]=cheb1ord(Wp,Ws,Rp,Rs);

[b,a]=cheby1(N,Rp,Wc,'high');

w=0:0.1:pi;

freqz(b,a,1000,Fs);

% [h,omega]=freqz(b,a,w,'whole');

% subplot(2,1,1);

% plot(omega/pi,20*log10(abs(h)))

% xlabel('omega/pi');

% ylabel('Gain,db');

% title(sprintf('N=%d Chebyshev Lowpass Filter',N));

% grid on;

% an=angle(h);

% subplot(2,1,2);

% plot(omega/pi,an);

% grid on;

% xlabel('normalized frequency');

% ylabel('phase in radians');


9.cheby worth low pass


clc;

clear all;

close all;

fp=1000;


fs=3000;

Fs=8000;

Wp=2*fp/Fs;

Ws=2*fs/Fs;

Rp=2;

Rs=20;

[N,Wc]=cheb1ord(Wp,Ws,Rp,Rs);

[b,a]=cheby1(N,Rp,Wc);

w=0:0.1:pi;

freqz(b,a,1000,Fs);

% [h,omega]=freqz(b,a,w,'whole');

% subplot(2,1,1);

% plot(omega/pi,20*log10(abs(h)))

% xlabel('omega/pi');

% ylabel('Gain,db');

% title(sprintf('N=%d Chebyshev Lowpass Filter',N));

% grid on;

% an=angle(h);

% subplot(2,1,2);

% plot(omega/pi,an);

% grid on;

% xlabel('normalized frequency');

% ylabel('phase in radians');


10.Coswave


clc;

close all;

clear all;

t=0.1;

fs=1000;

f0=50;

length=(fs*t);

y=zeros(1,length);

impulse(600)=1;

a1=2*(cos(2*pi*(f0/fs)));

bo=cos(2*pi*(f0/fs));

y(1)=cos(2*pi*(f0/fs));

y(2)=cos(4*pi*(f0/fs));

for i=3:length

    y(i)=impulse(i)-(bo*impulse(i-1))+(a1*y(i-1))-(y(i-2));

end

plot(y);

title('cosine waveform')

xlabel('n');

ylabel('y(n)');

grid on;


11.decimation

M=3;

b=[1 1];

a=[2 0];

n=1:100;

f=100;

fs=5000;

%tf(b,a,1/fs);

x=cos(2*pi*f/fs*n);

xfil=filter(b,a,x);

y=x([1:M:length(xfil)]);

subplot(2,1,1);

stem(n,x);

subplot(2,1,2);

stem(1:length(y),y);


12.DFT


clc;

clear all;

close all;

N=5 ;

x=[0,1,2,3 ,5];

if N>length(x)

x=[x, zeros(1,N-length(x))]

end

i=0:1:N-1;

Xk=zeros(1,N);

for k=1:1:N

    xk=0;

    for n=1:1:N

        Wn=exp((-1i)*2*(pi/N)*(n-1)*(k-1));

        xk=(x(n)*Wn)+xk;

    end

    Xk(k)=xk;

end

disp(Xk);

magX=abs(Xk);

phaseX=angle(Xk)*(180/pi);

subplot(3,1,1);

stem(i,magX,'filled','MarkerSize',5);

axis([-1,6,-1,15]);

xlabel('k');

ylabel('Magnitude');

title('Magnitude plot');

grid on;

subplot(3,1,2);

stem(i,phaseX,'filled','MarkerSize',5);

% axis([-1,6,-200,200]);

xlabel('k');

ylabel('Phase');

title('Phase Plot');

% grid on;

% Xn=zeros(1,N);

% for k=1:1:N

%     xn=0;

%     for n=1:1:N

%         Wn=exp((1j)*2*(pi/N)*(n-1)*(k-1));

%         xn=(Xk(n)*Wn)+xn;

%     end

%     Xn(k)=xn;

% end

% subplot(3,1,3);

% stem(i,(Xn./N),'filled','MarkerSize',5);

% axis([-1,6,-1,4]);

% xlabel('n');

% ylabel('x[n]');

% title('IDFT signal');

% % grid on;


13.DIT FFT


clc;

clearvars;

close all;

x = [1 2 3 7];

N = 8; 

if N >length(x)

 x=[x zeros(1,N-length(x))] 

end

y=bitrevorder(x);

M = log2(N);

for m=1:M 

 d=2^m;

 for i=1:d:N-d+1 

     for k=0:(d/2)-1

         w=exp(-1j*2*pi*k/d)

         G = y(i+k);

         H = y(i+k+d/2);

         y(i+k)=G+w*H; 

         y(i+k+d/2)=G-w*H;

     end

 end

 disp('Stage:');

 disp(m);

 disp(y);

end

toc

%Magnitude Plot

mgn=abs(y)

subplot(2,1,1)

stem(mgn);title('Magnitude')

%Phase Plot

ph=angle(y)*180/pi

subplot(2,1,2)

stem(ph);title('Phase');   


14.DIF FFT


clc; 


clear all; 


close all; 


tic 


x=[1 2 4 8]; 


N=8 


if N>length(x) 


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


end 


y=x; 


M=log2(N); 


for m=M:-1:1 


    d=2^m; 


    for i=1:d:N-d+1 


        for k=0:(d/2)-1 


            w=exp(-1j*2*pi*k/d); 


            G=y(i+k); 


            H=y(i+k+d/2); 


            y(i+k)=G+H; 


            y(i+k+d/2)=(G-H)*w; 


        end 


    end 


     disp('stage:'); 


    disp(m); 


    disp(y); 


    


end 


y=bitrevorder(y); 


toc 


mag=abs(y); 


subplot(2,1,1) 


stem(mag); 


title("Magnitude") 


ph=angle(y)*(180/pi); 


subplot(2,1,2) 


stem(ph); 


title('Phase'); 


 

15.DTMF


%DTMF

clc

clear all

close all

N = 1000;

fs= 3000;

freq = fs*(0:N/2)/N;

t = (0:0.1:N-1)/fs;

l=[697 770 852 941];

h = [1209 1336 1477];

a = [1 2 3;4 5 6;7 8 9;'*' 0 '#'];

disp('1 2 3');

disp('4 5 6');

disp('7 8 9');

disp('* 0 #');

k=input('enter the key: ');

%k=1;

for i=1:4

  for j=1:3

      if(k==a(i,j))

          x1= cos(2*pi*l(i)*t);

          x2 = cos(2*pi*h(j)*t);

          x = x1+x2;

      end

  end

end

X1 = abs(fft(x1));

X2 = abs(fft(x2));

X = abs(fft(x));

sound(x);


%sound(X);

subplot(3,2,1);

plot(t(1:100),x1(1:100));

title('low freq sig(x1)');

subplot(3,2,2);

plot(t(1:100),x2(1:100));

title('high freq sig(x2)');

subplot(3,2,3);

plot(t(1:100),x(1:100));  

title('low+high freq sig(x)');  

subplot(3,2,4);  

plot(freq,X1(1:N/2+1)); title('spec of x1');  

subplot(3,2,5);  

plot(freq,X2(1:N/2+1)); title('spec of x2');  

subplot(3,2,6);

7 / 23

plot(freq,X(1:N/2+1)); title('spec of x');


16.FIR Band pass


clc;

close all;

clear all;

n=20;

fp=100;

fs=1000;

fn=2*fp/fs;

window=hamming(n+1);

b1=fir1(n,fn,"bandpass",window);

w=0:0.001:pi;

[h,an]=freqz(b1,1,w);

a=20*log10(abs(h));

b=angle(h);

subplot(2,2,1);

plot(window);

title('window');

subplot(2,2,2);

plot(b1);

title('filtered output');

subplot(2,2,3)

plot(w/pi,a);

title('Magnitude Response');

subplot(2,2,4);

plot(w/pi,b);

title('Phase Response')


17.FIR High pass


clc;

close all;

clear all;

n=20;

fp=100;

fs=1000;

fn=2*fp/fs;

window=hamming(n+1);

b1=fir1(n,fn,"high",window);

w=0:0.001:pi;

[h,an]=freqz(b1,1,w);

a=20*log10(abs(h));

b=angle(h);

subplot(2,2,1);

plot(window);

title('window');

subplot(2,2,2);

plot(b1);

title('filtered output');

subplot(2,2,3)

plot(w/pi,a);

title('Magnitude Response');

subplot(2,2,4);

plot(w/pi,b);

title('Phase Response')


18.FIR low pass


clc;

close all;

clear all;

n=20;

fp=100;

fs=1000;

fn=2*fp/fs;

window=hamming(n+1);

b1=fir1(n,fn,window);

w=0:0.001:pi;

[h,an]=freqz(b1,1,w);

a=20*log10(abs(h));

b=angle(h);

subplot(2,2,1);

plot(window);

title('window');

subplot(2,2,2);

plot(b1);

title('filtered output');

subplot(2,2,3)

plot(w/pi,a);

title('Magnitude Response');

subplot(2,2,4);

plot(w/pi,b);

title('Phase Response')


19.Frequency Response


clc;

close all;

a=[1,-2];

b=[0.6,0.3]; 

w=-pi:0.1:pi

h=freqz(b,a,w);

subplot(2,1,1);

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

title('magnitude plot');

grid on;

subplot(2,1,2);

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

title('phase plot');

grid on;


20.Complex single zero


clc;

close all;

clear all;

set(0, 'DefaultLineLineWidth', 2);

w=0:1999;

h=freqz([1 -0.9*exp(-j*w)],1,2000,'whole');

subplot(2,1,1);

plot((0:1999)/2000,abs(h));

ylabel('magnitude');

xlabel('normalised frequency');

title('Frequency Response of Single Complex Zero');

grid on;

subplot(2,1,2);

plot((0:1999)/2000,20*log10(abs(h)));

ylabel('magnitude(db)');

xlabel('normalised frequency');

title('Frequency Response of Single Complex Zero');

grid on;


21.Complex single pole


clc;

close all;

clear all;

%set(0, 'DefaultLineLineWidth', 1);

h=freqz(1./([1 -0.9*exp(j*pi/4)]),1,2000,'whole');

subplot(2,1,1);

plot((0:1999)/2000,abs(h));

ylabel('magnitude');

xlabel('normalised frequency');

title('Frequency Response of Single Complex Pole');

grid on;

subplot(2,1,2);

plot((0:1999)/2000,20*log10(abs(h)));

ylabel('magnitude(db)');

xlabel('normalised frequency');

title('Frequency Response of Single Complex Pole');

grid on;


22.Impulse invariance


clc;

clear all;

close all;

b=[0.5266];

a=[1 1.03 0.5266];

fs=100;

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


23.Impulse response


%

clc;

close all;

clear all;

w=1:1:1000;

b=1;

a=[1,sin(w)];

imp=[1,zeros(1,100)];

h=filter(b,a,imp);

stem(0:100,h);

xlabel('n');

ylabel('y[n]');

title('impusle response')


24.Interpolation


clc;

close all;

clear all

t=0:0.0025:0.5;

x=cos(2*pi*10*t);

y=interp(x,7);

y1=decimate(y,2);

subplot(2,1,1);

stem(x);

subplot(2,1,2);

stem(1:length(y1),y1);


25.Lowpass to Highpass


clc;

clear all;

close all;

b=[1];

a=[1 3 2];

wc=0.25;

wd=0.0175;

[bz,az]=iirlp2hp(b,a,wc,wd);

w=0:0.1:pi;

h=freqz(b,a,w);

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


26.Noise Removing


clc;

close all;

clear all;

N=100;

n=0:N-1;

f=1000;

fs=8000;

y=sin(2*pi*(f/fs)*n);

figure

plot(n,y);

xlabel('Magnitude');

ylabel('Samples');

Ak=2*abs(fft(y))/length(y);

Ak(1)=Ak(1)/2;

f=[0:1:(length(y)-1)/2]*fs/length(y);

figure

plot(f,Ak(1:length(y+1)/2));

xlabel('Spectrum of y(n)');

ylabel('Frequency(Hz)');

title('FFT Spectrum of samples');

x=y+randn(size(n));

figure

plot(n,x);

xlabel('Magnitude');

ylabel('Samples');

title('Signal With noise');

b1=[0.028  0.053 0.071  0.053 0.028];

a1=[1.000 -2.026 2.148 -1.159 0.279];

y1=filter(b1,a1,x);

figure

plot(n,y1);

xlabel('Magnitude');

ylabel('Samples');

title('Filtered Output');

Ak=2*abs(fft(y1))/length(y1);

Ak(1)=Ak(1)/2;

f=[0:1:(length(y1)-1)/2]*fs/length(y1);

figure

plot(f,Ak(1:length(y1+1)/2));

xlabel('Spectrum of y(n)');

ylabel('Frequency(Hz)');

title('FFT Spectrum of samples');


27.Nortch Filter


%notchfilter 

clc;

clear;

close all; t= pi/3;

dw=2*pi/1000;

w=-1*pi/2: dw: pi/2; r=0.95;

a=[1-2 cos(t) 1];

b=[1 -2*r*cos(t) r*r];

H=freqz (a,b,w); subplot (4,3,1);

hold on;

plot (w, abs (H), 'Displayname', 'r=0.95'); legend;

title('magnitude'); xlabel('w');

ylabel('mag'); grid on;

subplot (4,3,2); hold on;

plot (w, angle (H)); title('phase');

xlabel('w');

ylabel('phase');

grid on;

subplot (4,3,3); zplane(a,b);

r=0.9;

a=[1 -2*cos(t) 1]; b=[1 -2*r*cos(t) r*r];

H1=freqz (a,b,w); subplot (4,3,1);

hold on;

plot (w,abs(H1),'Displayname','r=0.9');

legend; subplot (4,3,2);

hold on;

plot(w, angle (H1)); subplot (4,3,3);

zplane(a,b);

r=0.99;

a=[1-2*cos(t) 1]; ;

b=[1 -2*r*cos(t) r*r];

H1-freqz (a,b,w);

subplot (4,3,1);

hold on; plot (w, abs (H1), 'Displayname', 'r=0.99');

legend;

subplot (4,3,2);

hold on;

plot (w, angle (H1)); subplot (4,3,3);

zplane(a,b);


28.Power Denstiy Spectrum


%PSD

clc;

clear all;

close all;

N=100;

fs=600;

f=250;   

n=1:N;

X=sin(2*pi*f/fs*n);

freq=fs*(1:N/2)/N;

Psd=abs(fft(X,N)).^2/N;

subplot(2,1,1);

stem(freq,Psd(1:N/2));

title('psd of sine wave');

xlabel('samples n');

ylabel('Amplitude');

grid on;

y=[1111 1111];

N1=128;

Ns=length(y);

Psd=abs(fft(y,N)).^2/Ns;

freq1=(1:N1/2)/N;

subplot(2,1,2);

stem(freq,Psd(1:N/2));

title('psd of output sequence');

xlabel('samples n');

ylabel('amplitude');


29.Sine wave



clc;

close all;

clear all;

t=0.1;

fs=1000;

f0=50;

length=(fs*t);

y=zeros(1,length);

impulse(600)=1;

a1=2*(cos(2*pi*(f0/fs)));

bo=sin(2*pi*(f0/fs));

y(1)=sin(2*pi*(f0/fs));

y(2)=sin(4*pi*(f0/fs));

for i=3:length

    y(i)=(bo*impulse(i-1))+(a1*y(i-1))-(y(i-2));

end

plot(y);

title('sinusoidal waveform')

xlabel('n');

ylabel('y(n)');

grid on;


30.Stability of System


clc;

clear all;

close all;

num=[2,3,-1];

den=[-1,5,2];

sys=tf(num,den);

[z,p,k]=zpkdata(sys);

disp('zeros');

disp(z);

disp('poles');

disp(p);

disp('gain');

disp(k);

zplane(num,den);

title('Pole zero plot of the system');

%stability

if all(abs(roots(den))<1)

    disp('System is stable');

elseif any(abs(roots(den))==1)

    disp('System is Marginally Stable');

else

    disp('System is unstable');

end


if all(abs(roots(num))<1)

    disp('System is minimum phase system');

elseif all(abs(roots(num))>1)

    disp('System is maximum phase system');

else

    disp('System is neither minimum nor maximum phase sytem');

end


Popular posts from this blog

పరమ వీర చక్ర

ac lab