Poor Man’s FIWIZ¶
- Author or source: moc.oohay@ljbliam
- Type: Filter Design Utility
- Created: 2007-03-22 15:02:29
notes¶
FIWIZ is a neat little filter design utility that uses a genetic programming technique
called Differential Evolution. As useful as it is, it looks like it took about a week to
write, and is thus very undeserving of the $70 license fee. So I decided to write my own.
There's a freely available optimizer class that uses Differential Evolution and I patched
it together with some filter-specific logic.
Use of the utility requires the ability to do simple coding in C, but you need only revise
a single function, which basically describes your filter specification. There's a big
comment in the main source file that clarifies things a bit more.
Although it's not as easy to use as FIWIZ, it's arguably more powerful because your
specifications are limited only by what you can express in C, whereas FIWIZ is completely
GUI based.
Another thing: I'm afraid that due to the use of _kbhit and _getch, the code is a bit
Microsofty, but you can take those out and the code will still be basically usable.
code¶
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 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 | // First File: DESolver.cpp
#include <stdio.h>
#include <memory.h>
#include <conio.h>
#include "DESolver.h"
#define Element(a,b,c) a[b*nDim+c]
#define RowVector(a,b) (&a[b*nDim])
#define CopyVector(a,b) memcpy((a),(b),nDim*sizeof(double))
DESolver::DESolver(int dim,int popSize) :
nDim(dim), nPop(popSize),
generations(0), strategy(stRand1Exp),
scale(0.7), probability(0.5), bestEnergy(0.0),
trialSolution(0), bestSolution(0),
popEnergy(0), population(0)
{
trialSolution = new double[nDim];
bestSolution = new double[nDim];
popEnergy = new double[nPop];
population = new double[nPop * nDim];
return;
}
DESolver::~DESolver(void)
{
if (trialSolution) delete trialSolution;
if (bestSolution) delete bestSolution;
if (popEnergy) delete popEnergy;
if (population) delete population;
trialSolution = bestSolution = popEnergy = population = 0;
return;
}
void DESolver::Setup(double *min,double *max,
int deStrategy,double diffScale,double crossoverProb)
{
int i;
strategy = deStrategy;
scale = diffScale;
probability = crossoverProb;
for (i=0; i < nPop; i++)
{
for (int j=0; j < nDim; j++)
Element(population,i,j) = RandomUniform(min[j],max[j]);
popEnergy[i] = 1.0E20;
}
for (i=0; i < nDim; i++)
bestSolution[i] = 0.0;
switch (strategy)
{
case stBest1Exp:
calcTrialSolution = &DESolver::Best1Exp;
break;
case stRand1Exp:
calcTrialSolution = &DESolver::Rand1Exp;
break;
case stRandToBest1Exp:
calcTrialSolution = &DESolver::RandToBest1Exp;
break;
case stBest2Exp:
calcTrialSolution = &DESolver::Best2Exp;
break;
case stRand2Exp:
calcTrialSolution = &DESolver::Rand2Exp;
break;
case stBest1Bin:
calcTrialSolution = &DESolver::Best1Bin;
break;
case stRand1Bin:
calcTrialSolution = &DESolver::Rand1Bin;
break;
case stRandToBest1Bin:
calcTrialSolution = &DESolver::RandToBest1Bin;
break;
case stBest2Bin:
calcTrialSolution = &DESolver::Best2Bin;
break;
case stRand2Bin:
calcTrialSolution = &DESolver::Rand2Bin;
break;
}
return;
}
bool DESolver::Solve(int maxGenerations)
{
int generation;
int candidate;
bool bAtSolution;
int generationsPerLoop = 10;
bestEnergy = 1.0E20;
bAtSolution = false;
for (generation=0;
(generation < maxGenerations) && !bAtSolution && (0 == _kbhit());
generation++)
{
for (candidate=0; candidate < nPop; candidate++)
{
(this->*calcTrialSolution)(candidate);
trialEnergy = EnergyFunction(trialSolution,bAtSolution);
if (trialEnergy < popEnergy[candidate])
{
// New low for this candidate
popEnergy[candidate] = trialEnergy;
CopyVector(RowVector(population,candidate),trialSolution);
// Check if all-time low
if (trialEnergy < bestEnergy)
{
bestEnergy = trialEnergy;
CopyVector(bestSolution,trialSolution);
}
}
}
if ((generation % generationsPerLoop) == (generationsPerLoop - 1))
{
printf("Gens %u Cost %.15g\n", generation+1, Energy());
}
}
if (0 != _kbhit())
{
_getch();
}
generations = generation;
return(bAtSolution);
}
void DESolver::Best1Exp(int candidate)
{
int r1, r2;
int n;
SelectSamples(candidate,&r1,&r2);
n = (int)RandomUniform(0.0,(double)nDim);
CopyVector(trialSolution,RowVector(population,candidate));
for (int i=0; (RandomUniform(0.0,1.0) < probability) && (i < nDim); i++)
{
trialSolution[n] = bestSolution[n]
+ scale * (Element(population,r1,n)
- Element(population,r2,n));
n = (n + 1) % nDim;
}
return;
}
void DESolver::Rand1Exp(int candidate)
{
int r1, r2, r3;
int n;
SelectSamples(candidate,&r1,&r2,&r3);
n = (int)RandomUniform(0.0,(double)nDim);
CopyVector(trialSolution,RowVector(population,candidate));
for (int i=0; (RandomUniform(0.0,1.0) < probability) && (i < nDim); i++)
{
trialSolution[n] = Element(population,r1,n)
+ scale * (Element(population,r2,n)
- Element(population,r3,n));
n = (n + 1) % nDim;
}
return;
}
void DESolver::RandToBest1Exp(int candidate)
{
int r1, r2;
int n;
SelectSamples(candidate,&r1,&r2);
n = (int)RandomUniform(0.0,(double)nDim);
CopyVector(trialSolution,RowVector(population,candidate));
for (int i=0; (RandomUniform(0.0,1.0) < probability) && (i < nDim); i++)
{
trialSolution[n] += scale * (bestSolution[n] - trialSolution[n])
+ scale * (Element(population,r1,n)
- Element(population,r2,n));
n = (n + 1) % nDim;
}
return;
}
void DESolver::Best2Exp(int candidate)
{
int r1, r2, r3, r4;
int n;
SelectSamples(candidate,&r1,&r2,&r3,&r4);
n = (int)RandomUniform(0.0,(double)nDim);
CopyVector(trialSolution,RowVector(population,candidate));
for (int i=0; (RandomUniform(0.0,1.0) < probability) && (i < nDim); i++)
{
trialSolution[n] = bestSolution[n] +
scale * (Element(population,r1,n)
+ Element(population,r2,n)
- Element(population,r3,n)
- Element(population,r4,n));
n = (n + 1) % nDim;
}
return;
}
void DESolver::Rand2Exp(int candidate)
{
int r1, r2, r3, r4, r5;
int n;
SelectSamples(candidate,&r1,&r2,&r3,&r4,&r5);
n = (int)RandomUniform(0.0,(double)nDim);
CopyVector(trialSolution,RowVector(population,candidate));
for (int i=0; (RandomUniform(0.0,1.0) < probability) && (i < nDim); i++)
{
trialSolution[n] = Element(population,r1,n)
+ scale * (Element(population,r2,n)
+ Element(population,r3,n)
- Element(population,r4,n)
- Element(population,r5,n));
n = (n + 1) % nDim;
}
return;
}
void DESolver::Best1Bin(int candidate)
{
int r1, r2;
int n;
SelectSamples(candidate,&r1,&r2);
n = (int)RandomUniform(0.0,(double)nDim);
CopyVector(trialSolution,RowVector(population,candidate));
for (int i=0; i < nDim; i++)
{
if ((RandomUniform(0.0,1.0) < probability) || (i == (nDim - 1)))
trialSolution[n] = bestSolution[n]
+ scale * (Element(population,r1,n)
- Element(population,r2,n));
n = (n + 1) % nDim;
}
return;
}
void DESolver::Rand1Bin(int candidate)
{
int r1, r2, r3;
int n;
SelectSamples(candidate,&r1,&r2,&r3);
n = (int)RandomUniform(0.0,(double)nDim);
CopyVector(trialSolution,RowVector(population,candidate));
for (int i=0; i < nDim; i++)
{
if ((RandomUniform(0.0,1.0) < probability) || (i == (nDim - 1)))
trialSolution[n] = Element(population,r1,n)
+ scale * (Element(population,r2,n)
- Element(population,r3,n));
n = (n + 1) % nDim;
}
return;
}
void DESolver::RandToBest1Bin(int candidate)
{
int r1, r2;
int n;
SelectSamples(candidate,&r1,&r2);
n = (int)RandomUniform(0.0,(double)nDim);
CopyVector(trialSolution,RowVector(population,candidate));
for (int i=0; i < nDim; i++)
{
if ((RandomUniform(0.0,1.0) < probability) || (i == (nDim - 1)))
trialSolution[n] += scale * (bestSolution[n] - trialSolution[n])
+ scale * (Element(population,r1,n)
- Element(population,r2,n));
n = (n + 1) % nDim;
}
return;
}
void DESolver::Best2Bin(int candidate)
{
int r1, r2, r3, r4;
int n;
SelectSamples(candidate,&r1,&r2,&r3,&r4);
n = (int)RandomUniform(0.0,(double)nDim);
CopyVector(trialSolution,RowVector(population,candidate));
for (int i=0; i < nDim; i++)
{
if ((RandomUniform(0.0,1.0) < probability) || (i == (nDim - 1)))
trialSolution[n] = bestSolution[n]
+ scale * (Element(population,r1,n)
+ Element(population,r2,n)
- Element(population,r3,n)
- Element(population,r4,n));
n = (n + 1) % nDim;
}
return;
}
void DESolver::Rand2Bin(int candidate)
{
int r1, r2, r3, r4, r5;
int n;
SelectSamples(candidate,&r1,&r2,&r3,&r4,&r5);
n = (int)RandomUniform(0.0,(double)nDim);
CopyVector(trialSolution,RowVector(population,candidate));
for (int i=0; i < nDim; i++)
{
if ((RandomUniform(0.0,1.0) < probability) || (i == (nDim - 1)))
trialSolution[n] = Element(population,r1,n)
+ scale * (Element(population,r2,n)
+ Element(population,r3,n)
- Element(population,r4,n)
- Element(population,r5,n));
n = (n + 1) % nDim;
}
return;
}
void DESolver::SelectSamples(int candidate,int *r1,int *r2,
int *r3,int *r4,int *r5)
{
if (r1)
{
do
{
*r1 = (int)RandomUniform(0.0,(double)nPop);
}
while (*r1 == candidate);
}
if (r2)
{
do
{
*r2 = (int)RandomUniform(0.0,(double)nPop);
}
while ((*r2 == candidate) || (*r2 == *r1));
}
if (r3)
{
do
{
*r3 = (int)RandomUniform(0.0,(double)nPop);
}
while ((*r3 == candidate) || (*r3 == *r2) || (*r3 == *r1));
}
if (r4)
{
do
{
*r4 = (int)RandomUniform(0.0,(double)nPop);
}
while ((*r4 == candidate) || (*r4 == *r3) || (*r4 == *r2) || (*r4 == *r1));
}
if (r5)
{
do
{
*r5 = (int)RandomUniform(0.0,(double)nPop);
}
while ((*r5 == candidate) || (*r5 == *r4) || (*r5 == *r3)
|| (*r5 == *r2) || (*r5 == *r1));
}
return;
}
/*------Constants for RandomUniform()---------------------------------------*/
#define SEED 3
#define IM1 2147483563
#define IM2 2147483399
#define AM (1.0/IM1)
#define IMM1 (IM1-1)
#define IA1 40014
#define IA2 40692
#define IQ1 53668
#define IQ2 52774
#define IR1 12211
#define IR2 3791
#define NTAB 32
#define NDIV (1+IMM1/NTAB)
#define EPS 1.2e-7
#define RNMX (1.0-EPS)
double DESolver::RandomUniform(double minValue,double maxValue)
{
long j;
long k;
static long idum;
static long idum2=123456789;
static long iy=0;
static long iv[NTAB];
double result;
if (iy == 0)
idum = SEED;
if (idum <= 0)
{
if (-idum < 1)
idum = 1;
else
idum = -idum;
idum2 = idum;
for (j=NTAB+7; j>=0; j--)
{
k = idum / IQ1;
idum = IA1 * (idum - k*IQ1) - k*IR1;
if (idum < 0) idum += IM1;
if (j < NTAB) iv[j] = idum;
}
iy = iv[0];
}
k = idum / IQ1;
idum = IA1 * (idum - k*IQ1) - k*IR1;
if (idum < 0)
idum += IM1;
k = idum2 / IQ2;
idum2 = IA2 * (idum2 - k*IQ2) - k*IR2;
if (idum2 < 0)
idum2 += IM2;
j = iy / NDIV;
iy = iv[j] - idum2;
iv[j] = idum;
if (iy < 1)
iy += IMM1;
result = AM * iy;
if (result > RNMX)
result = RNMX;
result = minValue + result * (maxValue - minValue);
return(result);
}
// END FIRST FILE
// BEGIN SECOND FILE: DESolver.h
// Differential Evolution Solver Class
// Based on algorithms developed by Dr. Rainer Storn & Kenneth Price
// Written By: Lester E. Godwin
// PushCorp, Inc.
// Dallas, Texas
// 972-840-0208 x102
// godwin@pushcorp.com
// Created: 6/8/98
// Last Modified: 6/8/98
// Revision: 1.0
#if !defined(_DESOLVER_H)
#define _DESOLVER_H
#define stBest1Exp 0
#define stRand1Exp 1
#define stRandToBest1Exp 2
#define stBest2Exp 3
#define stRand2Exp 4
#define stBest1Bin 5
#define stRand1Bin 6
#define stRandToBest1Bin 7
#define stBest2Bin 8
#define stRand2Bin 9
class DESolver;
typedef void (DESolver::*StrategyFunction)(int);
class DESolver
{
public:
DESolver(int dim,int popSize);
~DESolver(void);
// Setup() must be called before solve to set min, max, strategy etc.
void Setup(double min[],double max[],int deStrategy,
double diffScale,double crossoverProb);
// Solve() returns true if EnergyFunction() returns true.
// Otherwise it runs maxGenerations generations and returns false.
virtual bool Solve(int maxGenerations);
// EnergyFunction must be overridden for problem to solve
// testSolution[] is nDim array for a candidate solution
// setting bAtSolution = true indicates solution is found
// and Solve() immediately returns true.
virtual double EnergyFunction(double testSolution[],bool &bAtSolution) = 0;
int Dimension(void) { return(nDim); }
int Population(void) { return(nPop); }
// Call these functions after Solve() to get results.
double Energy(void) { return(bestEnergy); }
double *Solution(void) { return(bestSolution); }
int Generations(void) { return(generations); }
protected:
void SelectSamples(int candidate,int *r1,int *r2=0,int *r3=0,
int *r4=0,int *r5=0);
double RandomUniform(double min,double max);
int nDim;
int nPop;
int generations;
int strategy;
StrategyFunction calcTrialSolution;
double scale;
double probability;
double trialEnergy;
double bestEnergy;
double *trialSolution;
double *bestSolution;
double *popEnergy;
double *population;
private:
void Best1Exp(int candidate);
void Rand1Exp(int candidate);
void RandToBest1Exp(int candidate);
void Best2Exp(int candidate);
void Rand2Exp(int candidate);
void Best1Bin(int candidate);
void Rand1Bin(int candidate);
void RandToBest1Bin(int candidate);
void Best2Bin(int candidate);
void Rand2Bin(int candidate);
};
// I added the following stuff 19 March 2007
// Brent Lehman
struct ASpectrum
{
unsigned mNumValues;
double* mReals;
double* mImags;
};
bool ComputeSpectrum(double* evenZeros, unsigned numEvenZeros, double* oddZero,
double* evenPoles, unsigned numEvenPoles, double* oddPole,
double gain, ASpectrum* spectrum);
class FilterSolver : public DESolver
{
public:
FilterSolver(int dim, int popSize, int spectrumSize,
unsigned numZeros, unsigned numPoles, bool minimumPhase) :
DESolver(dim, popSize)
{
mSpectrum.mNumValues = spectrumSize;
mSpectrum.mReals = new double[spectrumSize];
mSpectrum.mImags = new double[spectrumSize];
mNumZeros = numZeros;
mNumPoles = numPoles;
mMinimumPhase = minimumPhase;
}
virtual ~FilterSolver()
{
delete[] mSpectrum.mReals;
delete[] mSpectrum.mImags;
}
virtual double EnergyFunction(double testSolution[], bool& bAtSolution);
virtual ASpectrum* Spectrum() {return &mSpectrum;}
private:
unsigned mNumZeros;
unsigned mNumPoles;
bool mMinimumPhase;
ASpectrum mSpectrum;
};
#endif // _DESOLVER_H
// END SECOND FILE DESolver.h
// BEGIN FINAL FILE: FilterDesign.cpp
/*
*
* Filter Design Utility
* Source
*
* Brent Lehman
* 16 March 2007
*
*
*/
////////////////////////////////////////////////////////////////////
// //
// The idea is that an optimization algorithm passes a bunch of //
// different filter specifications to the function //
// "EnergyFunction" below. That function is supposed to //
// compute an "error" or "cost" value for each specification //
// it receives, which the algorithm uses to decide on other //
// filter specifications to try. Over the course of several //
// thousand different specifications, the algorithm will //
// eventually converge on a single best one. This one has the //
// lowest error value of all possible specifications. Thus, //
// you effectively tell the optimization algorithm what it's //
// looking for through code that you put into EnergyFunction. //
// //
// Look for a note in the code like this one to see what part //
// you need to change for your own uses. //
// //
////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <conio.h>
#include <math.h>
#include <time.h>
#include "DESolver.h"
#define kIntIsOdd(x) (((x) & 0x00000001) == 1)
double FilterSolver::EnergyFunction(double testSolution[], bool& bAtSolution)
{
unsigned i;
double tempReal;
double tempImag;
// You probably will want to keep this if statement and its contents
if (mMinimumPhase)
{
// Make sure there are no zeros outside the unit circle
unsigned lastEvenZero = (mNumZeros & 0xfffffffe) - 1;
for (i = 0; i <= lastEvenZero; i+=2)
{
tempReal = testSolution[i];
tempImag = testSolution[i+1];
if ((tempReal*tempReal + tempImag*tempImag) > 1.0)
{
return 1.0e+300;
}
}
if (kIntIsOdd(mNumZeros))
{
tempReal = testSolution[mNumZeros - 1];
if ((tempReal * tempReal) > 1.0)
{
return 1.0e+300;
}
}
}
// Make sure there are no poles on or outside the unit circle
// You probably will want to keep this too
unsigned lastEvenPole = mNumZeros + (mNumPoles & 0xfffffffe) - 2;
for (i = mNumZeros; i <= lastEvenPole; i+=2)
{
tempReal = testSolution[i];
tempImag = testSolution[i+1];
if ((tempReal*tempReal + tempImag*tempImag) > 0.999999999)
{
return 1.0e+300;
}
}
// If you keep the for loop above, keep this too
if (kIntIsOdd(mNumPoles))
{
tempReal = testSolution[mNumZeros + mNumPoles - 1];
if ((tempReal * tempReal) > 1.0)
{
return 1.0e+300;
}
}
double* evenZeros = &(testSolution[0]);
double* evenPoles = &(testSolution[mNumZeros]);
double* oddZero = NULL;
double* oddPole = NULL;
double gain = testSolution[mNumZeros + mNumPoles];
if (kIntIsOdd(mNumZeros))
{
oddZero = &(testSolution[mNumZeros - 1]);
}
if (kIntIsOdd(mNumPoles))
{
oddPole = &(testSolution[mNumZeros + mNumPoles - 1]);
}
ComputeSpectrum(evenZeros, mNumZeros & 0xfffffffe, oddZero,
evenPoles, mNumPoles & 0xfffffffe, oddPole,
gain, &mSpectrum);
unsigned numPoints = mSpectrum.mNumValues;
/////////////////////////////////////////////////////////////////
// //
// Use the impulse response, held in the variable //
// "mSpectrum", to compute a score for the solution that //
// has been passed into this function. You probably don't //
// want to touch any of the code above this point, but //
// from here to the end of this function, it's all you! //
// //
/////////////////////////////////////////////////////////////////
#define kLnTwoToThe127 88.02969193111305
#define kRecipLn10 0.4342944819032518
// Compute square sum of errors for magnitude
double magnitudeError = 0.0;
double magnitude = 0.0;
double logMagnitude = 0.0;
tempReal = mSpectrum.mReals[0];
tempImag = mSpectrum.mImags[0];
magnitude = tempReal*tempReal + tempImag*tempImag;
double baseMagnitude = 0.0;
if (0.0 == magnitude)
{
baseMagnitude = -kLnTwoToThe127;
}
else
{
baseMagnitude = log(magnitude) * kRecipLn10;
baseMagnitude *= 0.5;
}
for (i = 0; i < numPoints; i++)
{
tempReal = mSpectrum.mReals[i];
tempImag = mSpectrum.mImags[i];
magnitude = tempReal*tempReal + tempImag*tempImag;
if (0.0 == magnitude)
{
logMagnitude = -kLnTwoToThe127;
}
else
{
logMagnitude = log(magnitude) * kRecipLn10;
logMagnitude *= 0.5; // Half the log because it's mag squared
}
logMagnitude -= baseMagnitude;
magnitudeError += logMagnitude * logMagnitude;
}
// Compute errors for phase
double phaseError = 0.0;
double phase = 0.0;
double componentError = 0.0;
double degree = 1;//((mNumZeros + 1) & 0xfffffffe) - 1;
double angleSpacing = -3.141592653589793 * 0.5 / numPoints * degree;
double targetPhase = 0.0;
double oldPhase = 0.0;
double phaseDifference = 0;
double totalPhaseTraversal = 0.0;
double traversalError = 0.0;
for (i = 0; i < (numPoints - 5); i++)
{
tempReal = mSpectrum.mReals[i];
tempImag = mSpectrum.mImags[i];
oldPhase = phase;
phase = atan2(tempImag, tempReal);
phaseDifference = phase - oldPhase;
if (phaseDifference > 3.141592653589793)
{
phaseDifference -= 3.141592653589793;
phaseDifference -= 3.141592653589793;
}
else if (phaseDifference < -3.141592653589793)
{
phaseDifference += 3.141592653589793;
phaseDifference += 3.141592653589793;
}
totalPhaseTraversal += phaseDifference;
componentError = cosh(200.0*(phaseDifference - angleSpacing)) - 0.5;
phaseError += componentError * componentError;
targetPhase += angleSpacing;
if (targetPhase < -3.141592653589793)
{
targetPhase += 3.141592653589793;
targetPhase += 3.141592653589793;
}
}
traversalError = totalPhaseTraversal - angleSpacing * numPoints;
traversalError *= traversalError;
double baseMagnitudeError = baseMagnitude * baseMagnitude;
// Compute weighted sum of the two subtotals
// Take square root
return sqrt(baseMagnitudeError*1.0 + magnitudeError*100.0 +
phaseError*400.0 + traversalError*4000000.0);
}
///////////////////////////////
int main(int argc, char** argv)
{
srand((unsigned)time(NULL));
unsigned numZeros;
unsigned numPoles;
bool minimumPhase;
if (argc < 4)
{
printf("Usage: FilterDesign.exe <minimumPhase?> <numZeros> <numPoles>\n");
return 0;
}
else
{
if (0 == atoi(argv[1]))
{
minimumPhase = false;
}
else
{
minimumPhase = true;
}
numZeros = (unsigned)atoi(argv[2]);
if (0 == numZeros)
{
numZeros = 1;
}
numPoles = (unsigned)atoi(argv[3]);
}
unsigned vectorLength = numZeros + numPoles + 1;
unsigned populationSize = vectorLength * 10;
FilterSolver theSolver(vectorLength, populationSize, 200,
numZeros, numPoles, minimumPhase);
double* minimumSolution = new double[vectorLength];
unsigned i;
if (minimumPhase)
{
for (i = 0; i < numZeros; i++)
{
minimumSolution[i] = -sqrt(0.5);
}
}
else
{
for (i = 0; i < numZeros; i++)
{
minimumSolution[i] = -10.0;
}
}
for (; i < (vectorLength - 1); i++)
{
minimumSolution[i] = -sqrt(0.5);
}
minimumSolution[vectorLength - 1] = 0.0;
double* maximumSolution = new double[vectorLength];
if (minimumPhase)
{
for (i = 0; i < numZeros; i++)
{
maximumSolution[i] = sqrt(0.5);
}
}
else
{
for (i = 0; i < numZeros; i++)
{
maximumSolution[i] = 10.0;
}
}
for (i = 0; i < (vectorLength - 1); i++)
{
maximumSolution[i] = sqrt(0.5);
}
maximumSolution[vectorLength - 1] = 2.0;
theSolver.Setup(minimumSolution, maximumSolution, 0, 0.5, 0.75);
theSolver.Solve(1000000);
double* bestSolution = theSolver.Solution();
printf("\nZeros:\n");
unsigned numEvenZeros = numZeros & 0xfffffffe;
for (i = 0; i < numEvenZeros; i+=2)
{
printf("%.10f +/- %.10fi\n", bestSolution[i], bestSolution[i+1]);
}
if (kIntIsOdd(numZeros))
{
printf("%.10f\n", bestSolution[numZeros-1]);
}
printf("Poles:\n");
unsigned lastEvenPole = numZeros + (numPoles & 0xfffffffe) - 2;
for (i = numZeros; i <= lastEvenPole; i+=2)
{
printf("%.10f +/- %.10fi\n", bestSolution[i], bestSolution[i+1]);
}
unsigned numRoots = numZeros + numPoles;
if (kIntIsOdd(numPoles))
{
printf("%.10f\n", bestSolution[numRoots-1]);
}
double gain = bestSolution[numRoots];
printf("Gain: %.10f\n", gain);
_getch();
unsigned j;
ASpectrum* spectrum = theSolver.Spectrum();
double logMagnitude;
printf("Magnitude Response, millibels:\n");
for (i = 0; i < 20; i++)
{
for (j = 0; j < 10; j++)
{
logMagnitude = kRecipLn10 *
log(spectrum->mReals[i*10 + j] * spectrum->mReals[i*10 + j] +
spectrum->mImags[i*10 + j] * spectrum->mImags[i*10 + j]);
if (logMagnitude < -9.999)
{
logMagnitude = -9.999;
}
printf("%+5.0f ", logMagnitude*1000);
}
printf("\n");
}
_getch();
double phase;
printf("Phase Response, milliradians:\n");
for (i = 0; i < 20; i++)
{
for (j = 0; j < 10; j++)
{
phase = atan2(spectrum->mImags[i*10 + j], spectrum->mReals[i*10 + j]);
printf("%+5.0f ", phase*1000);
}
printf("\n");
}
_getch();
printf("Biquad Sections:\n");
unsigned numBiquadSections =
(numZeros > numPoles) ? ((numZeros + 1) >> 1) : ((numPoles + 1) >> 1);
double x0, x1, x2;
double y0, y1, y2;
if (numZeros >=2)
{
x0 = (bestSolution[0]*bestSolution[0] + bestSolution[1]*bestSolution[1]) *
gain;
x1 = 2.0 * bestSolution[0] * gain;
x2 = gain;
}
else if (1 == numZeros)
{
x0 = bestSolution[0] * gain;
x1 = gain;
x2 = 0.0;
}
else
{
x0 = gain;
x1 = 0.0;
x2 = 0.0;
}
if (numPoles >= 2)
{
y0 = (bestSolution[numZeros]*bestSolution[numZeros] +
bestSolution[numZeros+1]*bestSolution[numZeros+1]);
y1 = 2.0 * bestSolution[numZeros];
y2 = 1.0;
}
else if (1 == numPoles)
{
y0 = bestSolution[numZeros];
y1 = 1.0;
y2 = 0.0;
}
else
{
y0 = 1.0;
y1 = 0.0;
y2 = 0.0;
}
x0 /= y0;
x1 /= y0;
x2 /= y0;
y1 /= y0;
y2 /= y0;
printf("y[n] = %.10fx[n]", x0);
if (numZeros > 0)
{
printf(" + %.10fx[n-1]", x1);
}
if (numZeros > 1)
{
printf(" + %.10fx[n-2]", x2);
}
printf("\n");
if (numPoles > 0)
{
printf(" + %.10fy[n-1]", y1);
}
if (numPoles > 1)
{
printf(" + &.10fy[n-2]", y2);
}
if (numPoles > 0)
{
printf("\n");
}
int numRemainingZeros = numZeros - 2;
int numRemainingPoles = numPoles - 2;
for (i = 1; i < numBiquadSections; i++)
{
if (numRemainingZeros >= 2)
{
x0 = (bestSolution[i*2] * bestSolution[i*2] +
bestSolution[i*2+1] * bestSolution[i*2+1]);
x1 = -2.0 * bestSolution[i*2];
x2 = 1.0;
}
else if (numRemainingZeros >= 1)
{
x0 = bestSolution[i*2];
x1 = 1.0;
x2 = 0.0;
}
else
{
x0 = 1.0;
x1 = 0.0;
x2 = 0.0;
}
if (numRemainingPoles >= 2)
{
y0 = (bestSolution[i*2+numZeros] * bestSolution[i*2+numZeros] +
bestSolution[i*2+numZeros+1] * bestSolution[i*2+numZeros+1]);
y1 = -2.0 * bestSolution[i*2+numZeros];
y2 = 1.0;
}
else if (numRemainingPoles >= 1)
{
y0 = bestSolution[i*2+numZeros];
y1 = 1.0;
y2 = 0.0;
}
else
{
y0 = 1.0;
y1 = 0.0;
y2 = 0.0;
}
x0 /= y0;
x1 /= y0;
x2 /= y0;
y1 /= y0;
y2 /= y0;
printf("y[n] = %.10fx[n]", x0);
if (numRemainingZeros > 0)
{
printf(" + %.10fx[n-1]", x1);
}
if (numRemainingZeros > 1)
{
printf(" + %.10fx[n-2]", x2);
}
printf("\n");
if (numRemainingPoles > 0)
{
printf(" + %.10fy[n-1]", -y1);
}
if (numRemainingPoles > 1)
{
printf(" + %.10fy[n-2]", -y2);
}
if (numRemainingPoles > 0)
{
printf("\n");
}
numRemainingZeros -= 2;
numRemainingPoles -= 2;
}
_getch();
printf("Full Expansion:\n");
double* xpolynomial = new double[numRoots + 1];
memset(xpolynomial, 0, sizeof(double) * (numRoots + 1));
xpolynomial[0] = 1.0;
if (numZeros >= 2)
{
xpolynomial[0] = bestSolution[0] * bestSolution[0] +
bestSolution[1] * bestSolution[1];
xpolynomial[1] = -2.0 * bestSolution[0];
xpolynomial[2] = 1.0;
}
else if (numZeros == 1)
{
xpolynomial[0] = bestSolution[0];
xpolynomial[1] = 1.0;
}
else
{
xpolynomial[0] = 1.0;
}
for (i = 2, numRemainingZeros = numZeros; numRemainingZeros >= 2;
i += 2, numRemainingZeros-=2)
{
x2 = 1.0;
x1 = -2.0 * bestSolution[i];
x0 = bestSolution[i] * bestSolution[i] +
bestSolution[i+1] * bestSolution[i+1];
for (j = numRoots; j > 1; j--)
{
xpolynomial[j] = xpolynomial[j-2] + xpolynomial[j-1] * x1 +
xpolynomial[j] * x0;
}
xpolynomial[1] = xpolynomial[0] * x1 + xpolynomial[1] * x0;
xpolynomial[0] *= x0;
}
if (numRemainingZeros > 0)
{
x1 = 1.0;
x0 = bestSolution[numZeros-1];
for (j = numRoots; j > 0; j--)
{
xpolynomial[j] = xpolynomial[j-1] + xpolynomial[j] * x0;
}
xpolynomial[0] *= x0;
}
double* ypolynomial = new double[numRoots + 1];
memset(ypolynomial, 0, sizeof(double) * (numRoots + 1));
ypolynomial[0] = 1.0;
if (numPoles >= 2)
{
ypolynomial[0] = bestSolution[numZeros] * bestSolution[numZeros] +
bestSolution[numZeros+1] * bestSolution[numZeros+1];
ypolynomial[1] = -2.0 * bestSolution[numZeros];
ypolynomial[2] = 1.0;
}
else if (numPoles == 1)
{
ypolynomial[0] = bestSolution[numZeros];
ypolynomial[1] = 1.0;
}
else
{
xpolynomial[0] = 1.0;
}
for (i = 2, numRemainingPoles = numPoles; numRemainingPoles >= 2;
i += 2, numRemainingPoles-=2)
{
y2 = 1.0;
y1 = -2.0 * bestSolution[numZeros+i];
y0 = bestSolution[numZeros+i] * bestSolution[numZeros+i] +
bestSolution[numZeros+i+1] * bestSolution[numZeros+i+1];
for (j = numRoots; j > 1; j--)
{
ypolynomial[j] = ypolynomial[j-2] + ypolynomial[j-1] * y1 +
ypolynomial[j] * y0;
}
ypolynomial[1] = ypolynomial[0] * y1 + ypolynomial[1] * y0;
ypolynomial[0] *= y0;
}
if (numRemainingPoles > 0)
{
y1 = 1.0;
y0 = bestSolution[numZeros+numPoles-1];
for (j = numRoots; j > 0; j--)
{
ypolynomial[j] = ypolynomial[j-1] + ypolynomial[j] * y0;
}
ypolynomial[0] *= y0;
}
y0 = ypolynomial[0];
for (i = 0; i <= numRoots; i++)
{
xpolynomial[i] /= y0;
ypolynomial[i] /= y0;
}
printf("y[n] = %.10fx[n]", xpolynomial[0]*gain);
for (i = 1; i <= numZeros; i++)
{
printf(" + %.10fx[n-%d]", xpolynomial[i]*gain, i);
if ((i % 3) == 2)
{
printf("\n");
}
}
if ((i % 3) != 0)
{
printf("\n");
}
if (numPoles > 0)
{
printf(" ");
}
for (i = 1; i <= numPoles; i++)
{
printf(" + %.10fy[n-%d]", -ypolynomial[i], i);
if ((i % 3) == 2)
{
printf("\n");
}
}
if ((i % 3) != 0)
{
printf("\n");
}
delete[] minimumSolution;
delete[] maximumSolution;
delete[] xpolynomial;
delete[] ypolynomial;
}
bool ComputeSpectrum(double* evenZeros, unsigned numEvenZeros, double* oddZero,
double* evenPoles, unsigned numEvenPoles, double* oddPole,
double gain, ASpectrum* spectrum)
{
unsigned i, j;
// For equally spaced points on the unit circle
unsigned numPoints = spectrum->mNumValues;
double spacingAngle = 3.141592653589793 / (numPoints - 1);
double pointArgument = 0.0;
double pointReal = 0.0;
double pointImag = 0.0;
double rootReal = 0.0;
double rootImag = 0.0;
double differenceReal = 0.0;
double differenceImag = 0.0;
double responseReal = 1.0;
double responseImag = 0.0;
double recipSquareMagnitude = 0.0;
double recipReal = 0.0;
double recipImag = 0.0;
double tempRealReal = 0.0;
double tempRealImag = 0.0;
double tempImagReal = 0.0;
double tempImagImag = 0.0;
for (i = 0; i < numPoints; i++)
{
responseReal = 1.0;
responseImag = 0.0;
// The imaginary component is negated because we're using 1/z, not z
pointReal = cos(pointArgument);
pointImag = -sin(pointArgument);
// For each even zero
for (j = 0; j < numEvenZeros; j+=2)
{
rootReal = evenZeros[j];
rootImag = evenZeros[j + 1];
// Compute distance from that zero to that point
differenceReal = pointReal - rootReal;
differenceImag = pointImag - rootImag;
// Multiply that distance by the accumulating product
tempRealReal = responseReal * differenceReal;
tempRealImag = responseReal * differenceImag;
tempImagReal = responseImag * differenceReal;
tempImagImag = responseImag * differenceImag;
responseReal = tempRealReal - tempImagImag;
responseImag = tempRealImag + tempImagReal;
// Do the same with the conjugate root
differenceImag = pointImag + rootImag;
tempRealReal = responseReal * differenceReal;
tempRealImag = responseReal * differenceImag;
tempImagReal = responseImag * differenceReal;
tempImagImag = responseImag * differenceImag;
responseReal = tempRealReal - tempImagImag;
responseImag = tempRealImag + tempImagReal;
// The following way is little faster, if any
// response *= (1/z - r) * (1/z - conj(r))
// *= r*conj(r) - (r + conj(r))/z + 1/(z*z)
// *= real(r)*real(r) + imag(r)*imag(r) - 2*real(r)/z + 1/(z*z)
// *= ... - 2*real(r)*conj(z) + conj(z)*conj(z)
// *= ... - 2*real(r)*real(z) + 2i*real(r)*imag(z) +
// real(z)*real(z) - 2i*real(z)*imag(z) + imag(z)*imag(z)
// *= real(r)*real(r) + imag(r)*imag(r) - 2*real(r)*real(z) +
// real(z)*real(z) + imag(z)*imag(z) +
// 2i * imag(z) * (real(r) - real(z))
// *= (real(r) - real(z))^2 + imag(r)^2 + imag(z)^2 +
// 2i * imag(z) * (real(r) - real(z))
// This ends up being 8 multiplications, 6 additions
}
if (NULL != oddZero)
{
rootReal = *oddZero;
// Compute distance from that zero to that point
differenceReal = pointReal - rootReal;
differenceImag = pointImag;
// Multiply that distance by the accumulating product
tempRealReal = responseReal * differenceReal;
tempRealImag = responseReal * differenceImag;
tempImagReal = responseImag * differenceReal;
tempImagImag = responseImag * differenceImag;
responseReal = tempRealReal - tempImagImag;
responseImag = tempRealImag + tempImagReal;
}
// For each pole
for (j = 0; j < numEvenPoles; j+=2)
{
rootReal = evenPoles[j];
rootImag = evenPoles[j + 1];
// Compute distance from that pole to that point
differenceReal = pointReal - rootReal;
differenceImag = pointImag - rootImag;
// Multiply the reciprocal of that distance by the accumulating product
recipSquareMagnitude = 1.0 / (differenceReal * differenceReal +
differenceImag * differenceImag);
recipReal = differenceReal * recipSquareMagnitude;
recipImag = -differenceImag * recipSquareMagnitude;
tempRealReal = responseReal * recipReal;
tempRealImag = responseReal * recipImag;
tempImagReal = responseImag * recipReal;
tempImagImag = responseImag * recipImag;
responseReal = tempRealReal - tempImagImag;
responseImag = tempRealImag + tempImagReal;
// Do the same with the conjugate root
differenceImag = pointImag + rootImag;
recipSquareMagnitude = 1.0 / (differenceReal * differenceReal +
differenceImag * differenceImag);
recipReal = differenceReal * recipSquareMagnitude;
recipImag = -differenceImag * recipSquareMagnitude;
tempRealReal = responseReal * recipReal;
tempRealImag = responseReal * recipImag;
tempImagReal = responseImag * recipReal;
tempImagImag = responseImag * recipImag;
responseReal = tempRealReal - tempImagImag;
responseImag = tempRealImag + tempImagReal;
}
if (NULL != oddPole)
{
rootReal = *oddPole;
// Compute distance from that point to that zero
differenceReal = pointReal - rootReal;
differenceImag = pointImag;
// Multiply the reciprocal of that distance by the accumulating product
recipSquareMagnitude = 1.0 / (differenceReal * differenceReal +
differenceImag * differenceImag);
recipReal = differenceReal * recipSquareMagnitude;
recipImag = -differenceImag * recipSquareMagnitude;
tempRealReal = responseReal * recipReal;
tempRealImag = responseReal * recipImag;
tempImagReal = responseImag * recipReal;
tempImagImag = responseImag * recipImag;
responseReal = tempRealReal - tempImagImag;
responseImag = tempRealImag + tempImagReal;
}
// Multiply by the gain
responseReal *= gain;
responseImag *= gain;
spectrum->mReals[i] = responseReal;
spectrum->mImags[i] = responseImag;
pointArgument += spacingAngle;
}
return true;
}
// Half-band lowpass
/*
#define kLnTwoToThe127 88.02969193111305
#define kRecipLn10 0.4342944819032518
// Compute square sum of errors for bottom half band
unsigned numLoBandPoints = numPoints >> 1;
double loBandError = 0.0;
double magnitude = 0.0;
double logMagnitude = 0.0;
for (i = 0; i < numLoBandPoints; i++)
{
tempReal = mSpectrum.mReals[i];
tempImag = mSpectrum.mImags[i];
magnitude = tempReal*tempReal + tempImag*tempImag;
if (0.0 == magnitude)
{
logMagnitude = -kLnTwoToThe127;
}
else
{
logMagnitude = log(magnitude) * kRecipLn10;
logMagnitude *= 0.5; // Half the log because it's mag squared
}
loBandError += logMagnitude * logMagnitude;
}
// Compute errors for top half of band
double hiBandError = 0.0;
for ( ; i < numPoints; i++)
{
tempReal = mSpectrum.mReals[i];
tempImag = mSpectrum.mImags[i];
magnitude = tempReal*tempReal + tempImag*tempImag;
hiBandError += magnitude; // Already a squared value
}
// Compute weighted sum of the two subtotals
// Take square root
return sqrt(loBandError + 5000.0 * hiBandError);
*/
|