spandsp 0.0.6

biquad.h
1/*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * biquad.h - General telephony bi-quad section routines (currently this just
5 * handles canonic/type 2 form)
6 *
7 * Written by Steve Underwood <steveu@coppice.org>
8 *
9 * Copyright (C) 2001 Steve Underwood
10 *
11 * All rights reserved.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU Lesser General Public License version 2.1,
15 * as published by the Free Software Foundation.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27/*! \page biquad_page Bi-quadratic filter sections
28\section biquad_page_sec_1 What does it do?
29???.
30
31\section biquad_page_sec_2 How does it work?
32???.
33*/
34
35#if !defined(_SPANDSP_BIQUAD_H_)
36#define _SPANDSP_BIQUAD_H_
37
38 typedef struct
39{
40 int32_t gain;
41 int32_t a1;
42 int32_t a2;
43 int32_t b1;
44 int32_t b2;
45
46 int32_t z1;
47 int32_t z2;
48
49#if FIRST_ORDER_NOISE_SHAPING
50 int32_t residue;
51#elif SECOND_ORDER_NOISE_SHAPING
52 int32_t residue1;
53 int32_t residue2;
54#endif
55} biquad2_state_t;
56
57#if defined(__cplusplus)
58 extern "C"
59{
60#endif
61
62 static __inline__ void biquad2_init(biquad2_state_t *bq,
63 int32_t gain,
64 int32_t a1,
65 int32_t a2,
66 int32_t b1,
67 int32_t b2)
68{
69 bq->gain = gain;
70 bq->a1 = a1;
71 bq->a2 = a2;
72 bq->b1 = b1;
73 bq->b2 = b2;
74
75 bq->z1 = 0;
76 bq->z2 = 0;
77
78#if FIRST_ORDER_NOISE_SHAPING
79 bq->residue = 0;
80#elif SECOND_ORDER_NOISE_SHAPING
81 bq->residue1 = 0;
82 bq->residue2 = 0;
83#endif
84}
85/*- End of function --------------------------------------------------------*/
86
87 static __inline__ int16_t biquad2(biquad2_state_t *bq, int16_t sample)
88{
89 int32_t y;
90 int32_t z0;
91
92 z0 = sample*bq->gain + bq->z1*bq->a1 + bq->z2*bq->a2;
93 y = z0 + bq->z1*bq->b1 + bq->z2*bq->b2;
94
95 bq->z2 = bq->z1;
96 bq->z1 = z0 >> 15;
97#if FIRST_ORDER_NOISE_SHAPING
98 y += bq->residue;
99 bq->residue = y & 0x7FFF;
100#elif SECOND_ORDER_NOISE_SHAPING
101 y += (2*bq->residue1 - bq->residue2);
102 bq->residue2 = bq->residue1;
103 bq->residue1 = y & 0x7FFF;
104#endif
105 y >>= 15;
106 return (int16_t) y;
107}
108/*- End of function --------------------------------------------------------*/
109
110#if defined(__cplusplus)
111}
112#endif
113
114#endif
115/*- End of file ------------------------------------------------------------*/
biquad2_state_t
Definition biquad.h:39