Skip to content

Commit

Permalink
Commiting all the Matlab Codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Afaaqahamedx committed Dec 20, 2023
0 parents commit ff1fd88
Show file tree
Hide file tree
Showing 23 changed files with 980 additions and 0 deletions.
64 changes: 64 additions & 0 deletions BW_IIR filter.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
%IIR BUTTERWORTH FILTER
syms s
kp=-2;
ks=-10;
wp=20;
ws=30;
j=sqrt(-1);
N=log((10^(-kp/10)-1)/(10^(-ks/10)-1))/(2*log(wp/ws))
N=round(N)+1
wc=wp/(10^(-kp/10)-1)^(1/(2*N))
H1=1/(s+1);
H2=1/(s^2+1.414*s+1);
H3=1/((s^2+s+1)*(s+1));
H4=1/((s^2+0.765368*s+1)*(s^2+1.84776*s+1));
H5=1/((s+1)*(s^2+0.6180*s+1)*(s^2+1.6180*s+1));

if N==1
H(s)=H1;
elseif N==2
H(s)=H2;
elseif N==3
H(s)=H3;
elseif N==4
H(s)=H4;
elseif N==5
H(s)=H5;
end
H(s)=H(s/wc)
H(s)=H(j*wp)
n=abs(H(s))

m=20*log(H(s))
%IIR BUTTERWORTH FILTER
syms s
kp=-2;
ks=-10;
wp=20;
ws=30;
j=sqrt(-1);
N=log((10^(-kp/10)-1)/(10^(-ks/10)-1))/(2*log(wp/ws))
N=round(N)+1
wc=wp/(10^(-kp/10)-1)^(1/(2*N))
H1=1/(s+1);
H2=1/(s^2+1.414*s+1);
H3=1/((s^2+s+1)*(s+1));
H4=1/((s^2+0.765368*s+1)*(s^2+1.84776*s+1));
H5=1/((s+1)*(s^2+0.6180*s+1)*(s^2+1.6180*s+1));

if N==1
H(s)=H1;
elseif N==2
H(s)=H2;
elseif N==3
H(s)=H3;
elseif N==4
H(s)=H4;
elseif N==5
H(s)=H5;
end
H(s)=H(s/wc)
H(s)=H(j*wp)
n=abs(H(s))

m=20*log(H(s))
64 changes: 64 additions & 0 deletions BW_IIR_filter.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
%IIR BUTTERWORTH FILTER
syms s
kp=-2;
ks=-10;
wp=20;
ws=30;
j=sqrt(-1);
N=log((10^(-kp/10)-1)/(10^(-ks/10)-1))/(2*log(wp/ws))
N=round(N)+1
wc=wp/(10^(-kp/10)-1)^(1/(2*N))
H1=1/(s+1);
H2=1/(s^2+1.414*s+1);
H3=1/((s^2+s+1)*(s+1));
H4=1/((s^2+0.765368*s+1)*(s^2+1.84776*s+1));
H5=1/((s+1)*(s^2+0.6180*s+1)*(s^2+1.6180*s+1));

if N==1
H(s)=H1;
elseif N==2
H(s)=H2;
elseif N==3
H(s)=H3;
elseif N==4
H(s)=H4;
elseif N==5
H(s)=H5;
end
H(s)=H(s/wc)
H(s)=H(j*wp)
n=abs(H(s))

m=20*log(H(s))
%IIR BUTTERWORTH FILTER
syms s
kp=-2;
ks=-10;
wp=20;
ws=30;
j=sqrt(-1);
N=log((10^(-kp/10)-1)/(10^(-ks/10)-1))/(2*log(wp/ws))
N=round(N)+1
wc=wp/(10^(-kp/10)-1)^(1/(2*N))
H1=1/(s+1);
H2=1/(s^2+1.414*s+1);
H3=1/((s^2+s+1)*(s+1));
H4=1/((s^2+0.765368*s+1)*(s^2+1.84776*s+1));
H5=1/((s+1)*(s^2+0.6180*s+1)*(s^2+1.6180*s+1));

if N==1
H(s)=H1;
elseif N==2
H(s)=H2;
elseif N==3
H(s)=H3;
elseif N==4
H(s)=H4;
elseif N==5
H(s)=H5;
end
H(s)=H(s/wc)
H(s)=H(j*wp)
n=abs(H(s))

