1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
data = importdata("C:\Users\Jacob McDonnell\Documents\CMPEN-472-HW\cmpen472hw10_McDonnell\data.csv");
sawtooth = data.data(:,1);
triangle = data.data(:,2);
square = data.data(:,3);
sawtooth125 = data.data(:,4);
square125 = data.data(:,5);
sawtooth250 = data.data(:,7)
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");
psw250 = CreatePlot(sa)
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');
|