delivered
							
								
								
									
										
											BIN
										
									
								
								TP3_8/DiogoEliseu_TP3_8_Relatorio.pdf
									
									
									
									
									
										Normal file
									
								
							
							
						
						@@ -1,50 +0,0 @@
 | 
			
		||||
%% Inicialização do ambiente
 | 
			
		||||
clear; close all; clc
 | 
			
		||||
 | 
			
		||||
%% Exercício 8
 | 
			
		||||
% Input
 | 
			
		||||
[y, Fs] = audioread("Canto1.mp3"); % Signal and Sampling Frequency
 | 
			
		||||
signal = y(:,1);
 | 
			
		||||
Fn = Fs/2; % Nyquist Frequency (Hz)
 | 
			
		||||
%Ts = 1/Fs;
 | 
			
		||||
L = length(signal); % Signal Length
 | 
			
		||||
 | 
			
		||||
% Find the noise
 | 
			
		||||
FTsignal = fft(signal)/L;           % Fourier Transform
 | 
			
		||||
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
 | 
			
		||||
Iv = 1:numel(Fv);                   % Index Vector
 | 
			
		||||
figure(1)
 | 
			
		||||
% x is Hz, y is amplitude
 | 
			
		||||
plot(Fv, abs(FTsignal(Iv))*2)
 | 
			
		||||
grid
 | 
			
		||||
 | 
			
		||||
% Butterworth Low-Pass Order 7
 | 
			
		||||
%[num, den] = butter(7, fc, 'low'); %order, cutoff frequency, type
 | 
			
		||||
%[SOS,G] = tf2sos(num, den); % Convert To Second-Order-Section For Stability
 | 
			
		||||
%figure(1)
 | 
			
		||||
%freqz(SOS, 4096, Fs) % Check Filter Performance
 | 
			
		||||
%s_filtered = filtfilt(SOS, G, signal);
 | 
			
		||||
 | 
			
		||||
fcutlow = 3000;
 | 
			
		||||
fcuthigh = 3500;
 | 
			
		||||
Wp = [fcutlow fcuthigh]/Fn; % Passband Frequency (Normalised)
 | 
			
		||||
Ws = [fcutlow*0.95 fcuthigh/0.95]/Fn; % Stopband Frequency (Normalised)
 | 
			
		||||
Rp = 30; % Passband Ripple (dB)
 | 
			
		||||
Rs = 30; % Stopband Ripple (dB)
 | 
			
		||||
%[n,Wn] = buttord(Wp,Ws,Rp,Rs); % Filter Order
 | 
			
		||||
n=2;
 | 
			
		||||
Wn=3200/Fn;
 | 
			
		||||
[z,p,k] = butter(n, Wn, 'low'); % Filter Design
 | 
			
		||||
[sosbp,gbp] = zp2sos(z, p, k); % Convert To Second-Order-Section For Stability
 | 
			
		||||
%freqz(sosbp, 2^20, Fs) % Filter Bode Plot
 | 
			
		||||
signal_filtered = filtfilt(sosbp, gbp, signal); % Filter Signal
 | 
			
		||||
figure(2)
 | 
			
		||||
subplot(2,1,1)
 | 
			
		||||
plot(signal)
 | 
			
		||||
subplot(2,1,2)
 | 
			
		||||
plot(signal_filtered);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
% output
 | 
			
		||||
audiowrite('restored.flac', signal_filtered, Fs);
 | 
			
		||||
@@ -1,45 +0,0 @@
 | 
			
		||||
%% Inicialização do ambiente
 | 
			
		||||
clear; close all; clc
 | 
			
		||||
 | 
			
		||||
%% Exercício 8
 | 
			
		||||
[y, Fs] = audioread("Canto1.mp3"); % Signal and Sampling Frequency
 | 
			
		||||
s = y(:,1);
 | 
			
		||||
L = length(s);
 | 
			
		||||
 | 
			
		||||
Fn = Fs/2;                                              % Nyquist Frequency
 | 
			
		||||
Wp = [150  5800]/Fn;                                    % Normalised Passband
 | 
			
		||||
Ws = [ 50  6100]/Fn;                                    % Normalised Stopband
 | 
			
		||||
Rp =  1;                                                % Passband Ripple (dB)
 | 
			
		||||
