[r1820]: codec2-dev / octave / fuzzy_gray.m Maximize Restore History

Download this file

fuzzy_gray.m    587 lines (474 with data), 18.1 kB

  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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
% fuzzy_gray.m
% David Rowe
% 10 April 2014
%
% Various experiments in fuzzy gray codes and quantising and
% transmitting scalars.
1;
% fuzzy gray coding idea: use an extra parity bit, if we get a single
% bit error the value will be "close: to the original, so effect of
% error will be soft. Unlike data we don't need 0 bit errors. I
% struggled to extend this to larger m.
function three_bit_code
m=4;
log2_m=2;
value_to_codeword = ["000"; "001"; "101"; "111"];
codeword_to_value = [0 1 1 2 1 2 2 3 3];
printf("tx_value tx_codeword rx_codeword rx_value distance\n");
for i=1:m
tx_codeword = bin2dec(value_to_codeword(i,:));
tx_codeword_bin = value_to_codeword(i,:);
rx_value = codeword_to_value(tx_codeword+1);
distance = abs((i-1) - rx_value);
printf("%8d %11s %11s %8d %8d\n", i-1, tx_codeword_bin, tx_codeword_bin, ...
rx_value, distance );
end
printf("\n");
for i=1:m
tx_codeword = bin2dec(value_to_codeword(i,:));
tx_codeword_bin = value_to_codeword(i,:);
for j=1:(log2_m+1)
rx_codeword = bitxor(tx_codeword, bitset(0,j));
rx_codeword_bin = dec2bin(rx_codeword, 3);
rx_value = codeword_to_value(rx_codeword+1);
distance = abs((i-1) - rx_value);
printf("%8d %11s %11s %8d %8d\n", i-1, tx_codeword_bin, rx_codeword_bin, ...
rx_value, distance );
end
end
endfunction
% regular natural binary quantiser
function index = quantise_value(value, min_value, max_value, num_levels)
norm = (value - min_value)/(max_value - min_value);
index = floor(num_levels * norm + 0.5);
if (index < 0 )
index = 0;
end
if (index > (num_levels-1))
index = num_levels-1;
end
endfunction
function value = unquantise_value(index, min_value, max_value, num_levels)
step = (max_value - min_value)/num_levels;
value = min_value + step*(index);
endfunction
% converting natural binary to gray
function gray = binary_to_gray(natural)
gray = bitxor(bitshift(natural,-1),natural);
endfunction
function natural = gray_to_binary(gray)
for i=1:length(gray)
mask = bitshift(gray(i),-1);
num = gray(i);
while(mask)
num = bitxor(num, mask);
mask = bitshift(mask,-1);
end
natural(i) = num;
end
endfunction
function sim_out = test_baseline_uncoded(Ebvec, Nbits, Ntrials, enable_error_log, enable_gray)
Nlevels = 2.^ Nbits; powersOfTwo = 2 .^ fliplr(0:(Nbits-1));
Nsymb = Nbits;
sim_out.qnoise_log = zeros(length(Ebvec),Ntrials);
sim_out.error_log = [];
for ne = 1:length(Ebvec)
EbNodB = Ebvec(ne);
EbNo = 10^(EbNodB/10);
variance = 1/EbNo;
Terrs = 0; Tbits = 0;
qsignal = qnoise = 0;
for nn = 1:Ntrials
tx_value = rand(1,1);
tx_index = quantise_value(tx_value, 0, 1, Nlevels);
if enable_gray
tx_index = binary_to_gray(tx_index);
end
tx_bits = dec2bin(tx_index, Nbits) - '0';
tx_symbols = -1 + 2*tx_bits;
% AWGN noise and phase/freq offset channel simulation
% 0.5 factor ensures var(noise) == variance , i.e. splits power between Re & Im
noise = sqrt(variance*0.5)*(randn(1,Nsymb) + j*randn(1,Nsymb));
rx_symbols = tx_symbols + noise;
rx_bits = rx_symbols > 0;
error_positions = xor(rx_bits, tx_bits);
Nerrs = sum(error_positions);
Terrs += Nerrs;
Tbits += length(tx_bits);
if enable_error_log
sim_out.error_log = [sim_out.error_log error_positions];
end
rx_index = (powersOfTwo * rx_bits');
if enable_gray
rx_index = gray_to_binary(rx_index);
end
rx_value = unquantise_value(rx_index, 0, 1, Nlevels);
qsignal += tx_value*tx_value;
qnoise += (tx_value - rx_value) .^ 2;
sim_out.qnoise_log(ne,nn) = tx_value - rx_value;
end
sim_out.TERvec(ne) = Terrs;
sim_out.BERvec(ne) = Terrs/Tbits;
sim_out.QSNRvec(ne) = 10*log10(qsignal/qnoise);
printf("EbNo (dB): %3.2f Terrs: %6d BER %1.4f QSNR (dB): %3.2f\n",
EbNodB, Terrs, Terrs/Tbits, 10*log10(qsignal/qnoise));
end
endfunction
function sim_out = test_varpower(Ebvec, Nbits, Ntrials, amps, enable_error_log)
Nlevels = 2.^ Nbits; powersOfTwo = 2 .^ fliplr(0:(Nbits-1));
Nsymb = Nbits;
sim_out.qnoise_log = zeros(length(Ebvec), Ntrials);
sim_out.error_log = [];
for ne = 1:length(Ebvec)
EbNodB = Ebvec(ne);
EbNo = 10^(EbNodB/10);
variance = 1/EbNo;
Terrs = 0; Tbits = 0;
qsignal = qnoise = 0;
for nn = 1:Ntrials
tx_value = rand(1,1);
tx_index = quantise_value(tx_value, 0, 1, Nlevels);
tx_bits = dec2bin(tx_index, Nbits) - '0';
tx_symbols = (-1 + 2*tx_bits) .* amps;
% AWGN noise and phase/freq offset channel simulation
% 0.5 factor ensures var(noise) == variance , i.e. splits power between Re & Im
noise = sqrt(variance*0.5)*(randn(1,Nsymb) + j*randn(1,Nsymb));
rx_symbols = tx_symbols + noise;
rx_bits = rx_symbols > 0;
error_positions = xor(rx_bits, tx_bits);
if enable_error_log
sim_out.error_log = [sim_out.error_log error_positions];
end
Nerrs = sum(error_positions);
Terrs += Nerrs;
Tbits += length(tx_bits);
rx_index = (powersOfTwo * rx_bits');
rx_value = unquantise_value(rx_index, 0, 1, Nlevels);
qsignal += tx_value*tx_value;
qnoise += (tx_value - rx_value) .^ 2;
sim_out.qnoise_log(ne,nn) = tx_value - rx_value;
end
sim_out.TERvec(ne) = Terrs;
sim_out.BERvec(ne) = Terrs/Tbits;
sim_out.QSNRvec(ne) = 10*log10(qsignal/qnoise);
printf("EbNo (dB): %3.2f Terrs: %6d BER %1.4f QSNR (dB): %3.2f\n",
EbNodB, Terrs, Terrs/Tbits, 10*log10(qsignal/qnoise));
end
endfunction
% gray codes with specified number of data and parity bits. Soft
% decision decoding. Didn't really work out.
function valid_codewords = fuzzy_code_create(ndata,nparity)
Nbits = ndata + nparity;
Nvalid = 2 .^ ndata;
codewords = binary_to_gray(0:(2 .^ Nbits)-1);
valid_codewords = dec2bin(codewords(1:2:(2 .^ Nbits)), Nbits) - '0';
% check all valid codewords have a hamming distance of at least 2^nparity
bad_distance = 0;
for i=1:Nvalid
for k=i+1:Nvalid
distance = sum(bitxor(valid_codewords(i,:), valid_codewords(k,:)));
if distance < 2
bad_distance++;
end
end
end
if bad_distance != 0
printf("Error: Nvalid: %d bad_distance: %d\n", Nvalid, bad_distance);
return;
end
endfunction
function tx_codeword = fuzzy_code_encode(codewords, value)
tx_codeword = codewords(value+1,:);
endfunction
function [value, best_codeword] = fuzzy_code_decode(codewords, rx_symbols)
max_corr = 0;
value = 0;
[rows,cols] = size(codewords);
for i=1:rows
corr = (-1 + 2*codewords(i,:)) * transpose(rx_symbols);
if (corr > max_corr)
max_corr = corr;
value = i-1;
best_codeword = codewords(i,:);
end
end
endfunction
function sim_out = test_fuzzy_code(Ebvec, Ndata, Nparity, Ntrials)
Nbits = Ndata + Nparity;
Nlevels = 2 .^ Ndata;
Nsymb = Nbits;
powersOfTwo = 2 .^ fliplr(0:(Nbits-1));
codewords = fuzzy_code_create(Ndata,Nparity);
sim_out.qnoise_log = zeros(length(Ebvec), Ntrials);
for ne = 1:length(Ebvec)
EbNodB = Ebvec(ne);
EbNo = 10^(EbNodB/10);
variance = 1/EbNo;
Terrs = 0; Terrs_coded = 0; Tbits = 0;
Nsingle = Nsingle_corrected = 0;
qsignal = qnoise = 0;
for nn = 1:Ntrials
tx_value = rand(1,1);
tx_index = quantise_value(tx_value, 0, 1, Nlevels);
tx_codeword = fuzzy_code_encode(codewords, tx_index);
tx_symbols = -1 + 2*tx_codeword;
% AWGN noise and phase/freq offset channel simulation
% 0.5 factor ensures var(noise) == variance , i.e. splits power between Re & Im
noise = sqrt(variance*0.5)*(randn(1,Nsymb) + j*randn(1,Nsymb));
rx_symbols = tx_symbols + noise;
% uncoded BER
rx_bits = rx_symbols > 0;
error_positions = xor(rx_bits(1:Ndata), tx_codeword(1:Ndata));
Nerrs = sum(error_positions);
Terrs += Nerrs;
Tbits += Ndata;
% decode and determine QSNR
[rx_index, rx_codeword] = fuzzy_code_decode(codewords, rx_symbols);
rx_value = unquantise_value(rx_index, 0, 1, Nlevels);
qsignal += tx_value*tx_value;
qnoise += (tx_value - rx_value) .^ 2;
sim_out.qnoise_log(ne,nn) = tx_value - rx_value;
% coded BER
error_positions = xor(rx_codeword(1:Ndata), tx_codeword(1:Ndata));
Nerrs_coded = sum(error_positions);
if Nerrs == 1
Nsingle++;
if Nerrs_coded == 0
Nsingle_corrected++;
end
end
Terrs_coded += Nerrs_coded;
end
sim_out.BERvec(ne) = Terrs/Tbits;
sim_out.BERvec_coded(ne) = Terrs_coded/Tbits;
sim_out.Nsingle(ne) = Nsingle;
sim_out.Nsingle_corrected(ne) = Nsingle_corrected;
sim_out.QSNRvec(ne) = 10*log10(qsignal/qnoise);
printf("EbNo (dB): %3.2f Terrs: %6d BER %1.4f Terrs_coded: %6d BER_coded %1.4f QSNR (dB): %3.2f",
EbNodB, Terrs, Terrs/Tbits, Terrs_coded, Terrs_coded/Tbits, sim_out.QSNRvec(ne));
printf(" Nsingle: %d Nsingle_corrected: %d corrected: %3.1f\n", Nsingle, Nsingle_corrected, Nsingle_corrected*100/Nsingle);
end
endfunction
function compare_baseline_fuzzy
Ebvec = 0:3;
Ntrials = 5000;
Nbits = 4; Nparity = 1;
baseline = test_baseline_uncoded(Ebvec, Nbits, Ntrials, 0, 0);
fuzzy = test_fuzzy_code(Ebvec, Nbits, Nparity, Ntrials);
figure(1);
clf;
semilogy(Ebvec, baseline.BERvec)
xlabel('Eb/N0')
ylabel('BER')
grid("minor")
figure(2);
clf;
plot(Ebvec, baseline.QSNRvec,'b;baseline;')
hold on;
plot(Ebvec, fuzzy.QSNRvec,'r;fuzzy;')
hold off;
xlabel('Eb/N0')
ylabel('SNR')
grid("minor")
figure(3);
subplot(211)
hist(baseline.qnoise_log(4,:),50);
subplot(212)
hist(fuzzy.qnoise_log(4,:),50);
figure(4)
subplot(211)
plot(baseline.qnoise_log(4,1:250),'b;baseline;')
subplot(212)
plot(fuzzy.qnoise_log(4,1:250),'r;fuzzy;')
endfunction
% compare baseline and variable power schemes and make plots
function compare_baseline_varpower_plot
Ebvec = -2:5;
Ntrials = 5000;
Nbits = 5;
baseline = test_baseline_uncoded(Ebvec, Nbits, Ntrials, 0, 0);
amps = [2 1.5 1.0 0.5 0.5];
av_pwr = (amps*amps')/length(amps);
amps_norm = amps/sqrt(av_pwr);
varpower = test_varpower(Ebvec, Nbits, Ntrials, amps_norm, 0);
figure(1);
clf;
semilogy(Ebvec, baseline.BERvec)
xlabel('Eb/No (dB)')
ylabel('BER')
grid("minor")
title('BER versus Eb/No')
figure(2);
clf;
plot(Ebvec, baseline.QSNRvec,'b;baseline;')
hold on;
plot(Ebvec, varpower.QSNRvec,'r;varpower;')
hold off;
xlabel('Eb/No (dB)')
ylabel('SNR (dB)')
grid("minor")
title('Quantiser SNR versus Eb/No')
figure(3);
subplot(211)
hist(baseline.qnoise_log(1,:),50);
title('Baseline and Variable Power Error Histograms')
subplot(212)
hist(varpower.qnoise_log(1,:),50);
figure(4)
subplot(211)
plot(baseline.qnoise_log(1,1:250),'b;baseline;')
title('Baseline and Variable Power Error plots for Eb/No = -2dB')
subplot(212)
plot(varpower.qnoise_log(1,1:250),'r;varpower;')
endfunction
% Compare baseline and variable power schemes and make error pattern
% files for inserting into codec bit stream so we can listen to
% result.
function compare_baseline_varpower_error_files
Ebvec = -2;
Fs = 25; % number of samples per second
Nsec = 15; % seconds to simulate
Ntrials = Fs*Nsec;
Nbits = 5;
bits_per_frame = 52;
bits_per_frame_rounded = ceil(bits_per_frame/8)*8; % c2enc uses integer number of bytes/frame
start_bit = 12; % first energy bit (after 4 voicing, 7 Wo bits)
baseline = test_baseline_uncoded(Ebvec, Nbits, Ntrials, 1, 0);
amps = [2 1.5 1.0 0.5 0.5];
av_pwr = (amps*amps')/length(amps);
amps_norm = amps/sqrt(av_pwr);
varpower = test_varpower(Ebvec, Nbits, Ntrials, amps_norm, 1);
% construct error patterns to apply to c2enc bit stream
baseline_errors = [];
for i=1:Ntrials
error_positions = baseline.error_log(Nbits*(i-1)+1:Nbits*i);
baseline_errors = [baseline_errors zeros(1,start_bit-1) error_positions ...
zeros(1, bits_per_frame_rounded - Nbits - (start_bit-1))];
end
varpower_errors = [];
for i=1:Ntrials
error_positions = varpower.error_log(Nbits*(i-1)+1:Nbits*i);
varpower_errors = [varpower_errors zeros(1,start_bit-1) error_positions ...
zeros(1, bits_per_frame_rounded - Nbits - (start_bit-1))];
end
% save error patterns
fep=fopen("energy_errors_baseline.bin","wb"); fwrite(fep, baseline_errors, "short"); fclose(fep);
fep=fopen("energy_errors_varpower.bin","wb"); fwrite(fep, varpower_errors, "short"); fclose(fep);
endfunction
% compare natural and gray coding and make plots
function compare_natural_gray_plot
Ebvec = -2:10;
Ntrials = 5000;
Nbits = 7;
natural = test_baseline_uncoded(Ebvec, Nbits, Ntrials, 0, 0);
gray = test_baseline_uncoded(Ebvec, Nbits, Ntrials, 0, 1);
figure(1);
clf;
semilogy(Ebvec, natural.BERvec)
xlabel('Eb/No (dB)')
ylabel('BER')
grid("minor")
title('BER versus Eb/No')
figure(2);
clf;
plot(Ebvec, natural.QSNRvec,'b;natural;')
hold on;
plot(Ebvec, gray.QSNRvec,'r;gray;')
hold off;
xlabel('Eb/No (dB)')
ylabel('SNR (dB)')
grid("minor")
title('Quantiser SNR versus Eb/No')
figure(3);
subplot(211)
hist(natural.qnoise_log(1,:),50);
title('Natural and Gray coded Error Histograms')
subplot(212)
hist(gray.qnoise_log(1,:),50);
figure(4)
subplot(211)
plot(natural.qnoise_log(1,1:250),'b;natural;')
axis([0 250 -1 1])
title('Natural and Gray coded Error plots for Eb/No = -2dB')
subplot(212)
plot(gray.qnoise_log(1,1:250),'r;gray;')
axis([0 250 -1 1])
endfunction
% compare natural at different Eb/No and Nbitsmake plots
function compare_natural_nbit_plot
Ebvec = -2:10;
Ntrials = 5000;
figure(1);
clf;
for n = 2:7
natural = test_baseline_uncoded(Ebvec, n, Ntrials, 0, 0);
plot(Ebvec, natural.QSNRvec)
if n == 2
hold on;
end
end
hold off;
xlabel('Eb/No (dB)')
ylabel('SNR (dB)')
grid("minor")
title('Quantiser SNR versus Eb/No')
endfunction
function generate_varpower_error_files(EbNo, start_bit, end_bit, amps, error_file_name)
Fs = 25; % number of samples per second
Nsec = 3; % seconds to simulate
Ntrials = Fs*Nsec;
Nbits = end_bit - start_bit + 1;
bits_per_frame = 52;
bits_per_frame_rounded = ceil(bits_per_frame/8)*8; % c2enc uses integer number of bytes/frame
% first energy bit (after 4 voicing, 7 Wo bits)
% normalise powers and run test
av_pwr = (amps*amps')/length(amps);
amps_norm = amps/sqrt(av_pwr);
av_pwr2 = (amps_norm*amps_norm')/length(amps_norm)
varpower = test_varpower(EbNo, Nbits, Ntrials, amps_norm, 1);
% construct error patterns to apply to c2enc bit stream
varpower_errors = [];
for i=1:Ntrials
error_positions = varpower.error_log(Nbits*(i-1)+1:Nbits*i);
if 0
% reset single errors to tes effect of ideal single bit error correcting code
for i=1:7
st = 4*(i-1)+1
en = 4*i
if sum(error_positions(st:en)) == 1
error_positions(st:en) = 0;
end
end
for i=1:2
st = 7*4+3*(i-1)+1
en = 7*4+3*i
if sum(error_positions(st:en)) == 1
error_positions(st:en) = 0;
end
end
st = 7*4+3*2+1
en = 7*4+3*2+2
if sum(error_positions(st:en)) == 1
error_positions(st:en) = 0;
end
end
num_errors(i) = sum(error_positions);
varpower_errors = [varpower_errors zeros(1,start_bit-1) error_positions ...
zeros(1, bits_per_frame_rounded - Nbits - (start_bit-1))];
end
% save error pattern to file
fep=fopen(error_file_name,"wb"); fwrite(fep, varpower_errors, "short"); fclose(fep);
figure(1)
clf
hist(num_errors)
endfunction
more off;
%generate_varpower_error_files(0, 17, 52, ones(1,36), "lsp_baseline_errors_0dB.bin")
%amps = [1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 ];
%generate_varpower_error_files(0, 17, 52, amps, "lsp_varpower_errors_0dB.bin")
%compare_natural_nbit_plot
%compare_natural_gray_plot
%compare_baseline_varpower_plot
%compare_baseline_varpower_error_files
compare_baseline_fuzzy
%fuzzy_code_create(3,1)