-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.m
197 lines (166 loc) · 4.6 KB
/
main.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
clc;
clear;
close all;
% Start simulation
savepath = 'Assets/';
% Define signal properties
F = 1 * 10^3;
Fs = 40 * 10^3;
tStart = 0;
tStop = 0.01;
sigSNR = 20;
x_range = [-5 5];
% Define channel properties
SNR = 0 : 30;
record_SNR = 20;
% Define PCM properties
nBits = 1 : 48;
record_nBits = 8;
% Initialize the average error power matrix
y_avgerr_an = zeros(length(SNR), length(nBits));
y_avgerr_dg = zeros(length(SNR), length(nBits));
% Run for all SNR and nBits
for iSNR = 1 : length(SNR)
for inBits = 1 : length(nBits)
% Get the original signal
[x_sig, t] = sig_lab1(F, Fs, sigSNR, tStart, tStop);
% Pass it through AWGN channel to reciever side
y_arcv = awgn(x_sig, SNR(iSNR));
% Get the error power
y_aerr = (y_arcv - x_sig) .^ 2;
% Get the average error power
y_avgerr_an(iSNR, inBits) = sum(y_aerr) / length(y_aerr);
% Quantize the original signal
x_qua = sig_quantize(x_sig, x_range, 2^nBits(inBits));
% Encode the quantized signal
x_enc = sig_encode(x_qua, nBits(inBits));
% Pass the encoded signal through AWGN channel to reciever side
y_drcv = awgn(x_enc, SNR(iSNR));
% Decode the recieved signal
y_dec = sig_decode(y_drcv, nBits(inBits));
% Dequantize the recieved signal
y_deq = sig_dequantize(y_dec, x_range, 2^nBits(inBits));
% Get the error power
y_derr = (y_deq - x_sig) .^ 2;
% Get the average error power
y_avgerr_dg(iSNR, inBits) = sum(y_derr) / length(y_derr);
if(record_SNR == SNR(iSNR) && record_nBits == nBits(inBits))
% Draw the original signal
figure;
% Get the time-domain plot of input signal
subplot(2, 2, 1);
plot(t, x_sig);
xlim([tStart tStop]);
ylim(x_range);
title('Input Signal - x_sig(t)');
xlabel('Time (s)');
ylabel('Amplitude');
% Get the frequency spectrum of input signal
[Y_sigf, YF] = sig_freq(x_sig, Fs);
subplot(2, 2, 2);
plot(YF, abs(Y_sigf));
title('Frequency spectrum of Input Signal - x_sig(t)');
xlabel('Frequency (Hz)');
ylabel('Amplitude');
% Plot the recieved signal
% The recieved signal
subplot(2, 2, 3);
plot(t, y_arcv);
xlim([tStart tStop]);
ylim(x_range);
title('Recieved analog Signal - y_arcv(t)');
xlabel('Time (s)');
ylabel('Amplitude');
% The error power signal
subplot(2, 2, 4);
plot(t, y_aerr);
xlim([tStart tStop]);
title('Error power for analog - y_aerr(t)');
xlabel('Time (s)');
ylabel('Amplitude');
% Plot the pcm encoding signals
figure;
% The quantized signal
subplot(2, 2, 1);
plot(t, x_qua);
xlim([tStart tStop]);
ylim([-1 2^nBits(inBits)]);
title('Quantized PCM Signal - x_qua(t)');
xlabel('Time (s)');
ylabel('Amplitude');
% The encoded signal
subplot(2, 2, 2);
plot(x_enc);
ylim([-1 2]);
title('Encoded PCM Signal - x_enc(t)');
xlabel('Bit sample number');
ylabel('Bit Value');
% The recieved signal
subplot(2, 2, 3);
plot(y_drcv);
title('Recieved PCM Signal - y_drcv(t)');
xlabel('Bit sample number');
ylabel('Bit Value');
% The decoded signal
subplot(2, 2, 4);
plot(t, y_dec);
ylim([-1 2^nBits(inBits)]);
title('Decoded PCM Signal - y_dec(t)');
xlabel('Time (s)');
ylabel('Amplitude');
% Contd recieveing figure
figure;
% Get the quantized signal
subplot(2, 1, 1);
plot(t, y_deq);
xlim([tStart tStop]);
ylim(x_range);
title('Dequantized PCM Signal - y_deq(t)');
xlabel('Time (s)');
ylabel('Amplitude');
% Get the error signal
subplot(2, 1, 2);
plot(t, y_derr);
title('Error power for PCM - y_derr(t)');
xlabel('Time (s)');
ylabel('Amplitude');
xlim([tStart tStop]);
drawnow;
end
end
end
fig = figure;
for i = 1 : size(y_avgerr_an, 2)
semilogy(SNR, y_avgerr_an(:, i), 'r');
hold on;
semilogy(SNR, y_avgerr_dg(:, i), 'b');
ttl = sprintf('Bits - Variation of avg. error power (%f bits)', nBits(i));
title(ttl);
xlabel('SNR');
ylabel('Avg. error power - log10');
xlim([SNR(1) SNR(end)]);
saveas(fig, [savepath ttl '.jpg']);
clf(fig);
end
for i = 1 : size(y_avgerr_an, 1)
semilogy(nBits, y_avgerr_an(i, :), 'r');
hold on;
semilogy(nBits, y_avgerr_dg(i, :), 'b');
ttl = sprintf('SNR - Variation of avg. error power (%f SNR)', SNR(i));
title(ttl);
xlabel('No. of Bits');
ylabel('Avg. error power - log10');
xlim([nBits(1) nBits(end)]);
saveas(fig, [savepath ttl '.jpg']);
clf(fig);
end
close(fig);
figure;
surf(log10(y_avgerr_an), zeros(size(y_avgerr_an)));
hold on;
surf(log10(y_avgerr_dg), ones(size(y_avgerr_dg)));
ttl = 'Variation of avg. error power';
title(ttl);
ylabel('SNR');
xlabel('No. of bits');
zlabel('Avg. error power - log10');