Rs = 30;                                                % Stopband Ripple (dB)
 | 
			
		||||
[n,Ws] = cheb2ord(Wp,Ws,Rp,Rs);                         % Chebyshev Type II Order
 | 
			
		||||
[b,a] = cheby2(n,Rs,Ws);                                % IIR Filter Coefficients
 | 
			
		||||
[SOS,G] = tf2sos(b,a);                                  % Convert To Second-Order-Section For Stability
 | 
			
		||||
figure(1)
 | 
			
		||||
freqz(SOS, 4096, Fs)                                    % Check Filter Performance
 | 
			
		||||
 | 
			
		||||
s_filtered = filtfilt(SOS, G, s);
 | 
			
		||||
 | 
			
		||||
fcuts = [680 690 710 720  1190 1205  1210 1220  6000  6100];    % Frequency Vector
 | 
			
		||||
mags =   [1 0 1 0 1 0];                                         % Magnitude (Defines Passbands & Stopbands)
 | 
			
		||||
devs = [0.05  0.01  0.05  0.01 0.05  0.01];                     % Allowable Deviations
 | 
			
		||||
[n,Wn,beta,ftype] = kaiserord(fcuts,mags,devs,Fs);
 | 
			
		||||
n = n + rem(n,2);
 | 
			
		||||
hh = fir1(n,Wn,ftype,kaiser(n+1,beta),'scale');
 | 
			
		||||
figure(2)
 | 
			
		||||
freqz(hh,1, 4096, Fs)                                           % Filter Bode Plot
 | 
			
		||||
filt_sig = filtfilt(hh, 1, s);                                  % Filter Signal
 | 
			
		||||
FTS = fft(s)/L;                                                 % FFT Of Original Signal
 | 
			
		||||
FTFS = fft(filt_sig)/L;                                         % FFT Of Filtered Signal
 | 
			
		||||
Fv = linspace(0, 1, fix(L/2)+1)*Fn;                             % FFT Frequency Vector
 | 
			
		||||
Iv = 1:length(Fv);                                              % Index Vector
 | 
			
		||||
[pks,Frs] = findpeaks(abs(FTS(Iv))*2, Fv, 'MinPeakHeight',0.1); % Find Pure Tone Frequencies
 | 
			
		||||
figure(3)
 | 
			
		||||
semilogy(Fv, (abs(FTS(Iv)))*2)
 | 
			
		||||
hold on
 | 
			
		||||
plot(Fv, (abs(FTFS(Iv)))*2)
 | 
			
		||||
hold off
 | 
			
		||||
grid
 | 
			
		||||
axis([0  2500    ylim])
 | 
			
		||||
 | 
			
		||||
% output
 | 
			
		||||
audiowrite('restored.flac', s_filtered, Fs);
 | 
			
		||||
@@ -1,30 +0,0 @@
 | 
			
		||||
%% Inicialização do ambiente
 | 
			
		||||
clear; close all; clc
 | 
			
		||||
 | 
			
		||||
%% Exercício 8
 | 
			
		||||
% Input
 | 
			
		||||
[y, Fs] = audioread("Canto1.mp3"); % Signal and Sampling Frequency
 | 
			
		||||
signal = y(:,1);
 | 
			
		||||
Fn = Fs/2; % Nyquist Frequency (Hz)
 | 
			
		||||
%Ts = 1/Fs;
 | 
			
		||||
L = length(signal); % Signal Length
 | 
			
		||||
 | 
			
		||||
% Find the noise
 | 
			
		||||
FTsignal = fft(signal)/L;           % Fourier Transform
 | 
			
		||||
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
 | 
			
		||||
Iv = 1:numel(Fv);                   % Index Vector
 | 
			
		||||
figure(1)
 | 
			
		||||
% x is Hz, y is amplitude
 | 
			
		||||
plot(Fv, abs(FTsignal(Iv))*2)
 | 
			
		||||
grid
 | 
			
		||||
 | 
			
		||||
% Filter
 | 
			
		||||
signal_filtered = filter(nd_designed_sexy,signal);
 | 
			
		||||
figure(2)
 | 
			
		||||
subplot(2,1,1)
 | 
			
		||||
plot(signal)
 | 
			
		||||
subplot(2,1,2)
 | 
			
		||||
plot(signal_filtered);
 | 
			
		||||
 | 
			
		||||
