Skip to content

Commit cb10dfb

Browse files
filter signal in time domain
1 parent 63a004c commit cb10dfb

File tree

21 files changed

+241
-0
lines changed

21 files changed

+241
-0
lines changed

DSP-HW1/DSP_HW1.m

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
clear all
2+
close all
3+
clc
4+
a=0.95;
5+
teta=pi/4;
6+
h=[1,-2*a*cos(teta),a^2];
7+
Ak1=[1,0,0];
8+
N=3;
9+
10+
x=audioread("C:\Users\Desktop\a2.wav");
11+
x=x';
12+
u=zeros(1,10);
13+
u(1)=1;
14+
[Bk2,r]=deconv(u,h);
15+
16+
z1=output(x,Ak1,h,N,size(x,2));
17+
y1=output(z1,Ak1,Bk2,N,size(z1,2));
18+
e1=y1-x;
19+
20+
sound(y1,1e4)
21+
22+
plot(x,'r');
23+
hold on
24+
plot(y1,'b');
25+
plot(e1,'g');
26+
27+
%////////////////////////////////////////////
28+
29+
z2=output(x,Ak1,Bk2,N,size(x,2));
30+
y2=output(z2,Ak1,h,N,size(z2,2));
31+
e2=y2-x;
32+
33+
figure
34+
plot(x,'r');
35+
hold on
36+
plot(y2,'b');
37+
plot(e2,'g');
38+
39+
%///////////////////////////////////////////
40+
function y = output(x,Ak,Bk,N,M)
41+
y=zeros(1,M);
42+
for n=1:1:M
43+
sum=0;
44+
for m=0:1:M-1
45+
if((n-m)<=0 || (n-m)>size(x,2))
46+
continue;
47+
elseif((m+1)>size(Bk,2))
48+
break;
49+
else
50+
sum=sum+Bk(m+1)*x(n-m);
51+
end
52+
end
53+
for i=1:1:N-1
54+
if((n-i)<=0 || (i+1)>size(Ak,2))
55+
continue;
56+
else
57+
sum=sum-Ak(i+1)*y(n-i);
58+
end
59+
end
60+
y(n)=sum/Ak(1);
61+
end
62+
end
63+

DSP-HW1/DSP_HW1_Report.pdf

494 KB
Binary file not shown.

DSP-HW1/HW1_2.m

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
clc;
2+
clear;
3+
close all;
4+
5+
b = [2,3,1,7 ];
6+
a = [1,0.2 ];
7+
8+
x = randn(1000,1 );
9+
y = filter(b,a,x );
10+
y1 = filterbax(b,a,x );
11+
12+
subplot(3,1,1);
13+
plot(y,'b');
14+
title('input signal');
15+
16+
subplot(3,1,2);
17+
plot(y,'r');
18+
title('with filter function()');
19+
20+
subplot(3,1,3);
21+
plot(y1,'g');
22+
title('without filter function()');
23+
24+
25+
26+
function y = filterbax(b,a,x )
27+
% The function expects inputs to be column vectors
28+
29+
M = length(b );
30+
N = length(a );
31+
xbuffer = [];
32+
ybuffer = [];
33+
34+
for i = 1:1:length(x)
35+
xbuffer = [x(i)];
36+
if length(xbuffer) < M
37+
index = length(xbuffer );
38+
else
39+
index = M ;
40+
end
41+
42+
if length(ybuffer) < N-1
43+
yindex = length(ybuffer);
44+
else
45+
yindex = N-1 ;
46+
end
47+
48+
y(i) = (1/a(1)).*(sum(b(1:index).*xbuffer(1:index)) - sum(a(2:yindex+1).*ybuffer(1:yindex)));
49+
ybuffer = [y(i);ybuffer ];
50+
end
51+
52+
y = y(:); % Return as column vector
53+
end
54+
55+
56+
57+
58+
59+

