-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12_Parks-McClellan-Filter-Design.py
779 lines (779 loc) · 52.7 KB
/
12_Parks-McClellan-Filter-Design.py
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# The Parks-McClellan Filter Design Method\n",
"\n",
"The Parks-McClellan (PM) algorithm is a clever application of advanced polynomial fitting techniques to the problem of FIR filter design. In this notebook we will explore the key ideas behind the method by considering the design of a simple Type-I lowpass filter."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"application/javascript": [
"IPython.OutputArea.prototype._should_scroll = function(lines) {\n",
" return false;\n",
"}"
],
"text/plain": [
"<IPython.core.display.Javascript object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%javascript\n",
"IPython.OutputArea.prototype._should_scroll = function(lines) {\n",
" return false;\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# standard bookkeeping\n",
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from IPython.display import Audio, display\n",
"from ipywidgets import interactive, fixed"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"plt.rcParams[\"figure.figsize\"] = (14,4)"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"**IMPORTANT** \n",
"This notebook uses interactive widgets. If the interactive controls do not work or show up, please follow the instructions detailed here: https://github.com/jupyter-widgets/ipywidgets/blob/master/README.md"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The first intuition is to recognize that finding the filter's coefficients is equivalent to solving a polynomial fitting problem. Since we want a linear-phase filter, the filter will be symmetric and we can easily show that in this case its frequency response is the real-valued function:\n",
"\n",
"$$\n",
" H(e^{j\\omega}) = h[0] + \\sum_{n = 1}^{M} 2h[n]\\cos n\\omega\n",
"$$\n",
"\n",
"In the above expression, the $N = 2M+1$ nonzero taps of the impulse response $h[n]$ are $h[-M]$ to $h[M]$. Given a (positive) passband $[0, \\omega_p]$ and a stopband $[\\omega_s, \\pi]$, we need to fit $H(e^{j\\omega})$ to one in the passband and zero in the stopband as in the following figure. \n",
"\n",
"<img src=\"specs.png\" alt=\"Drawing\" style=\"width: 600px;\"/>\n",
"\n",
"Enter [Chebishev polynomials](https://en.wikipedia.org/wiki/Chebyshev_polynomials) (CP): they have a ton of interesting properties but the relevant one here is that $T_n(\\cos\\omega) = \\cos n\\omega$, where $T_n(x)$ is the CP of order $n$. Since the coefficients for the CP are easy to determine, with a simple substitution we can write:\n",
"\n",
"$$\n",
" H(e^{j\\omega}) = \\sum_{n = 0}^{M}p_n x^n \\qquad \\mbox{with $x = \\cos\\omega$}\n",
"$$\n",
"\n",
"While the relation between the $M+1$ $p_n$ coefficients and the $2M+1$ filter coefficients is nonlinear, it is easily invertible and therefore we can just concentrate on fitting the degree-$M$ polynomial over the desired response."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For the sake of simplicity, let's skip some of the details associated to the Chebishev substitution (for instance, the mapping $x=\\cos\\omega$ reverses the abscissae) and let's examine the following equivalent problem:\n",
"\n",
"> consider the desired function: $$ D(x) = \\begin{cases} 1 & \\mbox{for $x\\in [0, A]$} \\\\ 0 & \\mbox{for $x\\in [B,1]$} \\end{cases}$$ <br> find the coefficients of a degree-$M$ polynomial $P(x)$ so that $P(x)\\approx D(x)$ over $[0,A]\\cup [B,1]$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Standard polynomial fitting\n",
"\n",
"The simplest approach is to use standard polynomial fitting: choose $a$ points in $[0, A]$ and $b$ points in $[B, 1]$ so that $a+b = M+1$ and find an interpolator over the coordinate pairs:\n",
"\n",
"$$\n",
" (x_0, 1), (x_1, 1), \\ldots, (x_{a-1}, 1), (x_a, 0), (x_{a+1}, 0), \\ldots, (x_{a+b-1}, 0)\n",
"$$\n",
"\n",
"The result will minimize the mean square error between the interpolator and the piecewise characteristic we are trying to approximate. \n",
"\n",
"We can write a simple Python function to test this approach; you can play with the sliders below and see the result of the plain interpolation as the order and the size of the intervals change:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def MSE_fit(A, B, order):\n",
" if order < 3:\n",
" raise ValueError(\"order is too small, we need at least 3\")\n",
" \n",
" # interpolation points always one more than the order of the interpolator\n",
" pts = order+1\n",
" # split number of interpolation points across intervals proportionately \n",
" # with the length of each interval \n",
" ptsA = int(pts * A / (A+(1-B)))\n",
" if ptsA < 2:\n",
" ptsA = 2\n",
" ptsB = pts - ptsA\n",
" \n",
" # for the MSE fit, place a point at each interval edge and distribute the rest\n",
" # (if any) evenly over the interval\n",
" x = np.concatenate((\n",
" np.arange(0, ptsA) * (A / (ptsA-1)),\n",
" B + np.arange(0, ptsB) * ((1-B) / (ptsB-1)) \n",
" ))\n",
" y = np.concatenate((\n",
" np.ones(ptsA),\n",
" np.zeros(ptsB) \n",
" ))\n",
" \n",
" # now just solve the linear interpolation problem\n",
" p = np.poly1d(np.polyfit(x, y, order))\n",
" return p, x, y"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def MSE_fit_show(A=0.4, B=0.6, order=10):\n",
" p, x, y = MSE_fit(A, B, order)\n",
" \n",
" t = np.linspace(0, 1, 100)\n",
" lims = [(0,1,-.5,1.5), (0,A,0.8,1.2), (B,1,-0.2,0.2)]\n",
" for n, r in enumerate(lims):\n",
" plt.subplot(1,3,n+1)\n",
" plt.plot((0,A), (1,1), 'red', \n",
" (B,1), (0,0), 'red', \n",
" x, y, 'oy', \n",
" t, p(t), '-')\n",
" plt.xlim(r[0], r[1])\n",
" plt.ylim(r[2], r[3]) "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "76fe6665a40d4775b14106114006d99c",
"version_major": 2,
"version_minor": 0
},
"text/html": [
"<p>Failed to display Jupyter Widget of type <code>interactive</code>.</p>\n",
"<p>\n",
" If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n",
" that the widgets JavaScript is still loading. If this message persists, it\n",
" likely means that the widgets JavaScript library is either not installed or\n",
" not enabled. See the <a href=\"https://ipywidgets.readthedocs.io/en/stable/user_install.html\">Jupyter\n",
" Widgets Documentation</a> for setup instructions.\n",
"</p>\n",
"<p>\n",
" If you're reading this message in another frontend (for example, a static\n",
" rendering on GitHub or <a href=\"https://nbviewer.jupyter.org/\">NBViewer</a>),\n",
" it may mean that your frontend doesn't currently support widgets.\n",
"</p>\n"
],
"text/plain": [
"interactive(children=(FloatSlider(value=0.4, description='A', max=0.5), FloatSlider(value=0.6, description='B', max=1.0, min=0.5), IntSlider(value=10, description='order', max=30, min=3), Output()), _dom_classes=('widget-interact',))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"v = interactive(MSE_fit_show, order=(3,30), A=(0.0, 0.5), B=(0.5, 1.0))\n",
"display(v)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"As you can see, simple polynomial interpolation, while minimizing the MSE has two problems:\n",
"\n",
" * it becomes numerically unstable as soon as the order of the interpolation exceeds 16 or 17\n",
" * although the MSE is minimized, the **maximum** error can become very large\n",
" \n",
"Because of these problems, direct interpolation is rarely used in numerical analysis and filter design is no exception. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Minimax fitting\n",
"\n",
"As we said, the first clever intuition behind the the Parks-McClellan algorithm is rephrasing the filter design problem as a polynomial interpolation. The real stroke of genius, however, is the use of a different kind of polynomial fitting called *minimax* approximation. In this kind of fitting the goal is to minimixe the *maximum* error between the polynomial and the desired function over the intervals of interest:\n",
"\n",
"$$\n",
" E = \\min\\max_{[0,A]\\cup [B,1]} | P(x) - D(x) |\n",
"$$\n",
"\n",
"This is brilliant for two reasons: \n",
"\n",
"* the minimax criterion gives us a guarantee on the worst-case error for the filter response\n",
"* an obscure yet powerful theorem, called the **alternation theorem**, gives us a remarkably straightforward recipe to build a robust numerical algorithm that finds the solution."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### The Alternation Theorem\n",
"\n",
"Because of the fundamental theorem of algebra, a non-constant polynomial cannot be constant over an interval. Since our polynomial cannot be a constant (it needs to move from 1 in the passband to zero in the stopband), it will necessarily oscillate over the approximation intervals. As you could see from the previous demo, MSE minimization tries to keep the oscillations small over the approximation intervals, but the price to pay is potentially wide oscillations at the band edges; on the other hand, the minimax approximation will allow for oscillations that are larger overall but that do not swing wildly. Since the polynomial oscillates around the target value, the error will oscillate between positive and negative peaks; the alternation theorem states that \n",
"\n",
"> $P(x)$ is the minimax approximation to $D(x)$ if and only if $P(x) - D(x)$ alternates $M+2$ times between $+E$ and $-E$ over $[0,A]\\cup [B,1]$\n",
"\n",
"The alternation theorem gives one thing right away: the ability to recognize if a polynomial is the minimax solution. All we need to do is look at the extrema of the error and check that \n",
"\n",
"* they are $M+2$\n",
"* they alternate in sign\n",
"* their magnitude is exactly the same\n",
"\n",
"We can write a simple Python function to find the extrema of the error:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def find_error_extrema(p, A, B):\n",
" intervals = {\n",
" (0, A): 1, \n",
" (B, 1): 0\n",
" }\n",
" loc = [] # locations of the extrema\n",
" err = [] # values of the extrema\n",
" for rng, val in intervals.items():\n",
" # we don't need enormous precision, 100 points per interval will do\n",
" t = np.linspace(rng[0], rng[1], 100)\n",
" y = val - p(t) # error values\n",
" # this finds all the points where the error changes sign:\n",
" ix = np.diff(np.sign(np.diff(y))).nonzero()[0] + 1 # local min+max\n",
" loc = np.hstack((loc, t[0], t[ix], t[-1]))\n",
" err = np.hstack((err, y[0], y[ix], y[-1]))\n",
" return loc, err"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With this, it's easy to verify that the MSE fit does not satisfy the alternation theorem: the magnitude of the peaks is not constant."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAzwAAAD8CAYAAACy9qHHAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAIABJREFUeJzs3Xd8VeXhx/HPc+/NzZ4kEAhhb2SHEW2dFVx1z7rqQsVRVx3tr9Xa/qrWWqmt4I8q1WrFUVvFKm4FW1lhyYYwQkISCITsdcfz+yOxjRBIgEtOxvf9euWVnHOee843Kabnm3POc421FhERERERkY7I5XQAERERERGRY0WFR0REREREOiwVHhERERER6bBUeEREREREpMNS4RERERERkQ5LhUdERERERDosFR4REREREemwVHhERERERKTD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SJGm0HHgkSZIkjZYDjyRJkqTRcuCRJEmSNFoOPJIkSZJGy4FHkiRJ0mg58EiSJEkarf8BbhntXSXcxb4AAAAASUVORK5CYII=\n",
"text/plain": [
"<matplotlib.figure.Figure at 0x1de1bb51828>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"A = 0.4\n",
"B = 0.6\n",
"p, x, y = MSE_fit(A, B, 8)\n",
"\n",
"loc, err = find_error_extrema(p, 0.4, 0.6)\n",
"\n",
"t = np.linspace(0, 1, 100)\n",
"plt.plot(loc, p(loc), 'or', t, p(t), '-')\n",
"plt.plot((0,A), (1,1), 'red', \n",
" (B,1), (0,0), 'red', \n",
" t, p(t), '-',\n",
" loc, p(loc), 'or');"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"### The Remez Exchange Algorithm\n",
"\n",
"The alternation theorem provides us also with a very efficient way to find the coefficients of $P(x)$. Suppose we knew the exact ordinates $x_i$ of the $M+2$ alternations; in this case we could solve the following system of linear equations\n",
"\n",
"$$\n",
" \\left\\{\\begin{array}{lcl}\n",
" p_0 + p_1 x_0 + p_2 x_0^2 + \\ldots + p_Mx_0^M + (-1)^0\\epsilon &=& D(x_0) \\\\ \n",
" p_0 + p_1 x_1 + p_2 x_1^2 + \\ldots + p_Mx_1^M + (-1)^1\\epsilon &=& D(x_1) \\\\ \n",
" \\ldots \\\\ \n",
" p_0 + p_1 x_{M+1} + p_2 x_{M+1}^2 + \\ldots + p_Mx_{M+1}^M + (-1)^{M+1}\\epsilon &=& D(x_{M+1}) \n",
" \\end{array}\\right.\n",
"$$\n",
"\n",
"and find in one go both the $M+1$ polynomial coefficients *and* the value of the minimax error $E=|\\epsilon|$ (we use $\\epsilon$ instead of $E$ in the linear system because we don't know the sign of the first alternation, it could be positive or negative). Although the above is a non-standard linear system of equations, it can be shown rather easily that, as long as the $x_i$ are distinct, it does have a solution. \n",
"\n",
"Of course we don't know the $x_i$ in advance but we can start with a guess, and solve the system anyway. Once we find the polynomial coefficients from the guess, we use Remez's exchange algorithm:\n",
"\n",
"* find the locations for the maxima and minima of the error for the $P(x)$ we just found\n",
"* if the extrema satisfy the alternation theorem, we are done\n",
"* otherwise, use the new locations as a new guess, solve the system again and repeat.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"The Remez algorithm is remarkably fast and robust. Here is an implementation you can play with. First, we need an auxiliary function to solve the system of equations above; we will use standard linear algebra functions."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def solve(x, y):\n",
" # simple solver for the extended interpolation problem\n",
" # first build a Vandermonde matrix using the interpolation locations\n",
" # There are N+2 locations, so the matrix will be (N+2)x(N+2) but we \n",
" # will replace the last column with the error sign\n",
" V = np.vander(x, increasing=True)\n",
" # replace last column\n",
" V[:,-1] = pow(-1, np.arange(0, len(x)))\n",
" # just solve Ax = y\n",
" v = np.linalg.solve(V, y)\n",
" # need to reverse the vector because poly1d starts with the highest degree\n",
" p = np.poly1d(v[-2::-1])\n",
" e = np.abs(v[-1])\n",
" return p, e"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"And here comes the main course: the Remez routine. The code is quite straightforward; it doesn't have a termination conditions since the number of iterations is passed as a parameter (we want to be able to show intermediate results)."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def remez_fit(A, B, order, iterations):\n",
" if order < 3:\n",
" raise ValueError(\"order is too small, we need at least 3\")\n",
" pts = order+2\n",
" \n",
" # initial choice of interpolation points: distribute them evenly\n",
" # across the two regions as a proportion of each region's width\n",
" ptsA = int(pts * A / (A-B+1))\n",
" if ptsA < 2:\n",
" ptsA = 2\n",
" ptsB = pts - ptsA\n",
" \n",
" x = np.concatenate((\n",
" np.arange(1, ptsA+1) * (A / (ptsA+1)),\n",
" B + np.arange(1, ptsB+1) * ((1-B) / (ptsB+1)) \n",
" ))\n",
" y = np.concatenate((\n",
" np.ones(ptsA),\n",
" np.zeros(ptsB) \n",
" ))\n",
" \n",
" # the \"data\" dictionary only holds values that we will use in plotting\n",
" data = {}\n",
" \n",
" for n in range(0, iterations):\n",
" # previous interpolation points\n",
" data['prev_x'] = x\n",
" data['prev_y'] = y\n",
" \n",
" # solve the interpolation problem \n",
" p, e = solve(x, y)\n",
" data['err'] = e\n",
" # find the extrema of the error\n",
" loc, err = find_error_extrema(p, A, B) \n",
" \n",
" # find the alternations \n",
" alt = []\n",
" for n in range(0, len(loc)):\n",
" # each extremum is a new candidate for an alternation\n",
" c = {\n",
" 'loc': loc[n],\n",
" 'sign': np.sign(err[n]),\n",
" 'err_mag': np.abs(err[n])\n",
" }\n",
" # only keep extrema that are larger or equal than the minimum\n",
" # error returned by the interpolation solution\n",
" if c['err_mag'] >= e - 1e-3:\n",
" # ensure that the error alternates; if the candidate has the \n",
" # same sign, replace the last alternation with the candidate\n",
" # if its error value is larger\n",
" if alt == [] or alt[-1]['sign'] != c['sign']:\n",
" alt.append(c)\n",
" elif alt[-1]['err_mag'] < c['err_mag']:\n",
" alt.pop()\n",
" alt.append(c)\n",
" \n",
" # if there are more than the necessary number of alternations, trim \n",
" # from the left or the right keeping the largest errors\n",
" while len(alt) > order + 2:\n",
" if alt[0]['err_mag'] > alt[-1]['err_mag']:\n",
" alt.pop(-1)\n",
" else:\n",
" alt.pop(0)\n",
" \n",
" # the new set of interpolation points\n",
" x = [c['loc'] for c in alt]\n",
" y = [1 if c <= A else 0 for c in x]\n",
" data['new_x'] = x\n",
"\n",
" return p, data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, a simple auxiliary function to plot the results; the yellow dots indicate the guess used for the current interpolation while the blue stars show the new maxima that will be used as the new guess. As you can see the algorithm converges very rapidly. The error in passband and stopband is shown magnified in the bottom panels."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def remez_fit_show(A=0.4, B=0.6, order=5, iterations=1):\n",
" p, data = remez_fit(A, B, order, iterations)\n",
" t = np.linspace(0, 1, 200)\n",
" \n",
" def loc_plot(A, B, data): \n",
" e = data['err']\n",
" plt.plot((0,A), (1,1), 'red',\n",
" (B,1), (0,0), 'red', \n",
" (0,A), (1+e,1+e), 'cyan', (0,A), (1-e,1-e), 'cyan',\n",
" (B,1), (e,e), 'cyan', (B,1), (-e,-e), 'cyan',\n",
" data['prev_x'], data['prev_y'], 'oy', \n",
" data['new_x'], p(data['new_x']), '*',\n",
" t, p(t), '-') \n",
" \n",
" loc_plot(A, B, data)\n",
" plt.show()\n",
" \n",
" e = 1.5 * data['err']\n",
" lims = [(0, A , 1-e, 1+e), (B, 1, -e, e)]\n",
" for n, r in enumerate(lims):\n",
" plt.subplot(1,2,n+1)\n",
" loc_plot(A, B, data)\n",
" plt.xlim(r[0], r[1])\n",
" plt.ylim(r[2], r[3]) "
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "cb008e5a31284a1989108058c6480837",
"version_major": 2,
"version_minor": 0
},
"text/html": [
"<p>Failed to display Jupyter Widget of type <code>interactive</code>.</p>\n",
"<p>\n",
" If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n",
" that the widgets JavaScript is still loading. If this message persists, it\n",
" likely means that the widgets JavaScript library is either not installed or\n",
" not enabled. See the <a href=\"https://ipywidgets.readthedocs.io/en/stable/user_install.html\">Jupyter\n",
" Widgets Documentation</a> for setup instructions.\n",
"</p>\n",
"<p>\n",
" If you're reading this message in another frontend (for example, a static\n",
" rendering on GitHub or <a href=\"https://nbviewer.jupyter.org/\">NBViewer</a>),\n",
" it may mean that your frontend doesn't currently support widgets.\n",
"</p>\n"
],
"text/plain": [
"interactive(children=(FloatSlider(value=0.4, description='A', max=0.5, step=0.4), FloatSlider(value=0.6, description='B', max=1.0, min=0.5), IntSlider(value=5, description='order', max=12, min=3), IntSlider(value=1, description='iterations', max=10, min=1), Output()), _dom_classes=('widget-interact',))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"v = interactive(remez_fit_show, A=(0.0, 0.5, 0.4), B=(0.5, 1.0), order=(3,12), iterations=(1, 10))\n",
"display(v)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Weighting the error\n",
"\n",
"In the example above we used a very simple piecewise constant target function $D(x)$ but the Alternation Theorem is actually much more general and the Remez algorithm can be used to approximate more complicated characteristics. \n",
"\n",
"In particular, a *weighting function* can be used in the minimax problem formulation in order to tune the error in the approximation regions. Think of minimax filter design as a budgeting problem: for a given number of coefficients, we need to make sure that the bandpass and bandstop reqirements are fulfilled first, while the error is a consequence of the budget. With a weighting function we can decide to allocate a larger part of the budget to either passband or stopband. \n",
"\n",
"Mathematically, the minimax problem becomes:\n",
"\n",
"$$\n",
" E = \\min\\max_{[0,A]\\cup [B,1]} | W(x)[P(x) - D(x)] |\n",
"$$\n",
"\n",
"but the alternation theorem still holds:\n",
"\n",
"> $P(x)$ is the minimax approximation to $D(x)$ *weighted by $W(x)$* if and only if $W(x)[P(x) - D(x)]$ alternates $M+2$ times between $+E$ and $-E$ over $[0,A]\\cup [B,1]$\n",
"\n",
"For instance, suppose we want the error in the first interval to be 10 times smaller than in the second interval. In this case the weighting function will be equal to 0.1 over $[0,A]$ and just one over $[B,1]$. We can rewrite the extended interpolation problem as\n",
"\n",
"$$\n",
" \\left\\{\\begin{array}{lcl}\n",
" p_0 + p_1 x_0 + p_2 x_0^2 + \\ldots + p_Mx_0^M + (-1)^0\\epsilon/10 &=& 1 \\\\ \n",
" p_0 + p_1 x_1 + p_2 x_1^2 + \\ldots + p_Mx_1^M + (-1)^1\\epsilon/10 &=& 1 \\\\ \n",
" \\ldots \\\\ \n",
" p_0 + p_1 x_{M} + p_2 x_{M}^2 + \\ldots + p_Mx_{M}^M + (-1)^{M}\\epsilon &=& 0\\\\ \n",
" p_0 + p_1 x_{M+1} + p_2 x_{M+1}^2 + \\ldots + p_Mx_{M+1}^M + (-1)^{M+1}\\epsilon &=& 0 \n",
" \\end{array}\\right.\n",
"$$\n",
"\n",
"The following code is a simple modification of the algorithm detailed above to include error weighting:\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def remez_fit2(A, B, Aweight, order, iterations):\n",
" def weigh(x):\n",
" # the weighting function\n",
" if np.isscalar(x):\n",
" return 1.0/Aweight if x <= A else 1\n",
" else:\n",
" return [1.0/Aweight if v <= A else 1 for v in x]\n",
" \n",
" pts = order+2\n",
" ptsA = int(pts * A / (A-B+1))\n",
" if ptsA < 2:\n",
" ptsA = 2\n",
" ptsB = pts - ptsA\n",
" x = np.concatenate((\n",
" np.arange(1, ptsA+1) * (A / (ptsA+1)),\n",
" B + np.arange(1, ptsB+1) * ((1-B) / (ptsB+1)) \n",
" ))\n",
" y = np.concatenate((\n",
" np.ones(ptsA),\n",
" np.zeros(ptsB) \n",
" ))\n",
" \n",
" data = {} \n",
" for n in range(0, iterations):\n",
" data['prev_x'] = x\n",
" data['prev_y'] = y\n",
" \n",
" # solve the interpolation problem with weighted error\n",
" V = np.vander(x, increasing=True)\n",
" V[:,-1] = pow(-1, np.arange(0, len(x))) * weigh(x)\n",
" v = np.linalg.solve(V, y)\n",
" p = np.poly1d(v[-2::-1])\n",
" e = np.abs(v[-1])\n",
" data['Aerr'] = e / Aweight\n",
" data['Berr'] = e\n",
" \n",
" loc, err = find_error_extrema(p, A, B) \n",
" alt = []\n",
" for n in range(0, len(loc)):\n",
" c = {\n",
" 'loc': loc[n],\n",
" 'sign': np.sign(err[n]),\n",
" 'err_mag': np.abs(err[n]) / weigh(loc[n])\n",
" }\n",
" if c['err_mag'] >= e - 1e-3:\n",
" if alt == [] or alt[-1]['sign'] != c['sign']:\n",
" alt.append(c)\n",
" elif alt[-1]['err_mag'] < c['err_mag']:\n",
" alt.pop()\n",
" alt.append(c)\n",
" while len(alt) > order + 2:\n",
" if alt[0]['err_mag'] > alt[-1]['err_mag']:\n",
" alt.pop(-1)\n",
" else:\n",
" alt.pop(0)\n",
" \n",
" x = [c['loc'] for c in alt]\n",
" y = [1 if c <= A else 0 for c in x]\n",
" data['new_x'] = x\n",
"\n",
" return p, data"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "807e40bd3ca44081abcd003fc5838d39",
"version_major": 2,
"version_minor": 0
},
"text/html": [
"<p>Failed to display Jupyter Widget of type <code>interactive</code>.</p>\n",
"<p>\n",
" If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean\n",
" that the widgets JavaScript is still loading. If this message persists, it\n",
" likely means that the widgets JavaScript library is either not installed or\n",
" not enabled. See the <a href=\"https://ipywidgets.readthedocs.io/en/stable/user_install.html\">Jupyter\n",
" Widgets Documentation</a> for setup instructions.\n",
"</p>\n",
"<p>\n",
" If you're reading this message in another frontend (for example, a static\n",
" rendering on GitHub or <a href=\"https://nbviewer.jupyter.org/\">NBViewer</a>),\n",
" it may mean that your frontend doesn't currently support widgets.\n",
"</p>\n"
],
"text/plain": [
"interactive(children=(FloatSlider(value=0.4, description='A', max=0.5, step=0.4), FloatSlider(value=0.6, description='B', max=1.0, min=0.5), IntSlider(value=50, description='Aweight', min=1, step=10), IntSlider(value=10, description='order', max=20, min=5), IntSlider(value=1, description='iterations', max=10, min=1), Output()), _dom_classes=('widget-interact',))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"def remez_fit_show2(A=0.4, B=0.6, Aweight=50, order=10, iterations=1):\n",
" p, data = remez_fit2(A, B, Aweight, order, iterations)\n",
" \n",
" t = np.linspace(0, 1, 300)\n",
" Ae = data['Aerr']\n",
" Be = data['Berr']\n",
" \n",
" def loc_plot(A, B, data): \n",
" plt.plot((0,A), (1,1), 'red',\n",
" (B,1), (0,0), 'red', \n",
" (0,A), (1+Ae,1+Ae), 'cyan', (0,A), (1-Ae,1-Ae), 'cyan',\n",
" (B,1), (Be,Be), 'cyan', (B,1), (-Be,-Be), 'cyan',\n",
" data['prev_x'], data['prev_y'], 'oy', \n",
" data['new_x'], p(data['new_x']), '*',\n",
" t, p(t), '-') \n",
" \n",
" loc_plot(A, B, data)\n",
" plt.show()\n",
" \n",
" lims = [(0, A , 1-1.5*Ae, 1+1.5*Ae), (B, 1, -1.5*Be, 1.5*Be)]\n",
" for n, r in enumerate(lims):\n",
" plt.subplot(1,2,n+1)\n",
" loc_plot(A, B, data)\n",
" plt.xlim(r[0], r[1])\n",
" plt.ylim(r[2], r[3]) \n",
" \n",
" \n",
" \n",
"v = interactive(remez_fit_show2, \n",
" A=(0.0, 0.5, 0.4), B=(0.5, 1.0), \n",
" Aweight=(1,100,10),\n",
" order=(5,20), \n",
" iterations=(1, 10))\n",
"display(v)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}