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.92510857f, 58 0.80131732f,0.63683150f, 59 0.45386582f,0.27713082f, 60 0.13049554f,0.03376389f}; 61 62 // taken from http://www.codeproject.com/Articles/69941/Best-Square-Root-Method-Algorithm-Function-Precisi 63 // Algorithm: Babylonian Method + some manipulations on IEEE 32 bit floating point representation 64 static float sqrt3(const float x){ 65 union { 66 int i; 67 float x; 68 } u; 69 u.x = x; 70 u.i = (1<<29) + (u.i >> 1) - (1<<22); 71 72 // Two Babylonian Steps (simplified from:) 73 // u.x = 0.5f * (u.x + x/u.x); 74 // u.x = 0.5f * (u.x + x/u.x); 75 u.x = u.x + (x/u.x); 76 u.x = (0.25f*u.x) + (x/u.x); 77 78 return u.x; 79 } 80 81 static float btstack_cvsd_plc_absolute(float x){ 82 if (x < 0) x = -x; 83 return x; 84 } 85 86 static float btstack_cvsd_plc_cross_correlation(BTSTACK_CVSD_PLC_SAMPLE_FORMAT *x, BTSTACK_CVSD_PLC_SAMPLE_FORMAT *y){ 87 float num = 0; 88 float den = 0; 89 float x2 = 0; 90 float y2 = 0; 91 int m; 92 for (m=0;m<CVSD_M;m++){ 93 num+=((float)x[m])*y[m]; 94 x2+=((float)x[m])*x[m]; 95 y2+=((float)y[m])*y[m]; 96 } 97 den = (float)sqrt3(x2*y2); 98 return num/den; 99 } 100 101 int btstack_cvsd_plc_pattern_match(BTSTACK_CVSD_PLC_SAMPLE_FORMAT *y){ 102 float maxCn = -999999.0; // large negative number 103 int bestmatch = 0; 104 float Cn; 105 int n; 106 for (n=0;n<CVSD_N;n++){ 107 Cn = btstack_cvsd_plc_cross_correlation(&y[CVSD_LHIST-CVSD_M], &y[n]); 108 if (Cn>maxCn){ 109 bestmatch=n; 110 maxCn = Cn; 111 } 112 } 113 return bestmatch; 114 } 115 116 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){ 117 UNUSED(plc_state); 118 int i; 119 float sumx = 0; 120 float sumy = 0.000001f; 121 float sf; 122 123 for (i=0;i<num_samples;i++){ 124 sumx += btstack_cvsd_plc_absolute(y[CVSD_LHIST-num_samples+i]); 125 sumy += btstack_cvsd_plc_absolute(y[bestmatch+i]); 126 } 127 sf = sumx/sumy; 128 // This is not in the paper, but limit the scaling factor to something reasonable to avoid creating artifacts 129 if (sf<0.75f) sf=0.75f; 130 if (sf>1.0) sf=1.0f; 131 return sf; 132 } 133 134 BTSTACK_CVSD_PLC_SAMPLE_FORMAT btstack_cvsd_plc_crop_sample(float val){ 135 float croped_val = val; 136 if (croped_val > 32767.0) croped_val= 32767.0; 137 if (croped_val < -32768.0) croped_val=-32768.0; 138 return (BTSTACK_CVSD_PLC_SAMPLE_FORMAT) croped_val; 139 } 140 141 void btstack_cvsd_plc_init(btstack_cvsd_plc_state_t *plc_state){ 142 memset(plc_state, 0, sizeof(btstack_cvsd_plc_state_t)); 143 } 144 145 #ifdef OCTAVE_OUTPUT 146 typedef enum { 147 OCTAVE_FRAME_TYPE_UNKNOWN = 0, 148 OCTAVE_FRAME_TYPE_GOOD, 149 OCTAVE_FRAME_TYPE_BAD 150 } octave_frame_type_t; 151 152 static const char * octave_frame_type_name[] = { 153 "unknown", 154 "good", 155 "bad" 156 }; 157 158 static octave_frame_type_t octave_frame_type; 159 static char octave_base_name[1000]; 160 161 const char * octave_frame_type2str(int index){ 162 if (index <= 0 || index >= sizeof(octave_frame_type_t)) return octave_frame_type_name[0]; 163 return octave_frame_type_name[index]; 164 } 165 166 void btstack_cvsd_plc_octave_set_base_name(const char * base_name){ 167 strcpy(octave_base_name, base_name); 168 printf("OCTAVE: base name set to %s\n", octave_base_name); 169 } 170 171 static void octave_fprintf_array_int16(FILE * oct_file, char * name, int data_len, int16_t * data){ 172 fprintf(oct_file, "%s = [", name); 173 int i; 174 for (i = 0; i < data_len - 1; i++){ 175 fprintf(oct_file, "%d, ", data[i]); 176 } 177 fprintf(oct_file, "%d", data[i]); 178 fprintf(oct_file, "%s", "];\n"); 179 } 180 181 static FILE * open_octave_file(btstack_cvsd_plc_state_t *plc_state, octave_frame_type_t frame_type){ 182 char oct_file_name[1200]; 183 octave_frame_type = frame_type; 184 snprintf(oct_file_name, sizeof(oct_file_name), "%s_octave_plc_%d_%s.m", 185 octave_base_name, plc_state->frame_count, 186 octave_frame_type2str(octave_frame_type)); 187 oct_file_name[sizeof(oct_file_name) - 1] = 0; 188 189 FILE * oct_file = fopen(oct_file_name, "wb"); 190 if (oct_file == NULL){ 191 printf("OCTAVE: could not open file %s\n", oct_file_name); 192 return NULL; 193 } 194 printf("OCTAVE: opened file %s\n", oct_file_name); 195 return oct_file; 196 } 197 198 static void octave_fprintf_plot_history_frame(btstack_cvsd_plc_state_t *plc_state, FILE * oct_file, int frame_nr){ 199 char title[100]; 200 char hist_name[10]; 201 snprintf(hist_name, sizeof(hist_name), "hist%d", plc_state->nbf); 202 hist_name[sizeof(hist_name) - 1] = 0; 203 204 octave_fprintf_array_int16(oct_file, hist_name, CVSD_LHIST, plc_state->hist); 205 206 fprintf(oct_file, "y = [min(%s):1000:max(%s)];\n", hist_name, hist_name); 207 fprintf(oct_file, "x = zeros(1, size(y,2));\n"); 208 fprintf(oct_file, "b = [0: %d];\n", CVSD_LHIST+CVSD_FS+CVSD_RT+CVSD_OLAL); 209 210 int pos = CVSD_FS; 211 fprintf(oct_file, "shift_x = x + %d;\n", pos); 212 213 pos = CVSD_LHIST - 1; 214 fprintf(oct_file, "lhist_x = x + %d;\n", pos); 215 pos += CVSD_OLAL; 216 fprintf(oct_file, "lhist_olal1_x = x + %d;\n", pos); 217 pos += CVSD_FS - CVSD_OLAL; 218 fprintf(oct_file, "lhist_fs_x = x + %d;\n", pos); 219 pos += CVSD_OLAL; 220 fprintf(oct_file, "lhist_olal2_x = x + %d;\n", pos); 221 pos += CVSD_RT; 222 fprintf(oct_file, "lhist_rt_x = x + %d;\n", pos); 223 224 fprintf(oct_file, "pattern_window_x = x + %d;\n", CVSD_LHIST - CVSD_M); 225 226 fprintf(oct_file, "hf = figure();\n"); 227 snprintf(title, sizeof(title), "PLC %s frame %d", 228 octave_frame_type2str(octave_frame_type), frame_nr); 229 title[sizeof(title) - 1] = 0; 230 231 fprintf(oct_file, "hold on;\n"); 232 fprintf(oct_file, "h1 = plot(%s); \n", hist_name); 233 234 fprintf(oct_file, "title(\"%s\");\n", title); 235 236 fprintf(oct_file, "plot(lhist_x, y, 'k'); \n"); 237 fprintf(oct_file, "text(max(lhist_x) - 10, max(y)+1000, 'lhist'); \n"); 238 239 fprintf(oct_file, "plot(lhist_olal1_x, y, 'k'); \n"); 240 fprintf(oct_file, "text(max(lhist_olal1_x) - 10, max(y)+1000, 'OLAL'); \n"); 241 242 fprintf(oct_file, "plot(lhist_fs_x, y, 'k'); \n"); 243 fprintf(oct_file, "text(max(lhist_fs_x) - 10, max(y)+1000, 'FS'); \n"); 244 245 fprintf(oct_file, "plot(lhist_olal2_x, y, 'k'); \n"); 246 fprintf(oct_file, "text(max(lhist_olal2_x) - 10, max(y)+1000, 'OLAL'); \n"); 247 248 fprintf(oct_file, "plot(lhist_rt_x, y, 'k');\n"); 249 fprintf(oct_file, "text(max(lhist_rt_x) - 10, max(y)+1000, 'RT'); \n"); 250 251 if (octave_frame_type == OCTAVE_FRAME_TYPE_GOOD) return; 252 253 int x0 = plc_state->bestlag; 254 int x1 = plc_state->bestlag + CVSD_M - 1; 255 fprintf(oct_file, "plot(b(%d:%d), %s(%d:%d), 'rd'); \n", x0, x1, hist_name, x0, x1); 256 fprintf(oct_file, "text(%d - 10, -10, 'bestlag'); \n", x0); 257 258 x0 = plc_state->bestlag + CVSD_M ; 259 x1 = plc_state->bestlag + CVSD_M + CVSD_FS - 1; 260 fprintf(oct_file, "plot(b(%d:%d), %s(%d:%d), 'kd'); \n", x0, x1, hist_name, x0, x1); 261 262 x0 = CVSD_LHIST - CVSD_M; 263 x1 = CVSD_LHIST - 1; 264 fprintf(oct_file, "plot(b(%d:%d), %s(%d:%d), 'rd'); \n", x0, x1, hist_name, x0, x1); 265 fprintf(oct_file, "plot(pattern_window_x, y, 'g'); \n"); 266 fprintf(oct_file, "text(max(pattern_window_x) - 10, max(y)+1000, 'M'); \n"); 267 } 268 269 static void octave_fprintf_plot_output(btstack_cvsd_plc_state_t *plc_state, FILE * oct_file){ 270 if (!oct_file) return; 271 char out_name[10]; 272 snprintf(out_name, sizeof(out_name), "out%d", plc_state->nbf); 273 out_name[sizeof(out_name) - 1] = 0; 274 int x0 = CVSD_LHIST; 275 int x1 = x0 + CVSD_FS - 1; 276 octave_fprintf_array_int16(oct_file, out_name, CVSD_FS, plc_state->hist+x0); 277 fprintf(oct_file, "h2 = plot(b(%d:%d), %s, 'cd'); \n", x0, x1, out_name); 278 279 char rest_hist_name[10]; 280 snprintf(rest_hist_name, sizeof(rest_hist_name), "rest%d", plc_state->nbf); 281 rest_hist_name[sizeof(rest_hist_name) - 1] = 0; 282 x0 = CVSD_LHIST + CVSD_FS; 283 x1 = x0 + CVSD_OLAL + CVSD_RT - 1; 284 octave_fprintf_array_int16(oct_file, rest_hist_name, CVSD_OLAL + CVSD_RT, plc_state->hist+x0); 285 fprintf(oct_file, "h3 = plot(b(%d:%d), %s, 'kd'); \n", x0, x1, rest_hist_name); 286 287 char new_hist_name[10]; 288 snprintf(new_hist_name, sizeof(new_hist_name), "hist%d", plc_state->nbf); 289 new_hist_name[sizeof(new_hist_name) - 1] = 0; 290 octave_fprintf_array_int16(oct_file, new_hist_name, CVSD_LHIST, plc_state->hist); 291 fprintf(oct_file, "h4 = plot(%s, 'r--'); \n", new_hist_name); 292 293 fprintf(oct_file, "legend ([h1, h2, h3, h4], {\"hist\", \"out\", \"rest\", \"new hist\"}, \"location\", \"northeast\");\n "); 294 295 char fig_name[1200]; 296 snprintf(fig_name, sizeof(fig_name), "../%s_octave_plc_%d_%s", 297 octave_base_name, plc_state->frame_count, 298 octave_frame_type2str(octave_frame_type)); 299 fig_name[sizeof(fig_name) - 1] = 0; 300 fprintf(oct_file, "print(hf, \"%s.jpg\", \"-djpg\");", fig_name); 301 } 302 #endif 303 304 void btstack_cvsd_plc_bad_frame(btstack_cvsd_plc_state_t *plc_state, uint16_t num_samples, BTSTACK_CVSD_PLC_SAMPLE_FORMAT *out){ 305 float val; 306 int i; 307 float sf = 1; 308 plc_state->nbf++; 309 310 if (plc_state->max_consecutive_bad_frames_nr < plc_state->nbf){ 311 plc_state->max_consecutive_bad_frames_nr = plc_state->nbf; 312 } 313 if (plc_state->nbf==1){ 314 // printf("first bad frame\n"); 315 // Perform pattern matching to find where to replicate 316 plc_state->bestlag = btstack_cvsd_plc_pattern_match(plc_state->hist); 317 } 318 319 #ifdef OCTAVE_OUTPUT 320 FILE * oct_file = open_octave_file(plc_state, OCTAVE_FRAME_TYPE_BAD); 321 if (oct_file){ 322 octave_fprintf_plot_history_frame(plc_state, oct_file, plc_state->frame_count); 323 } 324 #endif 325 326 if (plc_state->nbf==1){ 327 // the replication begins after the template match 328 plc_state->bestlag += CVSD_M; 329 330 // Compute Scale Factor to Match Amplitude of Substitution Packet to that of Preceding Packet 331 sf = btstack_cvsd_plc_amplitude_match(plc_state, num_samples, plc_state->hist, plc_state->bestlag); 332 for (i=0; i<CVSD_OLAL; i++){ 333 val = sf*plc_state->hist[plc_state->bestlag+i]; 334 plc_state->hist[CVSD_LHIST+i] = btstack_cvsd_plc_crop_sample(val); 335 } 336 337 for (i=CVSD_OLAL; i<num_samples; i++){ 338 val = sf*plc_state->hist[plc_state->bestlag+i]; 339 plc_state->hist[CVSD_LHIST+i] = btstack_cvsd_plc_crop_sample(val); 340 } 341 342 for (i=num_samples; i<(num_samples+CVSD_OLAL); i++){ 343 float left = sf*plc_state->hist[plc_state->bestlag+i]; 344 float right = plc_state->hist[plc_state->bestlag+i]; 345 val = (left*rcos[i-num_samples]) + (right*rcos[CVSD_OLAL-1-i+num_samples]); 346 plc_state->hist[CVSD_LHIST+i] = btstack_cvsd_plc_crop_sample(val); 347 } 348 349 for (i=(num_samples+CVSD_OLAL); i<(num_samples+CVSD_RT+CVSD_OLAL); i++){ 350 plc_state->hist[CVSD_LHIST+i] = plc_state->hist[plc_state->bestlag+i]; 351 } 352 } else { 353 for (i=0; i<(num_samples+CVSD_RT+CVSD_OLAL); i++){ 354 plc_state->hist[CVSD_LHIST+i] = plc_state->hist[plc_state->bestlag+i]; 355 } 356 } 357 358 for (i=0; i<num_samples; i++){ 359 out[i] = plc_state->hist[CVSD_LHIST+i]; 360 } 361 362 // shift the history buffer 363 for (i=0; i<(CVSD_LHIST+CVSD_RT+CVSD_OLAL); i++){ 364 plc_state->hist[i] = plc_state->hist[i+num_samples]; 365 } 366 367 #ifdef OCTAVE_OUTPUT 368 if (oct_file){ 369 octave_fprintf_plot_output(plc_state, oct_file); 370 fclose(oct_file); 371 } 372 #endif 373 } 374 375 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){ 376 float val; 377 int i = 0; 378 #ifdef OCTAVE_OUTPUT 379 FILE * oct_file = NULL; 380 if (plc_state->nbf>0){ 381 oct_file = open_octave_file(plc_state, OCTAVE_FRAME_TYPE_GOOD); 382 if (oct_file){ 383 octave_fprintf_plot_history_frame(plc_state, oct_file, plc_state->frame_count); 384 } 385 } 386 #endif 387 if (plc_state->nbf>0){ 388 for (i=0;i<CVSD_RT;i++){ 389 out[i] = plc_state->hist[CVSD_LHIST+i]; 390 } 391 392 for (i=CVSD_RT;i<(CVSD_RT+CVSD_OLAL);i++){ 393 float left = plc_state->hist[CVSD_LHIST+i]; 394 float right = in[i]; 395 val = (left * rcos[i-CVSD_RT]) + (right *rcos[CVSD_OLAL+CVSD_RT-1-i]); 396 out[i] = btstack_cvsd_plc_crop_sample((BTSTACK_CVSD_PLC_SAMPLE_FORMAT)val); 397 } 398 } 399 400 for (;i<num_samples;i++){ 401 out[i] = in[i]; 402 } 403 // Copy the output to the history buffer 404 for (i=0;i<num_samples;i++){ 405 plc_state->hist[CVSD_LHIST+i] = out[i]; 406 } 407 // shift the history buffer 408 for (i=0;i<CVSD_LHIST;i++){ 409 plc_state->hist[i] = plc_state->hist[i+num_samples]; 410 } 411 412 #ifdef OCTAVE_OUTPUT 413 if (oct_file){ 414 octave_fprintf_plot_output(plc_state, oct_file); 415 fclose(oct_file); 416 } 417 #endif 418 plc_state->nbf=0; 419 } 420 421 static int count_equal_samples(BTSTACK_CVSD_PLC_SAMPLE_FORMAT * packet, uint16_t size){ 422 int count = 0; 423 int temp_count = 1; 424 int i; 425 for (i = 0; i < (size-1); i++){ 426 if (packet[i] == packet[i+1]){ 427 temp_count++; 428 continue; 429 } 430 if (count < temp_count){ 431 count = temp_count; 432 } 433 temp_count = 1; 434 } 435 if (temp_count > (count + 1)){ 436 count = temp_count; 437 } 438 return count; 439 } 440 441 static int count_zeros(BTSTACK_CVSD_PLC_SAMPLE_FORMAT * frame, uint16_t size){ 442 int nr_zeros = 0; 443 int i; 444 for (i = 0; i < (size-1); i++){ 445 if (frame[i] == 0){ 446 nr_zeros++; 447 } 448 } 449 return nr_zeros; 450 } 451 452 static int zero_frame(BTSTACK_CVSD_PLC_SAMPLE_FORMAT * frame, uint16_t size){ 453 return count_zeros(frame, size) == size; 454 } 455 456 // more than half the samples are the same -> bad frame 457 static int bad_frame(btstack_cvsd_plc_state_t *plc_state, BTSTACK_CVSD_PLC_SAMPLE_FORMAT * frame, uint16_t size){ 458 UNUSED(plc_state); 459 return count_equal_samples(frame, size) >= (size / 2); 460 } 461 462 463 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){ 464 if (num_samples == 0) return; 465 466 plc_state->frame_count++; 467 468 if (!is_bad_frame) { 469 bool is_zero_frame = zero_frame(in, num_samples); 470 if (is_zero_frame){ 471 plc_state->zero_frames_nr++; 472 } else { 473 is_bad_frame = bad_frame(plc_state, in, num_samples); 474 } 475 } 476 477 if (is_bad_frame){ 478 (void)memcpy(out, in, num_samples * 2); 479 if (plc_state->good_samples > CVSD_LHIST){ 480 btstack_cvsd_plc_bad_frame(plc_state, num_samples, out); 481 plc_state->bad_frames_nr++; 482 } else { 483 memset(out, 0, num_samples * 2); 484 } 485 } else { 486 btstack_cvsd_plc_good_frame(plc_state, num_samples, in, out); 487 plc_state->good_frames_nr++; 488 if (plc_state->good_frames_nr == 1){ 489 log_info("First good frame at index %d\n", plc_state->frame_count-1); 490 } 491 plc_state->good_samples += num_samples; 492 } 493 } 494 495 void btstack_cvsd_dump_statistics(btstack_cvsd_plc_state_t * state){ 496 log_info("Good frames: %d\n", state->good_frames_nr); 497 log_info("Bad frames: %d\n", state->bad_frames_nr); 498 log_info("Zero frames: %d\n", state->zero_frames_nr); 499 log_info("Max Consecutive bad frames: %d\n", state->max_consecutive_bad_frames_nr); 500 } 501