xref: /openbmc/linux/drivers/isdn/mISDN/dsp_biquad.h (revision 58e16d792a6a8c6b750f637a4649967fcac853dc)
1*74ba9207SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2960366cfSKarsten Keil /*
3960366cfSKarsten Keil  * SpanDSP - a series of DSP components for telephony
4960366cfSKarsten Keil  *
5960366cfSKarsten Keil  * biquad.h - General telephony bi-quad section routines (currently this just
6960366cfSKarsten Keil  *            handles canonic/type 2 form)
7960366cfSKarsten Keil  *
8960366cfSKarsten Keil  * Written by Steve Underwood <steveu@coppice.org>
9960366cfSKarsten Keil  *
10960366cfSKarsten Keil  * Copyright (C) 2001 Steve Underwood
11960366cfSKarsten Keil  *
12960366cfSKarsten Keil  * All rights reserved.
13960366cfSKarsten Keil  */
14960366cfSKarsten Keil 
15960366cfSKarsten Keil struct biquad2_state {
16960366cfSKarsten Keil 	int32_t gain;
17960366cfSKarsten Keil 	int32_t a1;
18960366cfSKarsten Keil 	int32_t a2;
19960366cfSKarsten Keil 	int32_t b1;
20960366cfSKarsten Keil 	int32_t b2;
21960366cfSKarsten Keil 
22960366cfSKarsten Keil 	int32_t z1;
23960366cfSKarsten Keil 	int32_t z2;
24960366cfSKarsten Keil };
25960366cfSKarsten Keil 
biquad2_init(struct biquad2_state * bq,int32_t gain,int32_t a1,int32_t a2,int32_t b1,int32_t b2)26960366cfSKarsten Keil static inline void biquad2_init(struct biquad2_state *bq,
27960366cfSKarsten Keil 				int32_t gain, int32_t a1, int32_t a2, int32_t b1, int32_t b2)
28960366cfSKarsten Keil {
29960366cfSKarsten Keil 	bq->gain = gain;
30960366cfSKarsten Keil 	bq->a1 = a1;
31960366cfSKarsten Keil 	bq->a2 = a2;
32960366cfSKarsten Keil 	bq->b1 = b1;
33960366cfSKarsten Keil 	bq->b2 = b2;
34960366cfSKarsten Keil 
35960366cfSKarsten Keil 	bq->z1 = 0;
36960366cfSKarsten Keil 	bq->z2 = 0;
37960366cfSKarsten Keil }
38960366cfSKarsten Keil 
biquad2(struct biquad2_state * bq,int16_t sample)39960366cfSKarsten Keil static inline int16_t biquad2(struct biquad2_state *bq, int16_t sample)
40960366cfSKarsten Keil {
41960366cfSKarsten Keil 	int32_t y;
42960366cfSKarsten Keil 	int32_t z0;
43960366cfSKarsten Keil 
44960366cfSKarsten Keil 	z0 = sample * bq->gain + bq->z1 * bq->a1 + bq->z2 * bq->a2;
45960366cfSKarsten Keil 	y = z0 + bq->z1 * bq->b1 + bq->z2 * bq->b2;
46960366cfSKarsten Keil 
47960366cfSKarsten Keil 	bq->z2 = bq->z1;
48960366cfSKarsten Keil 	bq->z1 = z0 >> 15;
49960366cfSKarsten Keil 	y >>= 15;
50960366cfSKarsten Keil 	return  y;
51960366cfSKarsten Keil }
52