sem2 done

This commit is contained in:
Priec
2025-12-07 00:10:47 +01:00
parent d94c92eead
commit 262a0f2bbe
7 changed files with 1453 additions and 0 deletions

207
semestralka2/spd_sumu.m Normal file
View File

@@ -0,0 +1,207 @@
clc;
close all;
clear;
% === Global figure styling (light mode defaults) ===
set(0, 'DefaultFigureColor', 'w'); % white figure background
set(0, 'DefaultAxesColor', 'w'); % white plotting area
set(0, 'DefaultAxesXColor', 'k'); % black x-axis lines and text
set(0, 'DefaultAxesYColor', 'k'); % black y-axis lines and text
set(0, 'DefaultTextColor', 'k'); % black text for titles, labels
set(0, 'DefaultLineColor', 'b'); % blue lines by default
clear saveFigure
% === Automatic figure export counter ===
fig_counter = 1;
save_fig = @saveFigure;
function saveFigure(name)
persistent counter
if isempty(counter)
counter = 1;
end
% Force painters renderer and white backgrounds
set(gcf, 'Renderer', 'painters');
set(gcf, 'Color', [1 1 1]);
axesHandles = findall(gcf, 'Type', 'axes');
for ax = axesHandles'
set(ax, 'Color', [1 1 1]);
end
% Export clean EPS with no transparency problems
print(gcf, '-depsc2', '-r300', '-painters', sprintf('%d_%s.eps', counter, name));
counter = counter + 1;
end
[y1, Fs1] = audioread('flac.wav');
[y2, Fs2] = audioread('flac2.wav');
% Časové priebehy
t1 = (0:length(y1)-1)/Fs1;
t2 = (0:length(y2)-1)/Fs2;
figure;
subplot(2,1,1);
plot(t1, y1);
xlabel('Čas [s]');
ylabel('Amplitúda');
title('flac.wav');
grid on;
subplot(2,1,2);
plot(t2, y2, 'r');
xlabel('Čas [s]');
ylabel('Amplitúda');
title('flac2.wav');
grid on;
save_fig('time'); fig_counter = fig_counter + 1;
% Fourierova transformácia
N1 = length(y1);
N2 = length(y2);
X1 = fft(y1);
X2 = fft(y2);
freq_shift1 = (-N1/2 : N1/2 - 1) * (Fs1 / N1);
freq_shift2 = (-N2/2 : N2/2 - 1) * (Fs2 / N2);
% Real a imag časť spektra
figure;
subplot(2,1,1);
plot(freq_shift2, real(fftshift(X2)));
xlabel('Frekvencia [Hz]');
ylabel('Amplitúda');
title('flac2.wav Reálna časť spektra');
xlim([-550 550]);
grid on;
subplot(2,1,2);
plot(freq_shift2, imag(fftshift(X2)), 'r');
xlabel('Frekvencia [Hz]');
ylabel('Amplitúda');
title('flac2.wav Imaginárna časť spektra');
xlim([-550 550]);
grid on;
save_fig('spectrum'); fig_counter = fig_counter + 1;
% Real a imag časť spektra
figure;
subplot(2,1,1);
plot(freq_shift1, real(fftshift(X1)));
xlabel('Frekvencia [Hz]');
ylabel('Amplitúda');
title('flac.wav Reálna časť spektra');
xlim([-3500 3500]);
grid on;
subplot(2,1,2);
plot(freq_shift1, imag(fftshift(X1)), 'r');
xlabel('Frekvencia [Hz]');
ylabel('Amplitúda');
title('flac.wav Imaginárna časť spektra');
xlim([-3500 3500]);
grid on;
save_fig('spectrum2'); fig_counter = fig_counter + 1;
% Určenie typu šumu na základe spektrálneho sklonu
[PSD, f] = pwelch(y1, hamming(4096), [], [], Fs1, 'onesided'); % PSD
mask = f > 10 & f < Fs1/2;
logf = log10(f(mask));
logP = log10(PSD(mask));
p = polyfit(logf, logP, 1);
slope = p(1);
intercept = p(2);
fprintf('Spektrálny sklon (alfa) = %.2f\n', slope);
% Vizualizácia
figure;
loglog(f, PSD); hold on;
loglog(f(mask), 10.^(polyval(p, logf)), 'r', 'LineWidth', 1.5);
xlabel('Frekvencia [Hz]');
ylabel('Výkonová spektrálna hustota (PSD)');
title(sprintf('Spektrálny sklon = %.2f', slope));
lgd = legend('Meraná PSD', 'Lineárna aproximácia (log-log)', 'Location', 'best');
set(lgd, 'Color', 'w', 'TextColor', 'k');
grid on;
save_fig('spectral_slope'); fig_counter = fig_counter + 1;
% === IIR Notch filtering flac2.wav and noise type analysis ===
fprintf('\n=== IIR Notch Filtering of flac2.wav ===\n');
% Detekcia dominantných frekvencií v flac2.wav pre notch filtráciu
halfN = floor(N2 / 2);
freq_pos = (0:halfN - 1) * (Fs2 / N2);
magX2 = abs(X2(1:halfN));
[pks2, locs2] = findpeaks(magX2, 'NPeaks', 3, 'SortStr', 'descend');
f_notch = sort(freq_pos(locs2));
fprintf('\nDetekované dominantné frekvencie vo flac2.wav:\n');
fprintf(' %.1f Hz\n', f_notch);
% === Konštrukcia a aplikácia IIR notch filtra ===
r = 0.98; % ostrosť zárezu (0.98 až 0.995)
Fs = Fs2;
sos_notch = zeros(length(f_notch), 6);
for k = 1:length(f_notch)
theta = 2 * pi * (f_notch(k) / Fs);
b = [1, -2*cos(theta), 1];
a = [1, -2*r*cos(theta), r^2];
sos_notch(k, :) = [b, a];
end
% Aplikácia filtra na flac2.wav
y2_notched = sosfilt(sos_notch, double(y2));
% Uloženie výsledného signálu
audiowrite('flac2_notched.wav', y2_notched / max(abs(y2_notched)), Fs2);
% Zobrazenie pred/po filtrácii
figure;
subplot(2,1,1);
plot(y2);
xlabel('Vzorky'); ylabel('Amplitúda');
title('Pôvodný signál flac2.wav');
grid on;
subplot(2,1,2);
plot(y2_notched, 'r');
xlabel('Vzorky'); ylabel('Amplitúda');
title('Filtrovaný signál flac2.wav (IIR Notch)');
grid on;
save_fig('flac2_notched'); fig_counter = fig_counter + 1;
% === Analýza zvyškového šumu po IIR filtri ===
[PSD_res, f_res] = pwelch(y2_notched, hamming(4096), [], [], Fs2, 'onesided');
mask_res = f_res > 10 & f_res < Fs2/2;
logf_res = log10(f_res(mask_res));
logP_res = log10(PSD_res(mask_res));
p_res = polyfit(logf_res, logP_res, 1);
slope_res = p_res(1);
fprintf('\nSpektrálny sklon po IIR filtrácii (alfa) = %.2f\n', slope_res);
% Vizualizácia
figure;
loglog(f_res, PSD_res); hold on;
loglog(f_res(mask_res), 10.^(polyval(p_res, logf_res)), 'r', 'LineWidth', 1.5);
xlabel('Frekvencia [Hz]');
ylabel('Výkonová spektrálna hustota (PSD)');
title(sprintf('Zvyškový šum po IIR notch filtrácii (α = %.2f)', slope_res));
legend('PSD po IIR filtri', 'Lineárna aproximácia', 'Location', 'best');
grid on;
save_fig('residual_noise_flac2'); fig_counter = fig_counter + 1;
% Interpretácia typu šumu podľa alfa
if slope_res > -0.5
noise_type = 'biely šum';
elseif slope_res > -1.5
noise_type = 'ružový šum';
else
noise_type = 'hnedý šum';
end
fprintf('Typ zostávajúceho šumu po notch filtrácii: %s\n', noise_type);