-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfig_2_single_sided_amplitude_spectrum_1.m
54 lines (39 loc) · 1.4 KB
/
fig_2_single_sided_amplitude_spectrum_1.m
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
clear;
% This script creates a sine wave and plots a segment of the sine and its
% DFT amplitude spectrum.
N = 200000; % signal length in samples; needs to be even
fs = 40000; % sampling frequency in Hz
t = (0 : N-1).'/fs; % vector with time instants in s
f0 = 1000; % frequency of the sine in Hz; note that it is not a
% coincidence that f0 is an integer fraction of fs
% sine with amplitude 1 and DC offset of 1
sig = sin(2*pi*f0*t) + 1;
% DFT
spec = fft(sig);
% normalize to an amplitude spectrum
spec = spec/N;
% convert to single-sided spectrum
spec = spec(1:end/2+1);
spec(2:end-1) = 2*spec(2:end-1);
% create a frequency axis for the plot in Hz
f = linspace(0, fs/2, length(spec)).';
% do the plotting
figure('Color', [242, 242, 242]/255, 'Position', [500 500 380 200]);
subplot(2, 1, 1);
plot(sig, 'k', 'Linewidth', 2);
xlim([1000 1100]);
ylim([-.2 2.2]);
grid on;
xlabel('Time index $n$', 'interpreter', 'latex');
ylh = ylabel('$x(n)$ (V)', 'interpreter', 'latex');
ylh.Position(1) = ylh.Position(1) - 2.5;
subplot(2, 1, 2);
plot(f, abs(spec), 'k', 'Linewidth', 2);
xlim([0 2000]);
ylim([-.2 1.2]);
grid on;
xlabel('Frequency (Hz)', 'interpreter', 'latex');
ylabel('$\left|\overline{X}_\mathrm{SS}(k)\right|$ (V)', 'interpreter', 'latex');
% make sure that background color is preserved when saving
set(gcf, 'InvertHardCopy', 'off');
%saveas(gcf, 'fig_2.png');