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
|
// lpcdata.C
/******************************************************************************
*
* MiXViews - an X window system based sound & data editor/processor
*
* Copyright (c) 1993, 1994 Regents of the University of California
*
* Author: Douglas Scott
* Date: December 13, 1994
*
* Permission to use, copy and modify this software and its documentation
* for research and/or educational purposes and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation. The author reserves the right to distribute this
* software and its documentation. The University of California and the author
* make no representations about the suitability of this software for any
* purpose, and in no event shall University of California be liable for any
* damage, loss of data, or profits resulting from its use.
* It is provided "as is" without express or implied warranty.
*
******************************************************************************/
#ifdef __GNUG__
#pragma implementation
#endif
#include "localdefs.h"
#include "application.h"
#include "controller.h"
#include "envelope.h"
#include "header.h"
#include "lpcdata.h"
#include "request.h"
#include <Complex.h>
#include <stream.h>
// class data initialization
int LPCData::default_NumberOfPoles = 34;
double LPCData::default_FrameRate = 220.5; // (44100 / 200)
// public methods
LPCData::LPCData(int length, int npoles, int srate, double frate)
: FrameData(FloatData, length,
(npoles ? npoles : defaultNumberOfPoles())+4,
srate ? srate : 44100,
frate ? frate : defaultFrameRate()) {
}
Data *
LPCData::newData() { return new LPCData(this); }
Data *
LPCData::newData(int length) { return new LPCData(this, length); }
Data *
LPCData::clone(const Range &r) { return new LPCData(this, r); }
Data *
LPCData::clone(const Range &r, const Range &c) {
return new LPCData(this, r, c);
}
// number of poles is always 4 less than the real nchans for the underlying
// object (independent of cloning)
int
LPCData::nPoles() const { return rep->realChannels() - 4; }
void
LPCData::print(FILE *out) {
int nframes = length();
int chans = channels();
fprintf(out, "LPC Data Dump\n");
for(int i = 0; i < nframes; ++i) {
fprintf(out, "Frame %d: RMS1: %0.5f RMS2: %0.5f ERROR: %f PTCH: %0.2f\n",
i, get(i, 0), get(i, 1), get(i, 2), get(i, 3));
fprintf(out, "Coeffs:");
for(int j = 4; j < chans; ++j) {
if(!((j-4) % 8)) fprintf(out, "\n");
fprintf(out, "%f ", get(i, j));
}
fprintf(out, "\n\n");
}
fflush(out);
}
void
LPCData::information(Controller *controller) {
AlertRequest request("LPC Data Information:");
request.setBell(false);
char str[128];
request.appendLabel("------------");
request.appendLabel("Filename: ", controller->fileName());
request.appendLabel("Sample Rate: ", toString(str, sRate()));
request.appendLabel("Length (frames): ",
toString(str, length()));
request.appendLabel("Original Duration (seconds): ",
toString(str, duration()));
request.appendLabel("File Size (bytes): ",
toString(str, sizeInBytes()));
request.appendLabel("NPoles: ", toString(str, nPoles()));
request.appendLabel("Frame Rate: ", toString(str, frameRate()));
controller->handleRequest(request);
}
const char *
LPCData::channelName(int chan) const {
static char *chan_names[4] =
{ "Resid. RMS", "Signal RMS", "Error", "Freq. in Hz" };
static char string[16];
char *label;
if(chan < 4) label = chan_names[chan];
else {
sprintf(string, "Coeff %d", chan - 4);
label = string;
}
return label;
}
// returns 0 - max for first 3 bands, and normal range for other bands
Range
LPCData::limits(int chan, boolean real) const {
Range rangeLimits = Super::limits(chan, real);
if (chan > 2)
return rangeLimits;
else
return Range(0.0, rangeLimits.absoluteMax());
}
// protected methods
Header *
LPCData::createHeader(DataFile *, boolean reading) {
LPCHeader *h = new LPCHeader(nPoles(), frameRate(), sRate(), duration());
if(!reading) configureHeader(h);
return h;
}
void
LPCData::mergePitchData(Data *pitches) {
Envelope pchEnv(nFrames());
static const int pitchChannel = 0; // first channel is pitch data...
pitches->getEnvelope(&pchEnv, pitchChannel, AbsoluteMagnitude);
replaceWith(&pchEnv);
}
void
LPCData::stabilizeFrames() {
for(int framenum = 0; framenum < length(); ++framenum) {
Data* frameData = cloneFrame(framenum);
frameData->ref();
if(!stabilize(frameData)) {
char report[32];
sprintf(report, "Stabilized frame %d",framenum);
Application::inform(report);
}
Resource::unref(frameData);
}
Notify();
}
int
LPCData::stabilize(Data* frameData) {
static const int arraySize = LPCHeader::maxPoles + 4;
static const int offset = 4; // coeffs start at loc 4 in frame
MXINT32 stable=0;
MXINT32 npoles = nPoles();
float frameIn[arraySize];
frameData->getArray(frameIn, channels());
float frameOut[arraySize];
for(int i=0; i<npoles; i++)
frameOut[i] = -frameIn[npoles+3-i];
if(!(stable = isStable(frameOut, npoles))) {
correct(frameIn, npoles, frameOut);
((LPCData *)frameData)->setFromArray(frameOut, int(npoles), 0, offset);
}
return stable;
}
double
LPCData::weightedAveragePitch(double threshold) const {
double totalWeight = .001;
double sum = .001;
for(int frame = 0; frame < length(); frame++) {
if(threshold < 0.0 || (get(frame, Error_Loc) <= threshold)) {
double rms_amp = get(frame, RMS_Loc);
totalWeight += rms_amp;
sum += (get(frame, Pitch_Loc) * rms_amp);
}
}
return (sum/totalWeight);
}
double
LPCData::averagePitchDeviation(double threshold) const {
double weight = weightedAveragePitch(threshold);
double sum = 0.0;
double xweight = 0.0;
for(int frame = 0; frame < length(); frame++) {
if(threshold < 0.0 || get(frame, Error_Loc) <= threshold) {
double rms_amp = get(frame, RMS_Loc);
sum += abs((get(frame, Pitch_Loc) - weight)) * rms_amp;
xweight += rms_amp;
}
}
return (xweight > 0.0) ? (sum/xweight) : 0.0;
}
// this is new version of readjust() and adjust() from old LPC code
void
LPCData::scalePitchDeviation(double newDeviation, double threshold, double pitchCutoff) {
double devFactor =
newDeviation / max(0.001, averagePitchDeviation(threshold));
if(devFactor == 1.0)
return;
double pitchAverage = weightedAveragePitch(threshold);
for(int frame = 0; frame < length(); frame++) {
double oldPitch = get(frame, Pitch_Loc);
double newPitch = (oldPitch - pitchAverage) * devFactor + pitchAverage;
if(newPitch > pitchCutoff)
set(newPitch, frame, Pitch_Loc);
}
Notify();
}
// the remaining functions are UGLY because they were converted from Fortran
static Complex jay(0., 1.);
static Complex tmp;
const int LPCData::ArrSize = LPCHeader::maxPoles + 3; // who knows what size??
int
LPCData::factor(double *b, MXINT32 *k4, double *rootr, double *rooti,
MXINT32 *kinsid, MXINT32 *kprint, double *eps) {
/* System generated locals */
MXINT32 i_1, i_2, i_3;
/* Local variables */
static double amax;
static MXINT32 kerr;
static double dist, rmin, rmax;
static MXINT32 i, j, k;
static double r;
static Complex z, res;
static double parti, distr, partr, r2, resmag, resmax;
static MXINT32 k4m;
static double coe[ArrSize];
#ifdef LPC_DEBUG
printf("in factor npoles = %d\n",*k4);
for(i=0; i<36; i++) printf(" %g ",b[i]);
#endif
/* Parameter adjustments */
--b;
--rootr;
--rooti;
/* Function Body */
/* sets up problem, calls dproot, */
/* and checks residual values at roots */
i_1 = *k4;
for (i = 1; i <= i_1; ++i) {
/* L550: */
coe[i - 1] = b[i];
}
k4m = *k4 - 1;
dproot(&k4m, coe, &rootr[1], &rooti[1], &kerr, kprint, eps);
if (kerr > 0) {
return 0;
}
*kinsid = 0;
resmax = 0.;
rmax = 0.;
rmin = 4294967296.;
dist = 4294967296.;
amax = 4294967296.;
r2 = pow(amax, 1.0 / *k4);
i_1 = k4m;
for (j = 1; j <= i_1; ++j) {
i_2 = j;
i_3 = j;
z = rootr[i_2] + (jay * rooti[i_3]);
/* Computing 2nd power */
r = hypot(rootr[j], rooti[j]);
/* skip residue calculation if root is too big */
if (r > r2) {
goto L713;
}
i_2 = *k4;
res = b[i_2];
i_2 = *k4;
for (k = 2; k <= i_2; ++k) {
i_3 = *k4 - k + 1;
res = (res * z) + b[i_3];
}
partr = res.real();
tmp = -jay * res;
parti = tmp.real();
/* Computing 2nd power */
resmag = hypot(partr, parti);
if (resmax <= resmag) {
resmax = resmag;
}
L713:
if (rmax < r) {
rmax = r;
}
if (rmin > r) {
rmin = r;
}
if (r < 1.) {
++(*kinsid);
}
distr = abs(r - 1.);
if (dist > distr) {
dist = distr;
}
}
return 1;
} /* factor */
int
LPCData::dproot(MXINT32 *mm, double *a, double *rootr, double *rooti, MXINT32 *kerr,
MXINT32 *kprint, double *eps) {
/* System generated locals */
MXINT32 i_1, i_2, i_3, i_4;
/* Local variables */
static MXINT32 mdec;
static double amin, amax, save[ArrSize];
static MXINT32 kmax, kpol;
static double temp, size;
static MXINT32 ktry;
static double real1, real2;
static Complex b[ArrSize], c[ArrSize];
static Complex p, w, z;
static double parti;
static MXINT32 kpolm;
static double partr;
static MXINT32 kount, newst, nroot;
static double r1, r2;
static MXINT32 ktrym;
static Complex bb[ArrSize], cc[ArrSize];
static MXINT32 mp;
static Complex pp;
static double sqteps, rkount, rr1, rr2;
static MXINT32 mmp;
int i, j, k, m;
/* Parameter adjustments */
--a;
--rootr;
--rooti;
/* Function Body */
/* mm=degree of polynomial */
/* a=coefficient array, lowest to highest degree */
/* kprint=1 for full printing */
/* kerr=0 is normal return */
mmp = *mm + 1;
m = *mm;
mp = mmp;
i_1 = mp;
for (i = 1; i <= i_1; ++i) {
save[i - 1] = a[i];
}
/* kount is number of iterations so far */
kount = 0;
/* kmax is maximum total number of iterations allowed */
kmax = m * 20;
/* newst is number of re-starts */
newst = 0;
/* ktrym is number of attempted iterations before re-starting */
ktrym = 20;
/* kpolm is number of attempted iterations before polishing is stopped */
kpolm = 20;
/* amax is the largest number we allow */
amax = 4294967296.;
amin = 1. / amax;
/* rr1 and rr2 are radii within which we work for polishing */
rr1 = pow(amin, 1. / m);
rr2 = pow(amax, 1. / m);
/* eps is the tolerance for convergence */
sqteps = sqrt(*eps);
/* main loop; m is current degree */
L10:
if (m <= 0) {
goto L200;
}
/* new z, a point on the unit circle */
rkount = double(kount);
z = (jay * sin(rkount)) + cos(rkount);
ktry = 0;
/* r1 and r2 are boundaries of an expanding annulus within which we
work*/
r1 = pow(amin, 1. / m);
r2 = pow(amax, 1. / m);
/* inside loop */
L20:
partr = z.real();
tmp = -jay * z;
parti = tmp.real();
/* Computing 2nd power */
size = hypot(partr, parti);
if (size < r1 || size > r2) {
goto L300;
}
if (ktry >= ktrym) {
goto L300;
}
++ktry;
if (kount >= kmax) {
goto L400;
}
++kount;
/* get value of polynomial at z, synthetic division */
i_1 = mp - 1;
i_2 = mp;
b[i_1] = a[i_2];
i_1 = m;
for (j = 1; j <= i_1; ++j) {
k = m - j + 1;
i_2 = k - 1;
i_3 = k;
i_4 = k;
b[i_2] = (z * b[i_3]) + a[i_4];
}
p = b[0];
partr = p.real();
tmp = -jay * p;
parti = tmp.real();
/* Computing 2nd power */
if (hypot(partr, parti) > amax) {
goto L300;
}
/* get value of derivative at z, synthetic division */
i_2 = mp - 1;
i_3 = mp - 1;
c[i_2] = b[i_3];
mdec = m - 1;
i_2 = mdec;
for (j = 1; j <= i_2; ++j) {
k = m - j + 1;
i_3 = k - 1;
i_4 = k;
i_1 = k - 1;
c[i_3] = (z * c[i_4]) + b[i_1];
}
pp = c[1];
partr = pp.real();
tmp = -jay * pp;
parti = tmp.real();
/* Computing 2nd power */
if (hypot(partr, parti) < amin) {
goto L300;
}
/* test for convergence */
partr = p.real();
tmp = -jay * p;
parti = tmp.real();
/* Computing 2nd power */
size = hypot(partr, parti);
if (size > *eps) {
goto L775;
}
nroot = *mm - m + 1;
goto L500;
L775:
z = z - (p / pp);
goto L20;
/* end of main loop */
/* normal return */
L200:
*kerr = 0;
goto L600;
/* new start */
L300:
rkount = double(kount);
z = (jay * sin(rkount)) + cos(rkount);
ktry = 0;
++newst;
goto L20;
/* too many iterations */
L400:
*kerr = 400;
goto L600;
/* root z located */
/* polish z to get w */
L500:
w = z;
kpol = 0;
L510:
partr = w.real();
tmp = -jay * w;
parti = tmp.real();
/* Computing 2nd power */
size = hypot(partr, parti);
/* give up polishing if w is outside annulus */
if (size < rr1 || size > rr2) {
goto L501;
}
/* give up polishing if kpol>=kpolm */
if (kpol >= kpolm) {
goto L501;
}
++kpol;
if (kount >= kmax) {
goto L400;
}
++kount;
i_3 = mmp - 1;
i_4 = mmp - 1;
bb[i_3] = save[i_4];
i_3 = *mm;
for (j = 1; j <= i_3; ++j) {
k = *mm - j + 1;
i_4 = k - 1;
i_1 = k;
i_2 = k - 1;
bb[i_4] = (w * bb[i_1]) + save[i_2];
}
p = bb[0];
partr = p.real();
tmp = -jay * p;
parti = tmp.real();
/* Computing 2nd power */
if (hypot(partr, parti) > amax) {
goto L300;
}
i_4 = mmp - 1;
i_1 = mmp - 1;
cc[i_4] = bb[i_1];
mdec = *mm - 1;
i_4 = mdec;
for (j = 1; j <= i_4; ++j) {
k = *mm - j + 1;
i_1 = k - 1;
i_2 = k;
i_3 = k - 1;
cc[i_1] = (w * cc[i_2]) + bb[i_3];
}
pp = cc[1];
partr = pp.real();
tmp = -jay * pp;
parti = tmp.real();
/* Computing 2nd power */
if (hypot(partr, parti) < amin) {
goto L300;
}
partr = p.real();
tmp = -jay * p;
parti = tmp.real();
/* Computing 2nd power */
size = hypot(partr, parti);
/* test for convergence of polishing */
if (size <= *eps) {
goto L501;
}
w = w - (p / pp);
goto L510;
/* deflate */
L501:
i_1 = mp - 1;
i_2 = mp;
b[i_1] = a[i_2];
i_1 = m;
for (j = 1; j <= i_1; ++j) {
k = m - j + 1;
i_2 = k - 1;
i_3 = k;
i_4 = k;
b[i_2] = (z * b[i_3]) + a[i_4];
}
p = b[0];
i_2 = m;
rootr[i_2] = w.real();
i_2 = m;
tmp = -jay * w;
rooti[i_2] = tmp.real();
--m;
--mp;
tmp = -jay * w;
parti = tmp.real();
if (abs(parti) > sqteps) {
goto L140;
}
/* real root */
rooti[m + 1] = 0.;
i_2 = mp;
for (j = 1; j <= i_2; ++j) {
i_3 = j;
i_4 = j;
a[i_3] = b[i_4].real();
}
goto L10;
/* complex root */
L140:
partr = z.real();
tmp = -jay * z;
parti = tmp.real();
z = partr - (parti * jay);
i_3 = mp - 1;
i_4 = mp;
c[i_3] = b[i_4];
i_3 = m;
for (j = 1; j <= i_3; ++j) {
k = m - j + 1;
i_4 = k - 1;
i_2 = k;
i_1 = k;
c[i_4] = (z * c[i_2]) + b[i_1];
}
i_4 = m;
rootr[i_4] = w.real();
i_4 = m;
tmp = -(-jay * w);
rooti[i_4] = tmp.real();
--m;
--mp;
i_4 = mp;
for (j = 1; j <= i_4; ++j) {
i_2 = j;
i_1 = j;
a[i_2] = c[i_1].real();
}
goto L10;
/* report and return */
L600:
real1 = double(kount);
real2 = double((*mm));
temp = real1 / real2;
return 1;
} /* dproot */
static const Complex one = 1, zero = 0;
void
LPCData::correct(float *frame, MXINT32 npoles, float *a) {
/* System generated locals */
MXINT32 i,i_1, i_2, i_3, i_4, i_5;
/* Local variables */
static MXINT32 nall;
static MXINT32 j, k;
static double r[ArrSize];
static Complex w[ArrSize];
static MXINT32 ndata;
static double y[97], rooti[96];
static MXINT32 l1, k4;
static double rootr[96];
static MXINT32 ii;
static double th[ArrSize];
static Complex ww;
static MXINT32 kinsid;
static double zz;
static MXINT32 kprint, k4m;
static double eps;
#ifdef LPC_DEBUG
printf("\nin correct npoles = %d \n",npoles);
for(i=0; i<36; i++) printf(" %g ",frame[i]);
#endif
/* Parameter adjustments */
--frame;
--a;
/* Function Body */
k4 = npoles + 1;
k4m = k4 - 1;
nall = k4 + 4;
i_1 = k4m;
for (ii = 1; ii <= i_1; ++ii) {
y[ii - 1] = -frame[ii + 4];
}
y[k4 - 1] = 1.;
eps = 1.0000000000000008e-8;
factor(y, &k4, rootr, rooti, &kinsid, &kprint, &eps);
i_1 = k4m;
for (j = 1; j <= i_1; ++j) {
/* Computing 2nd power */
r[j - 1] = hypot(rootr[j - 1], rooti[j - 1]);
th[j - 1] = atan2(rooti[j - 1], rootr[j - 1]);
if (r[j - 1] >= 1.) {
r[j - 1] = 1. / r[j - 1];
}
}
i_1 = k4m;
for (k = 1; k <= i_1; ++k) {
i_2 = k - 1;
w[i_2] = zero;
}
i_2 = k4 - 1;
w[i_2] = one;
i_2 = k4m;
for (k = 1; k <= i_2; ++k) {
/* ww=dcmplx(rootr(k),rooti(k)) */
ww = polar(r[k - 1], th[k - 1]);
l1 = k4 - k;
i_1 = k4m;
for (j = l1; j <= i_1; ++j) {
i_3 = j - 1;
i_4 = j;
i_5 = j - 1;
w[i_3] = w[i_4] - (ww * w[i_5]);
}
i_3 = k4 - 1;
i_4 = k4 - 1;
w[i_3] = -ww * w[i_4];
}
i_3 = k4;
for (j = 2; j <= i_3; ++j) {
i_4 = j - 1;
zz = w[i_4].real();
a[k4 + ndata + 1 - j] = -zz;
}
}
boolean
LPCData::isStable(float *frame, MXINT32 npoles) {
/* System generated locals */
double r_1;
static const int Size = 150;
static const int SizeP1 = Size + 1;
/* Local variables */
static float a[Size * Size] /* was [150][150] */;
MXINT32 i, m, mm;
static float rk[249];
/* Parameter adjustments */
--frame;
/* Function Body */
a[(npoles + 1) * Size - Size] = 1.0;
for (i = 1; i <= npoles; ++i) {
a[i + 1 + (npoles + 1) * Size - SizeP1] = *(frame + i);
}
for (mm = 1; mm <= npoles; ++mm) {
m = npoles - mm + 1;
rk[m - 1] = a[m + 1 + (m + 1) * Size - SizeP1];
if ((r_1 = rk[m - 1], abs(r_1)) < 1.) {
goto L20;
}
return false;
L20:
for (i = 1; i <= m; ++i) {
/* L25: */
/* Computing 2nd power */
r_1 = rk[m - 1];
a[i + m * Size - SizeP1] = (a[i + (m + 1) * Size - SizeP1]
- rk[m - 1] * a[m - i + 2 + (m + 1) * Size - SizeP1])
/ (1.0 - r_1 * r_1);
}
}
return true;
}
|