TP2 delivered.

This commit is contained in:
Diogo Cordeiro 2020-11-17 23:57:44 +00:00
parent d92f141b72
commit 3b119ac75c
6 changed files with 375 additions and 25 deletions

View File

@ -1,11 +1,24 @@
%% Inicialização do ambiente
clear ; close all; clc
%% Exercício 1 %% Exercício 1
figure(1)
p = [1.8, 0.5, -0.3]; p = [1.8, 0.5, -0.3];
d = [1, 0.3, -0.2];
w = -4*pi:(8*pi/999):4*pi;
freqz(p, d, w)
figure(2)
p = [1];
d = [1, 0.5]; d = [1, 0.5];
w = -4*pi:(8*pi/999):4*pi; w = -4*pi:(8*pi/999):4*pi;
X = freqz(p, d, w); freqz(p, d, w)
subplot(2,1,1),plot(w, abs(X)) figure(3)
xlabel('w'),title('Magnitude') p = [1 2 3 4 1 3];
subplot(2,1,2),plot(w, angle(X)) w = -4*pi:(8*pi/999):4*pi;
xlabel('w'),title('Phase') %[cena, coisa] = freqz(p, 1, w);
%plot(unwrap(angle(cena)));
%figure(4)
freqz(p, 1, w);

View File

@ -13,12 +13,14 @@ DTFT2 = fft(x2, 1024);
subplot(2,1,1) subplot(2,1,1)
stem(x2) stem(x2)
xlabel('n'), ylabel('Amplitude'), title('Sinal x2')
subplot(2,1,2) subplot(2,1,2)
hold on hold on
stem(X(16),abs(DFT2)) stem(X(16), abs(DFT2), "LineStyle", "none")
plot(X(1024), abs(DTFT2)) plot(X(1024), abs(DTFT2))
xlabel('\omega/2\pi'), ylabel('Modulo'), title('DFT / DTFT de x2')
hold off hold off
xlabel('cenas'), ylabel('Modulo'), title('DFT / DTFT de x2')
function f = X(N) function f = X(N)
f = 0:(1/N):(1 - 1/N); f = 0:(1/N):(1 - 1/N);

View File

@ -1,24 +1,27 @@
% Exercício 3 %% Inicialização do ambiente
clear ; close all; clc
%% Exercício 3
% signals % signals
N = 256; N = 256;
n = (0:N-1); n = (0:N-1);
X1 = 2*N*0.97.^n; X1 = 2.*n.*0.97.^n;
X2 = cos(6*pi*n/N); X2 = cos(6*pi.*n./N);
X3 = sin(12*pi*n/N); X3 = sin(12*pi.*n./N);
% freq conv
X1_fft = fft(X1);
X2_fft = fft(X2);
X3_fft = fft(X3);
X1_X2_f = ifft(X1_fft.*X2_fft);
X1_X3_f = ifft(X1_fft.*X3_fft);
X2_X3_f = ifft(X2_fft.*X3_fft);
% time conv % time conv
X1_X2_t = conv(X1, X2); X1_X2_t = conv(X1, X2);
X1_X3_t = conv(X1, X3); X1_X3_t = conv(X1, X3);
X2_X3_t = conv(X2, X3); X2_X3_t = conv(X2, X3);
% freq conv
X1_fft = fft([X1 zeros(1,N-1)]);
X2_fft = fft([X2 zeros(1,N-1)]);
X3_fft = fft([X3 zeros(1,N-1)]);
X1_X2_f = ifft(X1_fft.*X2_fft);
X1_X3_f = ifft(X1_fft.*X3_fft);
X2_X3_f = ifft(X2_fft.*X3_fft);
%--- plots %--- plots
% signals % signals
@ -38,36 +41,38 @@ plot(X3);
xlabel("n") xlabel("n")
ylabel("X3") ylabel("X3")
x_axis = -(N-1):(N-1);
% time % time
subplot(3,3,4) subplot(3,3,4)
plot(X1_X2_t); plot(x_axis, X1_X2_t);
%title("X1 * X2 no domínio do tempo") %title("X1 * X2 no domínio do tempo")
xlabel("n") xlabel("n")
ylabel("X1(t) * X2(t)") ylabel("X1(t) * X2(t)")
subplot(3,3,5) subplot(3,3,5)
plot(X1_X3_t); plot(x_axis, X1_X3_t);
title("*(t)") title("*(t)")
xlabel("n") xlabel("n")
ylabel("X1(t) * X3(t)") ylabel("X1(t) * X3(t)")
subplot(3,3,6) subplot(3,3,6)
plot(X2_X3_t); plot(x_axis, X2_X3_t);
%title("X2 * X3 no domínio do tempo") %title("X2 * X3 no domínio do tempo")
xlabel("n") xlabel("n")
ylabel("X2(t) * X3(t)") ylabel("X2(t) * X3(t)")
% freq % freq
subplot(3,3,7) subplot(3,3,7)
plot(X1_X2_f); plot(x_axis, X1_X2_f);
%title("X1 * X2 no domínio da freq") %title("X1 * X2 no domínio da freq")
xlabel("n") xlabel("n")
ylabel("X1(\omega) * X2(\omega)") ylabel("X1(\omega) * X2(\omega)")
subplot(3,3,8) subplot(3,3,8)
plot(X1_X3_f); plot(x_axis, X1_X3_f);
title("*(\omega)") title("*(\omega)")
xlabel("n") xlabel("n")
ylabel("X1(\omega) * X3(\omega)") ylabel("X1(\omega) * X3(\omega)")
subplot(3,3,9) subplot(3,3,9)
plot(X2_X3_f); plot(x_axis, X2_X3_f);
%title("X2 * X3 no domínio da freq") %title("X2 * X3 no domínio da freq")
xlabel("n") xlabel("n")
ylabel("X2(\omega) * X3(\omega)") ylabel("X2(\omega) * X3(\omega)")

