This repository has been archived by the owner on Dec 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v2.m
87 lines (82 loc) · 1.95 KB
/
v2.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
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
% global setting
dt = 1e-6; % precision
maxT = 2e-3; % max time
maxF = 10;
s1F = 3e3;
s2F = 5e3;
Fs = 6e3;
[s1, ss1, t1, n1] = signal_sampling(1, s1F, 0, Fs, maxT, dt);
[s2, ss2, t2, n2] = signal_sampling(1, s2F, 0, Fs, maxT, dt);
subplot(3, 3, 1);
plot(t1, s1);
xlabel('time(sec)');
ylabel('Amplitude');
title("Signal 1 " + s1F/1e3 + "kHz Sine");
subplot(3, 3, 2);
stem(n1, ss1);
xlabel('time(sec)');
ylabel('Amplitude');
title("Sampling signal 1 with " + Fs/1e3 + "kHz");
subplot(3, 3, 3);
pxx = pwelch(s1,[],[],[],Fs);
pxx = pxx(1:maxF);
plot(pxx);
xlabel('Frequency');
ylabel('Power');
title('Spectrum');
subplot(3, 3, 4);
plot(t2, s2);
xlabel('time(sec)');
ylabel('Amplitude');
title("Signal 2 " + s2F/1e3 + "kHz Sine")
subplot(3, 3, 5);
stem(n2, ss2);
xlabel('time(sec)');
ylabel('Amplitude');
title("Sampling signal 2 with " + Fs/1e3 + "kHz")
subplot(3, 3, 6);
pxx = pwelch(s2,[],[],[],Fs);
pxx = pxx(1:maxF);
plot(pxx);
xlabel('Frequency');
ylabel('Power');
title('Spectrum');
subplot(3, 3, 7);
plot(t1, s1+s2);
xlabel('time(sec)');
ylabel('Amplitude');
title('Signal 1+2');
subplot(3, 3, 8);
stem(n1, ss1+ss2);
xlabel('time(sec)');
ylabel('Amplitude');
title("Sampling signal 1+2 with " + Fs/1e3 + "kHz")
subplot(3, 3, 9);
pxx = pwelch(s1+s2,[],[],[],Fs);
pxx = pxx(1:maxF);
plot(pxx);
xlabel('Frequency');
ylabel('Power');
title('Spectrum');
function [s, ss, t, n] = signal_sampling(A, F, theta, Fs, maxT, dt)
% =========================================================
% This function is to generate signal and sampled signal
% Input:
% A: amplitude
% F: frequency
% theta: phase
% Fs: sampling frequency
% maxT: max time
% dt: time precision
% Output:
% s: signal
% ss: sampled signal
% t: time
% n: sampling time
% =========================================================
t = 0:dt:maxT;
s = A * sin(2*pi*F*t + theta);
T = 1/Fs;
n = 0:T:maxT;
ss = A * sin(2*pi*F*n + theta);
end