diff --git a/TP3a/DiogoEliseu_TP3_2.m b/TP3a/DiogoEliseu_TP3_2.m index 93f07bc..e484a25 100644 --- a/TP3a/DiogoEliseu_TP3_2.m +++ b/TP3a/DiogoEliseu_TP3_2.m @@ -14,18 +14,21 @@ EEG = EEG'; Ls = length(sintetico); Ts = 0.125; Fs = 1/Ts; -Fs_vector = (0:Ls/2-1)/(Ls/2) * Fs; +Fs_vector = linspace(0, 2*pi*Fs, Ls)/(pi*Fs); +Fs_vector_pi = Fs_vector(1:(length(Fs_vector)/2)); % sinal cello Lc = length(cello.x); Tc = cello.dt; Fc = 1/Tc; -Fc_vector = (0:Lc/2-1)/(Lc/2) * Fc; +Fc_vector = linspace(0, 2*pi*Fc, Lc)/(pi*Fc); +Fc_vector_pi = Fc_vector(1:(length(Fc_vector)/2)); % sinal EEG Le = length(EEG); Fe = 100; -Fe_vector = (1:Le/2-1)/(Le/2) * Fe/2; +Fe_vector = linspace(0, 2*pi*Fe, Le)/(pi*Fe); +Fe_vector_pi = Fe_vector(1:(length(Fe_vector)/2)); figure(1) @@ -36,7 +39,7 @@ xlabel('t'), ylabel('sintetico[t]'), title('sinal sintetico'); subplot(2,1,2) DFT_synth = fft(sintetico, Ls); DFT_synth_pi = DFT_synth(1 : (Ls)/2); -plot(Fs_vector, abs(DFT_synth_pi)) +plot(Fs_vector_pi, abs(DFT_synth_pi)) xlabel('\omega/\pi'), ylabel('|X(\omega)|'), title('DFT sintetico em [0, \pi]') figure(2) @@ -48,7 +51,7 @@ xlabel('t'), ylabel('cello[t]'), title('sinal cello'); subplot(2,1,2) DFT_cello = fft(cello.x, Lc); DFT_cello_pi = DFT_cello(1 : (Lc)/2); -plot(Fc_vector, abs(DFT_cello_pi)) +plot(Fc_vector_pi, abs(DFT_cello_pi)) xlabel('\omega/\pi'), ylabel('|X(\omega)|'), title('DFT cello em [0, \pi]') figure(3) @@ -58,9 +61,9 @@ plot(EEG); xlabel('t'), ylabel('eeg[t]'), title('sinal EEG'); subplot(2,1,2) -DFT_EEG = fft(EEG, Le-1); -DFT_EEG_pi = DFT_EEG(1 : (Le-1)/2); -plot(Fe_vector, abs(DFT_EEG_pi)) +DFT_EEG = fft(EEG, Le); +DFT_EEG_pi = DFT_EEG(1 : (Le)/2); +plot(Fe_vector_pi, abs(DFT_EEG_pi)) xlabel('\omega/\pi'), ylabel('|X(\omega)|'), title('DFT EEG em [0, \pi]') diff --git a/TP3b/Canto1.mp3 b/TP3b/Canto1.mp3 new file mode 100644 index 0000000..c7bfeb9 Binary files /dev/null and b/TP3b/Canto1.mp3 differ diff --git a/TP3b/DiogoEliseu_TP3_5.m b/TP3b/DiogoEliseu_TP3_5.m new file mode 100644 index 0000000..4b26b80 --- /dev/null +++ b/TP3b/DiogoEliseu_TP3_5.m @@ -0,0 +1,49 @@ +%% Inicialização do ambiente +clear; close all; clc + +%% Exercício 5 +load("DiogoEliseu_TP3_5filt.mat"); + +figure(1) +plotinhos(passa_baixa_SOS, passa_baixa_G) +sgtitle('Low-pass') + +figure(2) +plotinhos(passa_alta_SOS, passa_alta_G) +sgtitle('High-pass') + +figure(3) +plotinhos(passa_banda_SOS, passa_banda_G) +sgtitle('Band-pass') + +figure(4) +plotinhos(corta_banda_SOS, corta_banda_G) +sgtitle('Band-Cut') + +function plotinhos(SOS, G) +[num, den] = sos2tf(SOS, G); +[z, p, ~] = tf2zp(num, den); +[h, w] = freqz(num, den); +w_pi = w/pi; +gain = 20.*log10(abs(h)); + +subplot(2,2,1) +% Módulo +plot(w_pi, gain); +ylabel("Gain (dB)"); xlabel("\omega/\pi"); title('Filter Gain'); + +subplot(2,2,2) +% Fase +%freqz(p,1); +plot(w_pi, unwrap(angle(h))); +ylabel("Phase (degrees)"); xlabel("Normalized Frequency (x\pi rad/sample)"); title('Phase'); + +subplot(2,2,3) +% Resposta Impulsional +impulseplot(tf(num, den)); + +subplot(2,2,4) +% Zeros e Polos +zplane(z, p); +title("Pole-Zero plot"); +end \ No newline at end of file diff --git a/TP3b/DiogoEliseu_TP3_5filt.mat b/TP3b/DiogoEliseu_TP3_5filt.mat new file mode 100644 index 0000000..7232949 Binary files /dev/null and b/TP3b/DiogoEliseu_TP3_5filt.mat differ diff --git a/TP3b/DiogoEliseu_TP3_6.m b/TP3b/DiogoEliseu_TP3_6.m new file mode 100644 index 0000000..5ac017f --- /dev/null +++ b/TP3b/DiogoEliseu_TP3_6.m @@ -0,0 +1,65 @@ +%% Inicialização do ambiente +clear ; close all; clc + +%% Exercício 6 +sintetico_data = fopen('sintetico.csv'); +sintetico = textscan(sintetico_data, '%f', 'Delimiter', ','); +sintetico = sintetico{1,1}; +fclose(sintetico_data); + +fn = [0.91, 0.85, 0.91, 0.95]; + +xf = DiogoEliseu_TP3_6f(sintetico, fn(1), 0); +for i=2:(length(fn)-1) + %figure(i) + xf = DiogoEliseu_TP3_6f(xf, fn(i), 0); +end +figure(1) +DiogoEliseu_TP3_6f(xf, fn(length(fn)), 1); + + +function filtrado = DiogoEliseu_TP3_6f(xo,fn,graf) + Ls = length(xo); + d = fdesign.notch('N,F0,Q', 8, fn, 10); + Hd = design(d); + SOS = Hd.SOS; + G = Hd.ScaleValues; + [num, den] = sos2tf(SOS, G); + w_2pi = linspace(0, 2*pi, length(xo))/pi; + w_pi = w_2pi(1:(length(w_2pi)/2)); + h = freqz(num, den, w_2pi); + gain = 20.*log10(abs(h)); + [z, p, ~] = tf2zp(num, den); + filtrado = filter(num, den, xo); + if (graf == 1) + subplot(2,3,1) + plot(xo) + xlabel('t'), ylabel('xo[t]'), title('sinal xo'); + + subplot(2,3,2) + plot(filtrado) + xlabel('t'), ylabel('filtrado[t]'), title('sinal filtrado'); + + subplot(2,3,3) + DFT_xo = fft(xo); + DFT_xo_pi = DFT_xo(1 : (Ls)/2); + plot(w_pi, abs(DFT_xo_pi)); + xlabel('\omega/\pi'), ylabel('|X(\omega)|'), title('DFT xo em [0, \pi]') + + subplot(2,3,4) + DFT_filtrado = fft(filtrado); + DFT_filtrado_pi = DFT_filtrado(1 : (Ls)/2); + + plot(w_pi, abs(DFT_filtrado_pi)); + xlabel('\omega/\pi'), ylabel('|X(\omega)|'), title('DFT xo filtrado em [0, \pi]') + + subplot(2,3,5) + plot(w_2pi, gain); + ylabel("Ganho (dB)"); xlabel("\omega/2\pi"); title('Ganho do filtro'); + + subplot(2,3,6) + zplane(z, p) + title("Diagrama de Zeros e Polos"); + end +end + diff --git a/TP3b/DiogoEliseu_TP3_7.m b/TP3b/DiogoEliseu_TP3_7.m new file mode 100644 index 0000000..30077c2 --- /dev/null +++ b/TP3b/DiogoEliseu_TP3_7.m @@ -0,0 +1,67 @@ +%% Inicialização do ambiente +clear; close all; clc + +%% Exercício 7 +[y, Fs] = audioread("Canto1.mp3"); +s = y(:,1); +%Ts = 1/Fs; + +% S: short-time Fourier transform of the input signal +% F: vector of cyclical frequencies +% T: vector of time instants +% P: power spectral density (PSD) +[S, F, T, P] = spectrogram(s, 128, 96, 128, Fs); + +figure(1) +% Signal +subplot(2,3,1) +surf(T, F, 10*log10(P), 'edgecolor', 'none') +axis tight, view(0,90) +title("Original Signal (X)"); ylabel("Frequency (Hz)"); xlabel("Time (s)"); + +% Noise +subplot(2,3,2) +%T_noise = T(1377:4134); +s_noise = s((Fs * (1 - 1)) + 1 : Fs * (3 - 1)); % [1-3] +[S_noise, F_noise, T_noise, P_noise] = spectrogram(s_noise, 128, 96, 128, Fs); +surf(T_noise, F_noise, 10*log10(P_noise), 'edgecolor', 'none') +axis tight, view(0,90) +title("Background Noise (Xn) in 1-3s"); ylabel("Frequency (Hz)"); xlabel("Time (s)"); + +% Song +subplot(2,3,3) +s_chirp = s((Fs * (4 - 1)) + 1 : Fs * (6 - 1)); % [4-6] +[S_chirp, F_chirp, T_chirp, P_chirp] = spectrogram(s_chirp, 128, 96, 128, Fs); +surf(T_chirp, F_chirp, 10*log10(P_chirp), 'edgecolor', 'none') +axis tight, view(0,90) +title("Chirp (Xs) in 4-6s"); ylabel("Frequency (Hz)"); xlabel("Time (s)"); + +% DFT signal +subplot(2,3,4) +L_s = length(s); +f_s_2pi = linspace(0, Fs, L_s)/1000; % to kHz +f_s_half = f_s_2pi(1:(length(f_s_2pi)/2)); +DFT_s = fft(s); +DFT_s_half = DFT_s(1 : (L_s/2)); +plot(f_s_half, abs(DFT_s_half)); +title("X in function of the frequency (kHz)"); ylabel("Amplitude (Hz)"); xlabel("Frequency (kHz)"); + +% DFT noise +subplot(2,3,5) +L_s_noise = length(s_noise); +f_s_noise_2pi = linspace(0, Fs, L_s_noise)/1000; % to kHz +f_s_noise_half = f_s_noise_2pi(1:(length(f_s_noise_2pi)/2)); +DFT_s = fft(s_noise); +DFT_s_noise_half = DFT_s(1 : (L_s_noise/2)); +plot(f_s_noise_half, abs(DFT_s_noise_half)); +title("Xn in function of the frequency (kHz)"); ylabel("Amplitude (Hz)"); xlabel("Frequency (kHz)"); + +% DFT chirp +subplot(2,3,6) +L_s_chirp = length(s_chirp); +f_s_chirp_2pi = linspace(0, Fs, L_s_chirp)/1000; % to kHz +f_s_chirp_half = f_s_chirp_2pi(1:(length(f_s_chirp_2pi)/2)); +DFT_s = fft(s_chirp); +DFT_s_chirp_half = DFT_s(1 : (L_s_chirp/2)); +plot(f_s_chirp_half, abs(DFT_s_chirp_half)); +title("Xs in function of the frequency (kHz)"); ylabel("Amplitude (Hz)"); xlabel("Frequency (kHz)"); \ No newline at end of file diff --git a/TP3b/sintetico.csv b/TP3b/sintetico.csv new file mode 100644 index 0000000..281d5bd --- /dev/null +++ b/TP3b/sintetico.csv @@ -0,0 +1 @@ +1,-0.67391,1.1587,0.3128,0.93612,1.2837,0.90455,1.7785,1.3046,1.8455,1.852,1.9125,2.1352,2.2587,2.0989,2.7274,2.0553,2.978,2.2675,2.918,2.6486,2.7807,2.9186,2.8027,2.9677,2.9523,2.9453,3.0443,2.9973,3.0398,3.0493,3.0945,2.9478,3.2825,2.76,3.3982,2.78,3.1627,3.1682,2.6183,3.6665,2.1883,3.8052,2.2527,3.418,2.7086,2.8574,3.0586,2.6196,2.9617,2.7891,2.6157,2.9553,2.5121,2.7074,2.8485,2.1298,3.2838,1.7182,3.3201,1.825,2.8541,2.283,2.2651,2.622,1.9757,2.5832,2.0273,2.3258,2.14,2.143,2.1069,2.1005,2.001,2.0436,1.9762,1.8962,1.9922,1.8025,1.8617,1.9099,1.551,2.1063,1.2945,2.1095,1.323,1.8299,1.5411,1.5277,1.6054,1.5121,1.3558,1.7244,1.0681,1.7491,1.166,1.298,1.6937,0.60739,2.1863,0.24468,2.1365,0.50781,1.5443,1.0989,0.91705,1.4504,0.71957,1.3261,0.90774,1.0143,1.0578,0.93038,0.89404,1.1331,0.57644,1.3133,0.44862,1.2107,0.60587,0.9152,0.81754,0.70896,0.84929,0.70402,0.73493,0.75323,0.66173,0.71245,0.67024,0.65786,0.60662,0.74438,0.39813,0.90648,0.25176,0.85117,0.44768,0.41943,0.94474,-0.12966,1.3207,-0.3255,1.1964,-0.00067769,0.67569,0.49292,0.25683,0.65994,0.28125,0.41659,0.55213,0.17407,0.57822,0.34358,0.1627,0.84136,-0.35305,1.172,-0.47064,0.9837,-0.086321,0.4438,0.42079,0.022946,0.62844,-0.019783,0.4955,0.1566,0.30214,0.25911,0.25538,0.22224,0.28247,0.2009,0.22958,0.2713,0.13727,0.28929,0.19285,0.10775,0.4301,-0.16131,0.61471,-0.2356,0.51604,-0.025333,0.22939,0.2092,0.098326,0.15893,0.28298,-0.11741,0.50752,-0.20443,0.34821,0.18615,-0.23908,0.81811,-0.78944,1.1239,-0.79564,0.80571,-0.25768,0.16132,0.31715,-0.24173,0.46395,-0.16727,0.2268,0.096406,0.029416,0.13387,0.1476,-0.11362,0.4212,-0.33705,0.50661,-0.27613,0.30851,-0.020671,0.057516,0.14698,-0.012093,0.1148,0.065862,0.029884,0.10406,0.036342,0.061896,0.057948,0.093187,-0.060251,0.27064,-0.25277,0.38721,-0.22665,0.16269,0.17708,-0.36181,0.6993,-0.76408,0.86938,-0.66503,0.51987,-0.16845,0.0026036,0.22725,-0.18708,0.18064,0.034568,-0.11148,0.2563,-0.1579,0.066897,0.23816