hfp cvsd: plc for periodic sine signal

This commit is contained in:
Milanka Ringwald 2016-08-29 23:10:32 +02:00
parent 8cff923e81
commit c774f3a0e5
5 changed files with 523 additions and 3 deletions

View File

@ -56,7 +56,7 @@ VPATH += ${BTSTACK_ROOT}/platform/posix
CFLAGS = -g -Wall -I. -I../ -I${BTSTACK_ROOT}/src -I${BTSTACK_ROOT}/include -I${BTSTACK_ROOT}/ble
LDFLAGS += -lCppUTest -lCppUTestExt
EXAMPLES = hfp_ag_parser_test hfp_ag_client_test hfp_hf_parser_test hfp_hf_client_test
EXAMPLES = hfp_ag_parser_test hfp_ag_client_test hfp_hf_parser_test hfp_hf_client_test cvsd_plc_test
all: ${EXAMPLES}
@ -75,9 +75,12 @@ hfp_hf_client_test: ${MOCK_OBJ} hfp_hf.o hfp.o hfp_hf_client_test.c
hfp_ag_client_test: ${MOCK_OBJ} hfp_gsm_model.o hfp_ag.o hfp.o hfp_ag_client_test.c
${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@
cvsd_plc_test: btstack_cvsd_plc.o cvsd_plc_test.c
${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@
test: all
./hfp_ag_parser_test
./hfp_ag_client_test
./hfp_hf_parser_test
./hfp_hf_client_test
./cvsd_plc_test

202
test/hfp/btstack_cvsd_plc.c Normal file
View File

@ -0,0 +1,202 @@
/*
* Copyright (C) 2016 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
/*
* btstack_sbc_plc.c
*
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
#include "btstack_cvsd_plc.h"
static float rcos[OLAL] = {
0.99148655f,0.96623611f,0.92510857f,0.86950446f,
0.80131732f,0.72286918f,0.63683150f,0.54613418f,
0.45386582f,0.36316850f,0.27713082f,0.19868268f,
0.13049554f,0.07489143f,0.03376389f,0.00851345f};
static float CrossCorrelation(int8_t *x, int8_t *y);
static int PatternMatch(int8_t *y);
static float AmplitudeMatch(int8_t *y, int8_t bestmatch);
void btstack_cvsd_plc_init(btstack_cvsd_plc_state_t *plc_state){
int i;
plc_state->nbf=0;
plc_state->bestlag=0;
for (i=0;i<LHIST+CVSDRT;i++){
plc_state->hist[i] = 0;
}
}
void btstack_cvsd_plc_bad_frame(btstack_cvsd_plc_state_t *plc_state, int8_t *out){
int i;
float val;
float sf;
plc_state->nbf++;
sf=1.0f;
i=0;
if (plc_state->nbf==1){
/* Perform pattern matching to find where to replicate */
plc_state->bestlag = PatternMatch(plc_state->hist);
/* the replication begins after the template match */
plc_state->bestlag += M;
/* Compute Scale Factor to Match Amplitude of Substitution Packet to that of Preceding Packet */
sf = AmplitudeMatch(plc_state->hist, plc_state->bestlag);
for (i=0;i<OLAL;i++){
val = sf*plc_state->hist[plc_state->bestlag+i];//*rcos[OLAL-i-1];
if (val > 127.0) val= 127.0;
if (val < -128.0) val=-128.0;
plc_state->hist[LHIST+i] = (int8_t)val;
}
for (;i<FS;i++){
val = sf*plc_state->hist[plc_state->bestlag+i];
if (val > 127.0) val= 127.0;
if (val < -128.0) val=-128.0;
plc_state->hist[LHIST+i] = (int8_t)val;
}
for (;i<FS+OLAL;i++){
val = sf*plc_state->hist[plc_state->bestlag+i]*rcos[i-FS]
+ plc_state->hist[plc_state->bestlag+i]*rcos[OLAL-1-i+FS];
if (val > 127.0) val= 127.0;
if (val < -128.0) val=-128.0;
plc_state->hist[LHIST+i] = (int8_t)val;
}
for (;i<FS+CVSDRT+OLAL;i++)
plc_state->hist[LHIST+i] = plc_state->hist[plc_state->bestlag+i];
} else {
for (;i<FS;i++)
plc_state->hist[LHIST+i] = plc_state->hist[plc_state->bestlag+i];
for (;i<FS+CVSDRT+OLAL;i++)
plc_state->hist[LHIST+i] = plc_state->hist[plc_state->bestlag+i];
}
for (i=0;i<FS;i++)
out[i] = plc_state->hist[LHIST+i];
/* shift the history buffer */
for (i=0;i<LHIST+CVSDRT+OLAL;i++)
plc_state->hist[i] = plc_state->hist[i+FS];
}
void btstack_cvsd_plc_good_frame(btstack_cvsd_plc_state_t *plc_state, int8_t *in, int8_t *out){
int i;
i=0;
if (plc_state->nbf>0){
for (i=0;i<CVSDRT;i++)
out[i] = plc_state->hist[LHIST+i];
for (;i<CVSDRT+OLAL;i++)
out[i] = (int8_t)(plc_state->hist[LHIST+i]*rcos[i-CVSDRT] + in[i]*rcos[OLAL-1-i+CVSDRT]);
}
for (;i<FS;i++)
out[i] = in[i];
/*Copy the output to the history buffer */
for (i=0;i<FS;i++)
plc_state->hist[LHIST+i] = out[i];
/* shift the history buffer */
for (i=0;i<LHIST;i++)
plc_state->hist[i] = plc_state->hist[i+FS];
plc_state->nbf=0;
}
float CrossCorrelation(int8_t *x, int8_t *y){
int m;
float num;
float den;
float Cn;
float x2, y2;
num=0;
den=0;
x2=0.0;
y2=0.0;
for (m=0;m<M;m++){
num+=((float)x[m])*y[m];
x2+=((float)x[m])*x[m];
y2+=((float)y[m])*y[m];
}
den = (float)sqrt(x2*y2);
Cn = num/den;
return(Cn);
}
int PatternMatch(int8_t *y){
int n;
float maxCn;
float Cn;
int bestmatch;
maxCn=-999999.0; /* large negative number */
bestmatch=0;
for (n=0;n<N;n++){
Cn = CrossCorrelation(&y[LHIST-M] /* x */, &y[n]);
if (Cn>maxCn){
bestmatch=n;
maxCn = Cn;
}
}
return(bestmatch);
}
float AmplitudeMatch(int8_t *y, int8_t bestmatch) {
int i;
float sumx;
float sumy;
float sf;
sumx = 0.0;
sumy = 0.000001f;
for (i=0;i<FS;i++){
sumx += abs(y[LHIST-FS+i]);
sumy += abs(y[bestmatch+i]);
}
sf = sumx/sumy;
/* This is not in the paper, but limit the scaling factor to something reasonable to avoid creating artifacts */
if (sf<0.75f) sf=0.75f;
if (sf>1.2f) sf=1.2f;
return(sf);
}

