xref: /btstack/src/classic/btstack_cvsd_plc.c (revision 62f77245c7d8d744274593f3c94fb4f44eca18a4)
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 /*
39  * btstack_sbc_plc.c
40  *
41  */
42 
43 
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <fcntl.h>
49 #include <unistd.h>
50 #include <math.h>
51 
52 #include "btstack_cvsd_plc.h"
53 
54 static float rcos[CVSD_OLAL] = {
55     0.99148655,0.96623611,0.92510857,0.86950446,
56     0.80131732,0.72286918,0.63683150,0.54613418,
57     0.45386582,0.36316850,0.27713082,0.19868268,
58     0.13049554,0.07489143,0.03376389,0.00851345};
59 
60 static float CrossCorrelation(int8_t *x, int8_t *y){
61     float num = 0;
62     float den = 0;
63     float x2 = 0;
64     float y2 = 0;
65     int   m;
66     for (m=0;m<CVSD_M;m++){
67         num+=((float)x[m])*y[m];
68         x2+=((float)x[m])*x[m];
69         y2+=((float)y[m])*y[m];
70     }
71     den = (float)sqrt(x2*y2);
72     return num/den;
73 }
74 
75 static int PatternMatch(int8_t *y){
76     float maxCn = -999999.0;  // large negative number
77     int   bestmatch = 0;
78     float Cn;
79     int   n;
80     for (n=0;n<CVSD_N;n++){
81         Cn = CrossCorrelation(&y[CVSD_LHIST-CVSD_M], &y[n]);
82         if (Cn>maxCn){
83             bestmatch=n;
84             maxCn = Cn;
85         }
86     }
87     return bestmatch;
88 }
89 
90 static float AmplitudeMatch(int8_t *y, int8_t bestmatch) {
91     int   i;
92     float sumx = 0;
93     float sumy = 0.000001f;
94     float sf;
95 
96     for (i=0;i<CVSD_FS;i++){
97         sumx += abs(y[CVSD_LHIST-CVSD_FS+i]);
98         sumy += abs(y[bestmatch+i]);
99     }
100     sf = sumx/sumy;
101     // This is not in the paper, but limit the scaling factor to something reasonable to avoid creating artifacts
102     if (sf<0.75f) sf=0.75f;
103     if (sf>1.2f) sf=1.2f;
104     return sf;
105 }
106 
107 static int8_t crop_to_int8(float val){
108     float croped_val = val;
109     if (croped_val > 127.0)  croped_val= 127.0;
110     if (croped_val < -128.0) croped_val=-128.0;
111     return (int8_t) croped_val;
112 }
113 
114 
115 void btstack_cvsd_plc_init(btstack_cvsd_plc_state_t *plc_state){
116     plc_state->nbf=0;
117     plc_state->bestlag=0;
118     memset(plc_state->hist, 0, sizeof(plc_state->hist));
119 }
120 
121 void btstack_cvsd_plc_bad_frame(btstack_cvsd_plc_state_t *plc_state, int8_t *out){
122     float val;
123     int   i = 0;
124     float sf = 1;
125     plc_state->nbf++;
126 
127     if (plc_state->nbf==1){
128         // Perform pattern matching to find where to replicate
129         plc_state->bestlag = PatternMatch(plc_state->hist);
130         // the replication begins after the template match
131         plc_state->bestlag += CVSD_M;
132 
133         // Compute Scale Factor to Match Amplitude of Substitution Packet to that of Preceding Packet
134         sf = AmplitudeMatch(plc_state->hist, plc_state->bestlag);
135         for (i=0;i<CVSD_OLAL;i++){
136             val = sf*plc_state->hist[plc_state->bestlag+i];
137             plc_state->hist[CVSD_LHIST+i] = crop_to_int8(val);
138         }
139 
140         for (;i<CVSD_FS;i++){
141             val = sf*plc_state->hist[plc_state->bestlag+i];
142             plc_state->hist[CVSD_LHIST+i] = crop_to_int8(val);
143         }
144 
145         for (;i<CVSD_FS+CVSD_OLAL;i++){
146             float left  = sf*plc_state->hist[plc_state->bestlag+i];
147             float right = plc_state->hist[plc_state->bestlag+i];
148             val = left*rcos[i-CVSD_FS] + right*rcos[CVSD_OLAL-1-i+CVSD_FS];
149             plc_state->hist[CVSD_LHIST+i] = crop_to_int8(val);
150         }
151 
152         for (;i<CVSD_FS+CVSD_RT+CVSD_OLAL;i++){
153             plc_state->hist[CVSD_LHIST+i] = plc_state->hist[plc_state->bestlag+i];
154         }
155     } else {
156         for (;i<CVSD_FS+CVSD_RT+CVSD_OLAL;i++){
157             plc_state->hist[CVSD_LHIST+i] = plc_state->hist[plc_state->bestlag+i];
158         }
159     }
160     for (i=0;i<CVSD_FS;i++){
161         out[i] = plc_state->hist[CVSD_LHIST+i];
162     }
163 
164    // shift the history buffer
165    for (i=0;i<CVSD_LHIST+CVSD_RT+CVSD_OLAL;i++){
166         plc_state->hist[i] = plc_state->hist[i+CVSD_FS];
167     }
168 }
169 
170 void btstack_cvsd_plc_good_frame(btstack_cvsd_plc_state_t *plc_state, int8_t *in, int8_t *out){
171     float val;
172     int i = 0;
173     if (plc_state->nbf>0){
174         for (i=0;i<CVSD_RT;i++){
175             out[i] = plc_state->hist[CVSD_LHIST+i];
176         }
177 
178         for (i=CVSD_RT;i<CVSD_RT+CVSD_OLAL;i++){
179             float left  = plc_state->hist[CVSD_LHIST+i];
180             float right = in[i];
181             val = left * rcos[i-CVSD_RT] + right *rcos[CVSD_OLAL+CVSD_RT-1-i];
182             out[i] = (int8_t)val;
183         }
184     }
185 
186     for (;i<CVSD_FS;i++){
187         out[i] = in[i];
188     }
189     // Copy the output to the history buffer
190     for (i=0;i<CVSD_FS;i++){
191         plc_state->hist[CVSD_LHIST+i] = out[i];
192     }
193     // shift the history buffer
194     for (i=0;i<CVSD_LHIST;i++){
195         plc_state->hist[i] = plc_state->hist[i+CVSD_FS];
196     }
197     plc_state->nbf=0;
198 }
199