DSP-HW1/T1/Sigma.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function output = Sigma(input1,input2,input3)
2+
%intializing coefficients and input data
3+
a = input1;
4+
h = input2;
5+
N = input3;
6+
sum = 0;
7+
%Processing Sigma
8+
if(N>1)
9+
for k=2:(N);
10+
if(k<=length(a))
11+
sum = sum + (a(k)*h(N-k+1));
12+
end
13+
14+
end
15+
else
16+
sum=0;
17+
end
18+
output=sum;
19+
end
20+

DSP-HW1/T1/Untitled.m

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
%initalizing MATLAB
2+
clear all
3+
close all
4+
clc
5+
%intializing Coefficients and Input Datas
6+
[x Fs]=audioread('abc.wav');
7+
x=x';
8+
alpha = 1.1;
9+
%************************
10+
filter = 0 ;%if filter = 1 => filter in MATLAB , if filter = 0 => myfilter
11+
system = 1 ;%if system = 0 => h(z)=(1-alpha*e^j(tetta)*z^-1)*(1-alpha*e^-j(tetta)*z^-1)
12+
theta=pi/4 ;%if system = 1 => h(z)=1/((1-alpha*e^j(tetta)*z^-1)*(1-alpha*e^-j(tetta)*z^-1))
13+
%************************
14+
a = [1];
15+
b = [1 -2*(alpha)*cos(theta) (alpha^2)];
16+
%Processing 1
17+
if(filter == 0 )
18+
if(system == 0)
19+
z=myfilter(a,b,x);
20+
y=myfilter(b,a,z);
21+
else
22+
z=myfilter(b,a,x);
23+
y=myfilter(a,b,z);
24+
end
25+
else
26+
if(system == 0)
27+
z=filter(b,a,x);
28+
y=filter(a,b,z);
29+
else
30+
z=filter(a,b,x);
31+
y=filter(b,a,z);
32+
end
33+
end
34+
35+
%sound(y,Fs);
36+
%plotting
37+
string_title1="\alpha = "+alpha;
38+
string_title2="\theta = "+theta;
39+
if(system == 0)
40+
string_title3="H(z)=(1-\alphae^{+j\theta}z^{-1}).(1-\alphae^{-j\theta}z^{-1})";
41+
else
42+
string_title3="H(z)=1/(1-\alphae^{+j\theta}z^{-1}).(1-\alphae^{-j\theta}z^{-1})";
43+
end
44+
if(filter == 1 )
45+
string_title4 = "filter MATLAB";
46+
else
47+
string_title4 = "myfilter";
48+
end;
49+
50+
suptitle({'','',char(string_title1),char(string_title2),char(string_title3),char(string_title4)})
51+
subplot(3,2,3);
52+
n = 5000:14999;
53+
plot(n,x(5000:14999));
54+
title('x[n]')
55+
56+
subplot(3,2,4);
57+
plot(n,z(5000:14999));
58+
title('z[n]')
59+
60+
subplot(3,2,5);
61+
plot(n,y(5000:14999));
62+
title('y[n]')
63+
64+
subplot(3,2,6);
65+
e=y-x;
66+
plot(n,e(5000:14999));
67+
title('e[n]')
68+
69+
70+
71+
% ynew = filter(b,a,x);
72+
73+
74+
75+
76+
77+
78+

DSP-HW1/T1/abc.wav

324 KB
Binary file not shown.

DSP-HW1/T1/myfilter.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function output = myfilter(input1,input2,input3)
2+
%intializing coefficients and input data
3+
a = input1;
4+
b = input2;
5+
x = input3;
6+
size_h=12000;
7+
h=zeros(1,size_h);
8+
%processing Filter
9+
for i=1:size_h
10+
if(i<=length(b))
11+
h(i) = (b(i) - Sigma(a,h,i))/a(1);
12+
else
13+
h(i) = - (Sigma(a,h,i)/a(1));
14+
end
15+
end
16+
y = conv(h,x);
17+
ynew=y(1:length(x));
18+
%end process
19+
output = ynew;
20+
end
21+
689 KB
Binary file not shown.
689 KB
Binary file not shown.
689 KB
Binary file not shown.

0 commit comments

Comments
 (0)