% Louis Rosenblum
% EELE 477
% 4/25/2019
% Lab 15

%% Section 3-1
b = [1 -1];
a = [1];
h1 = freqz(b,a,512)

[y1,fs] = audioread(
"catsdogs22k.wav");

figure();
spectrogram(y1),title(
"Before")

y2 = filter(b,a,y1);

figure();
spectrogram(y2),title(
"After")

%% Section 3-2
w1 = 2*pi *215;
w2 = 2*pi * 1064;
tt = 0:1/8000:1.3;
b1 = (1.2 + cos(w1.*tt)).*cos(w2.*tt);

plot(tt,b1)

figure();

filterdesign
spectrogram(b1,1024)

y2 = filter(num,den,b1)

figure();
plot(tt,y2)

figure();

spectrogram(y2)

%%

figure();

plot(tt,abs(y2))

%% Section 4-1


% Input audio file
[x1,fs1] = audioread(
"catsdogs22k.wav");

soundsc(x1,fs1)

stem(x1),title(
"Audio Input"),xlabel("Samples"),ylabel("Amplitude")

% Pre-emphasis filter
pre = filter([1 -1],[1],x1);

% 40 point bandpass filter n vector
n = 0:1:39;

% Center frequencies for 8 bandpass filters
w_vect = [1 2 3 4 5 6 7 8] .*pi/8;

% Filter creation
h1 = 2/40 *cos(w_vect(1) .*n);
h2 = 2/40 *cos(w_vect(2) .*n);
h3 = 2/40 *cos(w_vect(3) .*n);
h4 = 2/40 *cos(w_vect(4) .*n);
h5 = 2/40 *cos(w_vect(5) .*n);
h6 = 2/40 *cos(w_vect(6) .*n);
h7 = 2/40 *cos(w_vect(7) .*n);
h8 = 2/40 *cos(w_vect(8) .*n);

% Convolution of pre-emphasis output with bandpass filters
y1 = conv(pre,h1);
y2 = conv(pre,h2);
y3 = conv(pre,h3);
y4 = conv(pre,h4);
y5 = conv(pre,h5);
y6 = conv(pre,h6);
y7 = conv(pre,h7);
y8 = conv(pre,h8);

% Coefficients for 40 pointlowpass filter
b1 = ones(1,40);
b2 = b1 ./40;

% Lowpass filter on bandpass filter output
lpf1 = filter(b2,[1],y1);
lpf2 = filter(b2,[1],y2);
lpf3 = filter(b2,[1],y3);
lpf4 = filter(b2,[1],y4);
lpf5 = filter(b2,[1],y5);
lpf6 = filter(b2,[1],y6);
lpf7 = filter(b2,[1],y7);
lpf8 = filter(b2,[1],y8);


length(lpf1)
t = 0:1/(length(lpf1)-1):1;

lpf1 = rot90(lpf1);
lpf2 = rot90(lpf2);
lpf3 = rot90(lpf3);
lpf4 = rot90(lpf4);
lpf5 = rot90(lpf5);
lpf6 = rot90(lpf6);
lpf7 = rot90(lpf7);
lpf8 = rot90(lpf8);


% Amplitude modulation
w1 = lpf1 .*cos(2*pi*fs1/8.*t+fs1/8);
w2 = lpf2 .*cos(2*pi*2*fs1/8.*t+(2*fs1/8));
w3 = lpf3 .*cos(2*pi*3*fs1/8.*t+(3*fs1/8));
w4 = lpf4 .*cos(2*pi*4*fs1/8.*t+(4*fs1/8));
w5 = lpf5 .*cos(2*pi*5*fs1/8.*t+(5*fs1/8));
w6 = lpf6 .*cos(2*pi*6*fs1/8.*t+(6*fs1/8));
w7 = lpf7 .*cos(2*pi*7*fs1/8.*t+(7*fs1/8));
w8 = lpf8 .*cos(2*pi*8*fs1/8.*t+(8*fs1/8));

% Output audio file
y10 = w1 + w2 + w3 + w4 + w5 + w6 + w7 +w8;

stem(y10),title(
"Audio Output"),xlabel("Samples"),ylabel("Amplitude")

audiowrite(
"output.wav",y10,fs1)