File: filter_design.cpp

package info (click to toggle)
libitpp 4.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,520 kB
  • ctags: 6,341
  • sloc: cpp: 51,608; sh: 9,248; makefile: 636; fortran: 8
file content (285 lines) | stat: -rw-r--r-- 8,137 bytes parent folder | download
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*!
 * \file
 * \brief Filter design functions
 * \author Tony Ottosson
 *
 * -------------------------------------------------------------------------
 *
 * IT++ - C++ library of mathematical, signal processing, speech processing,
 *        and communications classes and functions
 *
 * Copyright (C) 1995-2008  (see AUTHORS file for a list of contributors)
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * -------------------------------------------------------------------------
 */

#include <itpp/signal/filter_design.h>
#include <itpp/signal/poly.h>
#include <itpp/signal/filter.h>
#include <itpp/signal/transforms.h>
#include <itpp/base/math/elem_math.h>
#include <itpp/base/algebra/ls_solve.h>
#include <itpp/base/matfunc.h>
#include <itpp/base/specmat.h>
#include <itpp/base/math/trig_hyp.h>
#include <itpp/base/converters.h>


namespace itpp {


  void polystab(const vec &a, vec &out)
  {
    cvec r;
    roots(a, r);

    for (int i=0; i<r.size(); i++) {
      if (abs(r(i)) > 1)
	r(i) = std::complex<double>(1.0)/conj(r(i));
    }
    out = real(std::complex<double>(a(0)) * poly(r));
  }

  void polystab(const cvec &a, cvec &out)
  {
    cvec r;
    roots(a, r);

    for (int i=0; i<r.size(); i++) {
      if (abs(r(i)) > 1)
	r(i) = std::complex<double>(1.0)/conj(r(i));
    }
    out = a(0) * poly(r);
  }


  // ----------------------- freqz() ---------------------------------------------------------
  void freqz(const cvec &b, const cvec &a, const int N, cvec &h, vec &w)
  {
    w = pi*linspace(0, N-1, N)/double(N);

    cvec ha, hb;
    hb = fft( b, 2*N );
    hb = hb(0, N-1);

    ha = fft( a, 2*N );
    ha = ha(0, N-1);

    h = elem_div(hb, ha);
  }

  cvec freqz(const cvec &b, const cvec &a, const int N)
  {
    cvec h;
    vec w;

    freqz(b, a, N, h, w);

    return h;
  }


  cvec freqz(const cvec &b, const cvec &a, const vec &w)
  {
    int la = a.size(), lb = b.size(), k = std::max(la, lb);

    cvec h, ha, hb;

    // Evaluate the nominator and denominator at the given frequencies
    hb = polyval( zero_pad(b, k), to_cvec(cos(w), sin(w)) );
    ha = polyval( zero_pad(a, k), to_cvec(cos(w), sin(w)) );

    h = elem_div(hb, ha);

    return h;
  }

  void freqz(const vec &b, const vec &a, const int N, cvec &h, vec &w)
  {
    w = pi*linspace(0, N-1, N)/double(N);

    cvec ha, hb;
    hb = fft_real( b, 2*N );
    hb = hb(0, N-1);

    ha = fft_real( a, 2*N );
    ha = ha(0, N-1);

    h = elem_div(hb, ha);
  }

  cvec freqz(const vec &b, const vec &a, const int N)
  {
    cvec h;
    vec w;

    freqz(b, a, N, h, w);

    return h;
  }


  cvec freqz(const vec &b, const vec &a, const vec &w)
  {
    int la = a.size(), lb = b.size(), k = std::max(la, lb);

    cvec h, ha, hb;

    // Evaluate the nominator and denominator at the given frequencies
    hb = polyval( zero_pad(b, k), to_cvec(cos(w), sin(w)) );
    ha = polyval( zero_pad(a, k), to_cvec(cos(w), sin(w)) );

    h = elem_div(hb, ha);

    return h;
  }



