% HW2 Program Assignment
% By:105031247 張廷碩
% P1.
% (a)

n=linspace(0,98,99);
Y(n+1)=(n+1).*0.9.^(n);
stem(n,Y)
xlabel('n')
ylabel('y[n]')

% (b)

n=linspace(0,49,50);
x=0.9.^n;
h=x;
y=conv(x,h,"same");
stem(n,y)

% (c)

n=linspace(0,98,99);
x=0.9.^n;
h=x;
y=filter(x,1,h);
stem(n,y)

% (d)
%
% 2.
% (a)
% (b)

n=0:100;
x=sin(2*pi*0.01*(0:100)) + 0.05*randn(1,101);
h=ones(1,5);
y=conv(x,h,"same");
Y1=sum(y,"all")
X=sum(x,"all");
H=sum(h,"all");
Y2=X*H
figure
subplot(2,1,1),plot(n,x);grid on;
xlabel('n') ; ylabel('x') ; title('n versus x')
subplot(2,1,2),plot(n,y);grid on;
xlabel('n') ; ylabel('y') ; title('n versus y')

% (c)

h=[1/5 1/5 1/5 1/5 1/5];
Ah=sum(h,"all");
n=0:100;
x=sin(2*pi*0.01*(0:100)) + 0.05*randn(1,101);
y=conv(x,h,"same");
Y3=sum(y,"all")
X=sum(x,"all");
Y4=X*Ah
figure
subplot(2,1,1),plot(n,x);grid on;
xlabel('n') ; ylabel('x') ; title('n versus x')
subplot(2,1,2),plot(n,y);grid on;
xlabel('n') ; ylabel('y') ; title('n versus y')

% (d)
%
% 3.
% (a)

clc
b=[-6,6];
a=[-3,1];
[x,n]=delta(0,0,10);
y=filter(b,a,x)
h=impz(b,a,100)

% (b)

figure
zplane(b,a)

% 4.
% (a)



% (b)
%
% 5.
% (a)

syms z A w;
y=iztrans(A*sin(w)/(1-2*cos(w)*z^(-1)+z^(-2)))

% (b)

b=[0 0 0];
a=[1 -2*cos(0.1*pi) 1];
yic=[0 -2*sin(0.1*pi)];
xic=[0 0];
zic = filtic(b, a, yic, xic);
y = filter(b, a, x, zic);
plot(n,y)

%
%
% 9.

img = imread('DSP.png');
%imshow(img);
imwrite(img, 'DSP0.png');

% (a)

p1=ones(5,5)/25;
imgg=double(img);
img2=conv2(imgg,p1,"full");
%imshow(img2);

% (b)

h1=[1 0 -1;2 0 -2;1 0 -1];
h2=[1 2 1;0 0 0;-1 -2 -1];
a=conv2(h1,imgg,"full");
b=conv2(h2,imgg,"full");
A=uint8(a);
B=uint8(abs(b));
%imshow(A);
%imshow(B);

% (c)

c=filter2(h1,imgg,"full");
d=filter2(h2,imgg,"full");
C=uint8(c);
D=uint8(abs(d));
%imshow(C);

%
% 10.

[y,Fs] = audioread('handel.wav');
playerObj = audioplayer(y, Fs);
play(playerObj);
%audiowrite('handel0.wav', y, Fs);

% (a)

Ft=Fs/2;
playerObj = audioplayer(y, Ft);
%play(playerObj);

% (b)

Fu=Ft/2;
%playerObj = audioplayer(y, Fu);

% (c)
%
%