% output
 | 
			
		||||
audiowrite('restored.flac', signal_filtered, Fs);
 | 
			
		||||
| 
		 Before Width: | Height: | Size: 146 KiB  | 
| 
		 Before Width: | Height: | Size: 150 KiB  | 
| 
		 Before Width: | Height: | Size: 80 KiB  | 
@@ -1,23 +0,0 @@
 | 
			
		||||
function Hd = designed_sexy
 | 
			
		||||
%DESIGNED_SEXY Returns a discrete-time filter object.
 | 
			
		||||
 | 
			
		||||
% MATLAB Code
 | 
			
		||||
% Generated by MATLAB(R) 9.8 and Signal Processing Toolbox 8.4.
 | 
			
		||||
% Generated on: 28-Dec-2020 08:55:51
 | 
			
		||||
 | 
			
		||||
% Butterworth Lowpass filter designed using FDESIGN.LOWPASS.
 | 
			
		||||
 | 
			
		||||
% All frequency values are in Hz.
 | 
			
		||||
Fs = 44100;  % Sampling Frequency
 | 
			
		||||
 | 
			
		||||
Fpass = 3200;        % Passband Frequency
 | 
			
		||||
Fstop = 6900;        % Stopband Frequency
 | 
			
		||||
Apass = 1;           % Passband Ripple (dB)
 | 
			
		||||
Astop = 80;          % Stopband Attenuation (dB)
 | 
			
		||||
match = 'stopband';  % Band to match exactly
 | 
			
		||||
 | 
			
		||||
% Construct an FDESIGN object and call its BUTTER method.
 | 
			
		||||
h  = fdesign.lowpass(Fpass, Fstop, Apass, Astop, Fs);
 | 
			
		||||
Hd = design(h, 'butter', 'MatchExactly', match);
 | 
			
		||||
 | 
			
		||||
% [EOF]
 | 
			
		||||
| 
		 Before Width: | Height: | Size: 77 KiB  | 
| 
		 Before Width: | Height: | Size: 137 KiB  | 
| 
		 Before Width: | Height: | Size: 148 KiB  | 
| 
		 Before Width: | Height: | Size: 78 KiB  | 
@@ -1,27 +0,0 @@
 | 
			
		||||
function Hd = nd_designed_sexy
 | 
			
		||||
%ND_DESIGNED_SEXY Returns a discrete-time filter object.
 | 
			
		||||
 | 
			
		||||
% MATLAB Code
 | 
			
		||||
% Generated by MATLAB(R) 9.8 and DSP System Toolbox 9.10.
 | 
			
		||||
% Generated on: 28-Dec-2020 11:09:34
 | 
			
		||||
 | 
			
		||||
% Butterworth Bandpass filter designed using FDESIGN.BANDPASS.
 | 
			
		||||
 | 
			
		||||
% All frequency values are in Hz.
 | 
			
		||||
Fs = 48000;  % Sampling Frequency
 | 
			
		||||
 | 
			
		||||
Fstop1 = 730;         % First Stopband Frequency
 | 
			
		||||
Fpass1 = 3000;        % First Passband Frequency
 | 
			
		||||
Fpass2 = 5000;        % Second Passband Frequency
 | 
			
		||||
Fstop2 = 6800;        % Second Stopband Frequency
 | 
			
		||||
Astop1 = 50;          % First Stopband Attenuation (dB)
 | 
			
		||||
Apass  = 0.0005;      % Passband Ripple (dB)
 | 
			
		||||
Astop2 = 50;          % Second Stopband Attenuation (dB)
 | 
			
		||||
match  = 'passband';  % Band to match exactly
 | 
			
		||||
 | 
			
		||||
% Construct an FDESIGN object and call its BUTTER method.
 | 
			
		||||
h  = fdesign.bandpass(Fstop1, Fpass1, Fpass2, Fstop2, Astop1, Apass, ...
 | 
			
		||||
                      Astop2, Fs);
 | 
			
		||||
Hd = design(h, 'butter', 'MatchExactly', match);
 | 
			
		||||
 | 
			
		||||
% [EOF]
 | 
			
		||||
| 
		 Before Width: | Height: | Size: 75 KiB  | 
| 
		 Before Width: | Height: | Size: 74 KiB  | 
| 
		 Before Width: | Height: | Size: 75 KiB  |