om = linspace(-pi, pi, 1024);
om_len = length(om);
L = 21;
blackm = blackman(L);

blackm_dtft = freqz(blackm, 1, om);

blackm_dtft = blackm_dtft./max(abs(blackm_dtft));

[pks,locs] = findpeaks(20*log10(abs(blackm_dtft)),om);
pks_sort = sort(pks,"descend");
sidelobe = pks_sort(2); 
text_8a = [int2str(sidelobe), ' dB'];

figure; plot(om/pi, 20*log10(abs(blackm_dtft))); grid on;
title('Log-Magnitude Response of DTFT'); ylim([-100 0]);
line([om(1)/pi,om(om_len)/pi],[sidelobe,sidelobe],'linestyle','--', 'color', 'r');
text(-0.2,sidelobe,text_8a, 'Fontsize', 20);
ylabel('Magnitude(dB)');
xlabel('Time(pi)');

fprintf("Peak of the first sidelobes is %f dB (in Magnitude Response)", sidelobe);

%

close all; clear;
fprintf('8(b): L = 21\n');

om = linspace(-pi, pi, 1024);
om_len = length(om);
L = 21; M = L - 1;
blackm = blackman(L);
blackm_dtft = freqz(blackm, 1, om);
blackm_dtft = blackm_dtft./max(abs(blackm_dtft));
[pks,locs] = findpeaks(20*log10(abs(blackm_dtft)),om);
pks_sort = sort(pks,"descend");
sidelobe = pks_sort(2); 
text_8a = [int2str(sidelobe), ' dB'];

% Use Type I linear phase filter find amplitude response
blackm_amp = blackm((length(blackm)+1)/2);
j = 1;
for i = ((length(blackm)-1)/2):-1:1
    blackm_amp = blackm_amp + (2*blackm(i)*cos(om.*j));
    j = j + 1;
end

blackm_cum = cumsum(blackm_amp);
blackm_cum = blackm_cum ./ max(blackm_cum);

[pks,locs] = findpeaks(20*log10(abs(blackm_cum)),om);
sort_cum = sort(pks,"descend")
sidelobe_cum = sort_cum(5); 
text_8b = [int2str(sidelobe_cum), ' dB'];

figure; plot(om/pi, 20*log10(abs(blackm_cum))); grid on;
title('Log-Amplitude Response of DTFT'); ylim([-150 0]);
line([om(1)/pi,om(om_len)/pi],[sidelobe_cum,sidelobe_cum],'linestyle','--', 'color', 'r');
text(-0.2,sidelobe_cum,text_8b, 'Fontsize', 20);
legend('A(e^j^\omega)')
ylabel('Magnitude(dB)');
xlabel('Time(pi)');

fprintf("Peak of the first sidelobes is %f dB (in Magnitude Response)", sidelobe);
fprintf("Peak of the first sidelobes is %f dB (in Amplitude Response)\n", sidelobe_cum);

%

close all; clear;
om = linspace(-pi, pi, 1024);
om_len = length(om);

L = 41;
blackm = blackman(L);
blackm_dtft = freqz(blackm, 1, om);
blackm_dtft = blackm_dtft./max(abs(blackm_dtft)); % nomorlize

% Use Type I linear phase filter find amplitude response
blackm_amp = blackm((length(blackm)+1)/2);
j = 1;
for i = ((length(blackm)-1)/2):-1:1
    blackm_amp = blackm_amp + (2*blackm(i)*cos(om.*j));
    j = j + 1;
end

% Find the accumulated amplitude response
blackm_cum = cumsum(blackm_amp);
blackm_cum = blackm_cum ./ max(blackm_cum); % nomorlize

[pks,locs] = findpeaks(20*log10(abs(blackm_dtft)),om);
sort_dtft = sort(pks,"descend");
sidelobe = sort_dtft(2); 
text_dtft = [int2str(sidelobe), ' dB'];

[pks,locs] = findpeaks(20*log10(abs(blackm_cum)),om);
sort_cum = sort(pks,"descend")
sidelobe_cum = sort_cum(10); 
text_cum = [int2str(sidelobe_cum), ' dB'];

figure; plot(om/pi, 20*log10(abs(blackm_dtft))); grid on;
title('Log-Magnitude Response'); ylim([-130 0]);
line([om(1)/pi,om(om_len)/pi],[sidelobe,sidelobe],'linestyle','--', 'color', 'r');
text(-0.2,sidelobe,text_dtft, 'Fontsize', 20);
ylabel('Magnitude(dB)');
xlabel('Time(pi)');

figure; plot(om/pi, 20*log10(abs(blackm_cum))); grid on;
title('Log-Accumulated Amplitude Response'); ylim([-150 0]);
line([om(1)/pi,om(om_len)/pi],[sidelobe_cum,sidelobe_cum],'linestyle','--', 'color', 'r');
text(0,sidelobe_cum,text_cum, 'Fontsize', 20);
ylabel('Magnitude(dB)');
xlabel('Time(pi)');

fprintf("Peak of the first sidelobes is %f dB (in Magnitude Response)\n", sidelobe);
fprintf("Peak of the first sidelobes is %f dB (in Amplitude Response)\n", sidelobe_cum);