aboutsummaryrefslogtreecommitdiff
path: root/lib/equ/iir_cfs.c
blob: f8e6f88a665aa17c45d297ced92803eacb0a2b20 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 *   Copyright (C) 2002-2005  Felipe Rivera <liebremx at users.sourceforge.net>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 *   Coefficient stuff
 *
 *   $Id: iir_cfs.c,v 1.1 2005/10/17 01:57:59 liebremx Exp $
 */

#include "iir_cfs.h"
#include <stdio.h>
#include <math.h>

/*************************** 
 * IIR filter coefficients *
 ***************************/ 
static sIIRCoefficients iir_cf10_11k_11025[10] __attribute__((aligned));
static sIIRCoefficients iir_cf10_22k_22050[10] __attribute__((aligned));
static sIIRCoefficients iir_cforiginal10_44100[10] __attribute__((aligned));
static sIIRCoefficients iir_cforiginal10_48000[10] __attribute__((aligned));
static sIIRCoefficients iir_cf10_44100[10] __attribute__((aligned));
static sIIRCoefficients iir_cf10_48000[10] __attribute__((aligned));
static sIIRCoefficients iir_cf15_44100[15] __attribute__((aligned));
static sIIRCoefficients iir_cf15_48000[15] __attribute__((aligned));
static sIIRCoefficients iir_cf25_44100[25] __attribute__((aligned));
static sIIRCoefficients iir_cf25_48000[25] __attribute__((aligned));
static sIIRCoefficients iir_cf31_44100[31] __attribute__((aligned));
static sIIRCoefficients iir_cf31_48000[31] __attribute__((aligned));

/****************************************************************** 
 * Definitions and data structures to calculate the coefficients
 ******************************************************************/
static const double band_f011k[] =
{ 31, 62, 125, 250, 500, 1000, 2000, 3000, 4000, 5500 
};
static const double band_f022k[] =
{ 31, 62, 125, 250, 500, 1000, 2000, 4000, 8000, 11000 
};
static const double band_f010[] =
{ 31, 62, 125, 250, 500, 1000, 2000, 4000, 8000, 16000 
};
static const double band_original_f010[] =
{ 60, 170, 310, 600, 1000, 3000, 6000, 12000, 14000, 16000 
};
static const double band_f015[] =
{ 25,40,63,100,160,250,400,630,1000,1600,2500,4000,6300,10000,16000
};
static const double band_f025[] =
{ 20,31.5,40,50,80,100,125,160,250,315,400,500,800,
  1000,1250,1600,2500,3150,4000,5000,8000,10000,12500,16000,20000
};
static const double band_f031[] =
{ 20,25,31.5,40,50,63,80,100,125,160,200,250,315,400,500,630,800,
  1000,1250,1600,2000,2500,3150,4000,5000,6300,8000,10000,12500,16000,20000
};

#define GAIN_F0 1.0
#define GAIN_F1 GAIN_F0 / M_SQRT2

#define SAMPLING_FREQ 44100.0
#define TETA(f) (2*M_PI*(double)f/bands[n].sfreq)
#define TWOPOWER(value) (value * value)

#define BETA2(tf0, tf) \
(TWOPOWER(GAIN_F1)*TWOPOWER(cos(tf0)) \
 - 2.0 * TWOPOWER(GAIN_F1) * cos(tf) * cos(tf0) \
 + TWOPOWER(GAIN_F1) \
 - TWOPOWER(GAIN_F0) * TWOPOWER(sin(tf)))
#define BETA1(tf0, tf) \
    (2.0 * TWOPOWER(GAIN_F1) * TWOPOWER(cos(tf)) \
     + TWOPOWER(GAIN_F1) * TWOPOWER(cos(tf0)) \
     - 2.0 * TWOPOWER(GAIN_F1) * cos(tf) * cos(tf0) \
     - TWOPOWER(GAIN_F1) + TWOPOWER(GAIN_F0) * TWOPOWER(sin(tf)))
#define BETA0(tf0, tf) \
    (0.25 * TWOPOWER(GAIN_F1) * TWOPOWER(cos(tf0)) \
     - 0.5 * TWOPOWER(GAIN_F1) * cos(tf) * cos(tf0) \
     + 0.25 * TWOPOWER(GAIN_F1) \
     - 0.25 * TWOPOWER(GAIN_F0) * TWOPOWER(sin(tf)))

#define GAMMA(beta, tf0) ((0.5 + beta) * cos(tf0))
#define ALPHA(beta) ((0.5 - beta)/2.0)