View File

@ -0,0 +1,75 @@
/*
* Copyright (C) 2016 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
/*
* btstack_cvsd_plc.h
*
*/
#ifndef __BTSTACK_CVSD_PLC_H
#define __BTSTACK_CVSD_PLC_H
#include <stdint.h>
#if defined __cplusplus
extern "C" {
#endif
#define FS 24 /* Frame Size */
#define N 128 /* 16ms - Window Length for pattern matching */
#define M 32 /* 4ms - Template for matching */
#define LHIST (N+FS-1) /* Length of history buffer required */
#define CVSDRT 8 /* Reconvergence Time (samples) */
#define OLAL 16 /* OverLap-Add Length (samples) */
/* PLC State Information */
typedef struct cvsd_plc_state {
int8_t hist[LHIST+FS+CVSDRT+OLAL];
int8_t bestlag;
int nbf;
} btstack_cvsd_plc_state_t;
void btstack_cvsd_plc_init(btstack_cvsd_plc_state_t *plc_state);
void btstack_cvsd_plc_bad_frame(btstack_cvsd_plc_state_t *plc_state, int8_t *out);
void btstack_cvsd_plc_good_frame(btstack_cvsd_plc_state_t *plc_state, int8_t *in, int8_t *out);
uint8_t * btstack_cvsd_plc_zero_signal_frame(void);
#if defined __cplusplus
}
#endif
#endif // __BTSTACK_CVSD_PLC_H

238
test/hfp/cvsd_plc_test.c Normal file
View File