121
TP2/DiogoEliseu_TP2_4.m Normal file
View File

@ -0,0 +1,121 @@
%% Inicialização do ambiente
clear ; close all; clc
% Definimos uma seed estatica para o gerador de números aleatórios de forma
% a podermos repetir os experimentos com resultados determinísticos
rng(42,'twister');
%% Exercício 4
%a)
N = 255;
n = 0:N;
X0 = 0.25.*n.*exp(-0.03.*n);
Ra = (cos(0.82*pi.*n) + sin(0.85*pi.*n) + sin(0.91*pi.*n) + cos(0.95*pi.*n))/4;
Rb = (0.6-(-0.6)).*rand(1,N+1) + (-0.6);
Xa = X0+Ra;
Xb = X0+Rb;
Xc = X0 + Ra + Rb .* Xa;
figure(1)
subplot(2,3,1)
plot(X0);
title("X0");
subplot(2,3,2)
plot(Ra);
title("Ra");
subplot(2,3,3)
plot(Rb);
title("Rb");
subplot(2,3,4)
plot(Xa);
title("Xa");
subplot(2,3,5)
plot(Xb);
title("Xb");
subplot(2,3,6)
plot(Xc);
title("Xc");
%b)
DFT_range1 = fft(Xa, N+1); % entre 0 e 2pi
DFT_range2 = [ DFT_range1((N+1)/2+1 : (N+1)) , DFT_range1(1 : (N+1)/2) ];
DFT_range3 = DFT_range1(1 : (N+1)/2);
figure(2)
subplot(2,3,1)
plot(X(N+1, 0, 2),abs(DFT_range1))
xlabel('\omega/2\pi'), ylabel('|X(\omega)|'), title('[0,2\pi]')
subplot(2,3,2)
plot(X(N+1, -1, 1),abs(DFT_range2))
xlabel('\omega/2\pi'), ylabel('|X(\omega)|'), title('[-\pi,\pi]')
subplot(2,3,3)
plot(X((N+1)/2, 0, 1),abs(DFT_range3))
xlabel('\omega/\pi'), ylabel('|X(\omega)|'), title('[0,\pi]')
subplot(2,3,4)
plot(X(N+1, 0, 2),abs(DFT_range1))
xlabel('\omega/2\pi'), ylabel('\theta(\omega)'), title('[0,2\pi]')
subplot(2,3,5)
plot(X(N+1, 0, 2),abs(DFT_range1))
xlabel('\omega/2\pi'), ylabel('\theta(\omega)'), title('[-\pi,\pi]')
subplot(2,3,6)
plot(X(N+1, 0, 2),abs(DFT_range1))
xlabel('\omega/\pi'), ylabel('\theta(\omega)'), title('[0,\pi]')
%c)
DFT_X0 = fft(X0, N+1);
DFT_X0 = DFT_X0(1 : (N+1)/2);
DFT_Ra = fft(Ra, N+1);
DFT_Ra = DFT_Ra(1 : (N+1)/2);
DFT_Rb = fft(Rb, N+1);
DFT_Rb = DFT_Rb(1 : (N+1)/2);
DFT_Xa = fft(Xa, N+1);
DFT_Xa = DFT_Xa(1 : (N+1)/2);
DFT_Xb = fft(Xb, N+1);
DFT_Xb = DFT_Xb(1 : (N+1)/2);
DFT_Xc = fft(Xc, N+1);
DFT_Xc = DFT_Xc(1 : (N+1)/2);
figure(3)
subplot(2,3,1)
plot(X((N+1)/2, 0, 1),abs(DFT_X0))
xlabel('\omega/\pi'), ylabel('|X(\omega)|'), title('X0')
subplot(2,3,2)
plot(X((N+1)/2, 0, 1),abs(DFT_Ra))
xlabel('\omega/\pi'), ylabel('|X(\omega)|'), title('Ra')
subplot(2,3,3)
plot(X((N+1)/2, 0, 1),abs(DFT_Rb))
xlabel('\omega/\pi'), ylabel('|X(\omega)|'), title('Rb')
subplot(2,3,4)
plot(X((N+1)/2, 0, 1),abs(DFT_Xa))
xlabel('\omega/\pi'), ylabel('|X(\omega)|'), title('Xa')
subplot(2,3,5)
plot(X((N+1)/2, 0, 1),abs(DFT_Xb))
xlabel('\omega/\pi'), ylabel('|X(\omega)|'), title('Xb')
subplot(2,3,6)
plot(X((N+1)/2, 0, 1),abs(DFT_Xc))
xlabel('\omega/\pi'), ylabel('|X(\omega)|'), title('Xc')
function f = X(N, inicio, fim)
f = (0:(1/N):(1 - 1/N))*(fim-inicio)+inicio;
end

