struct Coef get_shelving_coef(void) { struct Coef coefficients; coefficients.b0 = float_to_fr16(0.46546409); coefficients.b1 = float_to_fr16(-0.77571291); coefficients.b2 = float_to_fr16(0.34163607); coefficients.a1 = float_to_fr16(-0.38094923); coefficients.a2 = float_to_fr16(0.16233647); return coefficients; }
void pre_emphasis(fract16 data[], int arr_length, struct Coef shelving_coef) { fract16 xBuffer[BUFFER_SIZE]; // input buffer fract16 yBuffer[BUFFER_SIZE]; // output buffer int current; for (current = 0; current < BUFFER_SIZE; ++current) { xBuffer[current] = yBuffer[current] = float_to_fr16(0.0); } current = 0; int index; for (index = 0; index < arr_length; index++) { fract16 temp_b0 = mult_fr1x16(shelving_coef.b0, data[index]); fract16 temp_b1 = mult_fr1x16(shelving_coef.b1, xBuffer[INDEX(current-1)]); fract16 temp_b2 = mult_fr1x16(shelving_coef.b2, xBuffer[INDEX(current-2)]); fract16 temp_a1 = mult_fr1x16(shelving_coef.a1, yBuffer[INDEX(current-1)]); fract16 temp_a2 = mult_fr1x16(shelving_coef.a2, yBuffer[INDEX(current-2)]); fract16 temp_b = add_fr1x16(add_fr1x16(temp_b0, temp_b1), temp_b2); fract16 temp_a = add_fr1x16(temp_a1, temp_a2); fract16 temp = sub_fr1x16(temp_b, temp_a); yBuffer[current] = shl_fr1x16(temp, 2); xBuffer[current] = data[index]; //input data[index] = yBuffer[current]; //output current++; current %= BUFFER_SIZE; } }
void adaptive_lms(void) { // First, produce y_lms. fir_fr16(pointer, pointer_out, 1, &state); //Call the in-built efficient FIR filter. // Find the error signal adder_unit(); // Calculate the step size------------------------------------ // If you do not want to update the step size automatically, // comment this section of step size calculation between the // top and bottom comments. _NLMS_step_updater_sum(sum_int_p, sum_fract_p, &state); sum = sum_int + fr16_to_float(sum_fract); meu = 1 / (0.5 + sum); //alpha = 1 and gamma = 0.5. if(meu>=1) { step_size=0x7fff; } else { step_size = float_to_fr16(meu); } //Step size calculation ends---------------------------------- // Lastly, update the filter coefficients _coeff_updater(error_sig, step_size, &state); }
int main() { fract16 data[WINDOW_LENGTH]; int index; for (index = 0; index < WINDOW_LENGTH; index++) { data[index] = float_to_fr16(test_input[index]); } struct Coef shelving_coef = get_shelving_coef(); pre_emphasis(data, WINDOW_LENGTH, shelving_coef); for (index = 0; index < WINDOW_LENGTH; ++index) { float test = fr16_to_float(data[index]); // printf("%f\n", fr16_to_float(data[index])); } return 0; }