summaryrefslogtreecommitdiff
path: root/MATLAB/HW11/hw11.m
blob: 811addce1c5ec7309c49442a2b9612d211df82b8 (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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");