uploaded all materials

This commit is contained in:
2025-03-24 00:43:09 -04:00
commit f2fb36f646
78 changed files with 15606 additions and 0 deletions

View File

@ -0,0 +1,138 @@
%%%% 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

View File

@ -0,0 +1,27 @@
function Hd = butterworth
%BUTTERWORTH Returns a discrete-time filter object.
% MATLAB Code
% Generated by MATLAB(R) 23.2 and Signal Processing Toolbox 23.2.
% Generated on: 27-Mar-2024 18:56:52
% Butterworth Bandpass filter designed using FDESIGN.BANDPASS.
% All frequency values are in Hz.
Fs = 44100; % Sampling Frequency
Fstop1 = 6300; % First Stopband Frequency
Fpass1 = 7350; % First Passband Frequency
Fpass2 = 14700; % Second Passband Frequency
Fstop2 = 17640; % Second Stopband Frequency
Astop1 = 50; % First Stopband Attenuation (dB)
Apass = 1; % Passband Ripple (dB)
Astop2 = 50; % Second Stopband Attenuation (dB)
match = 'stopband'; % 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]

12
hw6/feedback/chebyshev1.m Normal file
View File

@ -0,0 +1,12 @@
function Hcheb = chebyshev1
Fs = 44100;
Fpass = Fs/9;
Fstop = Fs/8;
Apass = 5;
Astop = 40;
specs = fdesign.lowpass(Fpass, Fstop, Apass, Astop, Fs);
Hcheb = design(specs,"cheby1",MatchExactly="passband");
end

12
hw6/feedback/chebyshev2.m Normal file
View File

@ -0,0 +1,12 @@
function Hcheb = chebyshev2
Fs = 44100;
Fpass = Fs/3;
Fstop = Fs/4;
Apass = 5;
Astop = 40;
specs = fdesign.highpass(Fstop, Fpass, Astop, Apass, Fs);
Hcheb = design(specs,"cheby2",MatchExactly="passband");
end

27
hw6/feedback/elliptic.m Normal file
View File

@ -0,0 +1,27 @@
function Hd = elliptic
%ELLIPTIC Returns a discrete-time filter object.
% MATLAB Code
% Generated by MATLAB(R) 23.2 and Signal Processing Toolbox 23.2.
% Generated on: 27-Mar-2024 18:58:12
% Elliptic Bandstop filter designed using FDESIGN.BANDSTOP.
% All frequency values are in Hz.
Fs = 44100; % Sampling Frequency
Fpass1 = 6300; % First Passband Frequency
Fstop1 = 7350; % First Stopband Frequency
Fstop2 = 14700; % Second Stopband Frequency
Fpass2 = 17640; % Second Passband Frequency
Apass1 = 1; % First Passband Ripple (dB)
Astop = 50; % Stopband Attenuation (dB)
Apass2 = 1; % Second Passband Ripple (dB)
match = 'both'; % Band to match exactly
% Construct an FDESIGN object and call its ELLIP method.
h = fdesign.bandstop(Fpass1, Fstop1, Fstop2, Fpass2, Apass1, Astop, ...
Apass2, Fs);
Hd = design(h, 'ellip', 'MatchExactly', match);
% [EOF]

View File

@ -0,0 +1,12 @@
function noise = fortune_favors_the_fontaine(Fs, duration)
% make some noise
output = "";
while strlength(output) < Fs*duration
[ret dirty_snippet] = unix('fortune');
% clean
snippet = regexprep(dirty_snippet, '[^A-Za-z]+', '');
output = strcat(output, snippet);
end
noise = normalize(letters2numbers(convertStringsToChars(output)));
end
% <+.03> what the fuck

View File

@ -0,0 +1,4 @@
function num = letters2numbers(word)
asc = double( upper(word) ); % http://www.asciitable.com/
num = 26 + double('A') - asc; % simple arithmetic
end