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
/
code7.m
141 lines (116 loc) · 4 KB
/
code7.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
% code3
% Generate 512 samples of digital signal consists of 3 sinewaves at different frequencies (150, 250, 350 Hz), with corresponding amplitude (1, 0.8, 0.75) respectively, and the sampling rate 1000 Hz, which are corrupted by white noise with SNR 0 dB.
function w5_code7
N=10000;
fs=10000;
n=[0:N-1];
k=[0:N-1];
f=150;
A=1;
omega=2*pi*f/fs;
xn=A*sin(omega*n);
xnPRMS = rms(xn)^2;
xnPowBP = bandpower(xn, fs, [0 fs/2]);
subplot(3, 4, 1);
plot(xn);
title(strcat('signal | f=', num2str(f), ' | pRMS=', num2str(xnPRMS), ' | powbp=', num2str(xnPowBP)));
xlabel('time');
ylabel('amplitude');
noise = 2*sqrt(3)*(rand(size(xn))-0.5);
nsPRMS = rms(noise)^2;
nsPowBP = bandpower(noise, fs, [0 fs/2]);
subplot(3, 4, 2);
plot(noise);
title(strcat('noise | pRMS=', num2str(nsPRMS), ' | powbp=', num2str(nsPowBP)));
xlabel('time');
ylabel('amplitude');
corruptedSignal = xn + noise;
csPRMS = rms(corruptedSignal)^2;
csPowBP = bandpower(corruptedSignal, fs, [0 fs/2]);
subplot(3, 4, 3);
plot(corruptedSignal);
title(strcat('corrupted signal | pRMS=', num2str(csPRMS), ' | powbp=', num2str(csPowBP)));
xlabel('time');
ylabel('amplitude');
Xk = fft(corruptedSignal, N);
magXk = 20*log10(abs(Xk));
SNR=snr(xn, sqrt(2)/2*noise);
subplot(3, 4, 4);
plot(k, magXk);
title(strcat('SNR=', num2str(SNR), 'dB | N=', num2str(N), ' | fs=', num2str(fs)));
axis([0, N/2, -inf, inf]);
xlabel('Frequency index, k');
ylabel('Magnitude in dB');
f=250;
A=0.8;
omega=2*pi*f/fs;
xn=A*sin(omega*n);
xnPRMS = rms(xn)^2;
xnPowBP = bandpower(xn, fs, [0 fs/2]);
subplot(3, 4, 5);
plot(xn);
title(strcat('signal | f=', num2str(f), ' | pRMS=', num2str(xnPRMS), ' | powbp=', num2str(xnPowBP)));
xlabel('time');
ylabel('amplitude');
noise = 2*sqrt(3)*(rand(size(xn))-0.5)*A;
nsPRMS = rms(noise)^2;
nsPowBP = bandpower(noise, fs, [0 fs/2]);
subplot(3, 4, 6);
plot(noise);
title(strcat('noise | pRMS=', num2str(nsPRMS), ' | powbp=', num2str(nsPowBP)));
xlabel('time');
ylabel('amplitude');
corruptedSignal = xn + noise;
csPRMS = rms(corruptedSignal)^2;
csPowBP = bandpower(corruptedSignal, fs, [0 fs/2]);
subplot(3, 4, 7);
plot(corruptedSignal);
title(strcat('corrupted signal | pRMS=', num2str(csPRMS), ' | powbp=', num2str(csPowBP)));
xlabel('time');
ylabel('amplitude');
Xk = fft(corruptedSignal, N);
magXk = 20*log10(abs(Xk));
SNR=snr(xn, sqrt(2)/2*noise);
subplot(3, 4, 8);
plot(k, magXk);
title(strcat('SNR=', num2str(SNR), 'dB | N=', num2str(N), ' | fs=', num2str(fs)));
axis([0, N/2, -inf, inf]);
xlabel('Frequency index, k');
ylabel('Magnitude in dB');
f=350;
A=0.75;
omega=2*pi*f/fs;
xn=A*sin(omega*n);
xnPRMS = rms(xn)^2;
xnPowBP = bandpower(xn, fs, [0 fs/2]);
subplot(3, 4, 9);
plot(xn);
title(strcat('signal | f=', num2str(f), ' | pRMS=', num2str(xnPRMS), ' | powbp=', num2str(xnPowBP)));
xlabel('time');
ylabel('amplitude');
noise = 2*sqrt(3)*(rand(size(xn))-0.5)*A;
nsPRMS = rms(noise)^2;
nsPowBP = bandpower(noise, fs, [0 fs/2]);
subplot(3, 4, 10);
plot(noise);
title(strcat('noise | pRMS=', num2str(nsPRMS), ' | powbp=', num2str(nsPowBP)));
xlabel('time');
ylabel('amplitude');
corruptedSignal = xn + noise;
csPRMS = rms(corruptedSignal)^2;
csPowBP = bandpower(corruptedSignal, fs, [0 fs/2]);
subplot(3, 4, 11);
plot(corruptedSignal);
title(strcat('corrupted signal | pRMS=', num2str(csPRMS), ' | powbp=', num2str(csPowBP)));
xlabel('time');
ylabel('amplitude');
Xk = fft(corruptedSignal, N);
magXk = 20*log10(abs(Xk));
SNR=snr(xn, sqrt(2)/2*noise);
subplot(3, 4, 12);
plot(k, magXk);
title(strcat('SNR=', num2str(SNR), 'dB | N=', num2str(N), ' | fs=', num2str(fs)));
axis([0, N/2, -inf, inf]);
xlabel('Frequency index, k');
ylabel('Magnitude in dB');
end