struct {
    sIIRCoefficients *coeffs;
    const double *cfs;
    double octave;
    int band_count;
    double sfreq;
} bands[] = {
  { iir_cf10_11k_11025,     band_f011k,         1.0,     10, 11025.0 },
  { iir_cf10_22k_22050,     band_f022k,         1.0,     10, 22050.0 },
  { iir_cforiginal10_44100, band_original_f010, 1.0,     10, 44100.0 },
  { iir_cforiginal10_48000, band_original_f010, 1.0,     10, 48000.0 },
  { iir_cf10_44100,         band_f010,          1.0,     10, 44100.0 },
  { iir_cf10_48000,         band_f010,          1.0,     10, 48000.0 },
  { iir_cf15_44100,         band_f015,          2.0/3.0, 15, 44100.0 },
  { iir_cf15_48000,         band_f015,          2.0/3.0, 15, 48000.0 },
  { iir_cf25_44100,         band_f025,          1.0/3.0, 25, 44100.0 },
  { iir_cf25_48000,         band_f025,          1.0/3.0, 25, 48000.0 },
  { iir_cf31_44100,         band_f031,          1.0/3.0, 31, 44100.0 },
  { iir_cf31_48000,         band_f031,          1.0/3.0, 31, 48000.0 },
  { 0, 0, 0, 0, 0 }
};

/*************
 * Functions *
 *************/

/* Get the coeffs for a given number of bands and sampling frequency */
sIIRCoefficients* get_coeffs(int *bands, int sfreq)
{
  sIIRCoefficients *iir_cf = 0;
  switch(sfreq)
  {
    case 11025: iir_cf = iir_cf10_11k_11025;
                *bands = 10;
                break;
    case 22050: iir_cf = iir_cf10_22k_22050;
                *bands = 10;
                break;
    case 48000: 
                switch(*bands)
                {
                  case 31: iir_cf = iir_cf31_48000; break;
                  case 25: iir_cf = iir_cf25_48000; break;
                  case 15: iir_cf = iir_cf15_48000; break;
                  default:
                           /*iir_cf = use_xmms_original_freqs ? 
                             iir_cforiginal10_48000 :
                             iir_cf10_48000;*/
                             iir_cf = iir_cforiginal10_48000;
                           break;
                }
                break;
    default:
                switch(*bands)
                {
                  case 31: iir_cf = iir_cf31_44100; break;
                  case 25: iir_cf = iir_cf25_44100; break;
                  case 15: iir_cf = iir_cf15_44100; break;
                  default:
                           /*iir_cf = use_xmms_original_freqs ? 
                             iir_cforiginal10_44100 :
                             iir_cf10_44100;*/
                             iir_cf = iir_cforiginal10_44100;
                           break;
                }
                break;
  }
  return iir_cf;
}

/* Get the freqs at both sides of F0. These will be cut at -3dB */
static void find_f1_and_f2(double f0, double octave_percent, double *f1, double *f2)
{
    double octave_factor = pow(2.0, octave_percent/2.0);
    *f1 = f0/octave_factor;
    *f2 = f0*octave_factor;
}

/* Find the quadratic root
 * Always return the smallest root */
static int find_root(double a, double b, double c, double *x0) {
  double k = c-((b*b)/(4.*a));
  double h = -(b/(2.*a));
  double x1 = 0.;
  if (-(k/a) < 0.)
    return -1; 
  *x0 = h - sqrt(-(k/a));
  x1 = h + sqrt(-(k/a));
  if (x1 < *x0)
    *x0 = x1;
  return 0;
}

/* Calculate all the coefficients as specified in the bands[] array */
void calc_coeffs()
{
  int i, n;
  double f1, f2;
  double x0;

  n = 0;
  for (; bands[n].cfs; n++) {
    double *freqs = (double *)bands[n].cfs;
    for (i=0; i<bands[n].band_count; i++)
    {

      /* Find -3dB frequencies for the center freq */
      find_f1_and_f2(freqs[i], bands[n].octave, &f1, &f2);
      /* Find Beta */
      if ( find_root(
            BETA2(TETA(freqs[i]), TETA(f1)), 
            BETA1(TETA(freqs[i]), TETA(f1)), 
            BETA0(TETA(freqs[i]), TETA(f1)), 
            &x0) == 0)
      {
        /* Got a solution, now calculate the rest of the factors */
        /* Take the smallest root always (find_root returns the smallest one)
         *
         * NOTE: The IIR equation is
         *	y[n] = 2 * (alpha*(x[n]-x[n-2]) + gamma*y[n-1] - beta*y[n-2])
         *  Now the 2 factor has been distributed in the coefficients
         */
        /* Now store the coefficients */
        bands[n].coeffs[i].beta = 2.0 * x0;
        bands[n].coeffs[i].alpha = 2.0 * ALPHA(x0);
        bands[n].coeffs[i].gamma = 2.0 * GAMMA(x0, TETA(freqs[i]));
#ifdef DEBUG
        printf("Freq[%d]: %f. Beta: %.10e Alpha: %.10e Gamma %.10e\n",
            i, freqs[i], bands[n].coeffs[i].beta,
            bands[n].coeffs[i].alpha, bands[n].coeffs[i].gamma);
#endif
      } else {
        /* Shouldn't happen */
        bands[n].coeffs[i].beta = 0.;
        bands[n].coeffs[i].alpha = 0.;
        bands[n].coeffs[i].gamma = 0.;
        printf("  **** Where are the roots?\n");
      }
    }// for i
  }//for n
}