1function fbr = eq_fir_blob_quant(b, bits, strip_trailing_zeros) 2 3%% Quantize FIR coefficients and return vector with length, 4% out shift, and coefficients to be used in the setup blob. 5% 6% fbr = eq_fir_blob_resp(b, bits) 7% b - FIR coefficients 8% bits - optional number of bits, defaults to 16 9% 10% fbr - vector with length, in shift, out shift, and quantized coefficients 11% 12 13% SPDX-License-Identifier: BSD-3-Clause 14% 15% Copyright (c) 2016, Intel Corporation. All rights reserved. 16% 17% Author: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com> 18 19b = (b(:))'; 20 21if nargin < 3 22 strip_trailing_zeros = 1; 23end 24 25if nargin < 2 26 bits = 16; 27end 28 29%% Quantize 30[bq, shift] = eq_fir_quantize(b, bits); 31 32%% Check trailing zeros 33nf = length(bq); 34nz = nf; 35while bq(nz) == 0 36 nz = nz - 1; 37end 38if nz < nf && strip_trailing_zeros 39 nb = nz + 1; 40 fprintf(1, 'Note: Filter length was reduced '); 41 fprintf(1, 'to %d -> %d due to trailing zeros.\n', nf, nb); 42 bq = bq(1:nb); 43else 44 nb = nf; 45end 46 47%% Make length multiple of four (optimized FIR core) 48mod4 = mod(nb, 4); 49if mod4 > 0 50 pad = zeros(1,4-mod4); 51 bqp = [bq pad]; 52 nnew = length(bqp); 53 fprintf(1,'Note: Filter length was %d, padded length into %d.\n', ... 54 nb, nnew); 55else 56 fprintf(1,'Note: Filter length is %d\n', nb); 57 nnew = nb; 58 bqp = bq; 59end 60 61%% Pack data into FIR coefficient format 62% int16_t length 63% int16_t out_shift 64% uint32_t reserved[4] 65% int16_t coef[] 66fbr = [nnew shift 0 0 0 0 0 0 0 0 bqp]; 67 68end 69 70function [bq, shift] = eq_fir_quantize(b, bits) 71 72% [bq, shift] = eq_fir_quantize(b, bits) 73% 74% Inputs 75% b - FIR coefficients 76% bits - number bits for 2s complement coefficient 77% 78% Outputs 79% bq - quantized coefficients 80% shift - shift right parameter to apply after FIR computation to 81% compensate for coefficients scaling 82% 83 84scale = 2^(bits-1); 85 86%% Output shift for coefficients 87m = max(abs(b)); 88shift = -ceil(log(m+1/scale)/log(2)); 89bsr = b*2^shift; 90 91%% Quantize to Q1.bits-1 format, e.g. Q1.15 for 16 bits 92bq = eq_coef_quant(bsr, bits, bits-1); 93 94end 95