  void filter_design_autocorrelation(const int N, const vec &f, const vec &m, vec &R)
  {
    it_assert(f.size() == m.size(), "filter_design_autocorrelation: size of f and m vectors does not agree");
    int N_f = f.size();

    it_assert(f(0) == 0.0, "filter_design_autocorrelation: first frequency must be 0.0");
    it_assert(f(N_f-1) == 1.0, "filter_design_autocorrelation: last frequency must be 1.0");

    // interpolate frequency-response
    int N_fft = 512;
    vec m_interp(N_fft+1);
    // unused variable:
    // double df_interp = 1.0/double(N_fft);

    m_interp(0) = m(0);
    double inc;

    int jstart = 0, jstop;

    for (int i=0; i<N_f-1; i++) {
      // calculate number of points to the next frequency
      jstop = floor_i( f(i+1)*(N_fft+1) ) - 1;
      //std::cout << "jstart=" << jstart << "jstop=" << jstop << std::endl;

      for (int j=jstart; j<=jstop; j++) {
	inc = double(j-jstart)/double(jstop-jstart);
	m_interp(j) = m(i)*(1-inc) + m(i+1)*inc;
      }
      jstart = jstop+1;
    }

    vec S = sqr(concat( m_interp, reverse(m_interp(2,N_fft)) )); // create a complete frequency response with also negative frequencies

    R = ifft_real(to_cvec(S)); // calculate correlation

    R = R.left(N);
  }


  // Calculate the AR coefficients of order \c n of the ARMA-process defined by the autocorrelation R
  // using the deternined modified Yule-Walker method
  // maxlag determines the size of the system to solve N>= n.
  // If N>m then the system is overdetermined and a least squares solution is used.
  // as a rule of thumb use N = 4*n
  void modified_yule_walker(const int m, const int n, const int N, const vec &R, vec &a)
  {
    it_assert(m>0, "modified_yule_walker: m must be > 0");
    it_assert(n>0, "modified_yule_walker: n must be > 0");
    it_assert(N <= R.size(), "modified_yule_walker: autocorrelation function too short");

    // create the modified Yule-Walker equations Rm * a = - rh
    // see eq. (3.7.1) in Stoica and Moses, Introduction to spectral analysis
    int M = N - m - 1;

    mat Rm;
    if(m-n+1 < 0)
      Rm= toeplitz( R(m, m+M-1), reverse(concat( R(1,std::abs(m-n+1)), R(0,m) ) ) );
    else
      Rm= toeplitz( R(m, m+M-1), reverse(R(m-n+1,m)) );


    vec rh = - R(m+1, m+M);

    // solve overdetermined system
    a = backslash(Rm, rh);

    // prepend a_0 = 1
    a = concat(1.0, a);

    // stabilize polynomial
    a = polystab(a);
  }



  void arma_estimator(const int m, const int n, const vec &R, vec &b, vec &a)
  {
    it_assert(m>0, "arma_estimator: m must be > 0");
    it_assert(n>0, "arma_estimator: n must be > 0");
    it_assert(2*(m+n)<=R.size(), "arma_estimator: autocorrelation function too short");


    // windowing the autocorrelation
    int N = 2*(m+n);
    vec Rwindow = elem_mult(R.left(N), 0.54 + 0.46*cos( pi*linspace(0.0, double(N-1), N)/double(N-1) ) ); // Hamming windowing

    // calculate the AR part using the overdetmined Yule-Walker equations
    modified_yule_walker(m, n, N, Rwindow, a);

    // --------------- Calculate MA part --------------------------------------
    // use method in ref [2] section VII.
    vec r_causal = Rwindow;
    r_causal(0) *= 0.5;

    vec h_inv_a = filter(1, a, concat(1.0, zeros(N-1))); // see eq (50) of [2]
    mat H_inv_a = toeplitz(h_inv_a, concat(1.0, zeros(m)));

    vec b_causal = backslash(H_inv_a, r_causal);

    // calculate the double-sided spectrum
    int N_fft = 256;
    vec H = 2.0*real(elem_div(fft_real(b_causal, N_fft), fft_real(a, N_fft))); // calculate spectrum

    // Do weighting and windowing in cepstrum domain
    cvec cepstrum = log(to_cvec(H));
    cvec q = ifft(cepstrum);

    // keep only causal part of spectrum (windowing)
    q.set_subvector(N_fft/2, N_fft-1, zeros_c(N_fft/2) );
    q(0) *= 0.5;

    cvec h = ifft(exp(fft(q))); // convert back to frequency domain, from cepstrum and do inverse transform to calculate impulse response
    b = real(backslash(to_cmat(H_inv_a), h(0,N-1))); // use Shank's method to calculate b coefficients
  }


  void yulewalk(const int N, const vec &f, const vec &m, vec &b, vec &a)
  {
    it_assert(f.size() == m.size(), "yulewalk: size of f and m vectors does not agree");
    int N_f = f.size();

    it_assert(f(0) == 0.0, "yulewalk: first frequency must be 0.0");
    it_assert(f(N_f-1) == 1.0, "yulewalk: last frequency must be 1.0");


    vec R;
    filter_design_autocorrelation(4*N, f, m, R);

    arma_estimator(N, N, R, b, a);
  }


} // namespace itpp