@ -0,0 +1,238 @@
#include "CppUTest/TestHarness.h"
#include "CppUTest/CommandLineTestRunner.h"
#include "btstack_cvsd_plc.h"
const int audio_samples_per_packet = 24;
static uint8_t audio_frame_in[audio_samples_per_packet];
static uint8_t audio_frame_out[audio_samples_per_packet];
static uint8_t test_data[][audio_samples_per_packet] = {
{ 0x05, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
{ 0xff, 0xff, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x05 },
{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05 },
{ 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
};
// input signal: pre-computed sine wave, 160 Hz at 8 kHz
static const uint8_t sine[] = {
0, 15, 31, 46, 61, 74, 86, 97, 107, 114,
120, 124, 126, 126, 124, 120, 114, 107, 97, 86,
74, 61, 46, 31, 15, 0, 241, 225, 210, 195,
182, 170, 159, 149, 142, 136, 132, 130, 130, 132,
136, 142, 149, 159, 170, 182, 195, 210, 225, 241,
};
static int phase = 0;
static btstack_cvsd_plc_state_t plc_state;
static void fill_audio_frame_with_error(int nr_zero_bytes){
int i;
for (i=0;i<audio_samples_per_packet;i++){
audio_frame_in[i] = 0;
if (i<audio_samples_per_packet-nr_zero_bytes){
audio_frame_in[i] = sine[phase];
}
phase++;
if (phase >= sizeof(sine)) phase = 0;
}
}
static int count_equal_bytes(uint8_t * packet){
int count = 0;
int temp_count = 1;
int i;
for (i = 0; i < audio_samples_per_packet-1; i++){
if (packet[i] == packet[i+1]){
temp_count++;
continue;
}
if (count < temp_count){
count = temp_count;
}
temp_count = 1;
}
if (temp_count > count + 1){
count = temp_count;
}
return count;
}
/* Write wav file utils */
typedef struct wav_writer_state {
FILE * wav_file;
int total_num_samples;
int num_channels;
int sampling_frequency;
int frame_count;
} wav_writer_state_t;
const char * cvsd_filename = "sine_test.wav";
wav_writer_state_t wav_writer_state;
void little_endian_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){
buffer[pos++] = value;
buffer[pos++] = value >> 8;
}
void little_endian_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){
buffer[pos++] = value;
buffer[pos++] = value >> 8;
buffer[pos++] = value >> 16;
buffer[pos++] = value >> 24;
}
static void little_endian_fstore_16(FILE *wav_file, uint16_t value){
uint8_t buf[2];
little_endian_store_32(buf, 0, value);
fwrite(&buf, 1, 2, wav_file);
}
static void little_endian_fstore_32(FILE *wav_file, uint32_t value){
uint8_t buf[4];
little_endian_store_32(buf, 0, value);
fwrite(&buf, 1, 4, wav_file);
}
static void write_wav_header(FILE * wav_file, int total_num_samples, int num_channels, int sample_rate){
unsigned int bytes_per_sample = 2;
/* write RIFF header */
fwrite("RIFF", 1, 4, wav_file);
// num_samples = blocks * subbands
uint32_t data_bytes = (uint32_t) (bytes_per_sample * total_num_samples * num_channels);
little_endian_fstore_32(wav_file, data_bytes + 36);
fwrite("WAVE", 1, 4, wav_file);
int byte_rate = sample_rate * num_channels * bytes_per_sample;
int bits_per_sample = 8 * bytes_per_sample;
int block_align = num_channels * bits_per_sample;
int fmt_length = 16;
int fmt_format_tag = 1; // PCM
/* write fmt chunk */
fwrite("fmt ", 1, 4, wav_file);
little_endian_fstore_32(wav_file, fmt_length);
little_endian_fstore_16(wav_file, fmt_format_tag);
little_endian_fstore_16(wav_file, num_channels);
little_endian_fstore_32(wav_file, sample_rate);
little_endian_fstore_32(wav_file, byte_rate);
little_endian_fstore_16(wav_file, block_align);
little_endian_fstore_16(wav_file, bits_per_sample);
/* write data chunk */
fwrite("data", 1, 4, wav_file);
little_endian_fstore_32(wav_file, data_bytes);
}
static void init_wav_writer(const char * cvsd_filename){
printf("Open sbc file: %s\n", cvsd_filename);
FILE * wav_file = fopen(cvsd_filename, "wb");
wav_writer_state.wav_file = wav_file;
wav_writer_state.frame_count = 0;
wav_writer_state.total_num_samples = 0;
wav_writer_state.num_channels = 1;
wav_writer_state.sampling_frequency = 8000;
write_wav_header(wav_writer_state.wav_file, 0, 1, 8000);
}
static void close_wav_writer(void){
rewind(wav_writer_state.wav_file);
write_wav_header(wav_writer_state.wav_file, wav_writer_state.total_num_samples,
wav_writer_state.num_channels, wav_writer_state.sampling_frequency);
fclose(wav_writer_state.wav_file);
}
static void write_wav_data(int num_samples, uint8_t * data){
fwrite(data, num_samples, 1, wav_writer_state.wav_file);
wav_writer_state.total_num_samples+=num_samples;
wav_writer_state.frame_count++;
}
//
static void process_sine_with_error_rate(const char * file_name, int error_rate, int num_bad_frames, int plc_enabled){
btstack_cvsd_plc_init(&plc_state);
init_wav_writer(file_name);
int total_num_samples = 5000;
int corruption_step = 0;
if (error_rate > 0){
int nr_corrupt_samples = total_num_samples * error_rate / 100;
corruption_step = total_num_samples / nr_corrupt_samples;
}
int i;
int num_bf = num_bad_frames;
printf("Num samples %d, corruption every %dth step\n", total_num_samples, corruption_step);
for (i=0; i<2000; i++){
if (i > corruption_step && corruption_step > 0 && i%corruption_step == 0) {
fill_audio_frame_with_error(24);
btstack_cvsd_plc_bad_frame(&plc_state, (int8_t*)audio_frame_out);
num_bf = num_bad_frames;
} else if (i > corruption_step && num_bf > 1) {
fill_audio_frame_with_error(24);
btstack_cvsd_plc_bad_frame(&plc_state, (int8_t*)audio_frame_out);
num_bf--;
} else {
fill_audio_frame_with_error(0);
btstack_cvsd_plc_good_frame(&plc_state, (int8_t*)audio_frame_in, (int8_t*)audio_frame_out);
}
if (plc_enabled){
write_wav_data(24, audio_frame_out);
} else {
write_wav_data(24, audio_frame_in);
}
}
close_wav_writer();
}
TEST_GROUP(CVSD_PLC){
};
TEST(CVSD_PLC, CountEqBytes){
CHECK_EQUAL(23, count_equal_bytes(test_data[0]));
CHECK_EQUAL(11, count_equal_bytes(test_data[1]));
CHECK_EQUAL(12, count_equal_bytes(test_data[2]));
CHECK_EQUAL(23, count_equal_bytes(test_data[3]));
}
TEST(CVSD_PLC, FillAudioFrame){
fill_audio_frame_with_error(15);
CHECK_EQUAL(15, count_equal_bytes(audio_frame_in));
}
TEST(CVSD_PLC, ProcessWavFileWithErrorRate){
process_sine_with_error_rate("sine_test_10_no_plc.wav", 10, 0, 0);
process_sine_with_error_rate("sine_test_10_plc.wav", 10, 0, 1);
}
// TEST(CVSD_PLC, ProcessWavFileWithErrorRate){
// // process_sine_with_error_rate("sine_test_0.wav", 0, 0, 1);
// // process_sine_with_error_rate("sine_test_1.wav", 1, 0, 1);
// // process_sine_with_error_rate("sine_test_2.wav", 2, 0, 1);
// // process_sine_with_error_rate("sine_test_3.wav", 3, 0, 1);
// // process_sine_with_error_rate("sine_test_4.wav", 4, 0, 1);
// // process_sine_with_error_rate("sine_test_5.wav", 5, 0, 1);
// // process_sine_with_error_rate("sine_test_6.wav", 6, 0, 1);
// // process_sine_with_error_rate("sine_test_7.wav", 7, 0, 1);
// // process_sine_with_error_rate("sine_test_8.wav", 8, 0, 1);
// // process_sine_with_error_rate("sine_test_9.wav", 9, 0, 1);
// // process_sine_with_error_rate("sine_test_10.wav", 10, 0, 1);
// }
TEST(CVSD_PLC, ProcessWavFileWithErrorRateAndSubseqBadSamples){
process_sine_with_error_rate("sine_test_10_2_no_plc.wav", 10, 2, 0);
process_sine_with_error_rate("sine_test_10_2_plc.wav", 10, 2, 1);
// process_sine_with_error_rate("sine_test_1_2.wav", 1, 2, 1);
// process_sine_with_error_rate("sine_test_1_4.wav", 1, 4, 1);
// process_sine_with_error_rate("sine_test_1_6.wav", 1, 6, 1);
// process_sine_with_error_rate("sine_test_1_8.wav", 1, 8, 1);
}
int main (int argc, const char * argv[]){
return CommandLineTestRunner::RunAllTests(argc, argv);
}

View File

@ -380,4 +380,6 @@ void inject_hfp_command_to_ag(uint8_t * data, int len){
}
int hci_extended_sco_link_supported(void){
return 1;
}