ece210/hw6/feedback/Ryan_HW6_ECE210.m

139 lines
4.2 KiB
Matlab

%%%% ECE-210-B HW6 - Filters - James Ryan
% James Ryan, 03/27/24
% preamble
close all; clc; clear;
%% Generate white noise
Fs = 44100; % Hz = s^-1
N = Fs*2; % 2s * 44100 samples/sec = 88200 samples of noise
F = linspace(-Fs/2, Fs/2, N); % range of noise
% noise generation
% good way
noise = randn(1, N); % intensity of noise
% funny way - requires `fortune` as a dependency
%noise = fortune_favors_the_fontaine(Fs, 2);
% thank you!
% you're welcome!
shifted_fft_mag = @(sig, len) fftshift(abs(fft(sig, len))) / len;
%% Butterworth Filter
% had to change `5` to `end`, but otherwise perfect.
Hbutter = butterworth;
fvm_butter = fvtool(Hbutter, 'magnitude');
axm_butter = fvm_butter.Children(end);
axm_butter.YLim = [-60 5];
axm_butter.Title.String = 'Butterworth Bandpass Filter Magnitude Plot';
fvp_butter = fvtool(Hbutter, 'phase');
axp_butter = fvp_butter.Children(end);
axp_butter.Title.String = 'Butterworth Bandpass Filter Phase Plot';
butter_noise = Hbutter.filter(noise);
fftm_butter = shifted_fft_mag(butter_noise, N);
figure;
plot(F, fftm_butter);
xlim([-Fs/2 Fs/2]);
xlabel("Frequency (Hz)");
ylabel("Magnitude (dB)");
title("FFT of noisy signal passed through Butterworth Bandpass Filter");
% to save you, i played it back in the matlab terminal:
%soundsc(noise)
%soundsc(fftm_butter)
% to me, its just a mostly mute audio track minus two bursts of sound.
% that must be the negative and positive passband regions
% so the filter took the noise, and just took the regions outside of
% the band and killed them.
%% Elliptic Filter
Hellip = elliptic;
fvm_ellip = fvtool(Hellip, 'magnitude');
axm_ellip = fvm_ellip.Children(end);
axm_ellip.YLim = [-60 5];
axm_ellip.Title.String = 'Elliptical Bandstop Filter Magnitude Plot';
fvp_ellip = fvtool(Hellip, 'phase');
axp_ellip = fvp_ellip.Children(end);
axp_ellip.Title.String = 'Elliptical Bandstop Filter Phase Plot';
ellip_noise = Hellip.filter(noise);
fftm_ellip = shifted_fft_mag(ellip_noise, N);
figure;
plot(F, fftm_ellip);
xlim([-Fs/2 Fs/2]);
xlabel("Frequency (Hz)");
ylabel("Magnitude (dB)");
title("FFT of noisy signal passed through Elliptical Bandstop Filter");
%soundsc(fftm_ellip)
% I can hear this one.. come back? like very faintly
% I guess it goes to show how butterworth doesnt have that elliptic
% stopband portion, and everything decays to -inf dB. Interesting
% difference!
% same pattern as the past passband
%% Chebyshev Type 1 Filter
Hcheb1 = chebyshev1;
fvm_cheb1 = fvtool(Hcheb1, 'magnitude');
axm_cheb1 = fvm_cheb1.Children(end);
axm_cheb1.YLim = [-60 5];
axm_cheb1.Title.String = 'Chebyshev Type I Lowpass Filter Magnitude Plot';
fvp_cheb1 = fvtool(Hcheb1, 'phase');
axp_cheb1 = fvp_cheb1.Children(end);
axp_cheb1.Title.String = 'Chebyshev Type I Lowpass Filter Phase Plot';
cheb1_noise = Hcheb1.filter(noise);
fftm_cheb1 = shifted_fft_mag(cheb1_noise, N);
figure;
plot(F, fftm_cheb1);
xlim([-Fs/2 Fs/2]);
xlabel("Frequency (Hz)");
ylabel("Magnitude (dB)");
title("FFT of noisy signal passed through Chebyshev I Lowpass Filter");
%soundsc(fftm_cheb1)
% It sounds choppy, almost, where it wibbles in and out for the starting portion
% where its present. Definitely not the choice for audio filters!
% Unless you `want` that distortion in like music production, in which case, go for it
% Sort of sounds like a car engine firing
%% Chebyshev Type 2 Filter
Hcheb2 = chebyshev2;
fvm_cheb2 = fvtool(Hcheb2, 'magnitude');
axm_cheb2 = fvm_cheb2.Children(end);
axm_cheb2.YLim = [-60 5];
axm_cheb2.Title.String = 'Chebyshev Type I Highpass Filter Magnitude Plot';
fvp_cheb2 = fvtool(Hcheb2, 'phase');
axp_cheb2 = fvp_cheb2.Children(end);
axp_cheb2.Title.String = 'Chebyshev Type II Highpass Filter Phase Plot';
cheb2_noise = Hcheb2.filter(noise);
fftm_cheb2 = shifted_fft_mag(cheb2_noise, N);
figure;
plot(F, fftm_cheb2);
xlim([-Fs/2 Fs/2]);
xlabel("Frequency (Hz)");
ylabel("Magnitude (dB)");
title("FFT of noisy signal passed through Chebyshev II Highpass Filter");
%soundsc(fftm_cheb2)
% Its passband is sorta similar to the butterworth passband, but its die-out
% has an audible drop off to me. maybe i missed that in others with elliptic
% stop bands? No just played back fftm_ellip, and that dropped immediately. hmm
% placebo, perhaps