108
TP2/DiogoEliseu_TP2_5.m Normal file
View File

@ -0,0 +1,108 @@
%% Inicialização do ambiente
clear ; close all; clc
%% Exercício 5
file_id = fopen("INS_10k.txt", "r");
A = fscanf(file_id, "%f %f %f %f %f %f %f", [7 Inf]);
A=A';
X = A(:,1); % ang_rate ( angles/sec )
Y = A(:,2);
Z = A(:,3);
DX = A(:,4);
DY = A(:,5);
DZ = A(:,6);
t = A(:,7);
fclose(file_id);
L = length(X); % Length of signal
total_time = (t(L)-t(1))/1000; % Time the sampling took in seconds
T = 0.02; % Sampling period in seconds
SF = 1/T; % Sampling frequency (cycles per sec)
tv = 0:1/SF:total_time; % secs (duh.)
F = SF*(1:(L))/L; % cycles per sec (Hertz)
% -- Normalizations -- %
F_rad = (2*pi) .* F ./ SF; % rads per sample
X_rad = X .* (pi/180); % ( radians / sec)
X_hertz = X_rad .* (1/(2*pi));
DFT_0_2pi = fft(X_rad, L); % entre 0 e 2pi
DFT_0_pi = DFT_0_2pi(1 : (L)/2);
%1)
figure(1)
subplot(2,2,1)
plot(tv,X) % X(t)
title("X(t)"); ylabel("Amplitude (angles)"); xlabel("Time (s)");
subplot(2,2,2)
plot(F_rad, abs(DFT_0_2pi)); % dft in 0:2pi
title("FFT of X in [0:2\pi]"); ylabel("Magnitude"); xlabel("\omega/\pi");
subplot(2,2,3)
plot(F_rad(1 : (L)/2), abs(DFT_0_pi)); %dft in 0:pi
title("FFT of X in [0:\pi]"); ylabel("Magnitude"); xlabel("\omega/\pi");
subplot(2,2,4)
plot(F,X_hertz); % X(f)
title("X in function of the frequency (Hz)"); ylabel("Amplitude (Hertz)"); xlabel("Frequency (Hertz)");
%2)
Y_rad = Y .* (pi/180);
Y_hertz = Y_rad .* (1/(2*pi));
Z_rad = Z .* (pi/180);
Z_hertz = Z_rad .* (1/(2*pi));
figure(2)
subplot(2,3,1)
plot(F,X_hertz); % X(f)
title("X in function of the frequency (Hz)"); ylabel("Amplitude (Hertz)"); xlabel("Frequency (Hertz)");
subplot(2,3,2)
plot(F,Y_hertz); % X(f)
title("Y in function of the frequency (Hz)"); ylabel("Amplitude (Hertz)"); xlabel("Frequency (Hertz)");
subplot(2,3,3)
plot(F,Z_hertz); % X(f)
title("Z in function of the frequency (Hz)"); ylabel("Amplitude (Hertz)"); xlabel("Frequency (Hertz)");
subplot(2,3,4)
plot(F,abs(fft(X_hertz)));
title("DFT of X in function of the frequency (Hz)"); ylabel("Magnitude"); xlabel("Frequency (Hertz)");
subplot(2,3,5)
plot(F,abs(fft(Y_hertz)));
title("DFT of Y in function of the frequency (Hz)"); ylabel("Magnitude"); xlabel("Frequency (Hertz)");
subplot(2,3,6)
plot(F,abs(fft(Z_hertz)));
title("DFT of Z in function of the frequency (Hz)"); ylabel("Magnitude"); xlabel("Frequency (Hertz)");
%3)
DX_rad = DX .* (pi/180);
DX_hertz = DX_rad .* (1/(2*pi));
DX_hertz = DX_hertz(1:601);
DY_rad = DY .* (pi/180);
DY_hertz = DY_rad .* (1/(2*pi));
DY_hertz = DY_hertz(1:601);
DZ_rad = DZ .* (pi/180);
DZ_hertz = DZ_rad .* (1/(2*pi));
DZ_hertz = DZ_hertz(1:601);
F3 = F(1:601);
figure(3)
subplot(2,3,1)
plot(F3,DX_hertz); % X(f)
title("X' in function of the frequency (Hz)"); ylabel("Amplitude (Hertz)"); xlabel("Frequency (Hertz)");
subplot(2,3,2)
plot(F3,DY_hertz); % X(f)
title("Y' in function of the frequency (Hz)"); ylabel("Amplitude (Hertz)"); xlabel("Frequency (Hertz)");
subplot(2,3,3)
plot(F3,DZ_hertz); % X(f)
title("Z' in function of the frequency (Hz)"); ylabel("Amplitude (Hertz)"); xlabel("Frequency (Hertz)");
subplot(2,3,4)
plot(F3,abs(fft(DX_hertz)));
title("DFT of X in function of the frequency (Hz)"); ylabel("Magnitude"); xlabel("Frequency (Hertz)");
subplot(2,3,5)
plot(F3,abs(fft(DY_hertz)));
title("DFT of Y in function of the frequency (Hz)"); ylabel("Magnitude"); xlabel("Frequency (Hertz)");
subplot(2,3,6)
plot(F3,abs(fft(DZ_hertz)));
title("DFT of Z in function of the frequency (Hz)"); ylabel("Magnitude"); xlabel("Frequency (Hertz)");

