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 sprintf(oct_file_name, "%s_octave_plc_%d_%s.m", octave_base_name, plc_state->frame_count, octave_frame_type2str(octave_frame_type)); 195 196 FILE * oct_file = fopen(oct_file_name, "wb"); 197 if (oct_file == NULL){ 198 printf("OCTAVE: could not open file %s\n", oct_file_name); 199 return NULL; 200 } 201 printf("OCTAVE: opened file %s\n", oct_file_name); 202 return oct_file; 203 } 204 205 static void octave_fprintf_plot_history_frame(btstack_cvsd_plc_state_t *plc_state, FILE * oct_file, int frame_nr){ 206 char title[100]; 207 char hist_name[10]; 208 sprintf(hist_name, "hist%d", plc_state->nbf); 209 210 octave_fprintf_array_int16(oct_file, hist_name, CVSD_LHIST, plc_state->hist); 211 212 fprintf(oct_file, "y = [min(%s):1000:max(%s)];\n", hist_name, hist_name); 213 fprintf(oct_file, "x = zeros(1, size(y,2));\n"); 214 fprintf(oct_file, "b = [0: %d];\n", CVSD_LHIST+CVSD_FS+CVSD_RT+CVSD_OLAL); 215 216 int pos = CVSD_FS; 217 fprintf(oct_file, "shift_x = x + %d;\n", pos); 218 219 pos = CVSD_LHIST - 1; 220 fprintf(oct_file, "lhist_x = x + %d;\n", pos); 221 pos += CVSD_OLAL; 222 fprintf(oct_file, "lhist_olal1_x = x + %d;\n", pos); 223 pos += CVSD_FS - CVSD_OLAL; 224 fprintf(oct_file, "lhist_fs_x = x + %d;\n", pos); 225 pos += CVSD_OLAL; 226 fprintf(oct_file, "lhist_olal2_x = x + %d;\n", pos); 227 pos += CVSD_RT; 228 fprintf(oct_file, "lhist_rt_x = x + %d;\n", pos); 229 230 fprintf(oct_file, "pattern_window_x = x + %d;\n", CVSD_LHIST - CVSD_M); 231 232 fprintf(oct_file, "hf = figure();\n"); 233 sprintf(title, "PLC %s frame %d", octave_frame_type2str(octave_frame_type), frame_nr); 234 235 fprintf(oct_file, "hold on;\n"); 236 fprintf(oct_file, "h1 = plot(%s); \n", hist_name); 237 238 fprintf(oct_file, "title(\"%s\");\n", title); 239 240 fprintf(oct_file, "plot(lhist_x, y, 'k'); \n"); 241 fprintf(oct_file, "text(max(lhist_x) - 10, max(y)+1000, 'lhist'); \n"); 242 243 fprintf(oct_file, "plot(lhist_olal1_x, y, 'k'); \n"); 244 fprintf(oct_file, "text(max(lhist_olal1_x) - 10, max(y)+1000, 'OLAL'); \n"); 245 246 fprintf(oct_file, "plot(lhist_fs_x, y, 'k'); \n"); 247 fprintf(oct_file, "text(max(lhist_fs_x) - 10, max(y)+1000, 'FS'); \n"); 248 249 fprintf(oct_file, "plot(lhist_olal2_x, y, 'k'); \n"); 250 fprintf(oct_file, "text(max(lhist_olal2_x) - 10, max(y)+1000, 'OLAL'); \n"); 251 252 fprintf(oct_file, "plot(lhist_rt_x, y, 'k');\n"); 253 fprintf(oct_file, "text(max(lhist_rt_x) - 10, max(y)+1000, 'RT'); \n"); 254 255 if (octave_frame_type == OCTAVE_FRAME_TYPE_GOOD) return; 256 257 int x0 = plc_state->bestlag; 258 int x1 = plc_state->bestlag + CVSD_M - 1; 259 fprintf(oct_file, "plot(b(%d:%d), %s(%d:%d), 'rd'); \n", x0, x1, hist_name, x0, x1); 260 fprintf(oct_file, "text(%d - 10, -10, 'bestlag'); \n", x0); 261 262 x0 = plc_state->bestlag + CVSD_M ; 263 x1 = plc_state->bestlag + CVSD_M + CVSD_FS - 1; 264 fprintf(oct_file, "plot(b(%d:%d), %s(%d:%d), 'kd'); \n", x0, x1, hist_name, x0, x1); 265 266 x0 = CVSD_LHIST - CVSD_M; 267 x1 = CVSD_LHIST - 1; 268 fprintf(oct_file, "plot(b(%d:%d), %s(%d:%d), 'rd'); \n", x0, x1, hist_name, x0, x1); 269 fprintf(oct_file, "plot(pattern_window_x, y, 'g'); \n"); 270 fprintf(oct_file, "text(max(pattern_window_x) - 10, max(y)+1000, 'M'); \n"); 271 } 272 273 static void octave_fprintf_plot_output(btstack_cvsd_plc_state_t *plc_state, FILE * oct_file){ 274 if (!oct_file) return; 275 char out_name[10]; 276 sprintf(out_name, "out%d", plc_state->nbf); 277 int x0 = CVSD_LHIST; 278 int x1 = x0 + CVSD_FS - 1; 279 octave_fprintf_array_int16(oct_file, out_name, CVSD_FS, plc_state->hist+x0); 280 fprintf(oct_file, "h2 = plot(b(%d:%d), %s, 'cd'); \n", x0, x1, out_name); 281 282 char rest_hist_name[10]; 283 sprintf(rest_hist_name, "rest%d", plc_state->nbf); 284 x0 = CVSD_LHIST + CVSD_FS; 285 x1 = x0 + CVSD_OLAL + CVSD_RT - 1; 286 octave_fprintf_array_int16(oct_file, rest_hist_name, CVSD_OLAL + CVSD_RT, plc_state->hist+x0); 287 fprintf(oct_file, "h3 = plot(b(%d:%d), %s, 'kd'); \n", x0, x1, rest_hist_name); 288 289 char new_hist_name[10]; 290 sprintf(new_hist_name, "hist%d", plc_state->nbf); 291 octave_fprintf_array_int16(oct_file, new_hist_name, CVSD_LHIST, plc_state->hist); 292 fprintf(oct_file, "h4 = plot(%s, 'r--'); \n", new_hist_name); 293 294 fprintf(oct_file, "legend ([h1, h2, h3, h4], {\"hist\", \"out\", \"rest\", \"new hist\"}, \"location\", \"northeast\");\n "); 295 296 char fig_name[1200]; 297 sprintf(fig_name, "../%s_octave_plc_%d_%s", octave_base_name, plc_state->frame_count, octave_frame_type2str(octave_frame_type)); 298 fprintf(oct_file, "print(hf, \"%s.jpg\", \"-djpg\");", fig_name); 299 } 300 #endif 301 302 void btstack_cvsd_plc_bad_frame(btstack_cvsd_plc_state_t *plc_state, uint16_t num_samples, BTSTACK_CVSD_PLC_SAMPLE_FORMAT *out){ 303 float val; 304 int i; 305 float sf = 1; 306 plc_state->nbf++; 307 308 if (plc_state->max_consecutive_bad_frames_nr < plc_state->nbf){ 309 plc_state->max_consecutive_bad_frames_nr = plc_state->nbf; 310 } 311 if (plc_state->nbf==1){ 312 // printf("first bad frame\n"); 313 // Perform pattern matching to find where to replicate 314 plc_state->bestlag = btstack_cvsd_plc_pattern_match(plc_state->hist); 315 } 316 317 #ifdef OCTAVE_OUTPUT 318 FILE * oct_file = open_octave_file(plc_state, OCTAVE_FRAME_TYPE_BAD); 319 if (oct_file){ 320 octave_fprintf_plot_history_frame(plc_state, oct_file, plc_state->frame_count); 321 } 322 #endif 323 324 if (plc_state->nbf==1){ 325 // the replication begins after the template match 326 plc_state->bestlag += CVSD_M; 327 328 // Compute Scale Factor to Match Amplitude of Substitution Packet to that of Preceding Packet 329 sf = btstack_cvsd_plc_amplitude_match(plc_state, num_samples, plc_state->hist, plc_state->bestlag); 330 for (i=0; i<CVSD_OLAL; i++){ 331 val = sf*plc_state->hist[plc_state->bestlag+i]; 332 plc_state->hist[CVSD_LHIST+i] = btstack_cvsd_plc_crop_sample(val); 333 } 334 335 for (i=CVSD_OLAL; i<num_samples; i++){ 336 val = sf*plc_state->hist[plc_state->bestlag+i]; 337 plc_state->hist[CVSD_LHIST+i] = btstack_cvsd_plc_crop_sample(val); 338 } 339 340 for (i=num_samples; i<(num_samples+CVSD_OLAL); i++){ 341 float left = sf*plc_state->hist[plc_state->bestlag+i]; 342 float right = plc_state->hist[plc_state->bestlag+i]; 343 val = (left*rcos[i-num_samples]) + (right*rcos[CVSD_OLAL-1-i+num_samples]); 344 plc_state->hist[CVSD_LHIST+i] = btstack_cvsd_plc_crop_sample(val); 345 } 346 347 for (i=(num_samples+CVSD_OLAL); i<(num_samples+CVSD_RT+CVSD_OLAL); i++){ 348 plc_state->hist[CVSD_LHIST+i] = plc_state->hist[plc_state->bestlag+i]; 349 } 350 } else { 351 for (i=0; i<(num_samples+CVSD_RT+CVSD_OLAL); i++){ 352 plc_state->hist[CVSD_LHIST+i] = plc_state->hist[plc_state->bestlag+i]; 353 } 354 } 355 356 for (i=0; i<num_samples; i++){ 357 out[i] = plc_state->hist[CVSD_LHIST+i]; 358 } 359 360 // shift the history buffer 361 for (i=0; i<(CVSD_LHIST+CVSD_RT+CVSD_OLAL); i++){ 362 plc_state->hist[i] = plc_state->hist[i+num_samples]; 363 } 364 365 #ifdef OCTAVE_OUTPUT 366 if (oct_file){ 367 octave_fprintf_plot_output(plc_state, oct_file); 368 fclose(oct_file); 369 } 370 #endif 371 } 372 373 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){ 374 float val; 375 int i = 0; 376 #ifdef OCTAVE_OUTPUT 377 FILE * oct_file = NULL; 378 if (plc_state->nbf>0){ 379 oct_file = open_octave_file(plc_state, OCTAVE_FRAME_TYPE_GOOD); 380 if (oct_file){ 381 octave_fprintf_plot_history_frame(plc_state, oct_file, plc_state->frame_count); 382 } 383 } 384 #endif 385 if (plc_state->nbf>0){ 386 for (i=0;i<CVSD_RT;i++){ 387 out[i] = plc_state->hist[CVSD_LHIST+i]; 388 } 389 390 for (i=CVSD_RT;i<(CVSD_RT+CVSD_OLAL);i++){ 391 float left = plc_state->hist[CVSD_LHIST+i]; 392 float right = in[i]; 393 val = (left * rcos[i-CVSD_RT]) + (right *rcos[CVSD_OLAL+CVSD_RT-1-i]); 394 out[i] = btstack_cvsd_plc_crop_sample((BTSTACK_CVSD_PLC_SAMPLE_FORMAT)val); 395 } 396 } 397 398 for (;i<num_samples;i++){ 399 out[i] = in[i]; 400 } 401 // Copy the output to the history buffer 402 for (i=0;i<num_samples;i++){ 403 plc_state->hist[CVSD_LHIST+i] = out[i]; 404 } 405 // shift the history buffer 406 for (i=0;i<CVSD_LHIST;i++){ 407 plc_state->hist[i] = plc_state->hist[i+num_samples]; 408 } 409 410 #ifdef OCTAVE_OUTPUT 411 if (oct_file){ 412 octave_fprintf_plot_output(plc_state, oct_file); 413 fclose(oct_file); 414 } 415 #endif 416 plc_state->nbf=0; 417 } 418 419 static int count_equal_samples(BTSTACK_CVSD_PLC_SAMPLE_FORMAT * packet, uint16_t size){ 420 int count = 0; 421 int temp_count = 1; 422 int i; 423 for (i = 0; i < (size-1); i++){ 424 if (packet[i] == packet[i+1]){ 425 temp_count++; 426 continue; 427 } 428 if (count < temp_count){ 429 count = temp_count; 430 } 431 temp_count = 1; 432 } 433 if (temp_count > (count + 1)){ 434 count = temp_count; 435 } 436 return count; 437 } 438 439 static int count_zeros(BTSTACK_CVSD_PLC_SAMPLE_FORMAT * frame, uint16_t size){ 440 int nr_zeros = 0; 441 int i; 442 for (i = 0; i < (size-1); i++){ 443 if (frame[i] == 0){ 444 nr_zeros++; 445 } 446 } 447 return nr_zeros; 448 } 449 450 static int zero_frame(BTSTACK_CVSD_PLC_SAMPLE_FORMAT * frame, uint16_t size){ 451 return count_zeros(frame, size) == size; 452 } 453 454 // more than half the samples are the same -> bad frame 455 static int bad_frame(btstack_cvsd_plc_state_t *plc_state, BTSTACK_CVSD_PLC_SAMPLE_FORMAT * frame, uint16_t size){ 456 UNUSED(plc_state); 457 return count_equal_samples(frame, size) >= (size / 2); 458 } 459 460 461 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){ 462 if (num_samples == 0) return; 463 464 plc_state->frame_count++; 465 466 if (!is_bad_frame) { 467 bool is_zero_frame = zero_frame(in, num_samples); 468 if (is_zero_frame){ 469 plc_state->zero_frames_nr++; 470 } else { 471 is_bad_frame = bad_frame(plc_state, in, num_samples); 472 } 473 } 474 475 if (is_bad_frame){ 476 (void)memcpy(out, in, num_samples * 2); 477 if (plc_state->good_samples > CVSD_LHIST){ 478 btstack_cvsd_plc_bad_frame(plc_state, num_samples, out); 479 plc_state->bad_frames_nr++; 480 } else { 481 memset(out, 0, num_samples * 2); 482 } 483 } else { 484 btstack_cvsd_plc_good_frame(plc_state, num_samples, in, out); 485 plc_state->good_frames_nr++; 486 if (plc_state->good_frames_nr == 1){ 487 log_info("First good frame at index %d\n", plc_state->frame_count-1); 488 } 489 plc_state->good_samples += num_samples; 490 } 491 } 492 493 void btstack_cvsd_dump_statistics(btstack_cvsd_plc_state_t * state){ 494 log_info("Good frames: %d\n", state->good_frames_nr); 495 log_info("Bad frames: %d\n", state->bad_frames_nr); 496 log_info("Zero frames: %d\n", state->zero_frames_nr); 497 log_info("Max Consecutive bad frames: %d\n", state->max_consecutive_bad_frames_nr); 498 } 499