m=20*log(H(s))
33 changes: 33 additions & 0 deletions Chebyshev_IIR.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
%Chebyshev Filter
syms s
kp=-2;
ks=-20;
wp=1;
ws=1.3;
j=sqrt(-1);
e=sqrt(((1/(10^(kp/20)))^2)-1);
dp=1-(1/sqrt(1+(e^2)));
ds=10^(ks/20);
%discrimination factor
d=sqrt(((1-dp)^(-2)-1)/((ds^(-2))-1));
%selectivity factor
k=wp/ws;
N=(acosh(1/d))/(acosh(1/k));
N=round(N)+1;
%to find a and b

a1=(1/2)*(((1+sqrt(1+e^2))/e)^(1/N));
a2=(1/2)*(((1+sqrt(1+e^2))/e)^(-1/N));
a=a1-a2;
b=a1+a2;

for k=1:1:N
S(k+1)=(-a)*sin(((2*k)-1)*(pi/(2*N)));
end

for k=1:1:N
w(k+1)=b*cos(((2*k)-1)*(pi/(2*N)));
end
for m=1:1:N
l(m+1)=(s-(S(m)+(j*w(m))));
end
26 changes: 26 additions & 0 deletions DFT.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
x = [1,2,3,4];
N = length(x);
% k = (1:N)
X = zeros(1, N);
j = sqrt(-1);
disp("DFT of x(n) ")
%DFT of x(n)
for k = 0:N-1
for n= 0:N-1
X(k+1) = X(k+1) + x(n+1)*(exp(-j * n * k *2 *pi / N))
end
end
ny = (1:N);
X_mag = abs(X);
X_phase = angle(X);
figure("Name","DFT")
subplot(2,1,1)
stem(ny,X_mag,"LineWidth",3)
xlabel("Frequency")
ylabel("Mag")
title("Magnitude plot")
subplot(2,1,2)
stem(ny, X_phase,"LineWidth",3)
xlabel("Frequency")
ylabel("Phase")
title("Phase plot")
26 changes: 26 additions & 0 deletions DFT2.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
x = [1,2,3,4,5];
N = length(x);
% k = (1:N)
X = zeros(1, N);
j = sqrt(-1);
disp("DFT of x(n) ")
%DFT of x(n)
for k = 0:N-1
for n= 0:N-1
X(k+1) = X(k+1) + x(n+1)*(exp(-j * n * k *(2 *pi / N)))
end
end
ny = (1:N);
X_mag = abs(X);
X_phase = angle(X);
figure("Name","DFT")
subplot(2,1,1)
stem(ny,X_mag,"LineWidth",3)
xlabel("Frequency")
ylabel("Mag")
title("Magnitude plot")
subplot(2,1,2)
stem(ny, X_phase,"LineWidth",3)
xlabel("Frequency")
ylabel("Phase")
title("Phase plot")
67 changes: 67 additions & 0 deletions FFT.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
syms Wnkm(k,m) Wnk(k)
x = [1 2 3 4 5 6 7 8];
N = length(x);
f1 = (zeros(1,round(N/2)));
f2 = (zeros(1,round(N/2)));
y=1;
%Decimating into EVEN and ODD
for i=1:N
if rem(i,2)==0
f2(y) = x(i);
y=y+1;
else
f1(y) = x(i);
end
end
j=sqrt(-1);
Wnkm(k,m) = exp((-j*4*pi*k*m)/N);
Wnk(k) = exp((-j*2*pi*k)/N);
F1 = (zeros(1,round(N/2)));
F2 = (zeros(1,round(N/2)));
%F1(k)
for k=1:(round(N/2))
for m=1:(round(N/2))
F1(k) = F1(k) + f1(m)*Wnkm(k-1,m-1);
end
end
%F2(k)
for k=1:(round(N/2))
for m=1:(round(N/2))
F2(k) = F2(k) + f2(m)*Wnkm(k-1,m-1);
end
end
F1;
F2;
X1 = (zeros(1,round(N/2)));
X2 = (zeros(1,round(N/2)));
%X1(k) = X(k) and X2(k) = X(k+N/2)
for k=1:(round(N/2))
X1(k) = F1(k) + Wnk(k-1)*F2(k);
X2(k) = F1(k) - Wnk(k-1)*F2(k);
end
X_without_fft = [X1,X2]
%X_without_fft = 1×8 complex
% 36.0000 + 0.0000i -4.0000 + 9.6569i -4.0000 + 4.0000i -4.0000 + 1.6569i
%
X_mag = abs(X_without_fft);
X_phase = angle(X_without_fft);
f = 1:N;
figure("Name","FFT")
subplot(3,1,1)
stem(f,x,"filled","LineWidth",3)
title("Input sequence")
ylabel("x(n)")
xlabel("N")
grid
subplot(3,1,2)
stem(f,X_mag,"filled","LineWidth",3)
title("Magnitude spectrum")
ylabel("Magnitude")
xlabel("Frequency")
grid
subplot(3,1,3)
stem(f,X_phase,"filled","LineWidth",3)
title("Phase spectrum")
ylabel("Phase")
xlabel("Frequency")
grid
58 changes: 58 additions & 0 deletions FIR.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
N=7;
wc=1;
alpha=(N-1)/2;
n=0:1:N-1;
%Desired impulse response
for i=0:1:(N-1)
if(i~=alpha)
hd(i+1)=(sin(wc*(i-3))/(pi*(i-3)));
else
hd(i+1)=wc/pi;
end
end
subplot(5,1,1);
stem(n,hd);
xlabel('n');
ylabel('hd(n)');
title('Desired impulse response');
wHm = zeros(1,7);
%Hamming window
for j=0: 6
wHm(j+1)=0.54-(0.46*cos(2*pi*j/(N-1)));
end
subplot(5,1,2);
stem(n,wHm);
xlabel('n');
ylabel('whm(n)');
title('Hamming window');
%Impulse response
hn=hd.*wHm;
subplot(5,1,3);
stem(n,hn);
xlabel('n');
ylabel('h(n)');
title('Impulse response');
hw=[];
for w=0:(1/pi):pi
t=0;
temp=0;
const=hn(alpha+1);
for m=0:1:((N-3)/2)
temp=temp+(2.*hn(m+1).*cos(w.*(alpha-m)));
end
temp=temp+const;
hw=[hw, temp];
end
w=0:1/pi:pi;
%Magnitude response
subplot(5,1,4);
stem(w,hw);
xlabel('w');
ylabel('|H(w)|');
title('Magnitude response');
phase=-w*alpha;
subplot(5,1,5);
stem(w,phase);
xlabel('w');
ylabel('Phase');
title('Phase response');
49 changes: 49 additions & 0 deletions FIR1.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
wc=1;
N=51;
alpha=(N-1)/2;
n=0:1:N-1;
%Rectangular window
for j=0:1:(N-1)
wR(j+1)=1;
end
subplot(5,1,1);
stem(n,wR);
xlabel('n');
ylabel('wR(n)');
title('Rectangular window');
%Bartlett window
for j=0:1:(N-1)
wBr(j+1)=1-(2.*abs(j-((N-1)/2)))/(N-1);
end;
subplot(5,1,2);
stem(n,wBr);
xlabel('n');
ylabel('wBr(n)');
title('Bartlett window');
%Hamming window
for j=0:1:(N-1)
wHm(j+1)=0.54-(0.46*cos(2*pi*j/(N-1)));
end;
subplot(5,1,3);
stem(n,wHm);
xlabel('n');
ylabel('whm(n)');
title('Hamming window');
%Hanning window
for j=0:1:(N-1)
wHn(j+1)=0.5-(0.5*cos(2*pi*j/(N-1)));
end;
subplot(5,1,4);
stem(n,wHn);
xlabel('n');
ylabel('whn(n)');
title('Hanning window');
%Blackmann window
for j=0:1:(N-1)
wBl(j+1)=0.42-(0.5*(cos(2*pi*j/(N-1))))+(0.08*(cos(4*pi*j/(N-1))));
end;
subplot(5,1,5);
stem(n,wBl);
xlabel('n');
ylabel('wbl(n)');
title('Blackmann window');
Loading

0 comments on commit ff1fd88

Please sign in to comment.