**********************
**********************
*LABORATORY SESSION 8*
**********************
**********************

%linear convolution
%task-a
x = [1 2 2 1]; h = [1 2 3];

m = length(h)-1; l = length(x)-1;
x0 = [x zeros(1,m)]; h0 = [h zeros(1,l)];
X = fft(x0); H = fft(h0);
Y = X.*H;
y = ifft(Y); % y = [1 4 9 11 8 3];
%compare result to conv(x,h)
     
%%%%
x = [1 0 2 -2 1]; h = [1 4 6 4 1];

m = length(h)-1; l = length(x)-1;
x0 = [x zeros(1,m)];h0 = [h zeros(1,l)];
X = fft(x0); H = fft(h0);
Y = X.*H;
y = ifft(Y); % y = [1 4 8 10 6 0 0 2 1];
%compare results to conv(x,h)




%Frequency response of LTI system
%task-a
%using only first 50 points starting from n=0
n = 0:49;
h = [0.6.^(n)+ones(1,50)];

fth = fft(h,100048);
plot(abs(fth))
plot(phase(fth))


%input signal
n = (0:49);
x = 5+1*sin((pi/2)*n)-20*cos(pi*n+pi/4);
plot(x);

ftx = fft(x,2048);

plot(abs(fth).*abs(ftx));
plot(phase(fth)+phase(ftx));

%task-b
%impulse responses of filter for spectral equalizer
Fs = 44100;
h1 = dlmread('h1.txt');
h2 = dlmread('h2.txt');
h3 = dlmread('h3.txt');
h4 = dlmread('h4.txt');
h5 = dlmread('h5.txt');
stem(h1)
stem(h2)
stem(h3)
stem(h4)
stem(h5)


N = 1024;
f = (0:N-1)/N*Fs;
half = (1:N/2); % to plot response only to Fs/2

fth1 = abs(fft(h1,1024));
fth2 = abs(fft(h2,1024));
fth3 = abs(fft(h3,1024));
fth4 = abs(fft(h4,1024));
fth5 = abs(fft(h5,1024));

hold on;
plot(f(half),fth1(half))
plot(f(half),fth2(half))
plot(f(half),fth3(half))
plot(f(half),fth4(half))
plot(f(half),fth5(half))
hold off;
