18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * SpanDSP - a series of DSP components for telephony 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * fir.h - General telephony FIR routines 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * Written by Steve Underwood <steveu@coppice.org> 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Copyright (C) 2002 Steve Underwood 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * All rights reserved. 128c2ecf20Sopenharmony_ci */ 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#if !defined(_FIR_H_) 158c2ecf20Sopenharmony_ci#define _FIR_H_ 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci/* 188c2ecf20Sopenharmony_ci Ideas for improvement: 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci 1/ Rewrite filter for dual MAC inner loop. The issue here is handling 218c2ecf20Sopenharmony_ci history sample offsets that are 16 bit aligned - the dual MAC needs 228c2ecf20Sopenharmony_ci 32 bit aligmnent. There are some good examples in libbfdsp. 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci 2/ Use the hardware circular buffer facility tohalve memory usage. 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci 3/ Consider using internal memory. 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci Using less memory might also improve speed as cache misses will be 298c2ecf20Sopenharmony_ci reduced. A drop in MIPs and memory approaching 50% should be 308c2ecf20Sopenharmony_ci possible. 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci The foreground and background filters currenlty use a total of 338c2ecf20Sopenharmony_ci about 10 MIPs/ch as measured with speedtest.c on a 256 TAP echo 348c2ecf20Sopenharmony_ci can. 358c2ecf20Sopenharmony_ci*/ 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci/* 388c2ecf20Sopenharmony_ci * 16 bit integer FIR descriptor. This defines the working state for a single 398c2ecf20Sopenharmony_ci * instance of an FIR filter using 16 bit integer coefficients. 408c2ecf20Sopenharmony_ci */ 418c2ecf20Sopenharmony_cistruct fir16_state_t { 428c2ecf20Sopenharmony_ci int taps; 438c2ecf20Sopenharmony_ci int curr_pos; 448c2ecf20Sopenharmony_ci const int16_t *coeffs; 458c2ecf20Sopenharmony_ci int16_t *history; 468c2ecf20Sopenharmony_ci}; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci/* 498c2ecf20Sopenharmony_ci * 32 bit integer FIR descriptor. This defines the working state for a single 508c2ecf20Sopenharmony_ci * instance of an FIR filter using 32 bit integer coefficients, and filtering 518c2ecf20Sopenharmony_ci * 16 bit integer data. 528c2ecf20Sopenharmony_ci */ 538c2ecf20Sopenharmony_cistruct fir32_state_t { 548c2ecf20Sopenharmony_ci int taps; 558c2ecf20Sopenharmony_ci int curr_pos; 568c2ecf20Sopenharmony_ci const int32_t *coeffs; 578c2ecf20Sopenharmony_ci int16_t *history; 588c2ecf20Sopenharmony_ci}; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci/* 618c2ecf20Sopenharmony_ci * Floating point FIR descriptor. This defines the working state for a single 628c2ecf20Sopenharmony_ci * instance of an FIR filter using floating point coefficients and data. 638c2ecf20Sopenharmony_ci */ 648c2ecf20Sopenharmony_cistruct fir_float_state_t { 658c2ecf20Sopenharmony_ci int taps; 668c2ecf20Sopenharmony_ci int curr_pos; 678c2ecf20Sopenharmony_ci const float *coeffs; 688c2ecf20Sopenharmony_ci float *history; 698c2ecf20Sopenharmony_ci}; 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_cistatic inline const int16_t *fir16_create(struct fir16_state_t *fir, 728c2ecf20Sopenharmony_ci const int16_t *coeffs, int taps) 738c2ecf20Sopenharmony_ci{ 748c2ecf20Sopenharmony_ci fir->taps = taps; 758c2ecf20Sopenharmony_ci fir->curr_pos = taps - 1; 768c2ecf20Sopenharmony_ci fir->coeffs = coeffs; 778c2ecf20Sopenharmony_ci fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL); 788c2ecf20Sopenharmony_ci return fir->history; 798c2ecf20Sopenharmony_ci} 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_cistatic inline void fir16_flush(struct fir16_state_t *fir) 828c2ecf20Sopenharmony_ci{ 838c2ecf20Sopenharmony_ci memset(fir->history, 0, fir->taps * sizeof(int16_t)); 848c2ecf20Sopenharmony_ci} 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_cistatic inline void fir16_free(struct fir16_state_t *fir) 878c2ecf20Sopenharmony_ci{ 888c2ecf20Sopenharmony_ci kfree(fir->history); 898c2ecf20Sopenharmony_ci} 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_cistatic inline int16_t fir16(struct fir16_state_t *fir, int16_t sample) 928c2ecf20Sopenharmony_ci{ 938c2ecf20Sopenharmony_ci int32_t y; 948c2ecf20Sopenharmony_ci int i; 958c2ecf20Sopenharmony_ci int offset1; 968c2ecf20Sopenharmony_ci int offset2; 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci fir->history[fir->curr_pos] = sample; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci offset2 = fir->curr_pos; 1018c2ecf20Sopenharmony_ci offset1 = fir->taps - offset2; 1028c2ecf20Sopenharmony_ci y = 0; 1038c2ecf20Sopenharmony_ci for (i = fir->taps - 1; i >= offset1; i--) 1048c2ecf20Sopenharmony_ci y += fir->coeffs[i] * fir->history[i - offset1]; 1058c2ecf20Sopenharmony_ci for (; i >= 0; i--) 1068c2ecf20Sopenharmony_ci y += fir->coeffs[i] * fir->history[i + offset2]; 1078c2ecf20Sopenharmony_ci if (fir->curr_pos <= 0) 1088c2ecf20Sopenharmony_ci fir->curr_pos = fir->taps; 1098c2ecf20Sopenharmony_ci fir->curr_pos--; 1108c2ecf20Sopenharmony_ci return (int16_t) (y >> 15); 1118c2ecf20Sopenharmony_ci} 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_cistatic inline const int16_t *fir32_create(struct fir32_state_t *fir, 1148c2ecf20Sopenharmony_ci const int32_t *coeffs, int taps) 1158c2ecf20Sopenharmony_ci{ 1168c2ecf20Sopenharmony_ci fir->taps = taps; 1178c2ecf20Sopenharmony_ci fir->curr_pos = taps - 1; 1188c2ecf20Sopenharmony_ci fir->coeffs = coeffs; 1198c2ecf20Sopenharmony_ci fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL); 1208c2ecf20Sopenharmony_ci return fir->history; 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_cistatic inline void fir32_flush(struct fir32_state_t *fir) 1248c2ecf20Sopenharmony_ci{ 1258c2ecf20Sopenharmony_ci memset(fir->history, 0, fir->taps * sizeof(int16_t)); 1268c2ecf20Sopenharmony_ci} 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_cistatic inline void fir32_free(struct fir32_state_t *fir) 1298c2ecf20Sopenharmony_ci{ 1308c2ecf20Sopenharmony_ci kfree(fir->history); 1318c2ecf20Sopenharmony_ci} 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_cistatic inline int16_t fir32(struct fir32_state_t *fir, int16_t sample) 1348c2ecf20Sopenharmony_ci{ 1358c2ecf20Sopenharmony_ci int i; 1368c2ecf20Sopenharmony_ci int32_t y; 1378c2ecf20Sopenharmony_ci int offset1; 1388c2ecf20Sopenharmony_ci int offset2; 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci fir->history[fir->curr_pos] = sample; 1418c2ecf20Sopenharmony_ci offset2 = fir->curr_pos; 1428c2ecf20Sopenharmony_ci offset1 = fir->taps - offset2; 1438c2ecf20Sopenharmony_ci y = 0; 1448c2ecf20Sopenharmony_ci for (i = fir->taps - 1; i >= offset1; i--) 1458c2ecf20Sopenharmony_ci y += fir->coeffs[i] * fir->history[i - offset1]; 1468c2ecf20Sopenharmony_ci for (; i >= 0; i--) 1478c2ecf20Sopenharmony_ci y += fir->coeffs[i] * fir->history[i + offset2]; 1488c2ecf20Sopenharmony_ci if (fir->curr_pos <= 0) 1498c2ecf20Sopenharmony_ci fir->curr_pos = fir->taps; 1508c2ecf20Sopenharmony_ci fir->curr_pos--; 1518c2ecf20Sopenharmony_ci return (int16_t) (y >> 15); 1528c2ecf20Sopenharmony_ci} 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci#endif 155