summaryrefslogtreecommitdiff
path: root/MATLAB/HW11/hw11.m
diff options
context:
space:
mode:
authorJacob McDonnell <jacob@jacobmcdonnell.com>2025-05-05 12:40:42 -0400
committerJacob McDonnell <jacob@jacobmcdonnell.com>2025-05-05 12:40:42 -0400
commitbae3f3b8301e8d13163f27c1e845302b9b65009d (patch)
treee63c306575ee55a30767ff3e08515b3cf200cf8a /MATLAB/HW11/hw11.m
parentb1ed855ea65d7c080e9c7921e00eddde4baf9f1b (diff)
Matlab files for HW10 & HW11
Diffstat (limited to 'MATLAB/HW11/hw11.m')
-rw-r--r--MATLAB/HW11/hw11.m122
1 files changed, 122 insertions, 0 deletions
diff --git a/MATLAB/HW11/hw11.m b/MATLAB/HW11/hw11.m
new file mode 100644
index 0000000..811addc
--- /dev/null
+++ b/MATLAB/HW11/hw11.m
@@ -0,0 +1,122 @@
+data = importdata("data.csv");
+
+sawtooth = data.data(:,1);
+triangle = data.data(:,2);
+square = data.data(:,3);
+sawtooth125 = data.data(:,4);
+square125 = data.data(:,5);
+sine100 = data.data(:,7);
+triangle100 = data.data(:,8);
+square100 = data.data(:,9);
+mixedSine = data.data(:,10);
+square100G = data.data(:,12);
+sawtooth100G = data.data(:,11);
+time = data.data(:,6);
+time = time / 1000000; % adjust to microseconds
+
+psw = CreatePlot(sawtooth,"Sawtooth Wave Two Cycles",time,1,512);
+saveas(psw,"sawtooth2cycles.png");
+psw = CreatePlot(sawtooth,"Sawtooth Wave",time,1,2048);
+saveas(psw,"sawtooth.png");
+
+pt = CreatePlot(triangle,"Triangle Wave Two Cycles",time,1,1024);
+saveas(pt,"triangle2cycles.png");
+pt = CreatePlot(triangle,"Triangle Wave",time,1,2048);
+saveas(pt,"triangle.png");
+
+psq = CreatePlot(square,"Square Wave Two Cycles",time,1,1024);
+saveas(psq,"square2cycles.png");
+psq = CreatePlot(square,"Square Wave",time,1,2048);
+saveas(psq,"square.png");
+
+psw125 = CreatePlot(sawtooth125,"Sawtooth Wave 125Hz Two Cycles",time,1,128);
+saveas(psw125,"sawtooth-125Hz2cycles.png");
+psw125 = CreatePlot(sawtooth125,"Sawtooth Wave 125Hz",time,1,2048);
+saveas(psw125,"sawtooth-125Hz.png");
+
+psq125 = CreatePlot(square125,"Square Wave 125Hz Two Cycles",time,1,128);
+saveas(psq125,"square-125Hz2cycles.png");
+psq125 = CreatePlot(square125,"Square Wave 125Hz",time,1,2048);
+saveas(psq125,"square-125Hz.png");
+
+psn100 = CreatePlot(sine100,"Sine Wave 100Hz Two Cycles",time,35,193);
+saveas(psn100,"sine-100Hz-2-cycles.png");
+psn100 = CreatePlot(sine100,"Sine Wave 100Hz",time,1,2048);
+saveas(psn100,"sine-100Hz.png");
+
+pt100 = CreatePlot(triangle100,"Triangle Wave 100Hz Two Cycles",time,62,220);
+saveas(pt100,"triangle-100Hz-2-cycles.png");
+pt100 = CreatePlot(triangle100,"Triangle Wave 100Hz",time,1,2048);
+saveas(pt100,"triangle-100Hz.png");
+
+pq100 = CreatePlot(square100,"Square Wave 100Hz Two Cycles",time,29,180);
+saveas(pq100,"square-100Hz-2-cycles.png");
+pq100 = CreatePlot(square100,"Square Wave 100Hz",time,1,2048);
+saveas(pq100,"square-100Hz.png");
+
+ms = CreatePlot(mixedSine,"Mixed Sine Wave Two Cycles",time,73,233);
+saveas(ms,"mixed-2-cycles.png");
+ms = CreatePlot(mixedSine,"Mixed Wave",time,1,2048);
+saveas(ms,"mixed-100Hz.png");
+
+psw100G = CreatePlot(sawtooth100G,"Sawtooth Wave 100Hz Generated Two Cycles",time,1,173);
+saveas(psw100G,"sawtooth-100Hz-Generated-2cycles.png");
+psw100G = CreatePlot(sawtooth100G,"Sawtooth Wave 100Hz Generated",time,1,2048);
+saveas(psw100G,"sawtooth-100Hz-Generated.png");
+
+psq100G = CreatePlot(square100G,"Square Wave 100Hz Generated Two Cycles",time,1,173);
+saveas(psq100G,"square-100Hz-Generated-2cycles.png");
+psq100G = CreatePlot(square100G,"Square Wave 100Hz Generated",time,1,2048);
+saveas(psq100G,"square-100Hz-Generated.png");
+
+Fs = 1/8000; % 8000Hz sampling frequency
+
+function p = CreatePlot(d, label, time, spos, epos)
+ p = figure;
+ plot(time(spos:epos), d(spos:epos));
+ xlabel('Time in Seconds');
+ ylabel('Signal Values');
+ title(label);
+ grid on;
+end
+
+function freqPlot = GeneratePlot(d,t,label)
+ Fs = 1/mean(diff(t));
+ x = d - mean(d);
+
+ n = length(x); % Number of samples
+ X = fft(x); % Compute the FFT
+
+ % Only take the first half of the spectrum (positive frequencies)
+ X_mag = abs(X(1:floor(n/2))); % Magnitude of FFT
+ f = Fs * (0:floor(n/2)-1) / n; % Frequency vector
+
+ freqPlot = figure;
+ plot(f, X_mag);
+ xlabel('Frequency (Hz)');
+ ylabel('Amplitude');
+ title(label);
+ grid on;
+end
+
+swfft = GeneratePlot(sawtooth,time,'Sawtooth');
+
+tfft = GeneratePlot(triangle,time,'Triangle');
+
+sqfft = GeneratePlot(square,time,'Square');
+
+sw125fft = GeneratePlot(sawtooth125,time,'Sawtooth 125Hz');
+
+sq125fft = GeneratePlot(square125,time,'Square 125Hz');
+
+sine100fft = GeneratePlot(sine100,time,"Sine Wave 100Hz");
+
+tri100fft = GeneratePlot(triangle100,time,"Triangle Wave 100Hz");
+
+sq100fft = GeneratePlot(square100,time,"Square Wave 100Hz");
+
+mixedfft = GeneratePlot(mixedSine,time,"Mixed Frequency");
+
+sq100Gfft = GeneratePlot(square100G,time,"Square Wave 100Hz Generated");
+
+sw100Gfft = GeneratePlot(sawtooth100G,time,"Sawtooth Wave 100Hz Generated"); \ No newline at end of file