101
TP2/DiogoEliseu_TP2_6.m Normal file
View File

@ -0,0 +1,101 @@
%% Análise de Resultados
%[+] Para n = 256
% [>] conv demorou 0.000481
% [>] ifft demorou 0.001027
%
%[+] Para n = 1337
% [>] conv demorou 0.000601
% [>] ifft demorou 0.003004
%
%[+] Para n = 31337
% [>] conv demorou 0.456320
% [>] ifft demorou 0.038267
%
%[+] Para n = 424242
% [>] conv demorou 5.849966
% [>] ifft demorou 0.885959
%
%
% Verifica-se que para n pequenos temos conv mais eficiente do que ifft,
% Para n maiores temos ifft mais eficiente do que conv.
%% Inicialização do ambiente
clear ; close all; clc
%% Exercício 6
% signals
N = 256;
n = (0:N-1);
X1 = 2.*n.*0.97.^n;
X2 = cos(6*pi.*n./N);
% time conv
tic
X1_X2_t = conv(X1, X2);
tocked1 = toc;
% freq conv
tic
X1_fft = fft([X1 zeros(1,N-1)]);
X2_fft = fft([X2 zeros(1,N-1)]);
X1_X2_f = ifft(X1_fft.*X2_fft);
tocked2 = toc;
fprintf("[+] Para n = %d\n [>] conv demorou %f\n [>] ifft demorou %f\n\n",N, tocked1, tocked2);
N = 1337;
n = (0:N-1);
X1 = 2.*n.*0.97.^n;
X2 = cos(6*pi.*n./N);
% time conv
tic
X1_X2_t = conv(X1, X2);
tocked1 = toc;
% freq conv
tic
X1_fft = fft([X1 zeros(1,N-1)]);
X2_fft = fft([X2 zeros(1,N-1)]);
X1_X2_f = ifft(X1_fft.*X2_fft);
tocked2 = toc;
fprintf("[+] Para n = %d\n [>] conv demorou %f\n [>] ifft demorou %f\n\n",N, tocked1, tocked2);
N = 31337;
n = (0:N-1);
X1 = 2.*n.*0.97.^n;
X2 = cos(6*pi.*n./N);
% time conv
tic
X1_X2_t = conv(X1, X2);
tocked1 = toc;
% freq conv
tic
X1_fft = fft([X1 zeros(1,N-1)]);
X2_fft = fft([X2 zeros(1,N-1)]);
X1_X2_f = ifft(X1_fft.*X2_fft);
tocked2 = toc;
fprintf("[+] Para n = %d\n [>] conv demorou %f\n [>] ifft demorou %f\n\n",N, tocked1, tocked2);
N = 424242;
n = (0:N-1);
X1 = 2.*n.*0.97.^n;
X2 = cos(6*pi.*n./N);
% time conv
tic
X1_X2_t = conv(X1, X2);
tocked1 = toc;
% freq conv
tic
X1_fft = fft([X1 zeros(1,N-1)]);
X2_fft = fft([X2 zeros(1,N-1)]);
X1_X2_f = ifft(X1_fft.*X2_fft);
tocked2 = toc;
fprintf("[+] Para n = %d\n [>] conv demorou %f\n [>] ifft demorou %f\n",N, tocked1, tocked2);