1 /* 2 * Copyright (C) 2016 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define BTSTACK_FILE__ "btstack_cvsd_plc.c" 39 40 /* 41 * btstack_CVSD_plc.c 42 * 43 */ 44 45 #include <stdint.h> 46 #include <stdlib.h> 47 #include <string.h> 48 49 #ifdef OCTAVE_OUTPUT 50 #include <stdio.h> 51 #endif 52 53 #include "btstack_cvsd_plc.h" 54 #include "btstack_debug.h" 55 56 // static float rcos[CVSD_OLAL] = { 57 // 0.99148655f,0.96623611f,0.92510857f,0.86950446f, 58 // 0.80131732f,0.72286918f,0.63683150f,0.54613418f, 59 // 0.45386582f,0.36316850f,0.27713082f,0.19868268f, 60 // 0.13049554f,0.07489143f,0.03376389f,0.00851345f}; 61 62 static float rcos[CVSD_OLAL] = { 63 0.99148655f,0.92510857f, 64 0.80131732f,0.63683150f, 65 0.45386582f,0.27713082f, 66 0.13049554f,0.03376389f}; 67 68 float btstack_cvsd_plc_rcos(int index){ 69 if (index > CVSD_OLAL) return 0; 70 return rcos[index]; 71 } 72 // taken from http://www.codeproject.com/Articles/69941/Best-Square-Root-Method-Algorithm-Function-Precisi 73 // Algorithm: Babylonian Method + some manipulations on IEEE 32 bit floating point representation 74 static float sqrt3(const float x){ 75 union { 76 int i; 77 float x; 78 } u; 79 u.x = x; 80 u.i = (1<<29) + (u.i >> 1) - (1<<22); 81 82 // Two Babylonian Steps (simplified from:) 83 // u.x = 0.5f * (u.x + x/u.x); 84 // u.x = 0.5f * (u.x + x/u.x); 85 u.x = u.x + (x/u.x); 86 u.x = (0.25f*u.x) + (x/u.x); 87 88 return u.x; 89 } 90 91 static float btstack_cvsd_plc_absolute(float x){ 92 if (x < 0) x = -x; 93 return x; 94 } 95 96 static float btstack_cvsd_plc_cross_correlation(BTSTACK_CVSD_PLC_SAMPLE_FORMAT *x, BTSTACK_CVSD_PLC_SAMPLE_FORMAT *y){ 97 float num = 0; 98 float den = 0; 99 float x2 = 0; 100 float y2 = 0; 101 int m; 102 for (m=0;m<CVSD_M;m++){ 103 num+=((float)x[m])*y[m]; 104 x2+=((float)x[m])*x[m]; 105 y2+=((float)y[m])*y[m]; 106 } 107 den = (float)sqrt3(x2*y2); 108 return num/den; 109 } 110 111 int btstack_cvsd_plc_pattern_match(BTSTACK_CVSD_PLC_SAMPLE_FORMAT *y){ 112 float maxCn = -999999.0; // large negative number 113 int bestmatch = 0; 114 float Cn; 115 int n; 116 for (n=0;n<CVSD_N;n++){ 117 Cn = btstack_cvsd_plc_cross_correlation(&y[CVSD_LHIST-CVSD_M], &y[n]); 118 if (Cn>maxCn){ 119 bestmatch=n; 120 maxCn = Cn; 121 } 122 } 123 return bestmatch; 124 } 125 126 float btstack_cvsd_plc_amplitude_match(btstack_cvsd_plc_state_t *plc_state, uint16_t num_samples, BTSTACK_CVSD_PLC_SAMPLE_FORMAT *y, BTSTACK_CVSD_PLC_SAMPLE_FORMAT bestmatch){ 127 UNUSED(plc_state); 128 int i; 129 float sumx = 0; 130 float sumy = 0.000001f; 131 float sf; 132 133 for (i=0;i<num_samples;i++){ 134 sumx += btstack_cvsd_plc_absolute(y[CVSD_LHIST-num_samples+i]); 135 sumy += btstack_cvsd_plc_absolute(y[bestmatch+i]); 136 } 137 sf = sumx/sumy; 138 // This is not in the paper, but limit the scaling factor to something reasonable to avoid creating artifacts 139 if (sf<0.75f) sf=0.75f; 140 if (sf>1.0) sf=1.0f; 141 return sf; 142 } 143 144 BTSTACK_CVSD_PLC_SAMPLE_FORMAT btstack_cvsd_plc_crop_sample(float val){ 145 float croped_val = val; 146 if (croped_val > 32767.0) croped_val= 32767.0; 147 if (croped_val < -32768.0) croped_val=-32768.0; 148 return (BTSTACK_CVSD_PLC_SAMPLE_FORMAT) croped_val; 149 } 150 151 void btstack_cvsd_plc_init(btstack_cvsd_plc_state_t *plc_state){ 152 memset(plc_state, 0, sizeof(btstack_cvsd_plc_state_t)); 153 } 154 155 #ifdef OCTAVE_OUTPUT 156 typedef enum { 157 OCTAVE_FRAME_TYPE_UNKNOWN = 0, 158 OCTAVE_FRAME_TYPE_GOOD, 159 OCTAVE_FRAME_TYPE_BAD 160 } octave_frame_type_t; 161 162 static const char * octave_frame_type_name[] = { 163 "unknown", 164 "good", 165 "bad" 166 }; 167 168 static octave_frame_type_t octave_frame_type; 169 static char octave_base_name[1000]; 170 171 const char * octave_frame_type2str(int index){ 172 if (index <= 0 || index >= sizeof(octave_frame_type_t)) return octave_frame_type_name[0]; 173 return octave_frame_type_name[index]; 174 } 175 176 void btstack_cvsd_plc_octave_set_base_name(const char * base_name){ 177 strcpy(octave_base_name, base_name); 178 printf("OCTAVE: base name set to %s\n", octave_base_name); 179 } 180 181 static void octave_fprintf_array_int16(FILE * oct_file, char * name, int data_len, int16_t * data){ 182 fprintf(oct_file, "%s = [", name); 183 int i; 184 for (i = 0; i < data_len - 1; i++){ 185 fprintf(oct_file, "%d, ", data[i]); 186 } 187 fprintf(oct_file, "%d", data[i]); 188 fprintf(oct_file, "%s", "];\n"); 189 } 190 191 static FILE * open_octave_file(btstack_cvsd_plc_state_t *plc_state, octave_frame_type_t frame_type){ 192 char oct_file_name[1200]; 193 octave_frame_type = frame_type; 194 snprintf(oct_file_name, sizeof(oct_file_name), "%s_octave_plc_%d_%s.m", 195 octave_base_name, plc_state->frame_count, 196 octave_frame_type2str(octave_frame_type)); 197 oct_file_name[sizeof(oct_file_name) - 1] = 0; 198 199 FILE * oct_file = fopen(oct_file_name, "wb"); 200 if (oct_file == NULL){ 201 printf("OCTAVE: could not open file %s\n", oct_file_name); 202 return NULL; 203 } 204 printf("OCTAVE: opened file %s\n", oct_file_name); 205 return oct_file; 206 } 207 208 static void octave_fprintf_plot_history_frame(btstack_cvsd_plc_state_t *plc_state, FILE * oct_file, int frame_nr){ 209 char title[100]; 210 char hist_name[10]; 211 snprintf(hist_name, sizeof(hist_name), "hist%d", plc_state->nbf); 212 hist_name[sizeof(hist_name) - 1] = 0; 213 214 octave_fprintf_array_int16(oct_file, hist_name, CVSD_LHIST, plc_state->hist); 215 216 fprintf(oct_file, "y = [min(%s):1000:max(%s)];\n", hist_name, hist_name); 217 fprintf(oct_file, "x = zeros(1, size(y,2));\n"); 218 fprintf(oct_file, "b = [0: %d];\n", CVSD_LHIST+CVSD_FS+CVSD_RT+CVSD_OLAL); 219 220 int pos = CVSD_FS; 221 fprintf(oct_file, "shift_x = x + %d;\n", pos); 222 223 pos = CVSD_LHIST - 1; 224 fprintf(oct_file, "lhist_x = x + %d;\n", pos); 225 pos += CVSD_OLAL; 226 fprintf(oct_file, "lhist_olal1_x = x + %d;\n", pos); 227 pos += CVSD_FS - CVSD_OLAL; 228 fprintf(oct_file, "lhist_fs_x = x + %d;\n", pos); 229 pos += CVSD_OLAL; 230 fprintf(oct_file, "lhist_olal2_x = x + %d;\n", pos); 231 pos += CVSD_RT; 232 fprintf(oct_file, "lhist_rt_x = x + %d;\n", pos); 233 234 fprintf(oct_file, "pattern_window_x = x + %d;\n", CVSD_LHIST - CVSD_M); 235 236 fprintf(oct_file, "hf = figure();\n"); 237 snprintf(title, sizeof(title), "PLC %s frame %d", 238 octave_frame_type2str(octave_frame_type), frame_nr); 239 title[sizeof(title) - 1] = 0; 240 241 fprintf(oct_file, "hold on;\n"); 242 fprintf(oct_file, "h1 = plot(%s); \n", hist_name); 243 244 fprintf(oct_file, "title(\"%s\");\n", title); 245 246 fprintf(oct_file, "plot(lhist_x, y, 'k'); \n"); 247 fprintf(oct_file, "text(max(lhist_x) - 10, max(y)+1000, 'lhist'); \n"); 248 249 fprintf(oct_file, "plot(lhist_olal1_x, y, 'k'); \n"); 250 fprintf(oct_file, "text(max(lhist_olal1_x) - 10, max(y)+1000, 'OLAL'); \n"); 251 252 fprintf(oct_file, "plot(lhist_fs_x, y, 'k'); \n"); 253 fprintf(oct_file, "text(max(lhist_fs_x) - 10, max(y)+1000, 'FS'); \n"); 254 255 fprintf(oct_file, "plot(lhist_olal2_x, y, 'k'); \n"); 256 fprintf(oct_file, "text(max(lhist_olal2_x) - 10, max(y)+1000, 'OLAL'); \n"); 257 258 fprintf(oct_file, "plot(lhist_rt_x, y, 'k');\n"); 259 fprintf(oct_file, "text(max(lhist_rt_x) - 10, max(y)+1000, 'RT'); \n"); 260 261 if (octave_frame_type == OCTAVE_FRAME_TYPE_GOOD) return; 262 263 int x0 = plc_state->bestlag; 264 int x1 = plc_state->bestlag + CVSD_M - 1; 265 fprintf(oct_file, "plot(b(%d:%d), %s(%d:%d), 'rd'); \n", x0, x1, hist_name, x0, x1); 266 fprintf(oct_file, "text(%d - 10, -10, 'bestlag'); \n", x0); 267 268 x0 = plc_state->bestlag + CVSD_M ; 269 x1 = plc_state->bestlag + CVSD_M + CVSD_FS - 1; 270 fprintf(oct_file, "plot(b(%d:%d), %s(%d:%d), 'kd'); \n", x0, x1, hist_name, x0, x1); 271 272 x0 = CVSD_LHIST - CVSD_M; 273 x1 = CVSD_LHIST - 1; 274 fprintf(oct_file, "plot(b(%d:%d), %s(%d:%d), 'rd'); \n", x0, x1, hist_name, x0, x1); 275 fprintf(oct_file, "plot(pattern_window_x, y, 'g'); \n"); 276 fprintf(oct_file, "text(max(pattern_window_x) - 10, max(y)+1000, 'M'); \n"); 277 } 278 279 static void octave_fprintf_plot_output(btstack_cvsd_plc_state_t *plc_state, FILE * oct_file){ 280 if (!oct_file) return; 281 char out_name[10]; 282 snprintf(out_name, sizeof(out_name), "out%d", plc_state->nbf); 283 out_name[sizeof(out_name) - 1] = 0; 284 int x0 = CVSD_LHIST; 285 int x1 = x0 + CVSD_FS - 1; 286 octave_fprintf_array_int16(oct_file, out_name, CVSD_FS, plc_state->hist+x0); 287 fprintf(oct_file, "h2 = plot(b(%d:%d), %s, 'cd'); \n", x0, x1, out_name); 288 289 char rest_hist_name[10]; 290 snprintf(rest_hist_name, sizeof(rest_hist_name), "rest%d", plc_state->nbf); 291 rest_hist_name[sizeof(rest_hist_name) - 1] = 0; 292 x0 = CVSD_LHIST + CVSD_FS; 293 x1 = x0 + CVSD_OLAL + CVSD_RT - 1; 294 octave_fprintf_array_int16(oct_file, rest_hist_name, CVSD_OLAL + CVSD_RT, plc_state->hist+x0); 295 fprintf(oct_file, "h3 = plot(b(%d:%d), %s, 'kd'); \n", x0, x1, rest_hist_name); 296 297 char new_hist_name[10]; 298 snprintf(new_hist_name, sizeof(new_hist_name), "hist%d", plc_state->nbf); 299 new_hist_name[sizeof(new_hist_name) - 1] = 0; 300 octave_fprintf_array_int16(oct_file, new_hist_name, CVSD_LHIST, plc_state->hist); 301 fprintf(oct_file, "h4 = plot(%s, 'r--'); \n", new_hist_name); 302 303 fprintf(oct_file, "legend ([h1, h2, h3, h4], {\"hist\", \"out\", \"rest\", \"new hist\"}, \"location\", \"northeast\");\n "); 304 305 char fig_name[1200]; 306 snprintf(fig_name, sizeof(fig_name), "../%s_octave_plc_%d_%s", 307 octave_base_name, plc_state->frame_count, 308 octave_frame_type2str(octave_frame_type)); 309 fig_name[sizeof(fig_name) - 1] = 0; 310 fprintf(oct_file, "print(hf, \"%s.jpg\", \"-djpg\");", fig_name); 311 } 312 #endif 313 314 void btstack_cvsd_plc_bad_frame(btstack_cvsd_plc_state_t *plc_state, uint16_t num_samples, BTSTACK_CVSD_PLC_SAMPLE_FORMAT *out){ 315 float val; 316 int i; 317 float sf = 1; 318 plc_state->nbf++; 319 320 if (plc_state->max_consecutive_bad_frames_nr < plc_state->nbf){ 321 plc_state->max_consecutive_bad_frames_nr = plc_state->nbf; 322 } 323 if (plc_state->nbf==1){ 324 // printf("first bad frame\n"); 325 // Perform pattern matching to find where to replicate 326 plc_state->bestlag = btstack_cvsd_plc_pattern_match(plc_state->hist); 327 } 328 329 #ifdef OCTAVE_OUTPUT 330 FILE * oct_file = open_octave_file(plc_state, OCTAVE_FRAME_TYPE_BAD); 331 if (oct_file){ 332 octave_fprintf_plot_history_frame(plc_state, oct_file, plc_state->frame_count); 333 } 334 #endif 335 336 if (plc_state->nbf==1){ 337 // the replication begins after the template match 338 plc_state->bestlag += CVSD_M; 339 340 // Compute Scale Factor to Match Amplitude of Substitution Packet to that of Preceding Packet 341 sf = btstack_cvsd_plc_amplitude_match(plc_state, num_samples, plc_state->hist, plc_state->bestlag); 342 for (i=0; i<CVSD_OLAL; i++){ 343 val = sf*plc_state->hist[plc_state->bestlag+i]; 344 plc_state->hist[CVSD_LHIST+i] = btstack_cvsd_plc_crop_sample(val); 345 } 346 347 for (i=CVSD_OLAL; i<num_samples; i++){ 348 val = sf*plc_state->hist[plc_state->bestlag+i]; 349 plc_state->hist[CVSD_LHIST+i] = btstack_cvsd_plc_crop_sample(val); 350 } 351 352 for (i=num_samples; i<(num_samples+CVSD_OLAL); i++){ 353 float left = sf*plc_state->hist[plc_state->bestlag+i]; 354 float right = plc_state->hist[plc_state->bestlag+i]; 355 val = (left*rcos[i-num_samples]) + (right*rcos[CVSD_OLAL-1-i+num_samples]); 356 plc_state->hist[CVSD_LHIST+i] = btstack_cvsd_plc_crop_sample(val); 357 } 358 359 for (i=(num_samples+CVSD_OLAL); i<(num_samples+CVSD_RT+CVSD_OLAL); i++){ 360 plc_state->hist[CVSD_LHIST+i] = plc_state->hist[plc_state->bestlag+i]; 361 } 362 } else { 363 for (i=0; i<(num_samples+CVSD_RT+CVSD_OLAL); i++){ 364 plc_state->hist[CVSD_LHIST+i] = plc_state->hist[plc_state->bestlag+i]; 365 } 366 } 367 368 for (i=0; i<num_samples; i++){ 369 out[i] = plc_state->hist[CVSD_LHIST+i]; 370 } 371 372 // shift the history buffer 373 for (i=0; i<(CVSD_LHIST+CVSD_RT+CVSD_OLAL); i++){ 374 plc_state->hist[i] = plc_state->hist[i+num_samples]; 375 } 376 377 #ifdef OCTAVE_OUTPUT 378 if (oct_file){ 379 octave_fprintf_plot_output(plc_state, oct_file); 380 fclose(oct_file); 381 } 382 #endif 383 } 384 385 void btstack_cvsd_plc_good_frame(btstack_cvsd_plc_state_t *plc_state, uint16_t num_samples, BTSTACK_CVSD_PLC_SAMPLE_FORMAT *in, BTSTACK_CVSD_PLC_SAMPLE_FORMAT *out){ 386 float val; 387 int i = 0; 388 #ifdef OCTAVE_OUTPUT 389 FILE * oct_file = NULL; 390 if (plc_state->nbf>0){ 391 oct_file = open_octave_file(plc_state, OCTAVE_FRAME_TYPE_GOOD); 392 if (oct_file){ 393 octave_fprintf_plot_history_frame(plc_state, oct_file, plc_state->frame_count); 394 } 395 } 396 #endif 397 if (plc_state->nbf>0){ 398 for (i=0;i<CVSD_RT;i++){ 399 out[i] = plc_state->hist[CVSD_LHIST+i]; 400 } 401 402 for (i=CVSD_RT;i<(CVSD_RT+CVSD_OLAL);i++){ 403 float left = plc_state->hist[CVSD_LHIST+i]; 404 float right = in[i]; 405 val = (left * rcos[i-CVSD_RT]) + (right *rcos[CVSD_OLAL+CVSD_RT-1-i]); 406 out[i] = btstack_cvsd_plc_crop_sample((BTSTACK_CVSD_PLC_SAMPLE_FORMAT)val); 407 } 408 } 409 410 for (;i<num_samples;i++){ 411 out[i] = in[i]; 412 } 413 // Copy the output to the history buffer 414 for (i=0;i<num_samples;i++){ 415 plc_state->hist[CVSD_LHIST+i] = out[i]; 416 } 417 // shift the history buffer 418 for (i=0;i<CVSD_LHIST;i++){ 419 plc_state->hist[i] = plc_state->hist[i+num_samples]; 420 } 421 422 #ifdef OCTAVE_OUTPUT 423 if (oct_file){ 424 octave_fprintf_plot_output(plc_state, oct_file); 425 fclose(oct_file); 426 } 427 #endif 428 plc_state->nbf=0; 429 } 430 431 static int count_equal_samples(BTSTACK_CVSD_PLC_SAMPLE_FORMAT * packet, uint16_t size){ 432 int count = 0; 433 int temp_count = 1; 434 int i; 435 for (i = 0; i < (size-1); i++){ 436 if (packet[i] == packet[i+1]){ 437 temp_count++; 438 continue; 439 } 440 if (count < temp_count){ 441 count = temp_count; 442 } 443 temp_count = 1; 444 } 445 if (temp_count > (count + 1)){ 446 count = temp_count; 447 } 448 return count; 449 } 450 451 static int count_zeros(BTSTACK_CVSD_PLC_SAMPLE_FORMAT * frame, uint16_t size){ 452 int nr_zeros = 0; 453 int i; 454 for (i = 0; i < (size-1); i++){ 455 if (frame[i] == 0){ 456 nr_zeros++; 457 } 458 } 459 return nr_zeros; 460 } 461 462 static int zero_frame(BTSTACK_CVSD_PLC_SAMPLE_FORMAT * frame, uint16_t size){ 463 return count_zeros(frame, size) == size; 464 } 465 466 // more than half the samples are the same -> bad frame 467 static int bad_frame(btstack_cvsd_plc_state_t *plc_state, BTSTACK_CVSD_PLC_SAMPLE_FORMAT * frame, uint16_t size){ 468 UNUSED(plc_state); 469 return count_equal_samples(frame, size) >= (size / 2); 470 } 471 472 473 void btstack_cvsd_plc_process_data(btstack_cvsd_plc_state_t * plc_state, bool is_bad_frame, BTSTACK_CVSD_PLC_SAMPLE_FORMAT * in, uint16_t num_samples, BTSTACK_CVSD_PLC_SAMPLE_FORMAT * out){ 474 if (num_samples == 0) return; 475 476 plc_state->frame_count++; 477 478 if (!is_bad_frame) { 479 bool is_zero_frame = zero_frame(in, num_samples); 480 if (is_zero_frame){ 481 plc_state->zero_frames_nr++; 482 } else { 483 is_bad_frame = bad_frame(plc_state, in, num_samples); 484 } 485 } 486 487 if (is_bad_frame){ 488 (void)memcpy(out, in, num_samples * 2); 489 if (plc_state->good_samples > CVSD_LHIST){ 490 btstack_cvsd_plc_bad_frame(plc_state, num_samples, out); 491 plc_state->bad_frames_nr++; 492 } else { 493 memset(out, 0, num_samples * 2); 494 } 495 } else { 496 btstack_cvsd_plc_good_frame(plc_state, num_samples, in, out); 497 plc_state->good_frames_nr++; 498 if (plc_state->good_frames_nr == 1){ 499 log_info("First good frame at index %d\n", plc_state->frame_count-1); 500 } 501 plc_state->good_samples += num_samples; 502 } 503 } 504 505 void btstack_cvsd_dump_statistics(btstack_cvsd_plc_state_t * state){ 506 log_info("Good frames: %d\n", state->good_frames_nr); 507 log_info("Bad frames: %d\n", state->bad_frames_nr); 508 log_info("Zero frames: %d\n", state->zero_frames_nr); 509 log_info("Max Consecutive bad frames: %d\n", state->max_consecutive_bad_frames_nr); 510 } 511